Longest Subsequence where index of next element is arr[arr[i] + i]
Last Updated :
12 Sep, 2022
Given an array arr[], the task is to find the maximum length sub-sequence from the array which satisfy the following condition:
Any element can be chosen as the first element of the sub-sequence but the index of the next element will be determined by arr[arr[i] + i] where i is the index of the previous element in the sequence.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 1 2 4
arr[0] = 1, arr[1 + 0] = arr[1] = 2, arr[2 + 1] = arr[3] = 4
Other possible sub-sequences are {2, 4}, {3}, {4} and {5}
Input: arr[] = {1, 6, 3, 1, 12, 1, 4}
Output: 3 1 4
Approach:
- Make use of two arrays temp and print.
- The temp array will store the array elements that are currently under consideration and the print array will store the array elements that are to be printed as the final output.
- Iterate from 0 to n – 1 and consider the current element as the first element of the sequence.
- Store all the elements of the current sequence into temp array.
- If the size of the temp array becomes greater than print array then copy all the contents of the temp array to the print array.
- When all the sequences have been considered, print the contents of the print array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxLengthSubSeq( int a[], int n)
{
int temp[n], print[n];
int y = 0;
for ( int i = 0; i < n; i++) {
int j = 0;
int x = 0;
temp[j++] = a[x];
x = a[x] + x;
while (x < n) {
temp[j++] = a[x];
x = a[x] + x;
}
if (y < j) {
for ( int k = 0; k < j; k++) {
print[k] = temp[k];
y = j;
}
}
}
for ( int i = 0; i < y; i++)
cout << print[i] << " " ;
}
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
int n = sizeof (a) / sizeof (a[0]);
maxLengthSubSeq(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void maxLengthSubSeq( int a[], int n)
{
int temp[]= new int [n];
int print[]= new int [n];
int y = 0 ;
for ( int i = 0 ; i < n; i++) {
int j = 0 ;
int x = 0 ;
temp[j++] = a[x];
x = a[x] + x;
while (x < n) {
temp[j++] = a[x];
x = a[x] + x;
}
if (y < j) {
for ( int k = 0 ; k < j; k++) {
print[k] = temp[k];
y = j;
}
}
}
for ( int i = 0 ; i < y; i++)
System.out.print(print[i] + " " );
}
public static void main (String[] args) {
int a[] = { 1 , 2 , 3 , 4 , 5 };
int n = a.length;
maxLengthSubSeq(a, n);
}
}
|
Python3
def maxLengthSubSeq(a, n):
temp = [ 0 for i in range (n)]
print1 = [ 0 for i in range (n)]
y = 0
for i in range ( 0 , n, 1 ):
j = 0
x = 0
temp[j] = a[x]
j + = 1
x = a[x] + x
while (x < n):
temp[j] = a[x]
j + = 1
x = a[x] + x
if (y < j):
for k in range ( 0 , j, 1 ):
print1[k] = temp[k]
y = j
for i in range ( 0 , y, 1 ):
print (print1[i], end = " " )
if __name__ = = '__main__' :
a = [ 1 , 2 , 3 , 4 , 5 ]
n = len (a)
maxLengthSubSeq(a, n)
|
C#
using System;
public class GFG{
static void maxLengthSubSeq( int []a, int n)
{
int []temp= new int [n];
int []print= new int [n];
int y = 0;
for ( int i = 0; i < n; i++) {
int j = 0;
int x = 0;
temp[j++] = a[x];
x = a[x] + x;
while (x < n) {
temp[j++] = a[x];
x = a[x] + x;
}
if (y < j) {
for ( int k = 0; k < j; k++) {
print[k] = temp[k];
y = j;
}
}
}
for ( int i = 0; i < y; i++)
Console.Write(print[i] + " " );
}
static public void Main (){
int []a = { 1, 2, 3, 4, 5 };
int n = a.Length;
maxLengthSubSeq(a, n);
}
}
|
PHP
<?php
function maxLengthSubSeq( $a , $n )
{
$y = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$j = 0;
$x = 0;
$temp [ $j ++] = $a [ $x ];
$x = $a [ $x ] + $x ;
while ( $x < $n )
{
$temp [ $j ++] = $a [ $x ];
$x = $a [ $x ] + $x ;
}
if ( $y < $j )
{
for ( $k = 0; $k < $j ; $k ++)
{
$print [ $k ] = $temp [ $k ];
$y = $j ;
}
}
}
for ( $i = 0; $i < $y ; $i ++)
echo $print [ $i ] . " " ;
}
$a = array (1, 2, 3, 4, 5);
$n = sizeof( $a );
maxLengthSubSeq( $a , $n );
|
Javascript
<script>
function maxLengthSubSeq(a, n)
{
let temp= new Array(n);
temp.fill(0);
let print= new Array(n);
print.fill(0);
let y = 0;
for (let i = 0; i < n; i++) {
let j = 0;
let x = 0;
temp[j++] = a[x];
x = a[x] + x;
while (x < n) {
temp[j++] = a[x];
x = a[x] + x;
}
if (y < j) {
for (let k = 0; k < j; k++) {
print[k] = temp[k];
y = j;
}
}
}
for (let i = 0; i < y; i++)
document.write(print[i] + " " );
}
let a = [ 1, 2, 3, 4, 5 ];
let n = a.length;
maxLengthSubSeq(a, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(n*n)
- Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...