Related Articles

# Maximize length of longest non-decreasing array possible by the elements present in either ends of the given array

• Difficulty Level : Hard
• Last Updated : 22 Apr, 2021

Given an array A[] of size N, the task is to find the maximum length of the longest non-decreasing array that can be generated from the given array by appending elements from either of the ends of the given array to the end of the resultant array and removing the appended element one by one.

Examples:

Input: A[] = {5, 6, 8, 7}
Output:: 4
Explanation:
If resArr[] = { } is a newly generated array then
Inserting A into resArr[] modifies A[] = {6, 8, 7} and resArr[] = {5}
Inserting A into resArr[] modifies A[] = {8, 7} and resArr[] = {5, 6}
Inserting A into resArr[] modifies A[] = {8} and resArr[] = {5, 6, 7}
Inserting A into resArr[] modifies A[] = {} and resArr[] = {5, 6, 7, 8}
Therefore, the length of the longest non-decreasing array that can be generated from the given array is 4.

Input: {1, 1, 3, 5, 4, 3, 6, 2, 1}
Output: 7

Approach: This problem can be solved using two pointer approach. Follow the steps below to solve the problem:

• Initialize a variable, say res = 0 to store the maximum length of a non-decreasing array that can be generated from the given array.
• Initialize two variables, say start = 0 and end = N – 1 to store the index of the start and end pointer.
• Traverse the array and check the following conditions:
• If A[start] <= A[end] then check if the value of A[start] is greater than or equal to the previously inserted element in the new array or not. If found to be true then increment the value of start and res by 1.
• If A[start] >= A[end] then check if the value of A[end] is greater than or equal to the previously inserted element in the new array or not. If found to be true then decrement the value of end by 1 and increment the value of res by 1.
• Finally, print the value of res.

Below is the implementation of the above approach:

## C++

 `//C++ program to implement``// the above approach`  `#include ``using` `namespace` `std;`   `// Function to find the length of the longest``// non-decreasing array that can be generated``int` `findLongestNonDecreasing(``int` `A[], ``int` `N)``{``    ` `    ` `    ` `    ``// Stores the length of the longest``    ``// non-decreasing array that can be``    ``// generated from the array``    ``int` `res = 0; ``    ` `    ` `    ` `    ``// Stores index of``    ``// start pointer``    ``int` `start = 0;``    ` `    ` `    ``// Stores index of``    ``// end pointer``    ``int` `end = N - 1;``    ` `    ` `    ` `    ``// Stores previously inserted``    ``// element into the new array``    ``int` `prev = -1;``    ` `    ` `    ` `    ``// Traverse the array``    ``while` `(start <= end) {``        ` `        ` `        ` `        ``// If A[start] is less than``        ``// or equal to A[end]``        ``if` `(A[start] <= A[end]) {``            ` `            ` `            ``// If no element inserted into``            ``// the newly generated array``            ``if` `(prev == -1) {``                ` `                ` `                ``// Update prev``                ``prev = A[start];``                ` `                ` `                ` `                ``// Update res``                ``res++;``                ` `                ` `                ` `                ``// Update start``                ``start++;``                ` `            ``}``            ` `            ``else` `{``                ` `                ` `                 ``// If A[start] is greater``                 ``// than or equal to prev``                 ``if` `(A[start] >= prev) {``                     ` `                     ` `                     ` `                    ``// Update res``                    ``res++;``                    ` `                    ` `                    ` `                    ``// Update prev``                    ``prev = A[start];``                    ` `                    ` `                    ` `                    ``// Update start``                    ``start++;``                 ``}``                 ` `                 ` `                 ` `                 ` `                ``//  If A[end] is greater``                 ``// than or equal to prev``                 ``else` `if` `(A[end] >= prev) {``                     ` `                     ` `                    ``// Update res``                    ``res++;``                    ` `                    ` `                    ` `                    ``// Update prev``                    ``prev = A[end];``                    ` `                    ` `                    ` `                    ``// Update end``                    ``end--;``                 ``}``                 ` `                 ``else` `{``                     ``break``;``                 ``}``            ``}``        ``}``        ` `        ` `        ``// If A[end] is``        ``// greater than A[start]``        ``else``{``            ` `            ` `            ``// If no element inserted into``            ``// the newly generated array``            ``if` `(prev == -1) {``                ` `                ` `                ``// Update prev``                ``prev = A[end];``                ` `                ` `                ``// Update res``                ``res++;``                ` `                ` `                ``// Update end``                ``end--;``            ``}``            ` `            ``else` `{``                ` `                ` `                ``// If A[end] is greater``                ``// than or equal to prev``                ``if` `(A[end] >= prev) {``                    ` `                    ` `                    ` `                    ``// Update res``                    ``res++;``                    ` `                    ` `                    ``//Update prev``                    ``prev = A[end];``                    ` `                    ` `                    ` `                    ``// Update end``                    ``end--;``                ``}``                ` `                ` `                ` `                ``// If A[start] is greater``                ``// than or equal to prev``                ``else` `if` `(A[start] >= prev) {``                    ` `                    ` `                    ` `                    ``// Update res``                    ``res++;``                    ` `                    ` `                    ` `                    ``//Update prev``                    ``prev = A[start];``                    ` `                    ` `                    ` `                    ``// Update start``                    ``start++;``                 ``}``                 ` `                 ``else` `{``                     ``break``;``                 ``}``            ``}``        ``}``    ``}``    ` `    ``return` `res;``     ` `}` `//Driver Code``int` `main()``{``    ` `    ``int` `A[]={ 1, 1, 3, 5, 4, 3, 6, 2, 1 };``    ` `    ``int` `N = ``sizeof``(A)/``sizeof``(A);``    ` `    ``//Function call``    ``cout<< findLongestNonDecreasing(A, N);``    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;` `class` `GFG{`` ` `// Function to find the length of the longest``// non-decreasing array that can be generated``static` `int` `findLongestNonDecreasing(``int` `A[],``                                    ``int` `N)``{``    ` `    ``// Stores the length of the longest``    ``// non-decreasing array that can be``    ``// generated from the array``    ``int` `res = ``0``; ``    ` `    ``// Stores index of``    ``// start pointer``    ``int` `start = ``0``;``    ` `    ``// Stores index of``    ``// end pointer``    ``int` `end = N - ``1``;``    ` `    ``// Stores previously inserted``    ``// element into the new array``    ``int` `prev = -``1``;``    ` `    ``// Traverse the array``    ``while` `(start <= end)``    ``{``        ` `        ``// If A[start] is less than``        ``// or equal to A[end]``        ``if` `(A[start] <= A[end])``        ``{``            ` `            ``// If no element inserted into``            ``// the newly generated array``            ``if` `(prev == -``1``)``            ``{``                ` `                ``// Update prev``                ``prev = A[start];``                ` `                ``// Update res``                ``res++;``                ` `                ``// Update start``                ``start++;``            ``}``            ``else``            ``{``                ` `                ``// If A[start] is greater``                ``// than or equal to prev``                ``if` `(A[start] >= prev)``                ``{``                    ` `                    ``// Update res``                    ``res++;``                    ` `                    ``// Update prev``                    ``prev = A[start];``                    ` `                    ``// Update start``                    ``start++;``                ``}``                ` `                ``//  If A[end] is greater``                ``// than or equal to prev``                ``else` `if` `(A[end] >= prev)``                ``{``                    ` `                    ``// Update res``                    ``res++;``                    ` `                    ``// Update prev``                    ``prev = A[end];``                    ` `                    ``// Update end``                    ``end--;``                ``}``                ``else``                ``{``                    ``break``;``                ``}``            ``}``        ``}``         ` `        ``// If A[end] is``        ``// greater than A[start]``        ``else``        ``{``            ` `            ``// If no element inserted into``            ``// the newly generated array``            ``if` `(prev == -``1``)``            ``{``                ` `                ``// Update prev``                ``prev = A[end];``                ` `                ``// Update res``                ``res++;``                ` `                ``// Update end``                ``end--;``            ``}``            ``else``            ``{``                ` `                ``// If A[end] is greater``                ``// than or equal to prev``                ``if` `(A[end] >= prev)``                ``{``                    ` `                    ``// Update res``                    ``res++;``                    ` `                    ``//Update prev``                    ``prev = A[end];``                    ` `                    ``// Update end``                    ``end--;``                ``}``                ` `                ``// If A[start] is greater``                ``// than or equal to prev``                ``else` `if` `(A[start] >= prev)``                ``{``                    ` `                    ``// Update res``                    ``res++;``                     ` `                    ``//Update prev``                    ``prev = A[start];``                     ` `                    ``// Update start``                    ``start++;``                ``}``                ``else``                ``{``                    ``break``;``                ``}``            ``}``        ``}``    ``}``    ``return` `res;``}`` ` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `A[] = { ``1``, ``1``, ``3``, ``5``, ``4``, ``3``, ``6``, ``2``, ``1` `};``     ` `    ``int` `N = A.length;``     ` `    ``// Function call``    ``System.out.print(findLongestNonDecreasing(A, N));``}``}` `// This code is contributed by susmitakundugoaldanga`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to find the length of``# the longest non-decreasing array``# that can be generated``def` `findLongestNonDecreasing(A, N):` `    ``# Stores the length of the longest``    ``# non-decreasing array that can be``    ``# generated from the array``    ``res ``=` `0``;` `    ``# Stores index of``    ``# start pointer``    ``start ``=` `0``;` `    ``# Stores index of``    ``# end pointer``    ``end ``=` `N ``-` `1``;` `    ``# Stores previously inserted``    ``# element into the new array``    ``prev ``=` `-``1``;` `    ``# Traverse the array``    ``while` `(start <``=` `end):` `        ``# If A[start] is less than``        ``# or equal to A[end]``        ``if` `(A[start] <``=` `A[end]):` `            ``# If no element inserted into``            ``# the newly generated array``            ``if` `(prev ``=``=` `-``1``):` `                ``# Update prev``                ``prev ``=` `A[start];` `                ``# Update res``                ``res ``+``=` `1` `                ``# Update start``                ``start ``+``=` `1` `            ``else``:` `                 ``# If A[start] is greater``                 ``# than or equal to prev``                 ``if` `(A[start] >``=` `prev):` `                    ``# Update res``                    ``res ``+``=` `1` `                    ``# Update prev``                    ``prev ``=` `A[start];` `                    ``# Update start``                    ``start ``+``=` `1` `                ``#  If A[end] is greater``                ``# than or equal to prev``                 ``elif` `(A[end] >``=` `prev):` `                    ``# Update res``                    ``res ``+``=` `1` `                    ``# Update prev``                    ``prev ``=` `A[end];` `                    ``# Update end``                    ``end ``-``=` `1` `                 ``else``:``                     ``break``;` `        ``# If A[end] is``        ``# greater than A[start]``        ``else``:` `            ``# If no element inserted into``            ``# the newly generated array``            ``if` `(prev ``=``=` `-``1``):` `                ``# Update prev``                ``prev ``=` `A[end];` `                ``# Update res``                ``res ``+``=` `1` `                ``# Update end``                ``end ``-``=` `1` `            ``else``:` `                ``# If A[end] is greater``                ``# than or equal to prev``                ``if` `(A[end] >``=` `prev):` `                    ``# Update res``                    ``res ``+``=` `1` `                    ``# Update prev``                    ``prev ``=` `A[end];` `                    ``# Update end``                    ``end ``-``=` `1` `                ``# If A[start] is greater``                ``# than or equal to prev``                ``elif` `(A[start] >``=` `prev):` `                    ``# Update res``                    ``res ``+``=` `1` `                    ``# Update prev``                    ``prev ``=` `A[start];` `                    ``# Update start``                    ``start ``+``=` `1` `                ``else` `:``                     ``break``;``               ` `    ``return` `res` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``  ` `    ``A ``=` `[``1``, ``1``, ``3``, ``5``,``         ``4``, ``3``, ``6``, ``2``, ``1``]``    ` `    ``N ``=` `len``(A)``    ` `    ``# Function call``    ``print` `(findLongestNonDecreasing(A, N));` `# This code is contributed by Chitranayal`

## C#

 `// C# program to implement``// the above approach ``using` `System;` `class` `GFG{``  ` `// Function to find the length of the longest``// non-decreasing array that can be generated``static` `int` `findLongestNonDecreasing(``int``[] A,``                                    ``int` `N)``{``  ` `    ``// Stores the length of the longest``    ``// non-decreasing array that can be``    ``// generated from the array``    ``int` `res = 0; ``     ` `    ``// Stores index of``    ``// start pointer``    ``int` `start = 0;``     ` `    ``// Stores index of``    ``// end pointer``    ``int` `end = N - 1;``     ` `    ``// Stores previously inserted``    ``// element into the new array``    ``int` `prev = -1;``     ` `    ``// Traverse the array``    ``while` `(start <= end)``    ``{``         ` `        ``// If A[start] is less than``        ``// or equal to A[end]``        ``if` `(A[start] <= A[end])``        ``{``             ` `            ``// If no element inserted into``            ``// the newly generated array``            ``if` `(prev == -1)``            ``{``                 ` `                ``// Update prev``                ``prev = A[start];``                 ` `                ``// Update res``                ``res++;``                 ` `                ``// Update start``                ``start++;``            ``}``            ``else``            ``{``                 ` `                ``// If A[start] is greater``                ``// than or equal to prev``                ``if` `(A[start] >= prev)``                ``{``                     ` `                    ``// Update res``                    ``res++;``                     ` `                    ``// Update prev``                    ``prev = A[start];``                     ` `                    ``// Update start``                    ``start++;``                ``}``                 ` `                ``//  If A[end] is greater``                ``// than or equal to prev``                ``else` `if` `(A[end] >= prev)``                ``{``                     ` `                    ``// Update res``                    ``res++;``                     ` `                    ``// Update prev``                    ``prev = A[end];``                     ` `                    ``// Update end``                    ``end--;``                ``}``                ``else``                ``{``                    ``break``;``                ``}``            ``}``        ``}``          ` `        ``// If A[end] is``        ``// greater than A[start]``        ``else``        ``{``             ` `            ``// If no element inserted into``            ``// the newly generated array``            ``if` `(prev == -1)``            ``{``                 ` `                ``// Update prev``                ``prev = A[end];``                 ` `                ``// Update res``                ``res++;``                 ` `                ``// Update end``                ``end--;``            ``}``            ``else``            ``{``                 ` `                ``// If A[end] is greater``                ``// than or equal to prev``                ``if` `(A[end] >= prev)``                ``{``                     ` `                    ``// Update res``                    ``res++;``                     ` `                    ``//Update prev``                    ``prev = A[end];``                     ` `                    ``// Update end``                    ``end--;``                ``}``                 ` `                ``// If A[start] is greater``                ``// than or equal to prev``                ``else` `if` `(A[start] >= prev)``                ``{``                     ` `                    ``// Update res``                    ``res++;``                      ` `                    ``//Update prev``                    ``prev = A[start];``                      ` `                    ``// Update start``                    ``start++;``                ``}``                ``else``                ``{``                    ``break``;``                ``}``            ``}``        ``}``    ``}``    ``return` `res;``}``  ` `// Driver Code``public` `static` `void` `Main()``{``    ``int``[] A = { 1, 1, 3, 5, 4, 3, 6, 2, 1 };``      ` `    ``int` `N = A.Length;``      ` `    ``// Function call``    ``Console.Write(findLongestNonDecreasing(A, N));``}``}` `// This code is contributed by sanjoy_62`

## Javascript

 ``
Output:
`7`

Time Complexity: O(N)
Space Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up