{"id":"c8ddfd44-4e1c-4274-9b8e-635b1acb5ee1","name":"Next Smaller 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 smaller element on the right\" for all elements of array\r\n4. Input and output is handled for you.\r\n\r\n\"Next smaller element on the right\" of an element x is defined as the first element to right of x having value smaller than x.\r\nNote -> If an element does not have any element on it's right side smaller than it, consider -1 as it's \"next smaller element on right\"\r\ne.g.\r\nfor the array [2 5 9 3 1 12 6 8 7]\r\nNext smaller for 2 is 1\r\nNext smaller for 5 is 3\r\nNext smaller for 9 is 3\r\nNext smaller for 3 is 1\r\nNext smaller for 1 is -1\r\nNext smaller for 12 is 6\r\nNext smaller for 6 is -1\r\nNext smaller for 8 is 7\r\nNext smaller 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\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 \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\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[] 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":""}},"points":10,"difficulty":"medium","sampleInput":"5\r\n5\r\n3\r\n8\r\n-2\r\n7","sampleOutput":"3\r\n-2\r\n-2\r\n-1\r\n-1\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":"4e24f4a9-ebbf-4a13-96bc-689eddf05f5f","name":"Next Smaller Element To The Right","slug":"next-smaller-element-to-the-right","type":1}],"next":{"id":"a3e5da88-a79d-453c-9800-e37380051ecc","name":"Next Smaller Element To The Right MCQ","type":0,"slug":"next-smaller-element-to-the-right-mcq"},"prev":{"id":"642ddd34-5618-4dd6-9684-c3527306f3df","name":"Next Greater Element To The Left","type":3,"slug":"next-greater-element-to-the-left"}}}

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

{"id":"c8ddfd44-4e1c-4274-9b8e-635b1acb5ee1","name":"Next Smaller 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 smaller element on the right\" for all elements of array\r\n4. Input and output is handled for you.\r\n\r\n\"Next smaller element on the right\" of an element x is defined as the first element to right of x having value smaller than x.\r\nNote -> If an element does not have any element on it's right side smaller than it, consider -1 as it's \"next smaller element on right\"\r\ne.g.\r\nfor the array [2 5 9 3 1 12 6 8 7]\r\nNext smaller for 2 is 1\r\nNext smaller for 5 is 3\r\nNext smaller for 9 is 3\r\nNext smaller for 3 is 1\r\nNext smaller for 1 is -1\r\nNext smaller for 12 is 6\r\nNext smaller for 6 is -1\r\nNext smaller for 8 is 7\r\nNext smaller 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\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 \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\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[] 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":""}},"points":10,"difficulty":"medium","sampleInput":"5\r\n5\r\n3\r\n8\r\n-2\r\n7","sampleOutput":"3\r\n-2\r\n-2\r\n-1\r\n-1\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":"4e24f4a9-ebbf-4a13-96bc-689eddf05f5f","name":"Next Smaller Element To The Right","slug":"next-smaller-element-to-the-right","type":1}],"next":{"id":"a3e5da88-a79d-453c-9800-e37380051ecc","name":"Next Smaller Element To The Right MCQ","type":0,"slug":"next-smaller-element-to-the-right-mcq"},"prev":{"id":"642ddd34-5618-4dd6-9684-c3527306f3df","name":"Next Greater Element To The Left","type":3,"slug":"next-greater-element-to-the-left"}}}
plane

Editor


Loading...

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

5 5 3 8 -2 7

Sample Output

3 -2 -2 -1 -1

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode