# C++ Program to find length of maximum non-decreasing subsegment

Suppose we have an array A with n elements. Amal has decided to make some money doing business on the Internet for exactly n days. On the i-th day he makes A[i] amount of money. Amal loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence A[i]. The subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the nondecreasing order.

## Problem Category

An array in the data structure is a finite collection of elements of a specific type. Arrays are used to store elements of the same type in consecutive memory locations. An array is assigned a particular name and it is referenced through that name in various programming languages. To access the elements of an array, indexing is required. We use the terminology 'name[i]' to access a particular element residing in position 'i' in the array 'name'. Various data structures such as stacks, queues, heaps, priority queues can be implemented using arrays. Operations on arrays include insertion, deletion, updating, traversal, searching, and sorting operations. Visit the link below for further reading.

https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm

So, if the input of our problem is like A = [2, 2, 1, 3, 4, 1], then the output will be 3, because the maximum non-decreasing subsegment is the numbers from the third to the fifth one.

## Steps

To solve this, we will follow these steps −

n := size of A
y := A
d := 1
c := 1
for initialize i := 1, when i < n, update (increase i by 1), do:
x := A[i]
if x >= y, then:
(increase d by 1)
Otherwise
d := 1
y := x
c := maximum of c and d
return c

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A){
int n = A.size();
int y = A;
int d = 1;
int c = 1;
for (int i = 1; i < n; i++){
int x = A[i];
if (x >= y)
d++;
else
d = 1;
y = x;
c = max(c, d);
}
return c;
}
int main(){
vector<int> A = { 2, 2, 1, 3, 4, 1 };
cout << solve(A) << endl;
}

## Input

{ 2, 2, 1, 3, 4, 1 }

## Output

3