📜  无序索引对的计数,使得这些索引处的元素比率与索引比率相同

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

无序索引对的计数,使得这些索引处的元素比率与索引比率相同

给定一个包含 N 个整数的数组 arr[],任务是找到数组中无序对 (i, j) 的数量,使得这些索引处元素的比率与索引的比率 ( arr[j] /arr[i] = j/i )。

例子:

朴素方法:给定问题可以通过迭代给定数组中的所有无序对 (i, j) 来解决,同时跟踪满足条件 arr[j] / arr[i] = j / i 的对数.

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function of find the count of unordered
// pairs (i, j) in the array such that
// arr[j] / arr[i] = j / i.
int countPairs(int arr[], int n)
{
    // Stores the count of valid pairs
    int count = 0;
 
    // Iterating over all possible pairs
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // Check if the pair is valid
            if ((arr[j] % arr[i] == 0)
                && (j + 1) % (i + 1) == 0
                && (arr[j] / arr[i]
                    == (j + 1) / (i + 1))) {
                count++;
            }
        }
    }
 
    // Return answer
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, -2, 4, 20, 25, -6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << countPairs(arr, n);
 
    return 0;
}


Java
// Java program of the above approach
import java.util.*;
 
class GFG
{
 
// Function of find the count of unordered
// pairs (i, j) in the array such that
// arr[j] / arr[i] = j / i.
static int countPairs(int arr[], int n)
{
   
    // Stores the count of valid pairs
    int count = 0;
 
    // Iterating over all possible pairs
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // Check if the pair is valid
            if ((arr[j] % arr[i] == 0)
                && (j + 1) % (i + 1) == 0
                && (arr[j] / arr[i]
                    == (j + 1) / (i + 1))) {
                count++;
            }
        }
    }
 
    // Return answer
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 5, -2, 4, 20, 25, -6 };
    int n = arr.length;
 
    // Function Call
    System.out.print(countPairs(arr, n));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 3 program of the above approach
 
# Function of find the count of unordered
# pairs (i, j) in the array such that
# arr[j] / arr[i] = j / i.
def countPairs(arr, n):
 
    # Stores the count of valid pairs
    count = 0
 
    # Iterating over all possible pairs
    for i in range(n - 1):
        for j in range(i + 1, n):
 
            # Check if the pair is valid
            if ((arr[j] % arr[i] == 0)
                and (j + 1) % (i + 1) == 0
                and (arr[j] // arr[i]
                     == (j + 1) // (i + 1))):
                count += 1
 
    # Return answer
    return count
 
# Driver Code
if __name__ == "__main__":
 
    arr = [5, -2, 4, 20, 25, -6]
    n = len(arr)
 
    # Function Call
    print(countPairs(arr, n))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
// Function of find the count of unordered
// pairs (i, j) in the array such that
// arr[j] / arr[i] = j / i.
static int countPairs(int[] arr, int n)
{
    
    // Stores the count of valid pairs
    int count = 0;
  
    // Iterating over all possible pairs
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
  
            // Check if the pair is valid
            if ((arr[j] % arr[i] == 0)
                && (j + 1) % (i + 1) == 0
                && (arr[j] / arr[i]
                    == (j + 1) / (i + 1))) {
                count++;
            }
        }
    }
  
    // Return answer
    return count;
}   
   
    // Driver Code
    public static void Main (string[] args)
    {
        int[] arr = { 5, -2, 4, 20, 25, -6 };
        int n = arr.Length;
  
        // Function Call
        Console.WriteLine(countPairs(arr, n));
    }
}
 
// This code is contributed by avijitmondal1998.


Javascript


C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function of find the count of unordered
// pairs (i, j) in the array such that
// arr[j] / arr[i] = j / i.
int countPairs(int arr[], int n)
{
    // Stores the count of valid pairs
    int count = 0;
 
    // Iterating over all values of
    // x in range [1, N].
    for (int x = 1; x <= n; x++) {
 
        // Iterating over all values
        // of y that are divisible by
        // x in the range [1, N].
        for (int y = 2 * x; y <= n; y += x) {
 
            // Check if the pair is valid
            if ((arr[y - 1] % arr[x - 1] == 0)
                && (arr[y - 1] / arr[x - 1]
                    == y / x)) {
                count++;
            }
        }
    }
 
    // Return answer
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, -2, 4, 20, 25, -6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << countPairs(arr, n);
 
    return 0;
}


Java
// Java program of the above approach
class GFG{
 
// Function of find the count of unordered
// pairs (i, j) in the array such that
// arr[j] / arr[i] = j / i.
static int countPairs(int arr[], int n)
{
   
    // Stores the count of valid pairs
    int count = 0;
 
    // Iterating over all values of
    // x in range [1, N].
    for (int x = 1; x <= n; x++) {
 
        // Iterating over all values
        // of y that are divisible by
        // x in the range [1, N].
        for (int y = 2 * x; y <= n; y += x) {
 
            // Check if the pair is valid
            if ((arr[y - 1] % arr[x - 1] == 0)
                && (arr[y - 1] / arr[x - 1]
                    == y / x)) {
                count++;
            }
        }
    }
 
    // Return answer
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 5, -2, 4, 20, 25, -6 };
    int n = arr.length;
 
    // Function Call
    System.out.print(countPairs(arr, n));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 3 program of the above approach
 
# Function of find the count of unordered
# pairs (i, j) in the array such that
# arr[j] / arr[i] = j / i.
def countPairs(arr, n):
   
    # Stores the count of valid pairs
    count = 0
 
    # Iterating over all values of
    # x in range [1, N].
    for x in range(1, n + 1, 1):
       
        # Iterating over all values
        # of y that are divisible by
        # x in the range [1, N].
        for y in range(2 * x, n + 1, x):
           
            # Check if the pair is valid
            if ((arr[y - 1] % arr[x - 1] == 0) and (arr[y - 1] // arr[x - 1] == y // x)):
                count += 1
 
    # Return answer
    return count
 
# Driver Code
if __name__ == '__main__':
    arr = [5, -2, 4, 20, 25, -6]
    n = len(arr)
 
    # Function Call
    print(countPairs(arr, n))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program of the above approach
using System;
class GFG {
 
    // Function of find the count of unordered
    // pairs (i, j) in the array such that
    // arr[j] / arr[i] = j / i.
    static int countPairs(int[] arr, int n)
    {
       
        // Stores the count of valid pairs
        int count = 0;
 
        // Iterating over all values of
        // x in range [1, N].
        for (int x = 1; x <= n; x++) {
 
            // Iterating over all values
            // of y that are divisible by
            // x in the range [1, N].
            for (int y = 2 * x; y <= n; y += x) {
 
                // Check if the pair is valid
                if ((arr[y - 1] % arr[x - 1] == 0)
                    && (arr[y - 1] / arr[x - 1] == y / x)) {
                    count++;
                }
            }
        }
 
        // Return answer
        return count;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 5, -2, 4, 20, 25, -6 };
        int n = arr.Length;
 
        // Function Call
        Console.WriteLine(countPairs(arr, n));
    }
}
 
// This code is contributed by ukasp.


Javascript



输出
3

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

Efficient Approach:上述方法可以优化 使用可以达到的任何对(x, y)y / x的最大值为N的观察。此外, y必须能被x整除。因此,对于范围[1, N]中的x ,迭代范围[ 1, N] 中的所有y以使y可被x整除并跟踪对(x, y)的数量,使得arr[ y] / arr[x] = y / x 。这可以使用埃拉托色尼筛来完成。下面是上述方法的实现:

C++

// C++ program of the above approach
#include 
using namespace std;
 
// Function of find the count of unordered
// pairs (i, j) in the array such that
// arr[j] / arr[i] = j / i.
int countPairs(int arr[], int n)
{
    // Stores the count of valid pairs
    int count = 0;
 
    // Iterating over all values of
    // x in range [1, N].
    for (int x = 1; x <= n; x++) {
 
        // Iterating over all values
        // of y that are divisible by
        // x in the range [1, N].
        for (int y = 2 * x; y <= n; y += x) {
 
            // Check if the pair is valid
            if ((arr[y - 1] % arr[x - 1] == 0)
                && (arr[y - 1] / arr[x - 1]
                    == y / x)) {
                count++;
            }
        }
    }
 
    // Return answer
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, -2, 4, 20, 25, -6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << countPairs(arr, n);
 
    return 0;
}

Java

// Java program of the above approach
class GFG{
 
// Function of find the count of unordered
// pairs (i, j) in the array such that
// arr[j] / arr[i] = j / i.
static int countPairs(int arr[], int n)
{
   
    // Stores the count of valid pairs
    int count = 0;
 
    // Iterating over all values of
    // x in range [1, N].
    for (int x = 1; x <= n; x++) {
 
        // Iterating over all values
        // of y that are divisible by
        // x in the range [1, N].
        for (int y = 2 * x; y <= n; y += x) {
 
            // Check if the pair is valid
            if ((arr[y - 1] % arr[x - 1] == 0)
                && (arr[y - 1] / arr[x - 1]
                    == y / x)) {
                count++;
            }
        }
    }
 
    // Return answer
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 5, -2, 4, 20, 25, -6 };
    int n = arr.length;
 
    // Function Call
    System.out.print(countPairs(arr, n));
}
}
 
// This code is contributed by shikhasingrajput

Python3

# Python 3 program of the above approach
 
# Function of find the count of unordered
# pairs (i, j) in the array such that
# arr[j] / arr[i] = j / i.
def countPairs(arr, n):
   
    # Stores the count of valid pairs
    count = 0
 
    # Iterating over all values of
    # x in range [1, N].
    for x in range(1, n + 1, 1):
       
        # Iterating over all values
        # of y that are divisible by
        # x in the range [1, N].
        for y in range(2 * x, n + 1, x):
           
            # Check if the pair is valid
            if ((arr[y - 1] % arr[x - 1] == 0) and (arr[y - 1] // arr[x - 1] == y // x)):
                count += 1
 
    # Return answer
    return count
 
# Driver Code
if __name__ == '__main__':
    arr = [5, -2, 4, 20, 25, -6]
    n = len(arr)
 
    # Function Call
    print(countPairs(arr, n))
     
    # This code is contributed by SURENDRA_GANGWAR.

C#

// C# program of the above approach
using System;
class GFG {
 
    // Function of find the count of unordered
    // pairs (i, j) in the array such that
    // arr[j] / arr[i] = j / i.
    static int countPairs(int[] arr, int n)
    {
       
        // Stores the count of valid pairs
        int count = 0;
 
        // Iterating over all values of
        // x in range [1, N].
        for (int x = 1; x <= n; x++) {
 
            // Iterating over all values
            // of y that are divisible by
            // x in the range [1, N].
            for (int y = 2 * x; y <= n; y += x) {
 
                // Check if the pair is valid
                if ((arr[y - 1] % arr[x - 1] == 0)
                    && (arr[y - 1] / arr[x - 1] == y / x)) {
                    count++;
                }
            }
        }
 
        // Return answer
        return count;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 5, -2, 4, 20, 25, -6 };
        int n = arr.Length;
 
        // Function Call
        Console.WriteLine(countPairs(arr, n));
    }
}
 
// This code is contributed by ukasp.

Javascript


输出
3

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