Longest Sorted Subsegment
Given an array of N integers, find the length of the longest contiguous subsegment that is sorted in non-decreasing order. If there are multiple such subsegments of the maximum length, return the length of the first one encountered.
Input
The first line contains an integer N. The second line contains N space-separated integers representing the array elements.
Output
Print a single integer, the length of the longest non-decreasing subsegment.
Constraints
1 <= N <= 200000 -10^9 <= array elements <= 10^9
Example 1
5 1 2 2 1 3
3
The longest non-decreasing subsegments are [1, 2, 2] (length 3) and [1, 3] (length 2). The longest is 3.
Example 2
7 3 1 4 1 5 9 2
3
The longest non-decreasing subsegment is [1, 4] or [1, 5, 9]. The subsegment [1, 5, 9] has length 3.
Hints
Similar problems
- Count Pairs with Specific SumEasy
- Longest Subarray with Constant DifferenceMedium
- Longest Subarray with Bounded DifferenceMedium
- Longest Subarray with Alternating ParityMedium
- Distinct Elements in a Sliding WindowMedium
- Longest Subarray with Increasing SumMedium
More topics: Arrays problems, Two Pointers problems, Sliding Window problems
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 1 2 2 1 3
7 3 1 4 1 5 9 2