Not All Flavours - Week 2

Any tips a to start with this problem? As to how to approach it?

Start with the range (1, 1). Keep expanding the range while all the flavours are not included in the range l…u (and take max). Once this condition becomes false, contract the range from the other side.

1 Like

Here are is O(n) my approach

Data structures you’ll need

  1. Hash table to store frequency of flavours occuring in [start,end]
  2. Hash set to store all the flavours currently in [start, end]

Start with start=0, end=0 and update the subarray length until the the set has all the flavours. Every time the set length is not the same as the number of flavours, you expand your range(end++). Once you have all flavours in your set, start contracting from the left. Everytime you contract, decrement the frequency of A[start]. If it drops to 0, remove the flavour from the set.

Can anyone help me with my code?
I ran it dry which was working good. Please help!

First, you are printing unnecessary things and not printing result in new line. Also your code is not giving correct output for sample tc.

I’m not getting how max is getting updated to 6 for the first test case.