You are on page 1of 2

#include<iostream> using namespace std; //if element at i is equal to i+n/2 due to sorted array element at i is the majo rity

element int linearsearch(int arr[],int n,int x) { int last=n%2?n/2:(n/2+1); for(int i=0;i<last;i++) { if(arr[i]==x && arr[i+n/2]==x) return i; } return -1; } int binarysearch(int arr[],int low,int high,int x) { int mid=low+(high-low)/2; if(low<=high) { if((mid==0 || arr[mid-1] > x) && arr[mid]==x) return mid; else if(x > arr[mid]) return binarysearch(arr,mid+1,high,x); else return binarysearch(arr,low,mid-1,x); } return -1; } bool isMajority(int arr[],int n,int x) { int i=binarysearch(arr,0,n-1,x); if(i+n/2 <= n-1 && arr[i+n/2] == x) return true; return false; } int main() { int arr[10] = {1, 2, 3, 3, 3, 3, 10}; int n = 7; int x = 3; if(isMajority(arr, n, x)) cout<<x<<" appears more than "<<n/2<<" times in arr[]"<<endl; else cout<<x<<" does not appear more than "<<n/2<<" times in arr[]"<< endl; if(linearsearch(arr, n, x)>=0) cout<<x<<" appears more than "<<n/2<<" times in arr[]"<<endl; else cout<<x<<" does not appear more than "<<n/2<<" times in arr[]"<< endl; int x1=-3,y=-5;

int z=y+((x1-y) & -(x1<y)); cout<<z; return 0; }

You might also like