`{"id":"1f7a496f-d7c1-4136-a560-d8b22f01d896","name":"Next Greater Element To The Left","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 find \"next greater element on the left\" for all elements of array\r\n4. Input and output is handled for you.\r\n\r\n\"Next greater element on the left\" of an element x is defined as the first element to left of x having value greater than x.\r\nNote -> If an element does not have any element on it's left side greater than it, consider -1 as it's \"next greater element on left\"\r\ne.g.\r\nfor the array [2 5 9 3 1 12 6 8 7]\r\nNext greater for 2 is -1\r\nNext greater for 5 is -1\r\nNext greater for 9 is -1\r\nNext greater for 3 is 9\r\nNext greater for 1 is 3\r\nNext greater for 12 is -1\r\nNext greater for 6 is 12\r\nNext greater for 8 is 12\r\nNext greater for 7 is 8","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\r\n","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>ngl(n,0);\n ngl=solve(arr);\n display(ngl);\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\n public 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[] ngl = solve(a);\r\n display(ngl);\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":""}},"points":10,"difficulty":"medium","sampleInput":"5\r\n5\r\n3\r\n8\r\n-2\r\n7","sampleOutput":"-1\r\n5\r\n-1\r\n8\r\n8\r\n\r\n","questionVideo":"","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":"9bbfafa1-88f9-4b6f-8bde-ab97e7665089","name":"Next Greater Element To The Left","slug":"next-greater-element-to-the-left","type":1}],"next":{"id":"88a925de-351d-435d-93bc-9c8f5be4fc6b","name":"Next Greater Element To The Left Medium MCQ","type":0,"slug":"next-greater-element-to-the-left-medium-mcq"},"prev":{"id":"7b31ec80-c8f4-4819-a14b-33f9fa6329a2","name":"Next Greater Element To The Right","type":3,"slug":"next-greater-element-to-the-right"}}}`

# Next Greater Element To The Left

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 find "next greater element on the left" for all elements of array 4. Input and output is handled for you. "Next greater element on the left" of an element x is defined as the first element to left of x having value greater than x. Note -> If an element does not have any element on it's left side greater than it, consider -1 as it's "next greater element on left" e.g. for the array [2 5 9 3 1 12 6 8 7] Next greater for 2 is -1 Next greater for 5 is -1 Next greater for 9 is -1 Next greater for 3 is 9 Next greater for 1 is 3 Next greater for 12 is -1 Next greater for 6 is 12 Next greater for 8 is 12 Next greater for 7 is 8

`{"id":"1f7a496f-d7c1-4136-a560-d8b22f01d896","name":"Next Greater Element To The Left","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 find \"next greater element on the left\" for all elements of array\r\n4. Input and output is handled for you.\r\n\r\n\"Next greater element on the left\" of an element x is defined as the first element to left of x having value greater than x.\r\nNote -> If an element does not have any element on it's left side greater than it, consider -1 as it's \"next greater element on left\"\r\ne.g.\r\nfor the array [2 5 9 3 1 12 6 8 7]\r\nNext greater for 2 is -1\r\nNext greater for 5 is -1\r\nNext greater for 9 is -1\r\nNext greater for 3 is 9\r\nNext greater for 1 is 3\r\nNext greater for 12 is -1\r\nNext greater for 6 is 12\r\nNext greater for 8 is 12\r\nNext greater for 7 is 8","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\r\n","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>ngl(n,0);\n ngl=solve(arr);\n display(ngl);\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\n public 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[] ngl = solve(a);\r\n display(ngl);\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":""}},"points":10,"difficulty":"medium","sampleInput":"5\r\n5\r\n3\r\n8\r\n-2\r\n7","sampleOutput":"-1\r\n5\r\n-1\r\n8\r\n8\r\n\r\n","questionVideo":"","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":"9bbfafa1-88f9-4b6f-8bde-ab97e7665089","name":"Next Greater Element To The Left","slug":"next-greater-element-to-the-left","type":1}],"next":{"id":"88a925de-351d-435d-93bc-9c8f5be4fc6b","name":"Next Greater Element To The Left Medium MCQ","type":0,"slug":"next-greater-element-to-the-left-medium-mcq"},"prev":{"id":"7b31ec80-c8f4-4819-a14b-33f9fa6329a2","name":"Next Greater Element To The Right","type":3,"slug":"next-greater-element-to-the-right"}}}` Editor

# Next Greater Element To The Left

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 find "next greater element on the left" for all elements of array 4. Input and output is handled for you. "Next greater element on the left" of an element x is defined as the first element to left of x having value greater than x. Note -> If an element does not have any element on it's left side greater than it, consider -1 as it's "next greater element on left" e.g. for the array [2 5 9 3 1 12 6 8 7] Next greater for 2 is -1 Next greater for 5 is -1 Next greater for 9 is -1 Next greater for 3 is 9 Next greater for 1 is 3 Next greater for 12 is -1 Next greater for 6 is 12 Next greater for 8 is 12 Next greater for 7 is 8

## 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;}-1 5 -1 8 8 ```

Discussions

Show Discussion

Related Resources 