# Longest Subsequence where index of next element is arr[arr[i] + i]

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ implementation of the approach ` `#include ` `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 ` `    ``int` `temp[n], print[n]; ` `    ``int` `y = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``int` `j = 0; ` `        ``int` `x = 0; ` ` `  `        ``// Store the first value into the temp array ` `        ``temp[j++] = a[x]; ` ` `  `        ``// Index of the next element ` `        ``x = a[x] + x; ` ` `  `        ``// Iterate till index is in range of the array ` `        ``while` `(x < n) { ` `            ``temp[j++] = a[x]; ` `            ``x = a[x] + x; ` `        ``} ` ` `  `        ``// If the length (temp) > the length (print) then ` `        ``// copy the contents of the temp array into ` `        ``// the print array ` `        ``if` `(y < j) { ` `            ``for` `(``int` `k = 0; k < j; k++) { ` `                ``print[k] = temp[k]; ` `                ``y = j; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Print the contents of the array ` `    ``for` `(``int` `i = 0; i < y; i++) ` `        ``cout << print[i] << ``" "``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 2, 3, 4, 5 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``maxLengthSubSeq(a, n); ` `    ``return` `0; ` `} `

 `//Java  implementation of the approach/ ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `// Function to print the maximum length sub-sequence ` `static` `void` `maxLengthSubSeq(``int` `a[], ``int` `n) ` `{ ` `    ``// Arrays to store the values to be printed ` `    ``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``; ` ` `  `        ``// Store the first value into the temp array ` `        ``temp[j++] = a[x]; ` ` `  `        ``// Index of the next element ` `        ``x = a[x] + x; ` ` `  `        ``// Iterate till index is in range of the array ` `        ``while` `(x < n) { ` `            ``temp[j++] = a[x]; ` `            ``x = a[x] + x; ` `        ``} ` ` `  `        ``// If the length (temp) > the length (print) then ` `        ``// copy the contents of the temp array into ` `        ``// the print array ` `        ``if` `(y < j) { ` `            ``for` `(``int` `k = ``0``; k < j; k++) { ` `                ``print[k] = temp[k]; ` `                ``y = j; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Print the contents of the array ` `    ``for` `(``int` `i = ``0``; i < y; i++) ` `            ``System.out.print(print[i] + ``" "``); ` `} ` ` `  `// Driver code ` `    ``public` `static` `void` `main (String[] args) { ` ` `  `    ``int` `a[] = { ``1``, ``2``, ``3``, ``4``, ``5` `}; ` `    ``int` `n = a.length; ` `    ``maxLengthSubSeq(a, n); ` `    ``} ` `//This code is contributed by @Tushil.     ` `} `

 `# Python 3 implementation of the approach ` ` `  `# Function to print the maximum length  ` `# sub-sequence ` `def` `maxLengthSubSeq(a, n): ` `     `  `    ``# Arrays to store the values to be printed ` `    ``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` ` `  `        ``# Store the first value into  ` `        ``# the temp array ` `        ``temp[j] ``=` `a[x] ` `        ``j ``+``=` `1` ` `  `        ``# Index of the next element ` `        ``x ``=` `a[x] ``+` `x ` ` `  `        ``# Iterate till index is in range  ` `        ``# of the array ` `        ``while` `(x < n): ` `            ``temp[j] ``=` `a[x] ` `            ``j ``+``=` `1` `            ``x ``=` `a[x] ``+` `x ` `         `  `        ``# If the length (temp) > the length  ` `        ``# (print) then copy the contents of  ` `        ``# the temp array into the print array ` `        ``if` `(y < j): ` `            ``for` `k ``in` `range``(``0``, j, ``1``): ` `                ``print1[k] ``=` `temp[k] ` `                ``y ``=` `j ` `             `  `    ``# Print the contents of the array ` `    ``for` `i ``in` `range``(``0``, y, ``1``): ` `        ``print``(print1[i], end ``=` `" "``) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``a ``=` `[``1``, ``2``, ``3``, ``4``, ``5``] ` `    ``n ``=` `len``(a) ` `    ``maxLengthSubSeq(a, n) ` ` `  `# This code is contributed by  ` `# Surendra_Gangwar `

 `//C# implementation of the approach/ ` ` `  `using` `System; ` ` `  `public` `class` `GFG{ ` `         `  `// Function to print the maximum length sub-sequence ` `static` `void` `maxLengthSubSeq(``int` `[]a, ``int` `n) ` `{ ` `    ``// Arrays to store the values to be printed ` `    ``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; ` ` `  `        ``// Store the first value into the temp array ` `        ``temp[j++] = a[x]; ` ` `  `        ``// Index of the next element ` `        ``x = a[x] + x; ` ` `  `        ``// Iterate till index is in range of the array ` `        ``while` `(x < n) { ` `            ``temp[j++] = a[x]; ` `            ``x = a[x] + x; ` `        ``} ` ` `  `        ``// If the length (temp) > the length (print) then ` `        ``// copy the contents of the temp array into ` `        ``// the print array ` `        ``if` `(y < j) { ` `            ``for` `(``int` `k = 0; k < j; k++) { ` `                ``print[k] = temp[k]; ` `                ``y = j; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Print the contents of the array ` `    ``for` `(``int` `i = 0; i < y; i++) ` `            ``Console.Write(print[i] + ``" "``); ` `} ` ` `  `// Driver code ` `    ``static` `public` `void` `Main (){ ` `         `  `    ``int` `[]a = { 1, 2, 3, 4, 5 }; ` `    ``int` `n = a.Length; ` `    ``maxLengthSubSeq(a, n); ` `    ``} ` `//This code is contributed by ajit.  ` `} `

 ` the length  ` `        ``// (print) then copy the contents of  ` `        ``// the temp array into the print array ` `        ``if` `(``\$y` `< ``\$j``) ` `        ``{ ` `            ``for` `(``\$k` `= 0; ``\$k` `< ``\$j``; ``\$k``++) ` `            ``{ ` `                ``\$print``[``\$k``] = ``\$temp``[``\$k``]; ` `                ``\$y` `= ``\$j``; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Print the contents of the array ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$y``; ``\$i``++) ` `        ``echo` `\$print``[``\$i``] . ``" "``; ` `} ` ` `  `// Driver code ` `\$a` `= ``array``(1, 2, 3, 4, 5); ` `\$n` `= sizeof(``\$a``); ` `maxLengthSubSeq(``\$a``, ``\$n``); ` ` `  `// This code is contributed ` `// by Akanksha Rai `

Output:
```1 2 4
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :