Saturday, 30 January 2016

Merge Three Arrays In Fourth Array Problem Solution Using C++.


Soluton:

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int ar1[1000],ar2[1000],ar3[1000],n,res[3000];
 cout << "How Many Elements You Want To Enter: ";
 cin >> n;
 cout << endl << "Enter Your First Array Elements (Separated By Space) : ";
 for(int i = 0; i < n; i++)cin >> ar1[i];
 cout << endl << "Enter Your Second Array Elements (Separated By Space) : ";
 for(int i = 0; i < n; i++)cin >> ar2[i];
 cout << endl << "Enter Your Third Array Elements (Separated By Space) : ";
 for(int i = 0; i < n; i++)cin >> ar3[i];
 
 for(int i = 0,j = 0; i < n; i++,j++){
  res[i] = ar1[i];
  res[i+n] = ar2[i];
  res[i+n+n] = ar3[i];
 }
 int sz = n*3;
 cout << endl << "Elements of Array After Merge: ";
 for(int i = 0; i < sz; i++)cout << res[i] << " ";
 cout << endl;
 return 0;
}

Delete Duplicate Elements in Array Problem Solution Using C++.


Solution:

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int max = -999999,result[1000],ar[] = {-1,-33,-2,9,1,11,2,5,4,7,8,9,2,3,1,4,7,5,2,3,6,9,0,4,5,1,1,4,0,10};
 int n = sizeof(ar)/sizeof(int);
 bool checkPos[1000],checkNeg[1000];
 memset(result,-1,sizeof(ar));
 memset(checkPos,false,sizeof(checkPos));
 memset(checkNeg,false,sizeof(checkNeg));
 
 for(int i = 0; i < n; i++){
  if(ar[i] < 0){
   checkNeg[ar[i]*-1] = true;
   result[ar[i]*-1]++;
  }
  else {
   result[ar[i]]++;
   checkPos[ar[i]] = true;
  }
  if(max < abs(ar[i]))max = abs(ar[i]);
 }
 for(int i = 0; i <= max; i++){
  if(checkPos[i] == true && result[i]!=-1){
   checkPos[i] = false;
   cout << i << " ";
  }
  if(checkNeg[i] == true && result[i]!=-1){
   checkNeg[i] = false;
   cout << -i <<  " ";
  }
 }
 cout << endl;
 return 0;
}

Delete Duplicate Elements in Array For Positive Value Problem Solution Using C++.



Solution:

#include <iostream>
#include<cstring>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int max = -999999,result[1000],ar[] = {8,9,1,11,2,5,4,7,8,9,2,3,1,4,7,5,2,3,6,9,0,4,5,1,1,4,0,10};
 int n = sizeof(ar)/sizeof(int);
 memset(result,-1,sizeof(ar));
 
 for(int i = 0; i < n; i++){
  result[ar[i]]++;
  if(max < ar[i])max = ar[i];
 }
 for(int i = 0; i <= max; i++)if(result[i]!=-1)cout << i << " ";
 cout << endl;
 return 0;
}

Monday, 25 January 2016

C Program For Tower Of Hanoi Problem Solution Using Recursive Function.

Solution:

#include<stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void tower(int n,char firstPillar[],char midPillar[],char lastPillar[]){

    if(n == 1){
        printf("\nMove Disk %d To Pillar %s To Pillar %s",n,firstPillar,midPillar);
    }
    else{
        tower(n-1,firstPillar,lastPillar,midPillar);
        printf("\nMove Disk %d To Pillar %s To Pillar %s",n,firstPillar,midPillar);
        tower(n-1,lastPillar,midPillar,firstPillar);
    }
}
int main(){

    int n ;
    char name[100];
    printf("Enter Disk Number: ");
    scanf("%d",&n);
    tower(n,"First","Mid","Last");
}

Thursday, 21 January 2016

Maximizing XOR Hacker Rank Problem Solution.

Given two integers, L and R, find the maximal value of A xor B, where A and B satisfy the following condition:
LABR
Input Format
The input contains two lines; L is present in the first line and R in the second line.
Constraints
1LR103
Output Format
The maximal value as mentioned in the problem statement.
Sample Input
10
15
Sample Output
7
Explanation
The input tells us that L=10 and R=15. All the pairs which comply to above condition are the following:
1010=0
1011=1
1012=6
1013=7
1014=4
1015=5
1111=0
1112=7
1113=6
1114=5
1115=4
1212=0
1213=1
1214=2
1215=3
1313=0
1314=3
1315=2
1414=0
1415=1
1515=0
Here two pairs (10, 13) and (11, 12) have maximum xor value 7, and this is the answer.
Solution;

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int maxXor(int l, int r) {
    int mx = -99999;
    for(int i = l; i <= r; i++){
        for(int j = i+1; j <= r; j++){
            if((i^j) > mx)mx = (i^j);
        }
    }
    return mx;
}

int main() {
    int res,_l;
    cin >> _l;
   
    int _r;
    cin >> _r;
   
    res = maxXor(_l, _r);
    cout << res;
    return 0;
}