Interview Questions/Coding/Count Target Pairs

Count Target Pairs

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Count Target Pairs

Count the number of distinct index pairs (i, j) such that: You are given an integer array nums and an integer target. 0 ≤ i < j < n nums[i] + nums[j] = target Return the total number of such pairs. Input Format- First line contains an integer n, the size of the array. Second line contains n space-separated integers representing nums. Third line contains an integer target. Output Format- Print the number of valid pairs.

Examples

Example 1
Input:
4
2 7 11 15
9

Output
1

Explanation
Only pair (0,1) has sum equal to 9.
Output:
1

Explanation: Only pair (0,1) has sum equal to 9.

Approach hint

Try checking every pair of indices.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp