Problem of the day
You are given an array 'ARR' of distinct positive integers. You need to find all numbers that are in the range of the elements of the array, but not in the array. The missing elements should be printed in sorted order.
Example:If the given array is [4, 2, 9] then you should print "3 5 6 7 8". As all these elements lie in the range but not present in the array.
The first line of input contains a single integer 'T', representing the number of test cases.
The first line of input contains an integer 'N' representing the length of the array.
The next line contains 'N' single space-separated integers representing elements of the array 'ARR'.
Output Format :
For each test case, return the list of elements that are not in the array 'ARR'.
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
0 <= 'ARR[i]' <= 150000
Where 'ARR[i]' denotes the array element.
Time Limit: 1 sec
2
4
1 2 4 6
3
1 2 3
3 5
[Blank]
In test case 1, As only 3 and 5 are not in the array and both lie in the range of the array. Thus answer would be "3 5" i.e sorted order.
In test case 2, Since all the elements are present from 1 to 3, the empty list is returned as a answer.
2
3
7 4 9
4
3 6 7 4
3 5 6 8
5
In test case 1, As only 3, 5, 6 and 8 are not in the array and lie in the range of the array. Thus answer would be "3 5 6 8" i.e sorted order.
In test case 2, As only 5 is not in the array and lie in the range of the array. Thus answer would be "5".
Think of searching all the numbers between the maximum element and the minimum element present in the array.
The steps are as follows:
O(MAX * N), Where ‘N’ is the length of the array and ‘MAX’ is the maximum element of the array.
Since we will be searching for each number between the minimum and maximum element that will take O(MAX) time. Thus total time complexity will be O(N * Max).
O(1).
Since we are using constant extra space. Thus the space complexity will be O(1).