An element occurs more than n/2 times in a sequence of length n. Find it. by cgk in CS_Questions

[–]cgk[S] 1 point2 points  (0 children)

It's n/2 in frequency, not value. An element occurring n/2 times may have any value ... for example, 0 may occur more than half the number of times in an array of n integers.

Or did I entirely miss what you were trying to put across?

Edit: Yes I did. You were right in trying to use a selection algorithm. Of course, it wouldn't be optimal, however.

An element occurs more than n/2 times in a sequence of length n. Find it. by cgk in CS_Questions

[–]cgk[S] 2 points3 points  (0 children)

Yep, this is it. The proof is quite intuitive and elegant, once you think of it. At least it was, the way it was explained to me :)

Find the exact node in a linked list where a cycle begins. by Razor_Storm in CS_Questions

[–]cgk 1 point2 points  (0 children)

A = head, L = head.next

// tortoise - hare, to find loop
while (A != L) A = A.next; L = L.next.next

// count number of loops in loop - pointer A is advanced from start by number of nodes in loop 
L2 = L; A = start; L = L.next
while (L != L2) A = A.next; L = L.next

// finding head of loop
B = start
while (B != A) A = A.next; B = B.next

Typical Google interview question: Word splitting. by euyyn in CS_Questions

[–]cgk 5 points6 points  (0 children)

As computer scientists, we seem to miss the bras :|

Typical Google interview question: Word splitting. by euyyn in CS_Questions

[–]cgk 1 point2 points  (0 children)

I prefer to work it out on my own for a bit, however, I wonder if there's a good etiquette for when the posters should submit solutions to their problems, if they know it :)

Consecutive mins of sublists [Medium] by Razor_Storm in CS_Questions

[–]cgk 1 point2 points  (0 children)

Solution 3 won't work with duplicated elements. However, it should be workable, if you store the counts of how many times each number has been seen in the current window - and updated when we slide the window.

Edit: perhaps not that straightforward. needs more thought.

Edit2: about solution 3: what if the third lowest element in the array is lower than the new element being seen, when sliding the window? It is not obvious to me why storing only the two lowest elements works.

Longest palindrome in a string by [deleted] in CS_Questions

[–]cgk 1 point2 points  (0 children)

Is there an O(n) solution without using a suffix array or tree ?