Count of pairs of elements with Sum X and multiplication Y in given Array
Last Updated :
15 Nov, 2023
Given array A[] (-109 <= A[i] <= 109) of size N (2 <= N <= 107) along with integers X (-2*109 <= X <= 2*109) and Y (-1018 <= Y <= 1018), the task for this problem is to find the count of pairs (A[i], A[j]) such that X = A[i] + A[j] and Y = A[i] * A[j] also i < j. Also A[i], X, Y != 0
Examples:
Input: A[] = {1, 4, -2, 3, 3, 3}, X = 7, Y = 12
Output: 3
Explanation: pairs (A[2], A[4]) = (4, 3), (A[2], A[5]) = (4, 3), and (A[2], A[6]) = (4, 3) are the pairs which satisfy above conditions.
Input: A[] = {1, 3, 2}, X = 5, Y = 6
Output: 1
Explanation: There is only one pair (A[2], A[3]) = (3, 2) which satisfy above conditions (X = A[2] + A[3] = 5 and Y = A[2] * A[3] = 6).
Naïve Approach: The basic way to solve the problem is as follows:
Simply iterating with two loops and maintaining a counter of pairs which satisfy above conditions.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To solve the problem follow the below idea:
If X = A[i] + A[j] and Y = A[i] * A[j] then a2 – Xa + Y = 0 which is Quadratic equation in terms of sum and product of roots.
Here roots are A[i] and A[j]. so we just have to find the count of roots of quadratic equation in array A[] their multiplication will be answer. If the roots are integers then only its possible to find them in array otherwise answer will be zero. Here root will be (X + sqrt(Discriminant)) / 2 and (X – sqrt(Discriminant)) / 2 where Discriminant = X * X – 4 * Y.
Below are the steps for the above approach:
- Creating Variable for discriminant and calculate it.
- Check condition whether roots are integers or not. if not integer return 0.
- Find both the roots by above formula.
- If Discriminant is zero then there will be only one unique root find its count as cntRoot in array A[] and return answer: cntRoot * (cntRoot – 1) / 2.
- If Discriminant is not zero then find count of both the roots as firstRootCnt and secondRootCnt then return answer as : firstRootCnt * secondRootCnt.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int number)
{
return sqrt (number) * sqrt (number) == number;
}
int findPairCount( int A[], int N, int X, int Y)
{
int discriminant = X * X - 4 * Y;
if (isPerfectSquare(discriminant)
and (X + ( int ) sqrt (discriminant)) % 2 == 0
and (X - ( int ) sqrt (discriminant)) % 2 == 0) {
int firstRoot = (X + sqrt (discriminant)) / 2;
int secondRoot = (X - sqrt (discriminant)) / 2;
if (discriminant == 0) {
int cntRoot = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == firstRoot)
cntRoot++;
}
return cntRoot * (cntRoot - 1) / 2;
}
else {
int firstRootCnt = 0, secondRootCnt = 0;
for ( int i = 0; i < N; i++) {
if (firstRoot == A[i])
firstRootCnt++;
else if (secondRoot == A[i])
secondRootCnt++;
}
return firstRootCnt * secondRootCnt;
}
}
else {
return 0;
}
}
int32_t main()
{
int X = 7, Y = 12;
int A[] = { 1, 4, -2, 3, 3, 3 }, N = 6;
cout << findPairCount(A, N, X, Y) << endl;
int X1 = 5, Y1 = 6;
int A1[] = { 1, 3, 2 }, N1 = 3;
cout << findPairCount(A1, N1, X1, Y1) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Solution {
static boolean isPerfectSquare( int number) {
int sqrt = ( int ) Math.sqrt(number);
return sqrt * sqrt == number;
}
static int findPairCount( int [] A, int N, int X, int Y) {
int discriminant = X * X - 4 * Y;
if (isPerfectSquare(discriminant)
&& (X + ( int ) Math.sqrt(discriminant)) % 2 == 0
&& (X - ( int ) Math.sqrt(discriminant)) % 2 == 0 ) {
int firstRoot = (X + ( int ) Math.sqrt(discriminant)) / 2 ;
int secondRoot = (X - ( int ) Math.sqrt(discriminant)) / 2 ;
if (discriminant == 0 ) {
int cntRoot = 0 ;
for ( int i = 0 ; i < N; i++) {
if (A[i] == firstRoot)
cntRoot++;
}
return cntRoot * (cntRoot - 1 ) / 2 ;
} else {
int firstRootCnt = 0 , secondRootCnt = 0 ;
for ( int i = 0 ; i < N; i++) {
if (firstRoot == A[i])
firstRootCnt++;
else if (secondRoot == A[i])
secondRootCnt++;
}
return firstRootCnt * secondRootCnt;
}
}
else {
return 0 ;
}
}
public static void main(String[] args) {
int X = 7 , Y = 12 ;
int [] A = { 1 , 4 , - 2 , 3 , 3 , 3 };
int N = 6 ;
System.out.println(findPairCount(A, N, X, Y));
int X1 = 5 , Y1 = 6 ;
int [] A1 = { 1 , 3 , 2 };
int N1 = 3 ;
System.out.println(findPairCount(A1, N1, X1, Y1));
}
}
|
Python3
def isPerfectSquare(number):
root = int (number * * 0.5 )
return root * root = = number
def findPairCount(A, N, X, Y):
discriminant = X * X - 4 * Y
if isPerfectSquare(discriminant):
root = int (discriminant * * 0.5 )
if (X + root) % 2 = = 0 and (X - root) % 2 = = 0 :
firstRoot = (X + root) / / 2
secondRoot = (X - root) / / 2
if discriminant = = 0 :
cntRoot = 0
for i in range (N):
if A[i] = = firstRoot:
cntRoot + = 1
return cntRoot * (cntRoot - 1 ) / / 2
else :
firstRootCnt = 0
secondRootCnt = 0
for i in range (N):
if firstRoot = = A[i]:
firstRootCnt + = 1
elif secondRoot = = A[i]:
secondRootCnt + = 1
return firstRootCnt * secondRootCnt
return 0
def main():
X = 7
Y = 12
A = [ 1 , 4 , - 2 , 3 , 3 , 3 ]
N = 6
print (findPairCount(A, N, X, Y))
X1 = 5
Y1 = 6
A1 = [ 1 , 3 , 2 ]
N1 = 3
print (findPairCount(A1, N1, X1, Y1))
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG
{
static bool IsPerfectSquare( int number)
{
int sqrt = ( int )Math.Sqrt(number);
return sqrt * sqrt == number;
}
static int FindPairCount( int [] A, int N, int X, int Y)
{
int discriminant = X * X - 4 * Y;
if (IsPerfectSquare(discriminant)
&& (X + ( int )Math.Sqrt(discriminant)) % 2 == 0
&& (X - ( int )Math.Sqrt(discriminant)) % 2 == 0)
{
int firstRoot = (X + ( int )Math.Sqrt(discriminant)) / 2;
int secondRoot = (X - ( int )Math.Sqrt(discriminant)) / 2;
if (discriminant == 0)
{
int cntRoot = 0;
for ( int i = 0; i < N; i++)
{
if (A[i] == firstRoot)
cntRoot++;
}
return cntRoot * (cntRoot - 1) / 2;
}
else
{
int firstRootCnt = 0, secondRootCnt = 0;
for ( int i = 0; i < N; i++)
{
if (firstRoot == A[i])
firstRootCnt++;
else if (secondRoot == A[i])
secondRootCnt++;
}
return firstRootCnt * secondRootCnt;
}
}
else
{
return 0;
}
}
public static void Main( string [] args)
{
int X = 7, Y = 12;
int [] A = { 1, 4, -2, 3, 3, 3 };
int N = 6;
Console.WriteLine(FindPairCount(A, N, X, Y));
int X1 = 5, Y1 = 6;
int [] A1 = { 1, 3, 2 };
int N1 = 3;
Console.WriteLine(FindPairCount(A1, N1, X1, Y1));
}
}
|
Javascript
function GFG(number) {
const sqrt = Math.sqrt(number);
return Math.floor(sqrt) * Math.floor(sqrt) === number;
}
function Count(A, N, X, Y) {
const discriminant = X * X - 4 * Y;
if (GFG(discriminant)
&& (X + Math.sqrt(discriminant)) % 2 === 0
&& (X - Math.sqrt(discriminant)) % 2 === 0) {
const firstRoot = (X + Math.sqrt(discriminant)) / 2;
const secondRoot = (X - Math.sqrt(discriminant)) / 2;
if (discriminant === 0) {
let cntRoot = 0;
for (let i = 0; i < N; i++) {
if (A[i] === firstRoot)
cntRoot++;
}
return (cntRoot * (cntRoot - 1)) / 2;
} else {
let firstRootCnt = 0;
let secondRootCnt = 0;
for (let i = 0; i < N; i++) {
if (firstRoot === A[i])
firstRootCnt++;
else if (secondRoot === A[i])
secondRootCnt++;
}
return firstRootCnt * secondRootCnt;
}
}
else {
return 0;
}
}
const X = 7;
const Y = 12;
const A = [1, 4, -2, 3, 3, 3];
const N = 6;
console.log(Count(A, N, X, Y));
const X1 = 5;
const Y1 = 6;
const A1 = [1, 3, 2];
const N1 = 3;
console.log(Count(A1, N1, X1, Y1));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Share your thoughts in the comments
Please Login to comment...