`{"id":"ce6c6d6d-ee82-48d0-8b16-0490afb2b6bc","name":"Next Greater Element I","description":"1: You are given two integer arrays query and nums containing unique elements.\r\n2: Here query is a subset of nums that means all elements of query are also present in nums.\r\n3: Find all the next greater numbers for query's elements in the corresponding places of nums. If it does not exist -1 is answer for this number.\r\n4: You are required to complete the body of the function nextGreaterI(int nums[], int query[]) which returns the answer array containing next greater element's.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1: 1 &lt;= query.length &lt;= nums.length &lt;= 1000\r\n2: 0 &lt;= query[i], nums[i] &lt;= 10^9\r\n3: All integers in query and nums are unique.\r\n4: All the integers of query also appear in nums","sampleCode":{"cpp":{"code":"#include<iostream>\n#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] ;\n cout<<endl;\n \n }\n}\n\nvector<int> nge;\nvector<int> nextGreaterRight(vector<int> &arr)\n{\n int n = arr.size();\n nge.resize(n, 0);\n nge[n - 1] = -1;\n stack<int>st;\n st.push(arr[n - 1]);\n for (int i = n - 2; i >= 0; i--)\n {\n while (st.size() > 0 && st.top() <= arr[i])\n {\n st.pop();\n }\n if (st.size() == 0)\n {\n nge[i] = -1;\n }\n else {\n nge[i] = st.top();\n }\n st.push(arr[i]);\n }\n return nge;\n}\n\nvector<int> nextgreater(vector<int> nums,vector<int> query)\n{\n //write your code here\n}\n\nint main(int argc, char** argv)\n{\n string str;\n getline(cin,str);\n\n vector<int> query;\n int idx = 0;\n string curr = \"\";\n while(idx < str.size()) {\n if(str[idx] == ' '){\n query.push_back(stoi(curr));\n curr = \"\";\n } else {\n curr.push_back(str[idx]);\n }\n idx++;\n }\n query.push_back(stoi(curr));\n \n string str1;\n getline(cin,str1);\n vector<int> nums; \n idx = 0;\n curr = \"\";\n while(idx < str1.size()) {\n if(str1[idx] == ' '){\n nums.push_back(stoi(curr));\n curr = \"\";\n } else {\n curr.push_back(str1[idx]);\n }\n idx++;\n }\n nums.push_back(stoi(curr));\n \n \n vector<int> ans = nextgreater(nums,query);\n cout<<ans.size()<<endl;\n display(ans);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static int[] nextGreaterElement(int[] nums, int[] query) {\r\n Stack<Integer> st = new Stack<>();\r\n \r\n return null;\r\n }\r\n\r\n public static int[] getArr(String s){\r\n String nums[] = s.split(\" \");\r\n int n = nums.length;\r\n int ar[] = new int[n];\r\n for(int i=0;i<n;i++){\r\n ar[i] = Integer.parseInt(nums[i]);\r\n }\r\n return ar;\r\n }\r\n \r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int query[] = getArr(read.readLine());\r\n int nums[] = getArr(read.readLine());\r\n \r\n int ans[] = nextGreaterElement(nums, query);\r\n\r\n int n = ans.length;\r\n\r\n System.out.println(n);\r\n for(int e: ans){\r\n System.out.println(e);\r\n }\r\n \r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4 1 2\r\n1 3 4 2","sampleOutput":"3\r\n-1\r\n3\r\n-1\r\n","questionVideo":"https://www.youtube.com/embed/aiB9r8oeVp4","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":"6b95869d-62b9-48bf-84f9-87c481617149","name":"Next Greater Element I","slug":"next-greater-element-i","type":1}],"next":{"id":"c92058dd-1aee-4152-b1ab-535f6f88e597","name":"Next Greater Element I MCQ","type":0,"slug":"next-greater-element-i-mcq"},"prev":{"id":"4c2dfe70-c9c8-49ee-99ba-97f55ce57238","name":"Next Smaller Element To The Left","type":3,"slug":"next-smaller-element-to-the-left"}}}`

# Next Greater Element I

1: You are given two integer arrays query and nums containing unique elements. 2: Here query is a subset of nums that means all elements of query are also present in nums. 3: Find all the next greater numbers for query's elements in the corresponding places of nums. If it does not exist -1 is answer for this number. 4: You are required to complete the body of the function nextGreaterI(int nums[], int query[]) which returns the answer array containing next greater element's.

`{"id":"ce6c6d6d-ee82-48d0-8b16-0490afb2b6bc","name":"Next Greater Element I","description":"1: You are given two integer arrays query and nums containing unique elements.\r\n2: Here query is a subset of nums that means all elements of query are also present in nums.\r\n3: Find all the next greater numbers for query's elements in the corresponding places of nums. If it does not exist -1 is answer for this number.\r\n4: You are required to complete the body of the function nextGreaterI(int nums[], int query[]) which returns the answer array containing next greater element's.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1: 1 &lt;= query.length &lt;= nums.length &lt;= 1000\r\n2: 0 &lt;= query[i], nums[i] &lt;= 10^9\r\n3: All integers in query and nums are unique.\r\n4: All the integers of query also appear in nums","sampleCode":{"cpp":{"code":"#include<iostream>\n#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] ;\n cout<<endl;\n \n }\n}\n\nvector<int> nge;\nvector<int> nextGreaterRight(vector<int> &arr)\n{\n int n = arr.size();\n nge.resize(n, 0);\n nge[n - 1] = -1;\n stack<int>st;\n st.push(arr[n - 1]);\n for (int i = n - 2; i >= 0; i--)\n {\n while (st.size() > 0 && st.top() <= arr[i])\n {\n st.pop();\n }\n if (st.size() == 0)\n {\n nge[i] = -1;\n }\n else {\n nge[i] = st.top();\n }\n st.push(arr[i]);\n }\n return nge;\n}\n\nvector<int> nextgreater(vector<int> nums,vector<int> query)\n{\n //write your code here\n}\n\nint main(int argc, char** argv)\n{\n string str;\n getline(cin,str);\n\n vector<int> query;\n int idx = 0;\n string curr = \"\";\n while(idx < str.size()) {\n if(str[idx] == ' '){\n query.push_back(stoi(curr));\n curr = \"\";\n } else {\n curr.push_back(str[idx]);\n }\n idx++;\n }\n query.push_back(stoi(curr));\n \n string str1;\n getline(cin,str1);\n vector<int> nums; \n idx = 0;\n curr = \"\";\n while(idx < str1.size()) {\n if(str1[idx] == ' '){\n nums.push_back(stoi(curr));\n curr = \"\";\n } else {\n curr.push_back(str1[idx]);\n }\n idx++;\n }\n nums.push_back(stoi(curr));\n \n \n vector<int> ans = nextgreater(nums,query);\n cout<<ans.size()<<endl;\n display(ans);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static int[] nextGreaterElement(int[] nums, int[] query) {\r\n Stack<Integer> st = new Stack<>();\r\n \r\n return null;\r\n }\r\n\r\n public static int[] getArr(String s){\r\n String nums[] = s.split(\" \");\r\n int n = nums.length;\r\n int ar[] = new int[n];\r\n for(int i=0;i<n;i++){\r\n ar[i] = Integer.parseInt(nums[i]);\r\n }\r\n return ar;\r\n }\r\n \r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int query[] = getArr(read.readLine());\r\n int nums[] = getArr(read.readLine());\r\n \r\n int ans[] = nextGreaterElement(nums, query);\r\n\r\n int n = ans.length;\r\n\r\n System.out.println(n);\r\n for(int e: ans){\r\n System.out.println(e);\r\n }\r\n \r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4 1 2\r\n1 3 4 2","sampleOutput":"3\r\n-1\r\n3\r\n-1\r\n","questionVideo":"https://www.youtube.com/embed/aiB9r8oeVp4","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":"6b95869d-62b9-48bf-84f9-87c481617149","name":"Next Greater Element I","slug":"next-greater-element-i","type":1}],"next":{"id":"c92058dd-1aee-4152-b1ab-535f6f88e597","name":"Next Greater Element I MCQ","type":0,"slug":"next-greater-element-i-mcq"},"prev":{"id":"4c2dfe70-c9c8-49ee-99ba-97f55ce57238","name":"Next Smaller Element To The Left","type":3,"slug":"next-smaller-element-to-the-left"}}}`

Editor

# Next Greater Element I

easy

1: You are given two integer arrays query and nums containing unique elements. 2: Here query is a subset of nums that means all elements of query are also present in nums. 3: Find all the next greater numbers for query's elements in the corresponding places of nums. If it does not exist -1 is answer for this number. 4: You are required to complete the body of the function nextGreaterI(int nums[], int query[]) which returns the answer array containing next greater element's.

## Constraints

1: 1 <= query.length <= nums.length <= 1000 2: 0 <= query[i], nums[i] <= 10^9 3: All integers in query and nums are unique. 4: All the integers of query also appear in nums

## 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;}4 1 2 1 3 4 2```

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

Question Video

Discussions

Show Discussion

Related Resources