## codingwarrior01

BAN USER1) Iterate over the array. If an element is a prime add it to a PrimesArray.

At the end of the loop we have all primes of original array stored in PrimesArray.

2) Sort PrimesArray

3) Linear scan of PrimesArray and find the minimum difference between consecutive elements.

1) Keep a running counter C (indicating subsequent length of 1s seen) =0

2) Iterate over the array:

When you see a 1, increment C. Check if it's the max if we have seen so far (if so update it).

When you see a 0, reset C to 0.

return the max.

We can solve this using union and find.

1) For the first set of equation: whenever you see something like A=C, do union of A and C. When you see A!=C, do nothing.

2) For the second set of equations:

When you find A=C. Go to the data structure built from first step and see root (or parent) of A and C are same, if so we are good. If not, then set of equations is not compatible.

Similarly when you see A!=C, you want to make sure root of A and C in data structure built from first step are not the same.

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

- codingwarrior01 January 30, 2017