Open In App

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

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[0] into resArr[] modifies A[] = {6, 8, 7} and resArr[] = {5} 
Inserting A[0] into resArr[] modifies A[] = {8, 7} and resArr[] = {5, 6} 
Inserting A[1] into resArr[] modifies A[] = {8} and resArr[] = {5, 6, 7} 
Inserting A[0] 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:

Below is the implementation of the above approach:




//C++ program to implement
// the above approach
 
 
#include <bits/stdc++.h>
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[0]);
     
    //Function call
    cout<< findLongestNonDecreasing(A, N);
    return 0;
}




// 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 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# 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




<script>
// Javascript program to implement
// the above approach
 
// Function to find the length of the longest
// non-decreasing array that can be generated
function findLongestNonDecreasing(A, N)
{
      
    // Stores the length of the longest
    // non-decreasing array that can be
    // generated from the array
    let res = 0;
      
    // Stores index of
    // start pointer
    let start = 0;
      
    // Stores index of
    // end pointer
    let end = N - 1;
      
    // Stores previously inserted
    // element into the new array
    let 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
     
    let A = [ 1, 1, 3, 5, 4, 3, 6, 2, 1 ];
       
    let N = A.length;
       
    // Function call
    document.write(findLongestNonDecreasing(A, N));
    
</script>

Output: 
7

 

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


Article Tags :