Advertisement
Programmin-in-Python

Python program for "Next Max Number"

Feb 5th, 2022
1,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. """
  2. Problem :-
  3. ----------
  4.  
  5. Given an array arr of size N having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array. If no such element exists, output -1.
  6.  
  7. Test Case 1 :
  8.    Input: N = 4, arr = [1, 3, 2, 4]
  9.    Output: 3 4 4 -1
  10.  
  11. Test Case 2 :
  12.    Input: N = 4, arr = [4, 3, 2, 1]
  13.    Output:-1 -1 -1 -1
  14. """
  15.  
  16. arr = [1, 3, 2, 4]
  17. #arr = [4, 3, 2, 1]
  18.  
  19. res = []
  20.  
  21. for i in range(len(arr)):
  22.     for j in range(i+1,len(arr)):
  23.         if arr[j]>arr[i]:
  24.             res.append(arr[j])
  25.             break
  26.     else:
  27.         res.append(-1)
  28. print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement