{"id":"23439c7a-a8d7-4808-a91b-f853e9055d54","name":"Next Smaller 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 \"next smaller element on the left\" for all elements of array\r\n4. Input and output is handled for you.\r\n\r\n\"Next smaller element on the left\" of an element x is defined as the first element to left of x having value smaller than x.\r\nNote -> If an element does not have any element on it's left side smaller than it, consider -1 as it's \"next smaller element on left\"\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 2\r\nNext smaller for 9 is 5\r\nNext smaller for 3 is 2\r\nNext smaller for 1 is -1\r\nNext smaller for 12 is 1\r\nNext smaller for 6 is 1\r\nNext smaller for 8 is 6\r\nNext smaller for 7 is 6","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>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\r\n","sampleOutput":"-1\r\n-1\r\n3\r\n-1\r\n-2\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":"1a647acb-ce58-4812-93bd-78e68c735aac","name":"Next Smaller Element To The Left","slug":"next-smaller-element-to-the-left","type":1}],"next":{"id":"f0fcf6ae-da7f-4eb5-914d-6ab252648ce4","name":"Next Smaller Element To The Left MCQ","type":0,"slug":"next-smaller-element-to-the-left-mcq"},"prev":{"id":"f0cdfeac-2a84-4f84-b0c3-75b2bbcfefe9","name":"Next Smaller Element To The Right","type":3,"slug":"next-smaller-element-to-the-right"}}}

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

{"id":"23439c7a-a8d7-4808-a91b-f853e9055d54","name":"Next Smaller 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 \"next smaller element on the left\" for all elements of array\r\n4. Input and output is handled for you.\r\n\r\n\"Next smaller element on the left\" of an element x is defined as the first element to left of x having value smaller than x.\r\nNote -> If an element does not have any element on it's left side smaller than it, consider -1 as it's \"next smaller element on left\"\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 2\r\nNext smaller for 9 is 5\r\nNext smaller for 3 is 2\r\nNext smaller for 1 is -1\r\nNext smaller for 12 is 1\r\nNext smaller for 6 is 1\r\nNext smaller for 8 is 6\r\nNext smaller for 7 is 6","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>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\r\n","sampleOutput":"-1\r\n-1\r\n3\r\n-1\r\n-2\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":"1a647acb-ce58-4812-93bd-78e68c735aac","name":"Next Smaller Element To The Left","slug":"next-smaller-element-to-the-left","type":1}],"next":{"id":"f0fcf6ae-da7f-4eb5-914d-6ab252648ce4","name":"Next Smaller Element To The Left MCQ","type":0,"slug":"next-smaller-element-to-the-left-mcq"},"prev":{"id":"f0cdfeac-2a84-4f84-b0c3-75b2bbcfefe9","name":"Next Smaller Element To The Right","type":3,"slug":"next-smaller-element-to-the-right"}}}
plane

Editor


Loading...

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

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

-1 -1 3 -1 -2

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode