
You are given,’coupons’ = [1, 2, 3, 4, 5, 3, 6], here you can pick up coupons that are [3, 4, 5, 3] and avail coupon number ‘3’, The total number of coupons picked up are 4. Hence the answer is 4.
The first line of input contains the integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of coupons.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array ‘coupons’.
For each test case, print a single integer representing the minimum number of coupons you have picked up from the given array.
Print a separate line for each test case.
1 <= T <= 10
2 <= N <= 10^6
0 <= coupons[i] <= 10^9
Time Limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the function.
In this approach, we will iterate through all the coupons, and if we find any coupon we as the same number as the current coupon, we will check its distance from the current coupon, and if it’s greater than the maximum distance till now, then we will update the maximum distance.
Algorithm:
In this approach, we will maintain a Hashmap. In it, we will store the index of the current coupon number, and if the coupon number is already in the hash map, we will get its last occurrence and calculate the number of coupons to be picked up.
Each time we calculate the distance, we will check against the current minimum distance.
Algorithm: