给定大小为N的数组arr [] ,任务是对可能的成对数组元素(arr [i],arr [j])进行计数,使得(arr [i] + arr [j])*(arr [i] – arr [j])为1。
例子:
Input: arr[] = {3, 1, 1, 0}
Output: 2
Explanation:
The two possible pairs are:
- (arr[1] + arr[3]) * (arr[1] – arr[3]) = 1
- (arr[2] + arr[3]) * (arr[2] – arr[3]) = 1
Input: arr[] = {12, 0, 1, 1, 14, 0, 9, 0}
Output: 4
Explanation:
The four possible pairs are as follows:
- (3, 6): (arr[3] + arr[6]) * (arr[3] – arr[6]) = 1
- (4, 6): (arr[4] + arr[6]) * (arr[4] – arr[6]) = 1
- (3, 8): (arr[3] + arr[8]) * (arr[3] – arr[8]) = 1
- (3, 6): (arr[4] + arr[8]) * (arr[4] – arr[8]) = 1
天真的方法:最简单的方法是遍历数组,并从给定的数组中生成所有可能的对,并对那些总和与差之和为1的对进行计数。最后,打印完成上述步骤后获得的最终计数。
时间复杂度: O(N 2 )
辅助空间: O(N)
高效的方法:可以将任意一对数组元素(arr [i],arr [j])的给定条件表示为:
(arr[i] + arr[j]) * (arr[i] – arr[j]) = 1
=> (arr[i]2 – arr[j]2) = 1
因此,可以得出结论,仅对条件arr [i] = 1和arr [j] = 0且i
- 初始化两个变量oneCount和desirePairs分别存储1的计数和所需的对。
- 遍历给定的数组并检查以下内容:
- 如果arr [i] = 1:将oneCount加1 。
- 如果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)