Note π - This is a follow up question of Find Maximum Length of Non-Decreasing Subarray Part 1 π¦
You can choose any one index and change its value to any number that you like. What will be the longest non decreasing subarray now:
In the same example as before, the answer would now be:
ans = 6, [2 4 6 8 0 9], by changing 0 -> 8 so the subarray becomes non-decreasing.
Examples
01 Β· Example 1
nums = [2, 4, 6, 8, 0, 9] return = 6
By changing the value at index 4 from 0 to 8, the array becomes [2, 4, 6, 8, 8, 9], which is non-decreasing. The length of this subarray is 6, which is the longest possible non-decreasing subarray after making one change.
Constraints
ππMore Google problems
- Consolidate On-Call RotationsOA Β· Seen Jun 2026
- Detonate Bombs with Chain ReactionsONSITE INTERVIEW Β· Seen May 2026
- Evaluate a Nested Math ExpressionONSITE INTERVIEW Β· Seen May 2026
- Tic-Tac-Toe Game StatusPHONE SCREEN Β· Seen May 2026
- Longest Dictionary TokenizationPHONE SCREEN Β· Seen May 2026
- Minimum Cars for Rental RequestsONSITE INTERVIEW Β· Seen Apr 2026
- Shortest Path with Mandatory WaypointONSITE INTERVIEW Β· Seen Apr 2026
- Count Divisible Coin SelectionsOA Β· Seen Dec 2025
public int longestNonDecreasingSubarray(int[] nums) {
// write your code here
}
nums[2, 4, 6, 8, 0, 9]
expected6
sign in to submit