{"id":"37ed153a-bde2-4d4e-ab69-893dbbfecaf8","name":"Number Of Valid Subarrays","description":"Given an array nums of integers, return the number of non-empty continuous subarrays that satisfy the following condition:\r\nThe leftmost element of the subarray is not larger than other elements in the subarray.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= nums.length &lt;= 50000\r\n2. 0 &lt;= nums[i] &lt;= 100000","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<stack>\n#include<vector>\nusing namespace std;\n\nint validSubarrays(vector<int> nums){\n // write your code here\n}\n\nint main(){\n int n;\n cin >> n;\n vector<int> nums(n);\n for(int i=0; i<n; i++){\n cin >> nums[i];\n }\n cout << validSubarrays(nums);\n}\n"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int validSubarrays(int[] nums) {\r\n return 0;\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 n = Integer.parseInt(read.readLine());\r\n int nums[] = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n nums[i] = Integer.parseInt(read.readLine());\r\n }\r\n\r\n int count = validSubarrays(nums);\r\n\r\n System.out.println(count);\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"5\r\n1\r\n4\r\n2\r\n5\r\n3","sampleOutput":"11\r\n","questionVideo":"https://www.youtube.com/embed/P0Fz-ZIVY5k","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":"0d5aa694-b447-45d7-8e36-878a1ba58cc9","name":"Number Of Valid Subarrays","slug":"number-of-valid-subarrays","type":1}],"next":{"id":"3d774223-ddde-47f8-8d0a-09076ce5ddba","name":"Number Of Valid Subarrays Hard MCQ","type":0,"slug":"number-of-valid-subarrays-hard-mcq"},"prev":{"id":"68d8c5e0-684f-4aab-8d20-50bb33c56104","name":"Trapping Rain Water - 2","type":3,"slug":"trapping-rain-water-2"}}}

Number Of Valid Subarrays

Given an array nums of integers, return the number of non-empty continuous subarrays that satisfy the following condition: The leftmost element of the subarray is not larger than other elements in the subarray.

{"id":"37ed153a-bde2-4d4e-ab69-893dbbfecaf8","name":"Number Of Valid Subarrays","description":"Given an array nums of integers, return the number of non-empty continuous subarrays that satisfy the following condition:\r\nThe leftmost element of the subarray is not larger than other elements in the subarray.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= nums.length &lt;= 50000\r\n2. 0 &lt;= nums[i] &lt;= 100000","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<stack>\n#include<vector>\nusing namespace std;\n\nint validSubarrays(vector<int> nums){\n // write your code here\n}\n\nint main(){\n int n;\n cin >> n;\n vector<int> nums(n);\n for(int i=0; i<n; i++){\n cin >> nums[i];\n }\n cout << validSubarrays(nums);\n}\n"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int validSubarrays(int[] nums) {\r\n return 0;\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 n = Integer.parseInt(read.readLine());\r\n int nums[] = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n nums[i] = Integer.parseInt(read.readLine());\r\n }\r\n\r\n int count = validSubarrays(nums);\r\n\r\n System.out.println(count);\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"5\r\n1\r\n4\r\n2\r\n5\r\n3","sampleOutput":"11\r\n","questionVideo":"https://www.youtube.com/embed/P0Fz-ZIVY5k","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":"0d5aa694-b447-45d7-8e36-878a1ba58cc9","name":"Number Of Valid Subarrays","slug":"number-of-valid-subarrays","type":1}],"next":{"id":"3d774223-ddde-47f8-8d0a-09076ce5ddba","name":"Number Of Valid Subarrays Hard MCQ","type":0,"slug":"number-of-valid-subarrays-hard-mcq"},"prev":{"id":"68d8c5e0-684f-4aab-8d20-50bb33c56104","name":"Trapping Rain Water - 2","type":3,"slug":"trapping-rain-water-2"}}}
plane

Editor


Loading...

Number Of Valid Subarrays

hard

Given an array nums of integers, return the number of non-empty continuous subarrays that satisfy the following condition: The leftmost element of the subarray is not larger than other elements in the subarray.

Constraints

1. 1 <= nums.length <= 50000 2. 0 <= nums[i] <= 100000

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

5 1 4 2 5 3

Sample Output

11

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode