inarr.cpp - #include <bits\/stdc.h> using namespace std Find a b = sum void swaping(int*a,int*b)cfw int temp temp =*a*a =*b*b = temp int partition(int

# inarr.cpp - #include <bits/stdc.h> using namespace std...

This preview shows page 1 - 5 out of 28 pages.

#include <bits/stdc++.h> using namespace std; // Find a + b == sum void swaping(int *a,int *b){ int temp; temp = *a; *a = *b; *b = temp; } int partition(int A[],int l, int r){ int pivot = A[r]; int index = l - 1; for(int j = l;j <= r-1;j++){ if(A[j] <= pivot){ index++; swaping(&A[index],&A[j]); } } swaping(&A[index+1],&A[r]); return (index+1); } void quicksort(int A[],int l,int r){ if(l < r){ int p = partition(A,l,r); quicksort(A,l,p-1); quicksort(A,p+1,r); } } void hasArrayTwoCandidates(int A[], int arr_size, int sum){ //sort(A,A+arr_size); quicksort(A,0,arr_size-1); for(int i = 0;i < arr_size;i++) cout << A[i] << " "; cout << endl; int l = 0; int r = arr_size - 1; while(l<r){ int y = A[l] + A[r]; if(y == sum){ cout << A[l] << endl; cout << A[r] << endl; l++; }else if(y<sum){ l++; }else{ r--; } } } // Find Majority int findcandidate(int A[],int arr_size){ int major = 0; int count = 1; for(int i = 1;i < arr_size;i++){
if(A[major] == A[i]) count++; else count--; if(count == 0){ major = i; count = 1; } } return major; } void findmajority(int A[],int arr_size){ int major = findcandidate(A,arr_size); int c = 0; for(int i = 0;i <arr_size;i++){ if(A[major] == A[i]) c++; } if(c > arr_size/2) cout << "Found Majority" << A[major] <<endl; else cout << "Not Found" << endl; } // Odd no. of times int getOddOccurrence(int arr[], int arr_size) { int sum = arr[0]; for(int i = 1;i < arr_size;i++){ sum = sum ^ arr[i]; } return sum; } // Largest Contiguos Array Sum // 1st Method int maxSubArraySum(int A[], int arr_size){ // If all negative return the max element or min abs value element int c = 0; int mini= INT_MAX; for(int i = 0;i < arr_size;i++){ if(A[i] < 0){ c++; mini =min(mini,abs(A[i])); } } if (c == arr_size){ return (mini*-1); } int max_so_far = 0; int curr_max = 0; for(int i = 0;i < arr_size;i++){ curr_max = curr_max + A[i]; if(curr_max < 0) curr_max = 0; else if(curr_max > max_so_far) max_so_far = curr_max; } return max_so_far;
} // 2nd Method int maxSubArraySum1(int A[], int arr_size){ int max_so_far = A[0]; int curr_max = A[0]; for(int i = 1;i < arr_size;i++){ curr_max = max(A[i],curr_max+A[i]); cout << curr_max << " "; max_so_far = max(max_so_far,curr_max); cout << max_so_far << " "; } cout << endl; return max_so_far; } // Find missing number int getMissingNo (int A[], int arr_size){ int sum = 0; for(int i = 0;i < arr_size;i++){ sum = sum ^ A[i]; } for(int i = 1;i <=arr_size+1;i++){ sum = sum ^ i; } return sum; } // FInd element in pivoted array int binarysearch(int arr[],int low,int high,int key){ if(low > high) return -1; int mid =(low+high)/2; if(arr[mid] == key) return mid; else if(key > arr[mid]) return binarysearch(arr,mid+1,high,key); else return binarysearch(arr,low,mid-1,key); } int findPivot(int arr[], int low, int high){ if(low>high) return -1; if(low==high) return low; int mid = (low+high)/2; if(mid < high && arr[mid]>arr[mid+1]) return mid; if(mid > low && arr[mid-1] > arr[mid]) return mid-1; if(arr[low] >= arr[mid]) return findPivot(arr,low,mid-1); else return findPivot(arr,mid+1,high); } int search1(int arr[], int l, int h, int key){
int pivot = findPivot(arr,l,h); if(arr[pivot] == key) return pivot; if(key >= arr[0]) binarysearch(arr,l,pivot-1,key); else binarysearch(arr,pivot+1,h,key); } // Find element in pivoted array(2ns method) int search(int arr[], int l, int h, int key){ if(l>h) return -1; int mid = (l+h)/2; if(arr[mid] == key) return mid; // let us suppose arr[l...mid] is sorted if(arr[l] <= arr[mid]){ if(key>=arr[l] && key<=arr[mid]) return search(arr,l,mid-1,key); else return search(arr,mid+1,h,key); } if(key>=arr[mid] && key<=arr[h]) return search(arr,mid+1,h,key); else return search(arr,l,mid-1,key); }

#### You've reached the end of your free preview.

Want to read all 28 pages?

• Spring '13
• SumitGanguly
• Addition, C Programming, Summation, Void type, Carriage return, Bi-directional text