Subarray Removal
Updated
Problem statement
Given an array arr of n integers, find the number of its subarrays such that removing the subarray creates a non-empty array that is sorted in increasing order.
Note: A subarray is defined as any contiguous segment of the array.
Function
getNumSubarrays(arr: int[]) → longExamples
Example 1
arr = [1, 2, 1, 2]return = 7
There are 7 subarrays that on removal produce a non-empty sorted array. Hence, the answer is 7.
The subarrays [3], [3, 1] and [1, 2] give a non-empty sorted array on removal.
Example 2
arr = [3, 1, 2]return = 3
The subarrays [3], [3, 1] and [1, 2] give a non-empty sorted array on removal.
Example 3
arr = [1, 2, 2, 6, 1, 3]return = 10
The subarrays [1, 2, 2, 6], [1, 2, 2, 6, 1], [2, 2, 6], [2, 2, 6, 1], [2, 6], [2, 6, 1], [6], [6, 1], [1, 3] and [1] give a non-empty sorted array on removal.
Constraints
1 ≤ n ≤ 2×10^51 ≤ arr[i] ≤ 10^9
More The D.E. Shaw Group interview questions
- IPOPHONE SCREEN · Seen Nov 2026
- Print Numbers in Turn with Three ThreadsPHONE SCREEN · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Koko Eating BananasPHONE SCREEN · Seen Nov 2026
- Sum of Smallest Non-DivisorsPHONE SCREEN · Seen Nov 2026
- Count Lucky TicketsOA · Seen Nov 2026
- Count Palindrome Addition ChoicesOA · Seen Nov 2026
- Coordinate Odd and Even Workers in OrderONSITE INTERVIEW · Seen Oct 2026
- Minimum Taps to Water a GardenONSITE INTERVIEW · Seen Oct 2026
- Remove the Fewest Magic BeansPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Fastest Average Speed over a Rolling KilometerOptiver · OAOptiver interview questions
- Maximum Points in a Perimeter-Bounded RectangleAmerican Express · OAAmerican Express interview questions
- Trapping Rain WaterGoogle · OAGoogle interview questions
- Running Through Walls with Limited EnergyScale AI · OAScale AI interview questions
- Configuration SystemPostman · OAPostman interview questions
- Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions
- Garden Sprinkler PlacementAirbnb · OAAirbnb interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions