- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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[0] 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[0]; 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

- Related Articles
- Program to find maximum length of non-sharing words in Python
- Program to find maximum element after decreasing and rearranging in Python
- Program to find length of longest strictly increasing then decreasing sublist in Python
- C++ program to find length of non empty substring whose sum is even
- Program to find maximum length of k ribbons of same length in Python
- Program to find maximum number of non-overlapping substrings in Python
- Maximum Sum Decreasing Subsequence in C++
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find maximum profit by cutting the rod of different length in C++
- Program to find maximum sum of two non-overlapping sublists in Python
- Program to find number not greater than n where all digits are non-decreasing in python
- Program to find maximum sum of non-adjacent nodes of a tree in Python
- Program to find maximum length of subarray with positive product in Python
- Non-decreasing Array in Python
- Program to find maximum non negative product in a matrix in Python