I tried to approach this problem by dividing the array into 3 parts
1st->[0 ,N/3)
2nd->[N/3,2N/3)
3rd->[2N/3,N)
and calculating the majority element in all three regions and stroing them in the variables e1,e2 and e3 (as there can be atmax 2 possible variables having frequency greater than N/3 and both of them will be having majority frequency in any of the three subarrays.)
and after that we will check for the frequncy of e1,e2 and e3 and insert into set so that no element gets repeated and then i printed that.
So ples tell me what is the problem with this approach?why are all test cases not getting passed?
Test cases not getting passes completely
hello @rohangupta569
[1,n/3] ,[n/3,2/3n] , [2/3n,n]
let say a number x is present in all three section(and their count in each section x1,x2,x3) and they are not in majority any of the section, then as per ur logic x can never be in majority .
which is wrong because it is totally possible that x1+x2+x3 can form majority in complete array.
But if an element is present in majority then in any of the section,it will definitely be in majority and will be considered as either element 1 or element 2.If it is not present in majority then we will get some values of element 1 and 2 which after checking we will find that their frequency is less than or equal to N/3 so cannot consider .
You can take any test case if you want…No test case will violate my conditions Hopefully
test for this ->
9
1 1 2 3 3 2 4 4 2
here 2 is in majority but it is not in majority in any of the section [1 1 2],[3 3 2], [4 4 2]
None of the elements in your test case is greater than N/3 so no need of considering them.I am only talking about the numbers whose frequency is greater than N/3 they will definitely appear in majority in any of the set
please share the complete problem statement with , is it >= N/3 or >N/3
consider this case->
10
1 1 5 2 2 5 3 3 5 5
here 5 is in majority . 4> 10/3
my code is still giving the right answer for this test case as well as i have divided the regions into floor(N/3) values. You can check it out https://ide.codingblocks.com/s/344486
sry ceil(n/3) values
