Longest Subsequence where index of next element is arr[arr[i] + i]
Last Updated :
18 Jul, 2024
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;
// Function to print the maximum length sub-sequence
void maxLengthSubSeq(int a[], int n)
{
// Arrays to store the values to be printed
vector<int> temp, print;
for (int i = 0; i < n; i++) {
int x = i;
temp.clear();
// Iterate till index is in range of the array
while (x < n) {
temp.push_back(a[x]);
x = a[x] + x; // Move to the next index
}
// If the length (temp) > the length (print) then
// copy the contents of the temp array into
// the print array
if (print.size() < temp.size()) {
print = temp;
}
}
// Print the contents of the array
for (int i = 0; i < print.size(); i++)
cout << print[i] << " ";
}
// Driver code
int main()
{
int a[] = {1, 6, 3, 1, 12, 1, 4};
int n = sizeof(a) / sizeof(a[0]);
maxLengthSubSeq(a, n);
return 0;
}
Java
import java.util.ArrayList;
public class MaxLengthSubSeq {
// Function to print the maximum length sub-sequence
public static void maxLengthSubSeq(int[] a, int n) {
ArrayList<Integer> temp = new ArrayList<>(); // Temp array to store current sequence
ArrayList<Integer> print = new ArrayList<>(); // Print array to store the longest sequence
for (int i = 0; i < n; i++) {
int x = i;
temp.clear();
// Build the sequence starting from index i
while (x < n) {
temp.add(a[x]);
x = a[x] + x; // Move to the next index
}
// Update print array if temp has a longer sequence
if (print.size() < temp.size()) {
print = new ArrayList<>(temp);
}
}
// Print the longest sequence
for (int num : print) {
System.out.print(num + " ");
}
}
public static void main(String[] args) {
int[] a = {1, 6, 3, 1, 12, 1, 4};
int n = a.length;
maxLengthSubSeq(a, n);
}
}
Python
def max_length_subseq(a):
n = len(a)
temp = []
print_seq = []
for i in range(n):
x = i
temp = []
# Build the sequence starting from index i
while x < n:
temp.append(a[x])
x = a[x] + x # Move to the next index
# Update print_seq if temp has a longer sequence
if len(print_seq) < len(temp):
print_seq = temp
# Print the longest sequence
for num in print_seq:
print(num, end=" ")
a = [1, 6, 3, 1, 12, 1, 4]
max_length_subseq(a)
C#
using System;
using System.Collections.Generic;
class MaxLengthSubSeq {
// Function to print the maximum length sub-sequence
static void MaxLengthSubSeqMethod(int[] a, int n) {
List<int> temp = new List<int>(); // Temp list to store current sequence
List<int> print = new List<int>(); // Print list to store the longest sequence
for (int i = 0; i < n; i++) {
int x = i;
temp.Clear();
// Build the sequence starting from index i
while (x < n) {
temp.Add(a[x]);
x = a[x] + x; // Move to the next index
}
// Update print list if temp has a longer sequence
if (print.Count < temp.Count) {
print = new List<int>(temp);
}
}
// Print the longest sequence
foreach (int num in print) {
Console.Write(num + " ");
}
}
static void Main(string[] args) {
int[] a = {1, 6, 3, 1, 12, 1, 4};
int n = a.Length;
MaxLengthSubSeqMethod(a, n);
}
}
JavaScript
function maxLengthSubSeq(a) {
let n = a.length;
let temp = []; // Temp array to store current sequence
let print = []; // Print array to store the longest sequence
for (let i = 0; i < n; i++) {
let x = i;
temp = [];
// Build the sequence starting from index i
while (x < n) {
temp.push(a[x]);
x = a[x] + x; // Move to the next index
}
// Update print array if temp has a longer sequence
if (print.length < temp.length) {
print = [...temp];
}
}
// Print the longest sequence
for (let num of print) {
console.log(num + " ");
}
}
let a = [1, 6, 3, 1, 12, 1, 4];
maxLengthSubSeq(a);
PHP
<?php
function maxLengthSubSeq($a, $n) {
$temp = array(); // Temp array to store current sequence
$print = array(); // Print array to store the longest sequence
for ($i = 0; $i < $n; $i++) {
$x = $i;
$temp = array();
// Build the sequence starting from index i
while ($x < $n) {
$temp[] = $a[$x];
$x = $a[$x] + $x; // Move to the next index
}
// Update print array if temp has a longer sequence
if (count($print) < count($temp)) {
$print = $temp;
}
}
// Print the longest sequence
foreach ($print as $num) {
echo $num . " ";
}
}
$a = array(1, 6, 3, 1, 12, 1, 4);
$n = count($a);
maxLengthSubSeq($a, $n);
?>
Complexity Analysis:
- Time Complexity: O(n*n)
- Auxiliary Space: O(n)
Similar Reads
Longest subsequence of even numbers in an Array Given an array arr[] containing N integers, the task is to print the length of the longest subsequence of even numbers in the array. Examples: Input: arr[] = {3, 4, 11, 2, 9, 21} Output: 2 Explanation: The longest subsequence containing even numbers is {4, 2}. Hence, the answer is 2. Input: arr[] =
5 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
Longest increasing sequence possible by the boundary elements of an Array Given an array arr[] of length N consisting of positive integers, the task is to find the longest increasing subsequence that can be formed by the elements from either end of the array. Examples : Input: N=4 arr[] ={ 1, 4, 2, 3 }Output: 1 3 4Explanation: Append arr[0] to the sequence. Sequence = {1}
14 min read
Longest Subsequence with at least one common digit in every element Given an array. The task is to find the length of the longest subsequence in which all elements must have at least one digit in common. Examples: Input : arr[] = { 11, 12, 23, 74, 13 } Output : 3 Explanation: The elements 11, 12, and 13 have the digit '1' as common. So it is the required longest sub
9 min read
Longest alternating subsequence in terms of positive and negative integers Given an array arr[] of positive and negative numbers only. The task is to find the length of the longest alternating (means negative-positive-negative or positive-negative-positive) subsequence present in the array. Examples: Input: arr[] = {-4, 3, -5, 9, 10, 12, 2, -1} Output: 5 Explanation: The l
15+ min read