📌  相关文章
📜  给定数组中的对数,其值的乘积等于其索引的总和 (arr[i]*arr[j] = i+j)

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

给定数组中的对数,其值的乘积等于其索引的总和 (arr[i]*arr[j] = i+j)

给定一个长度为N的数组arr[]具有从 1 到 2*N 的不同整数,任务是计算索引对(i, j)的数量,使得(i < j)arr[i] * arr[ j] = i + j ,即计算对的数量,使得它们的乘积等于它们的索引总和。

例子:

天真的方法: 使用(i < j)遍历所有索引对(i, j ) 并检查每一对是否满足上述条件,然后将答案加 1,否则转到下一对。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the number of
// unique pairs
int NumberOfRequiredPairs(int arr[], int N)
{
 
    // Variable that with stores number
    // of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++)
 
        // Traverse the array for every
        // possible j (i < j)
        // Please note that the indices
        // are used as 1 based indexing
        for (int j = i + 1; j < N; j++)
            if ((arr[i] * arr[j])
                == ((i + 1) + (j + 1)))
                ans++;
 
    // Return the ans
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 5;
    int arr[] = { 3, 1, 5, 9, 2 };
 
    // Function Call
    cout << NumberOfRequiredPairs(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
// Function to find the number of
// unique pairs
static int NumberOfRequiredPairs(int arr[], int N)
{
 
    // Variable that with stores number
    // of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++)
 
        // Traverse the array for every
        // possible j (i < j)
        // Please note that the indices
        // are used as 1 based indexing
        for (int j = i + 1; j < N; j++)
            if ((arr[i] * arr[j])
                == ((i + 1) + (j + 1)))
                ans++;
 
    // Return the ans
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
   
    // Given Input
    int N = 5;
    int arr[] = { 3, 1, 5, 9, 2 };
 
    // Function Call
    System.out.println(NumberOfRequiredPairs(arr, N));
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program for the above approach
 
# Function to find the number of
# unique pairs
def NumberOfRequiredPairs(arr, N):
 
    # Variable that with stores number
    # of valid pairs
    ans = 0
 
    # Traverse the array for every
    # possible index i
    for i in range(N):
 
        # Traverse the array for every
        # possible j (i < j)
        # Please note that the indices
        # are used as 1 based indexing
        for j in range(i + 1, N):
            if ((arr[i] * arr[j]) == ((i + 1) + (j + 1))):
                ans += 1
 
    # Return the ans
    return ans
 
# Driver Code
# Given Input
N = 5
arr = [3, 1, 5, 9, 2]
 
# Function Call
print(NumberOfRequiredPairs(arr, N))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to find the number of
// unique pairs
static int NumberOfRequiredPairs(int []arr, int N)
{
 
    // Variable that with stores number
    // of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++)
 
        // Traverse the array for every
        // possible j (i < j)
        // Please note that the indices
        // are used as 1 based indexing
        for (int j = i + 1; j < N; j++)
            if ((arr[i] * arr[j])
                == ((i + 1) + (j + 1)))
                ans++;
 
    // Return the ans
    return ans;
}
 
// Driver code
public static void  Main ()
{
   
    // Given Input
    int N = 5;
    int []arr = { 3, 1, 5, 9, 2 };
 
    // Function Call
    Console.Write(NumberOfRequiredPairs(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the number of
// unique pairs
int NumberOfRequiredPairs(int arr[], int N)
{
 
    // Variable that with stores
    // number of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++) {
 
        // Initialize a dummy variable
        // for arr[i]
        int k = arr[i];
 
        // We will loop through every
        // multiple of arr[i];
        // Looping through 2*N because
        // the maximum element
        // in array can be 2*N
        // Please not that i and j are
        // in 1 based indexing
        while (k <= 2 * N) {
 
            // Calculating j
            int j = k - i - 1;
 
            // Now check if this j lies
            // between the bounds
            // of the array
            if (j >= 1 && j <= N) {
 
                // Checking the required
                // condition
                if ((arr[j - 1] == k / arr[i])
                    && j > i + 1) {
                    ans++;
                }
            }
 
            // Increasing k to its next multiple
            k += arr[i];
        }
    }
 
    // Return the ans
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 5;
    int arr[] = { 3, 1, 5, 9, 2 };
 
    // Function Call
    cout << NumberOfRequiredPairs(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
 
// Function to find the number of
// unique pairs
static int NumberOfRequiredPairs(int arr[], int N)
{
 
    // Variable that with stores
    // number of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++) {
 
        // Initialize a dummy variable
        // for arr[i]
        int k = arr[i];
 
        // We will loop through every
        // multiple of arr[i];
        // Looping through 2*N because
        // the maximum element
        // in array can be 2*N
        // Please not that i and j are
        // in 1 based indexing
        while (k <= 2 * N) {
 
            // Calculating j
            int j = k - i - 1;
 
            // Now check if this j lies
            // between the bounds
            // of the array
            if (j >= 1 && j <= N) {
 
                // Checking the required
                // condition
                if ((arr[j - 1] == k / arr[i])
                    && j > i + 1) {
                    ans++;
                }
            }
 
            // Increasing k to its next multiple
            k += arr[i];
        }
    }
 
    // Return the ans
    return ans;
}
 
// Driver code
public static void main (String args[])
{
   
    // Given Input
    int N = 5;
    int arr[] = { 3, 1, 5, 9, 2 };
 
    // Function Call
    System.out.println(NumberOfRequiredPairs(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python3 program for the above approach
 
# Function to find the number of
# unique pairs
def NumberOfRequiredPairs(arr, N) :
 
    # Variable that with stores
    # number of valid pairs
    ans = 0;
 
    # Traverse the array for every
    # possible index i
    for i in range(N) :
 
        # Initialize a dummy variable
        # for arr[i]
        k = arr[i];
 
        # We will loop through every
        # multiple of arr[i];
        # Looping through 2*N because
        # the maximum element
        # in array can be 2*N
        # Please not that i and j are
        # in 1 based indexing
        while (k <= 2 * N) :
 
            # Calculating j
            j = k - i - 1;
 
            # Now check if this j lies
            # between the bounds
            # of the array
            if (j >= 1 and j <= N) :
 
                # Checking the required
                # condition
                if ((arr[j - 1] == k // arr[i]) and j > i + 1) :
                    ans += 1;
 
            # Increasing k to its next multiple
            k += arr[i];
 
    # Return the ans
    return ans;
 
# Driver Code
if __name__ == "__main__" :
 
    # Given Input
    N = 5;
    arr = [ 3, 1, 5, 9, 2 ];
 
    # Function Call
    print(NumberOfRequiredPairs(arr, N));
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to find the number of
// unique pairs
static int NumberOfRequiredPairs(int []arr, int N)
{
 
    // Variable that with stores
    // number of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++) {
 
        // Initialize a dummy variable
        // for arr[i]
        int k = arr[i];
 
        // We will loop through every
        // multiple of arr[i];
        // Looping through 2*N because
        // the maximum element
        // in array can be 2*N
        // Please not that i and j are
        // in 1 based indexing
        while (k <= 2 * N) {
 
            // Calculating j
            int j = k - i - 1;
 
            // Now check if this j lies
            // between the bounds
            // of the array
            if (j >= 1 && j <= N) {
 
                // Checking the required
                // condition
                if ((arr[j - 1] == k / arr[i])
                    && j > i + 1) {
                    ans++;
                }
            }
 
            // Increasing k to its next multiple
            k += arr[i];
        }
    }
 
    // Return the ans
    return ans;
}
 
// Driver code
public static void  Main ()
{
   
    // Given Input
    int N = 5;
    int []arr = { 3, 1, 5, 9, 2 };
 
    // Function Call
    Console.Write(NumberOfRequiredPairs(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
3

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

有效方法:将上述条件重写为


因此,对于arr[i] 的每个倍数,找到相应的j并检查arr[j]是否等于(i + j)/arr[i]。这种方法是有效的,因为对于每个i都需要遍历i的每个倍数直到2*N 。由于数组中的所有数字都是不同的,因此可以得出结论,计算j的总迭代次数如下:

这是logN的一系列扩展的众所周知的结果。有关更多信息,请在此处阅读。请按照以下步骤解决问题:

  • 将变量ans初始化为0以存储答案。
  • 使用变量i迭代范围[0, N]并执行以下步骤:
    • 将变量k初始化为arr[i] 的值。
    • 在 while 循环中迭代直到k小于等于2*N并执行以下任务:
      • 将变量j初始化为ki-1。
      • 如果j大于等于1且小于等于Narr[j – 1]等于k / arr[i]j大于i+1,则将ans的值增加1。
  • 执行上述步骤后,打印ans的值作为答案。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the number of
// unique pairs
int NumberOfRequiredPairs(int arr[], int N)
{
 
    // Variable that with stores
    // number of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++) {
 
        // Initialize a dummy variable
        // for arr[i]
        int k = arr[i];
 
        // We will loop through every
        // multiple of arr[i];
        // Looping through 2*N because
        // the maximum element
        // in array can be 2*N
        // Please not that i and j are
        // in 1 based indexing
        while (k <= 2 * N) {
 
            // Calculating j
            int j = k - i - 1;
 
            // Now check if this j lies
            // between the bounds
            // of the array
            if (j >= 1 && j <= N) {
 
                // Checking the required
                // condition
                if ((arr[j - 1] == k / arr[i])
                    && j > i + 1) {
                    ans++;
                }
            }
 
            // Increasing k to its next multiple
            k += arr[i];
        }
    }
 
    // Return the ans
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 5;
    int arr[] = { 3, 1, 5, 9, 2 };
 
    // Function Call
    cout << NumberOfRequiredPairs(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
public class GFG
{
 
// Function to find the number of
// unique pairs
static int NumberOfRequiredPairs(int arr[], int N)
{
 
    // Variable that with stores
    // number of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++) {
 
        // Initialize a dummy variable
        // for arr[i]
        int k = arr[i];
 
        // We will loop through every
        // multiple of arr[i];
        // Looping through 2*N because
        // the maximum element
        // in array can be 2*N
        // Please not that i and j are
        // in 1 based indexing
        while (k <= 2 * N) {
 
            // Calculating j
            int j = k - i - 1;
 
            // Now check if this j lies
            // between the bounds
            // of the array
            if (j >= 1 && j <= N) {
 
                // Checking the required
                // condition
                if ((arr[j - 1] == k / arr[i])
                    && j > i + 1) {
                    ans++;
                }
            }
 
            // Increasing k to its next multiple
            k += arr[i];
        }
    }
 
    // Return the ans
    return ans;
}
 
// Driver code
public static void main (String args[])
{
   
    // Given Input
    int N = 5;
    int arr[] = { 3, 1, 5, 9, 2 };
 
    // Function Call
    System.out.println(NumberOfRequiredPairs(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.

Python3

# Python3 program for the above approach
 
# Function to find the number of
# unique pairs
def NumberOfRequiredPairs(arr, N) :
 
    # Variable that with stores
    # number of valid pairs
    ans = 0;
 
    # Traverse the array for every
    # possible index i
    for i in range(N) :
 
        # Initialize a dummy variable
        # for arr[i]
        k = arr[i];
 
        # We will loop through every
        # multiple of arr[i];
        # Looping through 2*N because
        # the maximum element
        # in array can be 2*N
        # Please not that i and j are
        # in 1 based indexing
        while (k <= 2 * N) :
 
            # Calculating j
            j = k - i - 1;
 
            # Now check if this j lies
            # between the bounds
            # of the array
            if (j >= 1 and j <= N) :
 
                # Checking the required
                # condition
                if ((arr[j - 1] == k // arr[i]) and j > i + 1) :
                    ans += 1;
 
            # Increasing k to its next multiple
            k += arr[i];
 
    # Return the ans
    return ans;
 
# Driver Code
if __name__ == "__main__" :
 
    # Given Input
    N = 5;
    arr = [ 3, 1, 5, 9, 2 ];
 
    # Function Call
    print(NumberOfRequiredPairs(arr, N));
 
    # This code is contributed by AnkThon

C#

// C# program for the above approach
using System;
class GFG
{
 
// Function to find the number of
// unique pairs
static int NumberOfRequiredPairs(int []arr, int N)
{
 
    // Variable that with stores
    // number of valid pairs
    int ans = 0;
 
    // Traverse the array for every
    // possible index i
    for (int i = 0; i < N; i++) {
 
        // Initialize a dummy variable
        // for arr[i]
        int k = arr[i];
 
        // We will loop through every
        // multiple of arr[i];
        // Looping through 2*N because
        // the maximum element
        // in array can be 2*N
        // Please not that i and j are
        // in 1 based indexing
        while (k <= 2 * N) {
 
            // Calculating j
            int j = k - i - 1;
 
            // Now check if this j lies
            // between the bounds
            // of the array
            if (j >= 1 && j <= N) {
 
                // Checking the required
                // condition
                if ((arr[j - 1] == k / arr[i])
                    && j > i + 1) {
                    ans++;
                }
            }
 
            // Increasing k to its next multiple
            k += arr[i];
        }
    }
 
    // Return the ans
    return ans;
}
 
// Driver code
public static void  Main ()
{
   
    // Given Input
    int N = 5;
    int []arr = { 3, 1, 5, 9, 2 };
 
    // Function Call
    Console.Write(NumberOfRequiredPairs(arr, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript


输出
3

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