0

Given a list of N numbers(1-indexed), a continuous block is K-ordered block if it has more than K same elements occurring consecutively.

Example : [2,4,4,5,5,5,3,3] is having a 3-ordered block from index 4 to 6 and a 2-ordered block from 7 to 8. Block from 4 to 6 is 2-ordered block too.

Now if we are given Queries of form : LeftIndex,RightIndex,Order-K

We need to tell between LeftIndex and RightIndex how many Order-K blocks are present.

Say if query is of type 2,8,2 then answer is 3 as 3 blocks are with Order 2. They are from index 2 to 3,4 to 6,7 to 8.

How to solve this problem if queries are up to 100000, and list can be 100000.

HackAround
  • 85
  • 2
  • 9
  • 1
    This question asks solution for a problem from a running contest(https://www.codechef.com/APRIL17/problems/SMARKET). This question should be removed. – madMDT Apr 12 '17 at 17:31

1 Answers1

0

You should show what you've done and your idea. Refer to tour

Include details about what you have tried and exactly what you are trying to do.

The algorithm is quite simple.

Start a loop with i = leftIdx - 1, skip and count all the next elements that equal to list[i](use while() loop). If the number of same elements(including list[i]) is larger than or equal to K, a new Order-K block is found. Now update i to i+count, and continue the loop until i > rightIdx.

Just be careful with the corner cases(empty list, rightIdx<=leftIdx, etc.) and IndexOutOfBound exception.

Community
  • 1
  • 1
Hac
  • 3
  • 1
  • 4
  • But in this case for each query complexity can go upto O(N). Which will be too much. Is there any way to reduce complexity per query by using some data strcture like segment trees or doing some preprocessing – HackAround Apr 09 '17 at 10:00
  • You are right that it's too slow for query. I have no idea about how to do a elegant pre-processing for now. I'll try later. @HackAround – Hac Apr 09 '17 at 14:07