📌  相关文章
📜  给定数组中嵌套元素的最长子集

📅  最后修改于: 2021-09-06 06:14:09             🧑  作者: Mango

给定一个数组arr[][0, N – 1]范围内的数字排列组成,任务是从数组中找到最长子集的长度,使得子集中的元素的形式为 { arr [i], arr[arr[i]], arr[arr[arr[i]]], …}

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个变量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