给定一个大小为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 < j对才能满足所需条件。请按照以下步骤解决问题:
- 初始化两个变量oneCount和desiredPairs以分别存储 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