Your code is not even passing the sample test. But that’s not the concern, the concern is blindly copying things without understanding. Dear, don’t write anything in your code before getting the concept. Brainstorm, use pen and paper to see how loops working, think, but don’t blindly copy. It will hurt a lot in long run!
For sample case-
5 1
1 2 2 2 2
1 5 3
Your output is-
0 1 1 2 3
0 2 3 4 4 -1
Regarding where your code is failing, I need some time to debug it. Will update as soon as possible.
EDIT- Check this submission. This one makes another array to count the cumber of consecutive elements, and returns the element when the repetition is more than k. Be careful the array names are similar (at and ar1, don’t get confused!)
EDIT2- Yes, others are right, the left and right arrays are wrong. Please re-check the video from 2:22 onwards where he discusses this concept.
There is some problem in calculating left and right array.I am pretty sure those are not correct.check the method of calculating them or you may have made any other mistake.You are calculating them m times but you have to calculate in only once and then answer the queries.
You are constructing the left and right array completely wrong. left array is used to hold if a number repeats consecutively then what is the start index of that repeating number and right array is used to hold what is the end index of that repeating number.
Fixed… I commented the changes . You can go through it Click Here
Hi @adhish_kapoor,
In the code, you need to modify these statements:
left[i]=i-1; —> left[i]=left[i-1];
and
right[i]=i+1; —> right[i]=right[i+1];
I am sorry if I was ambiguous in the editorial. These are the values we set the array positions to, if we find a corresponding equal adjacent element. If there are further doubts, I will be happy to help