📌  相关文章
📜  从一个数组中计数对,它们的和与差之积等于1

📅  最后修改于: 2021-05-13 22:30:45             🧑  作者: 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 可以满足要求的条件。请按照以下步骤解决问题:

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

下面是上述方法的实现:

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


输出:
2













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