
You are given an integer array 'a' of size 'n'.
For each element in the array, check whether the immediate right element of the array is smaller or not.
If the next element is smaller, update the current index to that element. If not, then -1. The last element does not have any elements on its right.
Input: 'a' = [4, 7, 8, 2, 3, 1]
Output: Modified array 'a' = [-1, -1, 2, -1, 1, -1]
Explanation: In the array 'a':
4 has 7 on its right. Since 7 is not smaller, we update 4 to -1.
7 has 8 on its right. Since 8 is not smaller, we update 7 to -1.
8 has 2 on its right. Since 2 is smaller than 8, we update 8 to 2.
2 has 3 on its right. Since 3 is not smaller, we update 2 to -1.
3 has 1 on its right. Since 1 is smaller than 3, we update 3 to 1.
1 does not have any element on right. So we update 1 to -1.
The first line contains a single integer 'n' denoting the size of the array.
The second line contains 'n' space-separated integers denoting the array elements at various indices.
The output contains 'n' integers in a single line, the modified array 'a'.
You don’t have to print anything; it has already been taken care of. Just implement the given function and make the changes in the given array 'a'.
6
4 7 8 2 3 1
-1 -1 2 -1 1 -1
In the array 'a':
4 has 7 on its right. Since 7 is not smaller, we update 4 to -1.
7 has 8 on its right. Since 8 is not smaller, we update 7 to -1.
8 has 2 on its right. Since 2 is smaller than 8, we update 8 to 2.
2 has 3 on its right. Since 3 is not smaller, we update 2 to -1.
3 has 1 on its right. Since 1 is smaller than 3, we update 3 to 1.
1 does not have any element on right. So we update 1 to -1.
4
1 2 3 4
-1 -1 -1 -1
4
4 3 2 1
3 2 1 -1
The expected time complexity is O(n).
1 <= 'n' <= 10 ^ 5
1 <= 'a[i]' <= 10 ^ 9
Time Limit : 1 sec
Try and think of how you can check for each element, whether its previous element is greater or lesser than current.
The idea is to traverse the array and store the just previous element of the current element in a data structure, say Stack, and then compare its top with the next element
O(n), Where ‘n’ denotes the number of elements in the array
We are traversing the array once to find the final array that takes O(n) time. Hence, the overall Time Complexity is O(n).
O(n), Where ‘n’ denotes the number of elements in the array.
The Space Complexity O(n) is required for Stack to store elements. Hence, the overall Space Complexity is O(n).