Next Smaller Value
Chef gave you a list of numbers and challenged you to find the next
smaller value for every number in the list. Can you solve the
challenge?
Note: If for some value, there is no next smaller value then assume
the next smaller value for this number to be -1.
Input Format
. The first line contains an integer N - the length of array.
. The second line contains N integers - A1.A2...AN denoting the
integers in the array.
Output Format
Return the list which contains next smaller value for every number.
Constraints
Example:
Input
3
9 19 12
Output
-1 12 -1
Explanation: There is no next smaller value for both the elements.
Sample input >
2
Sample output
-1 -1
Program:
n=int(input())
l=list(map(int,input().split()))
r=[]
for i in range(n):
f=False
for j in range(i+1,n):
if l[j]<l[i]:
r.append(str(l[j]))
f=True
break
if not f:
r.append("-1")
print(" ".join(r))
0 Comments