Raushan1156's blog

By Raushan1156, 2 years ago, In English

within 5 minutes only, I solved the Medium level problem based on Moore's Voting Algorithm just after grabbing the working concept.

Moore's voting Algorithm is generally used to find the element frequency of an array with a condition i.e. frequencyCount>size/2. . During its implementation, just divide it into three parts to make it easier:

a] Take an extra variable i.e. ansIndex=0 and do count++ when you get the same item in the array or else just do count-- ;

a.1] if count becomes equal to 0, just update as count=1 and ansIndex=i (i.e. current index).

a.2] traverse the array till array.length-1 by doing the same process above mentioned.

b] then, at last, you will get an index value whose count will greater than 0 and that may be our majority element. so again we will go through the loop and will count the actual count of that element.

c] if the actual count>(array.length)/2, then you can return the element or actual count or else -1; . The problem link: https://lnkd.in/dv8bMPSV

The solution link: https://lnkd.in/dbnBhKFi

one more medium level problem but in more than one attempt I did:

Qn link: https://lnkd.in/dtvbSkbH

solution link: https://lnkd.in/dcPJ4uef

I hope it will be helpful to you.

  • Vote: I like it
  • +12
  • Vote: I do not like it

»
2 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by Raushan1156 (previous revision, new revision, compare).