📌  相关文章
📜  每个元素与数组中另一个元素的索引相等的对数

📅  最后修改于: 2021-05-04 15:41:37             🧑  作者: Mango

给定一个整数N和一个数组arr [] ,该数组包含范围为[1,N]的元素,任务是查找所有对的计数(arr [i],arr [j]) ,以使i i == arr [j]和j == arr [i]
例子:

天真的方法:
最简单的方法是生成给定数组的所有可能对,如果任何对满足给定条件,则增加count 。最后,打印count的值。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:
请按照以下步骤解决上述方法:

  • 遍历给定数组,并保留其索引等于arr [arr [index] – 1] – 1的元素计数(例如cnt )。这将根据给定的标准对有效对进行计数。
  • 遍历后,总计数由cnt / 2给出,因为我们在上述遍历中每对计数两次。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
// Function to print the count of pair
void countPairs(int N, int arr[])
{
    int count = 0;
 
    // Iterate over all the
    // elements of the array
    for(int i = 0; i < N; i++)
    {
        if (i == arr[arr[i] - 1] - 1)
        {
             
            // Increment the count
            count++;
        }
    }
 
    // Print the result
    cout << (count / 2) << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 1, 4, 3 };
    int N = sizeof(arr)/sizeof(arr[0]);
 
    countPairs(N, arr);
}
 
// This code is contributed by Amit Katiyar


Java
// Java Program to implement
// the above approach
import java.util.*;
 
class GFG {
 
    // Function to print the count of pair
    static void countPairs(int N, int[] arr)
    {
        int count = 0;
 
        // Iterate over all the
        // elements of the array
        for (int i = 0; i < N; i++) {
 
            if (i == arr[arr[i] - 1] - 1) {
 
                // Increment the count
                count++;
            }
        }
 
        // Print the result
        System.out.println(count / 2);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 2, 1, 4, 3 };
        int N = arr.length;
 
        countPairs(N, arr);
    }
}


Python3
# Python3 program to implement
# the above approach
# Function to print the count of pair
def countPairs(N, arr):
 
    count = 0
 
    # Iterate over all the
    # elements of the array
    for i in range(N):
        if (i == arr[arr[i] - 1] - 1):
        
            # Increment the count
            count += 1
 
    # Print the result
    print(count // 2)
 
# Driver Code
if __name__ == "__main__":
   
    arr = [2, 1, 4, 3]
    N = len(arr)
    countPairs(N, arr)
 
# This code is contributed by Chitranayal


C#
// C# Program to implement
// the above approach
using System;
class GFG{
  
  // Function to print the count of pair
  static void countPairs(int N, int[] arr)
  {
    int count = 0;
 
    // Iterate over all the
    // elements of the array
    for (int i = 0; i < N; i++)
    {
      if (i == arr[arr[i] - 1] - 1)
      {
 
        // Increment the count
        count++;
      }
    }
 
    // Print the result
    Console.Write(count / 2);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int[] arr = { 2, 1, 4, 3 };
    int N = arr.Length;
 
    countPairs(N, arr);
  }
}
 
// This code is contributed by Ritik Bansal


Javascript


输出:
2

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