COUNTPAIRS - Editorial

Problem Link - Count Pairs with Given Sum

Problem Statement:

Given an array of integers and a target sum, count how many pairs of numbers in the array add up to the target sum.

For example, if the array is [1, 2, 3, 4, 3, 5] and the target sum is 6, the pairs are (1, 5), (2, 4), and (3, 3).

Approach:

For each number in the array, check every other number in the array to see if their sum equals the target sum. Loop through all possible pairs and count how many satisfy this condition. This is a brute-force approach where we use two nested loops to check every pair of elements.

Time Complexity:

O(n^2): The time complexity is quadratic because for each element, we check every other element in the array.

Space Complexity:

O(1): The space complexity is constant as we are only using a few variables for counting and iterating through the array.