📌  相关文章
📜  通过将 arr[i] 移动到索引 arr[i] 再次访问同一索引所需的通过次数

📅  最后修改于: 2022-05-13 01:56:04.798000             🧑  作者: Mango

通过将 arr[i] 移动到索引 arr[i] 再次访问同一索引所需的通过次数

给定一个前N个自然数排列的数组(基于 1 的索引) arr[] ,每个元素的任务是找到到达该索引所需的移动次数,使得在每次移动中,索引i处的数组元素移动到索引arr[i]处的数组元素。

例子:

方法:给定的问题可以通过查找每个索引的每个数组元素所需的移动次数来解决。请按照以下步骤解决给定的问题:

  • 使用变量i遍历给定的数组arr[]并执行以下步骤:
    • 初始化一个变量,比如存储所需移动总数的count
    • 初始化一个变量,比如Ki并迭代一个 do-while 循环,在该循环中将K的值更新为arr[K]并将count的值增加1直到Ki的值不同。
    • 完成上述步骤后,将count的值打印为当前索引所需的移动结果计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the number of moves
// required to visit the same index
// again for every array element
void numberOfPasses(vector arr, int N)
{
    // Make given array 0 index based
    for (int i = 0; i < N; ++i) {
        --arr[i];
    }
 
    for (int i = 0; i < N; ++i) {
 
        // Stores the number of moves
        int cnt = 0;
 
        // Store index value
        int k = i;
 
        do {
 
            // Update the value of cnt
            ++cnt;
 
            // Make a pass
            k = arr[k];
 
        } while (k != i);
 
        // Print the value of cnt
        cout << cnt << " ";
    }
}
 
// Driver Code
int main()
{
    vector arr{ 4, 6, 2, 1, 5, 3 };
    int N = arr.size();
    numberOfPasses(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
 
  // Function to find the number of moves
  // required to visit the same index
  // again for every array element
  static void numberOfPasses(int[] arr, int N)
  {
 
    // Make given array 0 index based
    for (int i = 0; i < N; ++i) {
      --arr[i];
    }
 
    for (int i = 0; i < N; ++i) {
 
      // Stores the number of moves
      int cnt = 0;
 
      // Store index value
      int k = i;
 
      do {
 
        // Update the value of cnt
        ++cnt;
 
        // Make a pass
        k = arr[k];
 
      } while (k != i);
 
      // Print the value of cnt
      System.out.print(cnt+" ");
    }
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int[] arr = { 4, 6, 2, 1, 5, 3 };
    int N = arr.length;
    numberOfPasses(arr, N);
 
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
 
# Function to find the number of moves
# required to visit the same index
# again for every array element
def numberOfPasses(arr, N):
 
    # Make given array 0 index based
    for i in range(0, N): 
        arr[i] = arr[i] - 1
     
 
    for i in range(0, N):
 
        # Stores the number of moves
        cnt = 0;
 
        # Store index value
        k = i;
 
        while(True):
 
            # Update the value of cnt
            cnt = cnt + 1
 
            # Make a pass
            k = arr[k]
 
            if (k == i):
                break;
 
        # Print the value of cnt
        print(cnt, end=" ")
     
# Driver Code
 
arr = [4, 6, 2, 1, 5, 3]
N = len(arr)
numberOfPasses(arr, N)
 
# This code is contributed by ihritik


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the number of moves
// required to visit the same index
// again for every array element
static void numberOfPasses(int []arr, int N)
{
 
    // Make given array 0 index based
    for (int i = 0; i < N; ++i) {
        --arr[i];
    }
 
    for (int i = 0; i < N; ++i) {
 
    // Stores the number of moves
    int cnt = 0;
 
    // Store index value
    int k = i;
 
    do {
        // Update the value of cnt
        ++cnt;
 
        // Make a pass
        k = arr[k];
 
    } while (k != i);
 
    // Print the value of cnt
    Console.Write(cnt + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int []arr = { 4, 6, 2, 1, 5, 3 };
    int N = arr.Length;
    numberOfPasses(arr, N);
}
}
 
// This code is contributed by Samim Hossain Mondal


Javascript


输出
2 3 3 2 1 3 

时间复杂度: O(N 2 )
辅助空间: O(1)