Longest increasing sequence by the boundary elements of an Array
Last Updated :
20 Feb, 2023
Given an array arr[] of length N with unique elements, the task is to find the length of the longest increasing subsequence that can be formed by the elements from either end of the array.
Examples:
Input: arr[] = {3, 5, 1, 4, 2}
Output: 4
Explanation:
The longest sequence is: {2, 3, 4, 5}
Pick 2, Sequence is {2}, Array is {3, 5, 1, 4}
Pick 3, Sequence is {2, 3}, Array is {5, 1, 4}
Pick 4, Sequence is {2, 3, 4}, Array is {5, 1}
Pick 5, Sequence is {2, 3, 4, 5}, Array is {1}
Input: arr[] = {3, 1, 5, 2, 4}
Output: 2
The longest sequence is {3, 4}
Approach: This problem can be solved by Two Pointer Approach. Set two pointers at the first and last indices of the array. Select the minimum of the two values currently pointed and check if it is greater than the previously selected value. If so, update the pointer and increase the length of the LIS and repeat the process. Otherwise, print the length of the LIS obtained.
Below is the implementation of the above approach:
C++
// C++ Program to print the
// longest increasing
// subsequence from the
// boundary elements of an array
#include <bits/stdc++.h>
using namespace std;
// Function to return the length of
// Longest Increasing subsequence
int longestSequence(int n, int arr[])
{
// Set pointers at
// both ends
int l = 0, r = n - 1;
// Stores the recent
// value added to the
// subsequence
int prev = INT_MIN;
// Stores the length of
// the subsequence
int ans = 0;
while (l <= r) {
// Check if both elements
// can be added to the
// subsequence
if (arr[l] > prev
&& arr[r] > prev) {
if (arr[l] < arr[r]) {
ans += 1;
prev = arr[l];
l += 1;
}
else {
ans += 1;
prev = arr[r];
r -= 1;
}
}
// Check if the element
// on the left can be
// added to the
// subsequence only
else if (arr[l] > prev) {
ans += 1;
prev = arr[l];
l += 1;
}
// Check if the element
// on the right can be
// added to the
// subsequence only
else if (arr[r] > prev) {
ans += 1;
prev = arr[r];
r -= 1;
}
// If none of the values
// can be added to the
// subsequence
else {
break;
}
}
return ans;
}
// Driver Code
int main()
{
int arr[] = { 3, 5, 1, 4, 2 };
// Length of array
int n = sizeof(arr)
/ sizeof(arr[0]);
cout << longestSequence(n, arr);
return 0;
}
Java
// Java program to print the longest
// increasing subsequence from the
// boundary elements of an array
import java.util.*;
class GFG{
// Function to return the length of
// Longest Increasing subsequence
static int longestSequence(int n, int arr[])
{
// Set pointers at
// both ends
int l = 0, r = n - 1;
// Stores the recent
// value added to the
// subsequence
int prev = Integer.MIN_VALUE;
// Stores the length of
// the subsequence
int ans = 0;
while (l <= r)
{
// Check if both elements
// can be added to the
// subsequence
if (arr[l] > prev &&
arr[r] > prev)
{
if (arr[l] < arr[r])
{
ans += 1;
prev = arr[l];
l += 1;
}
else
{
ans += 1;
prev = arr[r];
r -= 1;
}
}
// Check if the element on the
// left can be added to the
// subsequence only
else if (arr[l] > prev)
{
ans += 1;
prev = arr[l];
l += 1;
}
// Check if the element on the
// right can be added to the
// subsequence only
else if (arr[r] > prev)
{
ans += 1;
prev = arr[r];
r -= 1;
}
// If none of the values
// can be added to the
// subsequence
else
{
break;
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 5, 1, 4, 2 };
// Length of array
int n = arr.length;
System.out.print(longestSequence(n, arr));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to print the
# longest increasing subsequence
# from the boundary elements
# of an array
import sys
# Function to return the length of
# Longest Increasing subsequence
def longestSequence(n, arr):
# Set pointers at
# both ends
l = 0
r = n - 1
# Stores the recent value
# added to the subsequence
prev = -sys.maxsize - 1
# Stores the length of
# the subsequence
ans = 0
while (l <= r):
# Check if both elements can be
# added to the subsequence
if (arr[l] > prev and
arr[r] > prev):
if (arr[l] < arr[r]):
ans += 1
prev = arr[l]
l += 1
else:
ans += 1
prev = arr[r]
r -= 1
# Check if the element
# on the left can be
# added to the
# subsequence only
elif (arr[l] > prev):
ans += 1
prev = arr[l]
l += 1
# Check if the element
# on the right can be
# added to the
# subsequence only
elif (arr[r] > prev):
ans += 1
prev = arr[r]
r -= 1
# If none of the values
# can be added to the
# subsequence
else:
break
return ans
# Driver code
arr = [ 3, 5, 1, 4, 2 ]
# Length of array
n = len(arr)
print(longestSequence(n, arr))
# This code is contributed by sanjoy_62
C#
// C# program to print the longest
// increasing subsequence from the
// boundary elements of an array
using System;
class GFG{
// Function to return the length of
// longest Increasing subsequence
static int longestSequence(int n, int []arr)
{
// Set pointers at
// both ends
int l = 0, r = n - 1;
// Stores the recent value
// added to the subsequence
int prev = int.MinValue;
// Stores the length of
// the subsequence
int ans = 0;
while (l <= r)
{
// Check if both elements
// can be added to the
// subsequence
if (arr[l] > prev &&
arr[r] > prev)
{
if (arr[l] < arr[r])
{
ans += 1;
prev = arr[l];
l += 1;
}
else
{
ans += 1;
prev = arr[r];
r -= 1;
}
}
// Check if the element on the
// left can be added to the
// subsequence only
else if (arr[l] > prev)
{
ans += 1;
prev = arr[l];
l += 1;
}
// Check if the element on the
// right can be added to the
// subsequence only
else if (arr[r] > prev)
{
ans += 1;
prev = arr[r];
r -= 1;
}
// If none of the values
// can be added to the
// subsequence
else
{
break;
}
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 5, 1, 4, 2 };
// Length of array
int n = arr.Length;
Console.Write(longestSequence(n, arr));
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// Javascript Program to print the
// longest increasing
// subsequence from the
// boundary elements of an array
// Function to return the length of
// Longest Increasing subsequence
function longestSequence(n, arr)
{
// Set pointers at
// both ends
var l = 0, r = n - 1;
// Stores the recent
// value added to the
// subsequence
var prev = -1000000000;
// Stores the length of
// the subsequence
var ans = 0;
while (l <= r) {
// Check if both elements
// can be added to the
// subsequence
if (arr[l] > prev
&& arr[r] > prev) {
if (arr[l] < arr[r]) {
ans += 1;
prev = arr[l];
l += 1;
}
else {
ans += 1;
prev = arr[r];
r -= 1;
}
}
// Check if the element
// on the left can be
// added to the
// subsequence only
else if (arr[l] > prev) {
ans += 1;
prev = arr[l];
l += 1;
}
// Check if the element
// on the right can be
// added to the
// subsequence only
else if (arr[r] > prev) {
ans += 1;
prev = arr[r];
r -= 1;
}
// If none of the values
// can be added to the
// subsequence
else {
break;
}
}
return ans;
}
// Driver Code
var arr = [ 3, 5, 1, 4, 2 ];
// Length of array
var n = arr.length;
document.write( longestSequence(n, arr));
// This code is contributed by itsok.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Two Pointers Technique Two pointers is really an easy and effective technique that is typically used for Two Sum in Sorted Arrays, Closest Two Sum, Three Sum, Four Sum, Trapping Rain Water and many other popular interview questions. Given a sorted array arr (sorted in ascending order) and a target, find if there exists an
11 min read
Two Pointers Technique in Different languages
Java Program for Two Pointers Technique Two pointers is really an easy and effective technique which is typically used for searching pairs in a sorted array.Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X. Letâs see the naive so
4 min read
C Program for Two Pointers Technique Two pointers is really an easy and effective technique which is typically used for searching pairs in a sorted array.Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X. Letâs see the naive so
3 min read
Problems based on Two-Pointer Technique
Find four elements that sum to a given value | Two-Pointer approach Given an array arr of integers of size N and a target number, the task is to find all unique quadruplets in it, whose sum is equal to the target number. Examples:Input: arr[] = {4, 1, 2, -1, 1, -3], target = 1 Output: [[-3, -1, 1, 4], [-3, 1, 1, 2]] Explanation: Both the quadruplets sum equals to ta
10 min read
Rearrange an array in maximum minimum form using Two Pointer Technique Given a sorted array of positive integers, rearrange the array alternately i.e first element should be a maximum value, at second position minimum value, at third position second max, at fourth position second min, and so on. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2
6 min read
Pair with largest sum which is less than K in the array Given an array arr of size N and an integer K. The task is to find the pair of integers such that their sum is maximum and but less than K Examples: Input : arr = {30, 20, 50} , K = 70 Output : 30, 20 30 + 20 = 50, which is the maximum possible sum which is less than K Input : arr = {5, 20, 110, 100
10 min read
Longest increasing sequence by the boundary elements of an Array Given an array arr[] of length N with unique elements, the task is to find the length of the longest increasing subsequence that can be formed by the elements from either end of the array.Examples: Input: arr[] = {3, 5, 1, 4, 2} Output: 4 Explanation: The longest sequence is: {2, 3, 4, 5} Pick 2, Se
8 min read
Two Sum - Pair with given Sum Given an array arr[] of n integers and a target value, check if there exists a pair whose sum equals the target. This is a variation of the 2Sum problem.Examples: Input: arr[] = [0, -1, 2, -3, 1], target = -2Output: trueExplanation: There is a pair (1, -3) with the sum equal to given target, 1 + (-3
15+ min read