给定一个数组arr[]由[0, N – 1]范围内的数字排列组成,任务是从数组中找到最长子集的长度,使得子集中的元素的形式为 { arr [i], arr[arr[i]], arr[arr[arr[i]]], …}
例子:
Input: arr[] = {5, 4, 0, 3, 1, 6, 2}
Output: 4
Explanation:
arr[arr[0]] is equal to arr[5]
arr[arr[arr[0]]] is equal to arr[6]
arr[arr[arr[arr[0]]]] is equal to arr[2]
One of the possible subsets from the array are {arr[0], arr[5], arr[6], arr[2]} = {5, 6, 2, 0}, which is the longest subset satisfying the given condition. Therefore, the required output is 4.
Input: arr[] ={3, 1, 4, 0, 2}
Output: 2
Explanation:
arr[arr[0]] is equal to arr[3]
One of the possible subset from the array is {arr[0], arr[3]} = {3, 0}
Therefore, the required output is 2.
处理方法:按照以下步骤解决问题:
- 初始化一个变量res = 0来存储满足条件的数组最长子集的长度。
- 遍历数组arr[]并执行以下操作:
- 检查arr[i]是否等于i 。如果发现为真,则更新res = max(res, 1) 。
- 初始化一个变量,比如index = i ,以存储给定数组中子集元素的索引。
- 在arr[index] != index时迭代子集的元素,将子集的当前元素更新为当前索引,并更新index = arr[index] 。
- 最后,更新资源的最大资源,并在当前子集元素的个数。
- 最后,打印res 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find length of longest subset
// such that subset { arr[i], arr[arr[i]], .. }
int arrayNesting(vector arr)
{
// Stores length of the longest subset
// that satisfy the condition
int res = 0;
// Traverse in the array, arr[]
for (int i = 0; i < arr.size(); i++)
{
// If arr[i] equals to i
if (arr[i] == i)
{
// Update res
res = max(res, 1);
}
else
{
// Count of elements in a subset
int count = 0;
// Stores index of elements in
// the current subset
int curr_index = i;
// Calculate length of a subset that
// satisfy the condition
while (arr[curr_index] != curr_index)
{
int next_index = arr[curr_index];
// Make visited the current index
arr[curr_index] = curr_index;
// Update curr_index
curr_index = next_index;
// Update count
count++;
}
// Update res
res = max(res, count);
}
}
return res;
}
// Driver Code
int main()
{
vector arr = { 5, 4, 0, 3, 1, 6, 2 };
int res = arrayNesting(arr);
cout<
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class sol {
// Function to find length of longest subset
// such that subset { arr[i], arr[arr[i]], .. }
public int arrayNesting(int[] arr)
{
// Stores length of the longest subset
// that satisfy the condition
int res = 0;
// Traverse in the array, arr[]
for (int i = 0; i < arr.length; i++) {
// If arr[i] equals to i
if (arr[i] == i) {
// Update res
res = Math.max(res, 1);
}
else {
// Count of elements in a subset
int count = 0;
// Stores index of elements in
// the current subset
int curr_index = i;
// Calculate length of a subset that
// satisfy the condition
while (arr[curr_index]
!= curr_index) {
int next_index = arr[curr_index];
// Make visited the current index
arr[curr_index] = curr_index;
// Update curr_index
curr_index = next_index;
// Update count
count++;
}
// Update res
res = Math.max(res, count);
}
}
return res;
}
}
// Driver Code
class GFG {
public static void main(String[] args)
{
sol st = new sol();
int[] arr = { 5, 4, 0, 3, 1, 6, 2 };
int res = st.arrayNesting(arr);
System.out.println(res);
}
}
C#
// C# program to implement
// the above approach
using System;
class sol {
// Function to find length of longest subset
// such that subset { arr[i], arr[arr[i]], .. }
public int arrayNesting(int[] arr)
{
// Stores length of the longest subset
// that satisfy the condition
int res = 0;
// Traverse in the array, arr[]
for (int i = 0; i < arr.Length; i++) {
// If arr[i] equals to i
if (arr[i] == i) {
// Update res
res = Math.Max(res, 1);
}
else {
// Count of elements in a subset
int count = 0;
// Stores index of elements in
// the current subset
int curr_index = i;
// Calculate length of a subset that
// satisfy the condition
while (arr[curr_index] != curr_index) {
int next_index = arr[curr_index];
// Make visited the current index
arr[curr_index] = curr_index;
// Update curr_index
curr_index = next_index;
// Update count
count++;
}
// Update res
res = Math.Max(res, count);
}
}
return res;
}
}
// Driver Code
class GFG {
static public void Main()
{
sol st = new sol();
int[] arr = { 5, 4, 0, 3, 1, 6, 2 };
int res = st.arrayNesting(arr);
Console.WriteLine(res);
}
}
// This code is contributed by Dharanendra L V
Python3
# Python program for the above approach
# Function to find length of longest subset
# such that subset { arr[i], arr[arr[i]], .. }
def arrayNesting(arr) :
# Stores length of the longest subset
# that satisfy the condition
res = 0
# Traverse in the array, arr[]
for i in range(len(arr)) :
# If arr[i] equals to i
if arr[i] == i :
# Update res
res = max(res, 1)
else :
# Count of elements in a subset
count = 0
# Stores index of elements in
# the current subset
curr_index = i
# Calculate length of a subset that
# satisfy the condition
while arr[curr_index] != curr_index :
next_index = arr[curr_index]
# Make visited the current index
arr[curr_index] = curr_index
# Update curr_index
curr_index = next_index
# Update count
count += 1
# Update res
res = max(res, count)
return res
# Driver Code
if __name__ == "__main__" :
arr = [ 5, 4, 0, 3, 1, 6, 2 ]
res = arrayNesting(arr)
print(res)
# This code is contributed by jana_sayantam..
Javascript
4
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live