0 0 votes A Sorted array of n elements contains 0 and 1 to find out majority of 0 and 1.How much time it will take??? and please explain Meaning -majority of 0 and 1?? Data Structures array + – sandeep singh gaur 2.3k views answer comment Share Follow Print See all 4 Comments 4 4 Comments reply prashant jha 1 commented Feb 18, 2019 reply Follow flag If I'm understanding it correctly , it's asking for calculating the nunber of 0's and 1's in the array , and comparing them , and finding which is in majority . If this is the correct interpretation , then this problem can be done in $O(logn)$ time using binary search , since the array is sorted. 0 0 replyShare sandeep singh gaur commented Feb 18, 2019 reply Follow flag i am agree with your interpretation ,but given ans is O(1). i am also confused with interpretation 0 0 replyShare prashant jha 1 commented Feb 18, 2019 reply Follow flag Can you send the solution with the actual problem statement? 0 0 replyShare sandeep singh gaur commented Feb 18, 2019 reply Follow flag i have only ans not solution if u want we can send 0 0 replyShare Please log in or register to add a comment.
5 5 votes I think the answer should be O(1) as the middle element can be found in constant time as the n/2th element is the middle element in. Once found, it can be compared. If it is found to be 1 then the majority is 1 otherwise 0 as it is given as sorted array. Priyadrasta Raut answered Feb 18, 2019 Priyadrasta Raut comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments Priyadrasta Raut commented Oct 21, 2019 reply Follow flag In the question ,the array given is sorted while in your case it's not. 0 0 replyShare Priyadrasta Raut commented Oct 21, 2019 reply Follow flag I think my approach is right if n is odd otherwise we've to check both mid and mid-1 to confirm whether the array has a majority or equality.Still the answer is O(1) as it will take constant time. 0 0 replyShare Gupta g commented Sep 30, 2020 reply Follow flag But in question it is said that find out majority of 0 and 1. We can find majority only when no of 0’s and no of 1’s are not equal. If both are equal than no(either 0 or 1) will be in majority. 0 0 replyShare Please log in or register to add a comment.