Monday, 9 March 2020

Find the majority element using the Boyer–Moore algorithm

If you are here, chances are you are trying to solve the “Find majority element in an array” problem and came across the term Boyer-Moore algorithm.
Let's fast forward to the problem description :
The majority element is an element in an array that occurs more than (size/2) times in an array (where size​ is the number of elements stored in an array).
For Example, Majority element is 3 in the array {3,6,7,3,45,3,5,3,3}
Now let’s have a look at the basic approaches first.

Brute Force

Use nested loops and count the frequency of each element. If we reach an element with a frequency greater than n/2, that’s our majority element.
Complexity Analysis
Time Complexity: O(n²)
Space Complexity: O(1)

Use Sorting

Sort the array, all the similar elements will be next to each other. We can easily check the frequency of each element using the starting position and ending position of the respective element.
Complexity Analysis
Time Complexity: Sorting + Linear Traversal (Here each element is visited only once) = O(nlogn) + O(n) = O(nlogn)
Space Complexity: O(n) (In case of merge sort)

Use Hashmap

Store the count of occurrences of an element and return the element with a count greater than (size/2)
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)

Boyer-Moore Algorithm

We can find the majority element in linear time and constant space using this algorithm. It requires exactly 2 passes over the input list. Simple to implement, little trickier to understand.
In the first pass, we need two parameters, A candidate value(initially set to any value), and a count( store the occurrences of candidate value, set to zero initially)
For each element in the array, compare it to the current candidate value. If they are the same, we increment count by 1. If they are different, we decrement count by 1. If count becomes zero, we change the candidate with the element at the current index.
A second O(N) pass can verify that the candidate is the majority element.

How does it work

Try to think of it as a war, where a number of groups are fighting with each other. In our case(shown below), there are 4 different groups(A,B,C,D). Any soldier can kill another group’s soldier by killing himself. In the end, whatever group is left with more than half soldiers, is the winner of the war.
Try to relate it with the below diagram:
In partially pairing, the soldier of group B is killed by group C. Group A is left with more than half the soldiers, hence, the winner.
The second iteration is to verify the count of the element (found in the first iteration)
If you want to check the mathematical proof for this approach, please check the link

Tuesday, 9 July 2013

My Journey From Punjabi University To IIT Kanpur

Today, I am going to share my thoughts and experience related to Gate Preparation, hoping that it will be beneficial for those who are preparing for the same and chasing their dream.

I completed my B.Tech from Punjabi University Patiala. At that time, I had a mind set that the persons, who are studying in good colleges like THAPAR, PEC, NIT's, IIT's are briliant and extra-ordinary. It is very difficult to get into these colleges. This thinking always deter me for achieving something big. But One of my friend from THAPAR always inspired me and used to mooted this issue that if anyone else can do this, then why can't you ?? This rekindled the HOPE again in me :) :) After getting placed in INFOSYS, I was sceptic that Was it the aim that I wanted to achieve in my life? And started thinking about higher education. So it is my serious advice for all those students, whose thinking is same as I had, here nothing matters more than your "HARDWORK". Just give your best and everything else will be yours :) :)

Untill the last year of B.Tech, I was not much conscious about the GATE. I started preparing for GATE by my own. But the issue was that my concepts was not clear and I studied 3-4 subjects only. I was not much confident at that time and appeared for GATE 2012. Result was out and I was too :P :P But my mind was prepared for it.

So, By that time I strongly realized that I need guidance and coaching. So, I made a thought of joining MADE EASY DELHI. After joining class, I felt that my concepts was seriously very weak. So, I noticed that I need a lot of practice and hard work to improve and sharpen my concepts. During the initial months of my coaching, I was not that much serious and I did not care about my preparation until i scored very less in the class test. After that, I got to know about my level of preparation and then I started really working hard. I made a schedule according to the subjects. I used to read standard books of related subjects and solved the back exercise. I collected questions of CSE from last years GATE papers and practiced those questions. This helped alot and I joined MADE EASY and GATEFORUM test series. Videos from NPTEL site is also a good source for learning. Slowly-2, I gained confidence and Started dreaming for IIT. At the end of JAN, I revised all the subjects and my performance was quite good in Test Series.
Finally the day came, although this time I was confident, but nervousness overcame this, an assorted feeling. I appeared for GATE 2013. This time paper was tough as compared to previous papers. I did my best but in the haste did some silly mistakes also :( :(  After the paper I was sad, because I knew I could do better than this.

Finally the most awaited day came, 15 MARCH. Result was out and this time I was in :) :) I was satisfied by scoring 339 AIR. Thanks to Almighty for his benign gaze on me. But I was expecting rank with in 200. Still I was satisfied with my toil now :) :)

After that I got call from IISC for Computational Science and Engineering. But this course is much inclined towards Maths. It is not a cup of tea for those who are not interested in Mathematics. I did not appear for the written test. After that I got call from IIT KANPUR for interview. I reached Kanpur a day before the written test. Syllabus for the test was same as that of GATE. Aptitude, Probability, ALGO and TOC was in high quantity. I attempted near about 35 questions out of 40. This time, I was satisfied with my performance. They shortlisted 110 students for the interview and I was among one of them.

Interview was on next day. There was 2 professors in the interview panel. One of them asked me about my favourite subjects. I replied TOC and ALGO. Then he asked about PUNJABI UNIVERSITY. I replied.


They asked the following questions :
  • Implement the Indian Number System with the help of automata. - I drew the FA on the board. He seemed to be not happy with it, he asked me "Do you know about the regular language?"  I said "Yes". After that he asked me to write the regular grammer or regular expression. After thinking for a while I explained it on the board. This time he was satisfied with my answer to some extent. Then he asked some questions related to TM, Semi-Infinite TM and 2-3 more. I answered all.
  • Implement queue with the help of stack. - This was a cake walk for me :)  I explained it and its complexities on the board. He told the another professor to ask question but he said no need. 
Finally my Interview was over. After 5-6 days I got a mail that I was selected for M.Tech in CSE in IITK. I was on ninth cloud on that day and finally my DREAM come true :) :)



With Regards,
KAMNA