Data Structure & Algorithms
DSA
Introduction to arrays
Good Pair

Good Pair

Problem Description

Given an array A and an integer B. A pair(i, j) in the array is a good pair if i != j and (A[i] + A[j] == B). Check if any good pair exist or not.

Problem Constraints

1 <= A.size() <= 10^4
1 <= A[i] <= 10^9
1 <= B <= 10^9

Input Format

First argument is an integer array A.
Second argument is an integer B.

Output Format

Return 1 if good pair exist otherwise return 0.

Example Input

Input 1:
A = [1,2,3,4]
B = 7

Input 2:
A = [1,2,4]
B = 4

Input 3:
A = [1,2,2]
B = 4

Example Output

Output 1:
1

Output 2:
0

Output 3:
1

Example Explanation

Explanation 1:
(i,j) = (3,4)

Explanation 2:
No pair has sum equal to 4.

Explanation 3:
(i,j) = (2,3)

Output

Java
import java.util.HashSet;
 
public class GoodPair {
    public int solve(int[] A, int B) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : A) {
            if (set.contains(B - num)) {
                return 1;
            }
            set.add(num);
        }
        return 0;
    }
}
Python
def solve(A, B):
    seen = set()
    for num in A:
        if B - num in seen:
            return 1
        seen.add(num)
    return 0
JavaScript
function solve(A, B) {
    let set = new Set();
    for (let num of A) {
        if (set.has(B - num)) {
            return 1;
        }
        set.add(num);
    }
    return 0;
}