下一个元素的索引为 arr[arr[i] + i] 的最长子序列
给定一个数组arr[] ,任务是从数组中找到满足以下条件的最大长度子序列:
可以选择任何元素作为子序列的第一个元素,但下一个元素的索引将由arr[arr[i] + i]确定,其中i是序列中前一个元素的索引。
例子:
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
方法:
- 利用两个数组temp和print 。
- temp数组将存储当前正在考虑的数组元素,而print数组将存储要作为最终输出打印的数组元素。
- 从0迭代到n – 1并将当前元素视为序列的第一个元素。
- 将当前序列的所有元素存储到临时数组中。
- 如果临时数组的大小变得大于打印数组,则将临时数组的所有内容复制到打印数组。
- 考虑完所有序列后,打印打印数组的内容。
下面是上述方法的实现:
C++
// 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
//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.
}
Python3
# 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#
//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.
}
PHP
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
Javascript
输出:
1 2 4