Find Minimum difference pair [ Amazon Interview Question]

Problem: Find the minimum difference between any pair in an unsorted  array. #include <stdio.h> #include <algorithm> #include <iostream> using namespace std; int arr[105]; int res[100005]; int main() { int t,n,k,a; scanf("%d",&t); while(t–) { scanf("%d",&n); for(int i=0;i<105;i++) arr[i]=0; for(int i=0;i<n;i++) { scanf("%d",&a); arr[a]++; } //sort(arr,arr+n); k=0; for(int i=0;i<105;i++) { if(arr[i]==0) continue; for(int j=1;j<=arr[i];j++) res[k++]=i; } int

Kadane’s Algorithm [ Macrosoft – Amazon – FlipKart ]

Problem: Given an array containing both negative and positive integers. Find the contiguous sub-array with maximum sum. Link #include <stdio.h> int main() { //code int t,n,i,a; int mx_end_here,mx_so_far; scanf("%d",&t); while(t–) { scanf("%d",&n); mx_end_here=0,mx_so_far=-1000; for(i=0;i<n;i++) { scanf("%d",&a); if(a<0) { if(mx_so_far<a) mx_so_far=a; mx_end_here=mx_end_here+a; if(mx_end_here<0) mx_end_here=0; } else if(a>=0) { mx_end_here=mx_end_here+a; if(mx_end_here>mx_so_far) mx_so_far=mx_end_here; } } printf("%d\n",mx_so_far); } return 0;

Maximum Product Sub array [ Microsoft – Amazon ]

Problem : Given an array that contains both positive and negative integers, find the product of the maximum product subarray. Link Discussion: It is a Similiar problem to maximum sum sub array problem. Notice,maximum product can also be obtained by minimum (negative) product ending with the last element multiplied by this element. we use mn_end_here(can be

Search in rotational sorted array[ Microsoft – Amazon – FlipKart ]

Problem: Find a value from a rotational sorted array. Solution Hint: binary search. Link #include <stdio.h> int arr[100010]; int bin_search(int lw,int hi,int v) { if(v<arr[lw] || v>arr[hi]) return 0; int mid; while(lw<=hi) { mid=(lw+hi)/2; if(arr[mid]==v) { printf("%d\n",mid); return 1; } else if(arr[mid]<v) lw=mid+1; else if(arr[mid]>v) hi=mid; } return 0; } int main() { //code int t,i,j,n,val,a,idx;

Parenthesis Checker [ Snapdel – Amazon ]

Problem: Check the balance of parenthesis sequence, the string contains (),{} and []. Link #include <stdio.h> #include <string.h> int main() { //code char s[105]; char arr[105]; int balance; int t,idx,i; scanf("%d",&t); while(t–) { scanf("%s",s); idx=0; balance=1; for(i=0;i<strlen(s);i++) { if(s[i]=='(' || s[i]=='{' || s[i]=='[') arr[idx++]=s[i]; else if(s[i]==')') { if(arr[idx-1]=='(') { idx–; } else balance=0; } else

Find Maximum Distance [ Google-Amazon ]

Problem: An array of integers will be given, find the maximum distance of indexes of  [j – i]  subjected to the constraint of A[i] <= A[j]. A : [4 6 5 3]; Output : 2; For the pair (4, 5). Link #include <stdio.h> int arr[1005]; int t,n,mx; int main() { //code int i,j; scanf("%d",&t); while(t–) { scanf("%d",&n); for(

Removing duplicate chars[Microsoft]

Problem: Given a string, Remove all the duplicate chars, string may include spaces. Solution Hints:solN; ASCII char valaus range from -128 to 127. Link The following solution did not get AC from OJ. My guess is, there may be a problem with that OJ. #include <iostream> #include <string> #include <stdio.h> using namespace std; int main() { //code int

Find Largest Number formed from an Array [PayTm , Amazon]

Problem:A list of non negative integers will be provided, arrange the numbers in such a way that they form the largest number possible. Example: From a list : 9 30 31 4 10 ; Largest number formed : 94313010. Link #include <stdio.h> #include <algorithm> #include <vector> #include <iostream> #include <string> using namespace std; //vector<string> vb; string vb[105]; int n; bool