Advertisement
tanmay_newatia

Q4

Oct 29th, 2022 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | Source Code | 0 0
  1. for i in range(int(input())):
  2.     n=int(input())
  3.     arr=[int(x) for x in input().split()]
  4.     s = list()
  5.  
  6.     arr1 = [0 for i in range(n)]
  7.  
  8.     for i in range(n - 1, -1, -1):
  9.         while (len(s) > 0 and s[-1] <= arr[i]):
  10.             s.pop()
  11.  
  12.         if (len(s) == 0):
  13.             arr1[i] = -1      
  14.         else:
  15.             arr1[i] = s[-1]    
  16.  
  17.         s.append(arr[i])
  18.      
  19.     for i in range(n):
  20.         print(arr1[i],end=' ')
  21.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement