Longest Non-Decreasing Subarray After One Modification
Updated
Problem statement
You are given an integer array nums. You may modify at most one element, replacing it with any integer.
Return the maximum possible length of a contiguous non-decreasing subarray after the optional modification. A subarray is non-decreasing when every element after the first is greater than or equal to the preceding element.
Function
maxNonDecreasingLength(nums: int[]) → intExamples
Example 1
nums = [1,2,5,3,4]return = 5Replace 5 with 3 to obtain [1,2,3,3,4].
Example 2
nums = [4,2,3,1,5]return = 4Replace the value 1 at index 3 with 3. The subarray [2,3,3,5] is then non-decreasing.
Example 3
nums = [5,4,3]return = 2One adjacent pair can be made non-decreasing, but all three positions cannot be fixed with one modification.
Constraints
1 <= nums.length <= 200000.-10^9 <= nums[i] <= 10^9.- The replacement value may be any signed 64-bit integer.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Maximum Subarray Sum with One DeletionServiceNow · PHONE SCREENServiceNow interview questions
- Maximum Product of a Contiguous SubarrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- Fewest Coins CombinationBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions
- House RobberVisa · PHONE SCREENVisa interview questions