`{"id":"12418707-f39e-42a3-b99b-2dc169c687d7","name":"Next Greater Element To The Right","description":"1. You are given a number n, representing the size of array a.\r\n2. You are given n numbers, representing elements of array a.\r\n3. You are required to \"next greater element on the right\" for all elements of array\r\n4. Input and output is handled for you.\r\n\r\n\"Next greater element on the right\" of an element x is defined as the first element to right of x having value greater than x.\r\nNote -> If an element does not have any element on it's right side greater than it, consider -1 as it's \"next greater element on right\"\r\ne.g.\r\nfor the array [2 5 9 3 1 12 6 8 7]\r\nNext greater for 2 is 5\r\nNext greater for 5 is 9\r\nNext greater for 9 is 12\r\nNext greater for 3 is 12\r\nNext greater for 1 is 12\r\nNext greater for 12 is -1\r\nNext greater for 6 is 8\r\nNext greater for 8 is -1\r\nNext greater for 7 is -1","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"0 &lt;= n &lt; 10^5\r\n-10^9 &lt;= a[i] &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\nvoid display(vector<int>a){\n for(int i=0;i<a.size();i++)\n {\n cout<<a[i]<<endl;\n }\n}\nvector<int> solve(vector<int>arr)\n{\n //write your code here\n}\nint main(int argc, char **argv)\n{ \n int n;\n cin>>n;\n vector<int>arr(n,0);\n for(int i=0;i<n;i++)\n {\n cin>>arr[i];\n }\n vector<int>nge(n,0);\n nge=solve(arr);\n display(nge);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n public static void display(int[] a){\r\n StringBuilder sb = new StringBuilder();\r\n\r\n for(int val: a){\r\n sb.append(val + \"\\n\");\r\n }\r\n System.out.println(sb);\r\n }\r\n\r\npublic static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(br.readLine());\r\n int[] a = new int[n];\r\n for(int i = 0; i < n; i++){\r\n a[i] = Integer.parseInt(br.readLine());\r\n }\r\n\r\n int[] nge = solve(a);\r\n display(nge);\r\n }\r\n\r\n public static int[] solve(int[] arr){\r\n // solve\r\n return null;\r\n }\r\n\r\n}"},"python":{"code":"from sys import stdin\ndef display(arr) :\n\tfor i in range(len(arr)) :\n\t\tprint(arr[i], end = \"\\n\")\ndef solve(arr2):\n #write your code here\n\nn = int(input())\narr =[]\nfor i in range (n):\n x = int(input())\n arr.append(x)\n\nnge = solve(arr)\ndisplay(nge)"}},"points":10,"difficulty":"medium","sampleInput":"5\r\n5\r\n3\r\n8\r\n-2\r\n7","sampleOutput":"8\r\n8\r\n-1\r\n7\r\n-1","questionVideo":"https://www.youtube.com/embed/Zy9XnXw8E7U","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"4f7ddcc7-d2d6-4384-9163-067d37fb4880","name":"Next Greater Element To The Right","slug":"next-greater-element-to-the-right","type":1}],"next":{"id":"af605437-374a-4272-b364-9717d877a29c","name":"Next Greater Element To The Right Medium MCQ","type":0,"slug":"next-greater-element-to-the-right-medium-mcq"},"prev":null}}`

# Next Greater Element To The Right

1. You are given a number n, representing the size of array a. 2. You are given n numbers, representing elements of array a. 3. You are required to "next greater element on the right" for all elements of array 4. Input and output is handled for you. "Next greater element on the right" of an element x is defined as the first element to right of x having value greater than x. Note -> If an element does not have any element on it's right side greater than it, consider -1 as it's "next greater element on right" e.g. for the array [2 5 9 3 1 12 6 8 7] Next greater for 2 is 5 Next greater for 5 is 9 Next greater for 9 is 12 Next greater for 3 is 12 Next greater for 1 is 12 Next greater for 12 is -1 Next greater for 6 is 8 Next greater for 8 is -1 Next greater for 7 is -1

`{"id":"12418707-f39e-42a3-b99b-2dc169c687d7","name":"Next Greater Element To The Right","description":"1. You are given a number n, representing the size of array a.\r\n2. You are given n numbers, representing elements of array a.\r\n3. You are required to \"next greater element on the right\" for all elements of array\r\n4. Input and output is handled for you.\r\n\r\n\"Next greater element on the right\" of an element x is defined as the first element to right of x having value greater than x.\r\nNote -> If an element does not have any element on it's right side greater than it, consider -1 as it's \"next greater element on right\"\r\ne.g.\r\nfor the array [2 5 9 3 1 12 6 8 7]\r\nNext greater for 2 is 5\r\nNext greater for 5 is 9\r\nNext greater for 9 is 12\r\nNext greater for 3 is 12\r\nNext greater for 1 is 12\r\nNext greater for 12 is -1\r\nNext greater for 6 is 8\r\nNext greater for 8 is -1\r\nNext greater for 7 is -1","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"0 &lt;= n &lt; 10^5\r\n-10^9 &lt;= a[i] &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\nvoid display(vector<int>a){\n for(int i=0;i<a.size();i++)\n {\n cout<<a[i]<<endl;\n }\n}\nvector<int> solve(vector<int>arr)\n{\n //write your code here\n}\nint main(int argc, char **argv)\n{ \n int n;\n cin>>n;\n vector<int>arr(n,0);\n for(int i=0;i<n;i++)\n {\n cin>>arr[i];\n }\n vector<int>nge(n,0);\n nge=solve(arr);\n display(nge);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n public static void display(int[] a){\r\n StringBuilder sb = new StringBuilder();\r\n\r\n for(int val: a){\r\n sb.append(val + \"\\n\");\r\n }\r\n System.out.println(sb);\r\n }\r\n\r\npublic static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(br.readLine());\r\n int[] a = new int[n];\r\n for(int i = 0; i < n; i++){\r\n a[i] = Integer.parseInt(br.readLine());\r\n }\r\n\r\n int[] nge = solve(a);\r\n display(nge);\r\n }\r\n\r\n public static int[] solve(int[] arr){\r\n // solve\r\n return null;\r\n }\r\n\r\n}"},"python":{"code":"from sys import stdin\ndef display(arr) :\n\tfor i in range(len(arr)) :\n\t\tprint(arr[i], end = \"\\n\")\ndef solve(arr2):\n #write your code here\n\nn = int(input())\narr =[]\nfor i in range (n):\n x = int(input())\n arr.append(x)\n\nnge = solve(arr)\ndisplay(nge)"}},"points":10,"difficulty":"medium","sampleInput":"5\r\n5\r\n3\r\n8\r\n-2\r\n7","sampleOutput":"8\r\n8\r\n-1\r\n7\r\n-1","questionVideo":"https://www.youtube.com/embed/Zy9XnXw8E7U","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"4f7ddcc7-d2d6-4384-9163-067d37fb4880","name":"Next Greater Element To The Right","slug":"next-greater-element-to-the-right","type":1}],"next":{"id":"af605437-374a-4272-b364-9717d877a29c","name":"Next Greater Element To The Right Medium MCQ","type":0,"slug":"next-greater-element-to-the-right-medium-mcq"},"prev":null}}` Editor

# Next Greater Element To The Right

medium

1. You are given a number n, representing the size of array a. 2. You are given n numbers, representing elements of array a. 3. You are required to "next greater element on the right" for all elements of array 4. Input and output is handled for you. "Next greater element on the right" of an element x is defined as the first element to right of x having value greater than x. Note -> If an element does not have any element on it's right side greater than it, consider -1 as it's "next greater element on right" e.g. for the array [2 5 9 3 1 12 6 8 7] Next greater for 2 is 5 Next greater for 5 is 9 Next greater for 9 is 12 Next greater for 3 is 12 Next greater for 1 is 12 Next greater for 12 is -1 Next greater for 6 is 8 Next greater for 8 is -1 Next greater for 7 is -1

## Constraints

0 <= n < 10^5 -10^9 <= a[i] <= 10^9

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 5 3 8 -2 7```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}8 8 -1 7 -1```

Question Video

Discussions

Show Discussion

Related Resources 