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:
- 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 <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
// 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
<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> |
7
Time Complexity: O(N)
Space Complexity: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!