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++
#include <bits/stdc++.h>
using namespace std;
int findLongestNonDecreasing( int A[], int N)
{
int res = 0;
int start = 0;
int end = N - 1;
int prev = -1;
while (start <= end) {
if (A[start] <= A[end]) {
if (prev == -1) {
prev = A[start];
res++;
start++;
}
else {
if (A[start] >= prev) {
res++;
prev = A[start];
start++;
}
else if (A[end] >= prev) {
res++;
prev = A[end];
end--;
}
else {
break ;
}
}
}
else {
if (prev == -1) {
prev = A[end];
res++;
end--;
}
else {
if (A[end] >= prev) {
res++;
prev = A[end];
end--;
}
else if (A[start] >= prev) {
res++;
prev = A[start];
start++;
}
else {
break ;
}
}
}
}
return res;
}
int main()
{
int A[]={ 1, 1, 3, 5, 4, 3, 6, 2, 1 };
int N = sizeof (A)/ sizeof (A[0]);
cout<< findLongestNonDecreasing(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findLongestNonDecreasing( int A[],
int N)
{
int res = 0 ;
int start = 0 ;
int end = N - 1 ;
int prev = - 1 ;
while (start <= end)
{
if (A[start] <= A[end])
{
if (prev == - 1 )
{
prev = A[start];
res++;
start++;
}
else
{
if (A[start] >= prev)
{
res++;
prev = A[start];
start++;
}
else if (A[end] >= prev)
{
res++;
prev = A[end];
end--;
}
else
{
break ;
}
}
}
else
{
if (prev == - 1 )
{
prev = A[end];
res++;
end--;
}
else
{
if (A[end] >= prev)
{
res++;
prev = A[end];
end--;
}
else if (A[start] >= prev)
{
res++;
prev = A[start];
start++;
}
else
{
break ;
}
}
}
}
return res;
}
public static void main(String[] args)
{
int A[] = { 1 , 1 , 3 , 5 , 4 , 3 , 6 , 2 , 1 };
int N = A.length;
System.out.print(findLongestNonDecreasing(A, N));
}
}
|
Python3
def findLongestNonDecreasing(A, N):
res = 0 ;
start = 0 ;
end = N - 1 ;
prev = - 1 ;
while (start < = end):
if (A[start] < = A[end]):
if (prev = = - 1 ):
prev = A[start];
res + = 1
start + = 1
else :
if (A[start] > = prev):
res + = 1
prev = A[start];
start + = 1
elif (A[end] > = prev):
res + = 1
prev = A[end];
end - = 1
else :
break ;
else :
if (prev = = - 1 ):
prev = A[end];
res + = 1
end - = 1
else :
if (A[end] > = prev):
res + = 1
prev = A[end];
end - = 1
elif (A[start] > = prev):
res + = 1
prev = A[start];
start + = 1
else :
break ;
return res
if __name__ = = "__main__" :
A = [ 1 , 1 , 3 , 5 ,
4 , 3 , 6 , 2 , 1 ]
N = len (A)
print (findLongestNonDecreasing(A, N));
|
C#
using System;
class GFG{
static int findLongestNonDecreasing( int [] A,
int N)
{
int res = 0;
int start = 0;
int end = N - 1;
int prev = -1;
while (start <= end)
{
if (A[start] <= A[end])
{
if (prev == -1)
{
prev = A[start];
res++;
start++;
}
else
{
if (A[start] >= prev)
{
res++;
prev = A[start];
start++;
}
else if (A[end] >= prev)
{
res++;
prev = A[end];
end--;
}
else
{
break ;
}
}
}
else
{
if (prev == -1)
{
prev = A[end];
res++;
end--;
}
else
{
if (A[end] >= prev)
{
res++;
prev = A[end];
end--;
}
else if (A[start] >= prev)
{
res++;
prev = A[start];
start++;
}
else
{
break ;
}
}
}
}
return res;
}
public static void Main()
{
int [] A = { 1, 1, 3, 5, 4, 3, 6, 2, 1 };
int N = A.Length;
Console.Write(findLongestNonDecreasing(A, N));
}
}
|
Javascript
<script>
function findLongestNonDecreasing(A, N)
{
let res = 0;
let start = 0;
let end = N - 1;
let prev = -1;
while (start <= end)
{
if (A[start] <= A[end])
{
if (prev == -1)
{
prev = A[start];
res++;
start++;
}
else
{
if (A[start] >= prev)
{
res++;
prev = A[start];
start++;
}
else if (A[end] >= prev)
{
res++;
prev = A[end];
end--;
}
else
{
break ;
}
}
}
else
{
if (prev == -1)
{
prev = A[end];
res++;
end--;
}
else
{
if (A[end] >= prev)
{
res++;
prev = A[end];
end--;
}
else if (A[start] >= prev)
{
res++;
prev = A[start];
start++;
}
else
{
break ;
}
}
}
}
return res;
}
let A = [ 1, 1, 3, 5, 4, 3, 6, 2, 1 ];
let N = A.length;
document.write(findLongestNonDecreasing(A, N));
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!