GeeksforGeeks
A computer science portal for geeks Home Q&A Interview Corner Ask a question Feedback Contribute About us Subscribe Arrays Articles Bit Magic C/C++ Puzzles GFacts Linked Lists MCQ Misc Output Strings Trees
5/12/12
/ Pormt cekfrmjrt eeeti asre ary* * rga o hc o aoiy lmn n otd ra / #icue<ti.> nld sdoh #dfn bo it eie ol n bo iMjrt(n ar] itn itx ol saoiyit r[, n , n ) { iti n ; / gtls idxacrigt n(vno od * * e at ne codn o ee r d) / itls_ne =n2 n2 (/ +1; n atidx %? /: n2 ) / sac frfrtocrec o xi ar]/ * erh o is curne f n r[* fri=0 i<ls_ne;i+ o( ; atidx +) { / ceki xi peetadi peetmr ta n2tms* * hc f s rsn n s rsn oe hn / ie / i(ari = x& arin2 = x f r[] = & r[+/] = ) rtr 1 eun ;
} rtr 0 eun ;
/ Die pormt cekaoefnto * * rvr rga o hc bv ucin / itmi( n an) { itar1]={,2 3 3 3 3 1} n r[0 1 , , , , , 0; itn=7 n ; itx=3 n ; i(saoiyar n x) fiMjrt(r, , ) pit(% apasmr ta % tmsi ar],x n2; rnf"d per oe hn d ie n r[" , /) es le pit(% de ntapa mr ta % tmsi ar],x n2; rnf"d os o per oe hn d ie n r[" , /) gthr) eca(; rtr 0 eun ;
METHOD 2 (Using Binary Search) Use binary search methodology to find the first occurrence of the given number. The criteria for binary search is important here. / Pormt cekfrmjrt eeeti asre ary* * rga o hc o aoiy lmn n otd ra /
www.geeksforgeeks.org/archives/4722 2/15
5/12/12
#icue<ti.> nld sdoh; #dfn bo it eie ol n / I xi peeti arlw.hg]te rtrsteidxo * f s rsn n r[o..ih hn eun h ne f frtocrneo x ohriertrs- * is cuac f , tews eun 1 / it_iayerhitar] itlw ithg,itx itn; n bnrSac(n r[, n o, n ih n , n ) / Ti fnto rtrstu i texi peetmr ta n2 * hs ucin eun re f h s rsn oe hn / tmsi ar]o sz n* ie n r[ f ie / bo iMjrt(n ar] itn itx ol saoiyit r[, n , n ) { iti=_iayerhar 0 n1 x n; n bnrSac(r, , -, , ) / ceki teeeeti peetmr ta n2tms* * hc f h lmn s rsn oe hn / ie / i(( +n2 < ( -) & ari+n2 = x f(i /) = n 1) & r[ /] = ) rtr 1 eun ; es le rtr 0 eun ;
/ Sadr Bnr Sac fnto* * tnad iay erh ucin/ it _iayerhitar] itlw ithg,itx itn n bnrSac(n r[, n o, n ih n , n ) { i(ih> lw fhg = o) { itmd=(o +hg)2 /lw+(ih-lw/;/ n i lw ih/; *o hg o)2* / Ceki armd i tefrtocrec o x * hc f r[i] s h is curne f . armd i frtocrec i xi oeo tefloig r[i] s is curne f s n f h olwn i tu: s re () md= 0adarmd = x i i = n r[i] = (i armd1 <xadarmd = x i) r[i-] n r[i] = * / i( md= 0| x>armd1)& (r[i]= x) f( i = | r[i-] & armd = ) rtr md eun i; es i( >armd) le fx r[i] rtr _iayerhar (i +1,hg,x n; eun bnrSac(r, md ) ih , ) es le rtr _iayerhar lw (i -) x n; eun bnrSac(r, o, md 1, , )
} }
rtr -; eun 1
/ Die pormt cekaoefntos* * rvr rga o hc bv ucin / itmi( n an) { itar1]={,2 3 3 3 3 1} n r[0 1 , , , , , 0;
www.geeksforgeeks.org/archives/4722 3/15
5/12/12
itn=7 n ; itx=3 n ; i(saoiyar n x) fiMjrt(r, , ) pit(% apasmr ta % tmsi ar],x n2; rnf"d per oe hn d ie n r[" , /) es le pit(% de ntapa mr ta % tmsi ar],x n2; rnf"d os o per oe hn d ie n r[" , /) gthr) eca(; rtr 0 eun ;
Time Complexity: O(Logn) Algorithmic Paradigm: Divide and Conquer Please write comments if you find any bug in the above program/algorithm or a better way to solve the same problem.
Send 2 people
You may also like following posts 1. 2. 3. 4. 5. Search an element in a sorted and pivoted array Majority Element Find the smallest and second smallest element in an array Median of two sorted arrays Given an array A[] and a number x, check for pair in A[] with sum as x
20 comments so far
1. tauseef says: December 18, 2011 at 12:46 AM If array is sorted its not a problem if(array[mid+1]==array[0] || array[mid-1]==array[last]) array[mid] is majority element else no majority element exists
www.geeksforgeeks.org/archives/4722 4/15
5/12/12
Reply 2. baskin says: March 18, 2011 at 10:35 AM @GeeksforGeeks How about this? bo iMjrt(n ar] itn itx ol saoiyit r[, n , n ) { rtr x= arn2; eun = r[/] } Reply Sandeep says: March 18, 2011 at 11:00 AM @baskin: This approach won't if there is no majority element. For example, if x is 4, then this will return true for following array {1, 1, 2, 4, 4, 4, 6, 6} Reply Algoseekar says: April 24, 2011 at 9:02 PM @sandeep i don't understand ..how make sure about 1st algo..also i think u made code complex.. here is simply implementation /* Program to check for majority element in a sorted array */ # include # define bool int bool isMajority(int arr[], int n, int x) { int i; int count=0; /* search for first occurrence of x in arr[]*/ for(i = 0; i (n/2)) return 1;
www.geeksforgeeks.org/archives/4722 5/15
5/12/12
} return 0; } /* Driver program to check above function */ int main() { int arr[10] = {1, 2, 3, 3, 3, 3, 10}; int n = 7; int x = 3; if(isMajority(arr, n, x)) printf("%d appears more than %d times in arr[]", x, n/2); else printf("%d does not appear more than %d times in arr[]", x, n/2); getchar(); return 0; } please let know if anything wrong Reply Algoseekar says: April 24, 2011 at 9:05 PM Also Please Check in Cse of Method You are Passing extra parameter n in binary search..i don't understand this as wel.. see here is i modified program & its working fine /* Program to check for majority element in a sorted array */ #include # define bool int /* If x is present in arr[low...high] then returns the index of first occurance of x, otherwise returns -1 */ int _binarySearch(int arr[], int low, int high, int x); /* This function returns true if the x is present more than n/2 times in arr[] of size n */ bool isMajority(int arr[], int n, int x) { int i = _binarySearch(arr, 0, n-1, x); /* check if the element is present more than n/2 times */ if(((i + n/2) = low)
www.geeksforgeeks.org/archives/4722 6/15
5/12/12
{ int mid = (low + high)/2; /*low + (high - low)/2;*/ /* Check if arr[mid] is the first occurrence of x. arr[mid] is first occurrence if x is one of the following is true: (i) mid == 0 and arr[mid] == x (ii) arr[mid-1] arr[mid-1]) && (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 if element does not appear more than n/2 times*/ return -1; } /* Driver program to check above functions */ int main() { int arr[] = {1, 3, 3, 3,3,4,10};//sorted int n = 7; int x = 3; if(isMajority(arr, n, x)) printf("%d appears more than %d times in arr[]", x, n/2); else printf("%d does not appear more than %d times in arr[]", x, n/2); getchar(); return 0; } Please let me know if anything wrong..??? Reply 3. sharat says: February 24, 2011 at 12:25 AM Also the Comment /*Return -1 if element does not appear more than n/2 times*/ in _binarysearch method seems in appropriate.. Reply
www.geeksforgeeks.org/archives/4722 7/15
5/12/12
4. sharat says: February 24, 2011 at 12:21 AM in _binarySearch method There is no use of n, Please remove it. Reply 5. Nitin Taluja says: January 17, 2011 at 1:11 PM Soln. 1) find the median of the array, as the number repeated more than n/2 times or equal to n/2 times will only be the median or in other words find the index of(n/2) using modified version of the quicksort. Reply 6. includealldoth says: December 11, 2010 at 9:27 PM What if i want to actually find the majority element..divide and conquer would fail. Reply 7. someone says: December 2, 2010 at 10:02 PM How about using a hashset? Reply 8. vijayk says: November 9, 2010 at 8:26 PM in the linear search method, if condition should be modified as given below or else it will give wrong result ( i([]= x& (+/ < n1 & ain2 = x fai = & in2 = -) & [+/] = ) similarly in binary search, the if condition should be i( md= 0| (i- > 0 & x>amd1)& ([i]= x) f( i = | md1 = ) & [i-] & amd = ) Reply 9. seeker7 says:
www.geeksforgeeks.org/archives/4722 8/15
5/12/12
October 6, 2010 at 4:13 PM for linear search array need not b sorted ,so method 1 does not work,in case my input is:2 3 4 2 2 2 3 2 Reply GeeksforGeeks says: October 6, 2010 at 4:37 PM @seeker7: The question is about sorted array only (see title). The method 1 uses linear search to check whether the element is majority or not in a given sorted array. Reply 10. codegeek says: September 25, 2010 at 9:29 AM bo iMjrt(n ar] itn itx ol saoiyit r[, n , n ) { / i mdeeeti ntxte xi ntmjrt eeet/ * f i lmn s o hn s o aoiy lmn* i (% = 0 { f n2 = ) i( ! arn2) fx = r[/] rtr 0 eun ; }es i (x! arn2)& ( ! arn2-1) le f ( = r[/] & x = r[/ ]) rtr 0 eun ; iti=_iayerhar 0 n1 x n; n bnrSac(r, , -, , ) / ceki teeeeti peetmr ta n2tms* * hc f h lmn s rsn oe hn / ie / i(( +n2 < ( -) & ari+n2 = x f(i /) = n 1) & r[ /] = ) rtr 1 eun ; es le rtr 0 eun ;
last comment in _binarySearch() should be /*Return -1 if element is not present in array*/ Reply codegeek says: September 25, 2010 at 10:23 AM correction: / i mdeeeti ntxte xi ntmjrt eeet/ * f i lmn s o hn s o aoiy lmn* i (% ! 0 { f n2 = ) i( ! arn2) fx = r[/]
www.geeksforgeeks.org/archives/4722 9/15
5/12/12
rtr 0 eun ; }es i (n=)& ( ! arn2)& ( ! arn2-1) le f (>2 & x = r[/] & x = r[/ ]) rtr 0 eun ; Reply Shrikant says: December 3, 2010 at 9:33 PM I agree with this solution. Reply 11. seeker7 says: August 11, 2010 at 3:30 PM is there any proof to the logic used i.e. is it is a majority element it should also occur at i+n/2..coz it occurs atleast n/2 times? Reply 12. GeeksforGeeks says: February 12, 2010 at 8:50 AM @devendra: x may not be equal to arr[n/2] when it is not a majority element. For example arr[] = [1, 2, 3, 4, 4] and x = 4 Reply Ashish says: November 26, 2010 at 12:38 PM Yaa but integer should occur more than n/2 times ... so it should be minimum n/2+1 times ..... so here 4 should be minimum 5/2+1=3 times... show we will not return 4 as the majority element. Reply 13. devendra says: February 11, 2010 at 5:34 PM In above Solution-2 x will always equal to a[n/2] so we dont have to give manually or consider as a parameter Reply
Comment
www.geeksforgeeks.org/archives/4722 10/15
5/12/12
[oreoelnug=C] succd agae"" / Pseyu cd hr (o mydlt teelns * at or oe ee Yu a eee hs ie i ntwiigcd)* f o rtn oe / [succd] /oreoe
Search
Popular Tags
GATE Java Dynamic Programming Backtracking Pattern Searching Divide & Conquer Graph Operating Systems Recursion
www.geeksforgeeks.org/archives/4722
11/15
5/12/12
Popular Posts
All permutations of a given string Memory Layout of C Programs Understanding extern keyword in C Median of two sorted arrays Tree traversal without recursion and without stack! Structure Member Alignment, Padding and Data Packing Intersection point of two Linked Lists Lowest Common Ancestor in a BST. Check if a binary tree is BST or not Sorted Linked List to Balanced BST
www.geeksforgeeks.org/archives/4722
12/15
5/12/12
250
Subscribe
tree to file
Last Post By: Dheeraj Inside: Interview Questions
www.geeksforgeeks.org/archives/4722 13/15
5/12/12
FB interview question
Last Post By: ranganath111 Inside: Interview Questions
numeric puzzle
Last Post By: asm Inside: Algorithms
Forum Categories
Interview Questions C/C++ Programming Questions Algorithms Trees specific questions Linked List specific questions Multiple Choice Questions Object oriented queries GPuzzles
www.geeksforgeeks.org/archives/4722 14/15
5/12/12
Recent Comments
Venki on Structure Member Alignment, Padding and Data Packing avi on Structure Member Alignment, Padding and Data Packing atul on A Program to check if strings are rotations of each other or not Venki on Structure Member Alignment, Padding and Data Packing rahul on Dynamic Programming | Set 13 (Cutting a Rod) Sandeep on Dynamic Programming | Set 3 (Longest Increasing Subsequence) Yueming on Dynamic Programming | Set 3 (Longest Increasing Subsequence) avi on Structure Member Alignment, Padding and Data Packing @geeksforgeeks, Some rights reserved Powered by WordPress & MooTools, customized by geeksforgeeks team
www.geeksforgeeks.org/archives/4722
15/15