You are given an array ‘ARR’, and you are supposed to answer 'Q' queries. In each query you have three integers, ‘L’, ‘R’, and ‘X’. You are supposed to find if there exists an element in the subarray [ARR[L], ARR[L+1], …, ARR[R-1], ARR[R] that occurs at least ‘X’ number of times under the following conditions:
1. 0 <= L <= R < N, where L is the left-most index of the subarray, R is the right-most index of the subarray, and N is the number of elements in the array ‘ARR’.
2. 2 * X> R - L + 1 i.e. the threshold is always greater than the majority length of the subarray.
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line contains an integer ‘N’, which denotes the number of elements in the array ‘ARR’.
The second line contains 'N' integers denoting the elements of the array ‘ARR’.
The third line contains an integer ‘Q’, which denotes the number of queries for the given array.
The next 'Q' lines contain three integers, ‘L’, ‘R’, and ‘X’, where 'L' is the left-most index of the subarray, 'R' is the right-most index of the subarray, and ‘X’ is the threshold value for the given query.
Output Format:
For each test case, return the majority elements if it exists. Otherwise, return -1.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1<= T <= 50
1 <= N <= 2* 10^4
1 <= ARR[i] <= 2* 10^4
1 <= Q <= 10^4
0 <= L <= R < N
Time Limit: 1 sec
2
6
2 2 1 1 2 2
3
0 5 4
0 3 3
2 3 2
6
1 2 3 4 5 6
3
0 5 4
0 3 3
2 3 2
2
-1
1
-1
-1
-1
In the first test case, the array elements are [2, 2, 1, 1, 2,2].
In the first query, we have to find a number from indices 0 to 5 that occur at least 4 times. In this range, 2 satisfies the conditions.
In the second query, we have to find a number from indices 0 to 3 that occur at least 3 times. In this range, no number satisfies the condition. So, the answer is -1.
In the third query, we have to find a number from indices 2 to 3 that occur at least 2 times. In this range, 1 satisfies the conditions.
In the second test case, the array elements are [1, 2, 3, 4, 5, 6].
In the first query, we have to find a number from indices 0 to 5 that occur at least 4 times. In this range, no number satisfies the condition. So, the answer is -1.
In the second query, we have to find a number from indices 0 to 3 that occur at least 3 times. In this range, no number satisfies the condition. So, the answer is -1.
In the third query, we have to find a number from indices 2 to 3 that occur at least 2 times. In this range, no number satisfies the condition. So, the answer is -1.
2
10
2 2 1 2 1 2 2 1 1 2
4
2 5 4
0 5 6
0 1 2
2 3 2
10
2 2 1 2 1 2 2 1 1 2
4
6 6 1
0 3 3
4 9 6
4 8 4
-1
-1
2
-1
2
2
-1
-1
Can you traverse over the given sub-array and find the count of every element?
The idea is to traverse over the given sub-array and store the count of every element in a hash map. Then, traverse over the hashmap, if the count of any element is greater than the threshold, return that element.
O(N*Q), where ‘N’ is the number of elements in the array and ‘Q’ is the number of queries.
Preprocessing: O(1) time
SubarrayMajorityFinder: O(N) time.
As we are iterating over the given sub-array that may take O(N) in the worst case. Hence the overall time complexity for all the queries is O(N*Q).
O(N), where ‘N’ is the number of elements in the array.
As we are storing the count of every element in a hash map that may grow up to size ‘N’ in the worst case. Hence, the overall space complexity is O(N).