📜  从数组中计算对的总和和差的乘积等于 1

📅  最后修改于: 2021-09-03 04:19:38             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是计算可能的数组元素对(arr[i], arr[j])使得(arr[i] + arr[j]) * (arr[i] – arr[j])为 1。

例子:

朴素的方法:最简单的方法是遍历数组,从给定的数组中生成所有可能的对,并对和与差的乘积为1的对进行计数。最后,打印完成上述步骤后得到的最终计数。

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

有效方法:对于任何一对数组元素(arr[i], arr[j]) ,给定条件可以表示为:

因此,可以得出结论,只有arr[i] = 1arr[j] = 0i < j对才能满足所需条件。请按照以下步骤解决问题:

  • 初始化两个变量oneCountdesiredPairs以分别存储 1 和所需对的计数。
  • 遍历给定数组并检查以下内容:
    • 如果 arr[i] = 1:将oneCount增加1
    • 如果 arr[i] = 0:将到目前为止获得的1的计数添加到desiredPairs
  • 完成上述步骤后,打印desiredPairs为所需答案的价值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the desired
// number of pairs
int countPairs(int arr[], int n)
{
    // Initialize oneCount
    int oneCount = 0;
 
    // Initialize the desiredPair
    int desiredPair = 0;
 
    // Traverse the given array
    for (int i = 0; i < n; i++) {
 
        // If 1 is encountered
        if (arr[i] == 1) {
            oneCount++;
        }
 
        // If 0 is encountered
        if (arr[i] == 0) {
 
            // Update count of pairs
            desiredPair += oneCount;
        }
    }
 
    // Return the final count
    return desiredPair;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 3, 1, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    // Function Call
    cout << countPairs(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to count the desired
// number of pairs
static int countPairs(int arr[], int n)
{
     
    // Initialize oneCount
    int oneCount = 0;
 
    // Initialize the desiredPair
    int desiredPair = 0;
 
    // Traverse the given array
    for(int i = 0; i < n; i++)
    {
         
        // If 1 is encountered
        if (arr[i] == 1)
        {
            oneCount++;
        }
 
        // If 0 is encountered
        if (arr[i] == 0)
        {
             
            // Update count of pairs
            desiredPair += oneCount;
        }
    }
 
    // Return the final count
    return desiredPair;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 3, 1, 1, 0 };
    int N = arr.length;
 
    // Function call
    System.out.println(countPairs(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to count the desired
# number of pairs
def countPairs(arr, n):
     
    # Initialize oneCount
    oneCount = 0
 
    # Initialize the desiredPair
    desiredPair = 0
 
    # Traverse the given array
    for i in range(n):
 
        # If 1 is encountered
        if (arr[i] == 1):
            oneCount += 1
 
        # If 0 is encountered
        if (arr[i] == 0):
 
            # Update count of pairs
            desiredPair += oneCount
 
    # Return the final count
    return desiredPair
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 3, 1, 1, 0 ]
    N = len(arr)
     
    # Function call
    print(countPairs(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the desired
// number of pairs
static int countPairs(int []arr, int n)
{
     
    // Initialize oneCount
    int oneCount = 0;
 
    // Initialize the desiredPair
    int desiredPair = 0;
 
    // Traverse the given array
    for(int i = 0; i < n; i++)
    {
         
        // If 1 is encountered
        if (arr[i] == 1)
        {
            oneCount++;
        }
 
        // If 0 is encountered
        if (arr[i] == 0)
        {
             
            // Update count of pairs
            desiredPair += oneCount;
        }
    }
 
    // Return the readonly count
    return desiredPair;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 3, 1, 1, 0 };
    int N = arr.Length;
 
    // Function call
    Console.WriteLine(countPairs(arr, N));
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live