## Directi Interview Question

Senior Software Development Engineers**Country:**India

**Interview Type:**Phone Interview

You can take assumption that there is no negative value and gems are not 0.

your solution seems to be correct.

But can you do it in O(n) ?

If gems are not less than 1 then the value will never be able to decrease, so simply keep updating the value in one loop, assuming we have already passed even number of Reds

I think you would need a double for loop. In the outer loop, you iterate through all the houses to find one to begin at, then in the inner loop with the starting house defined in the outer loop, you will go through the rest of the houses and check to see what the best values you can make are, by continuously multiplying by the gems you encounter and only updating the values if you have visited an even number of red houses. I make an assumption that you can't have negative gems but that you can encounter a house with 0 gems, in which case your range will mess up, so you break out of the inner loop since there's nothing more you can do if you continue (since you will just multiply by 0).

- SK January 21, 2015