Find Pairs in Array with given Sum in C
Find Pairs in Array with given Sum in C Language
On this page, we will look into a coding question where we will learn how to Find Pairs in an Array with a given sum in C Programing Language.
There might be different approaches to solve this question, one you will find here. If your approach is a bit different post it in the comment section.

Find Pairs in Array with given Sum in C
To find pairs in an array with a given sum in C, we can use two methods
- Brute Force (Time complexity: O(n^2) )
- Using Sorting (Time complexity: O(n log n) )
Both methods have their own advantages and disadvantages. The brute force method is simple to implement but can be slow for large arrays. The sorting method is faster for large arrays, but requires extra space for sorting and modifying the original array.

Method 1 : Brute Force Method
- In the brute force method, we compare each element in the array with all the other elements in the array to check if their sum is equal to the given sum.
- This method has a time complexity of O(n^2) as we have to compare each element with every other element
Prime Course Trailer
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
C code for brute force method
#include <stdio.h> void findPair (int nums[], int n, int target) { int flag = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (nums[i] + nums[j] == target) { printf ("Pair found (%d, %d) \n", nums[i], nums[j]); flag = 1; } } } if (flag == 0) { printf ("Pair not found"); } } int main () { int nums[] = {5, 2, 3, 4, 1, 6, 7}; int target = 7; int n = sizeof (nums) / sizeof (nums[0]); findPair (nums, n, target); return 0; }
Output
Pair found (5, 2) Pair found (3, 4) Pair found (1, 6)
Method 2 : Sorting Method
- The array is first sorted in ascending order in the sorting technique.
- Then, we employ two pointers, one of which points to the first element and the other to the last element.
- These two pointers values are added, and we then check to see if the result equals the specified sum.
The left pointer is increased if the value is less than the total, and the right pointer is decreased if the value is higher. - This method has a time complexity of O(n log n) due to the sorting algorithm used.
C code for Sorting Method
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <math.h> int cmpfunc (const void *a, const void *b) { return (*(int *) a - *(int *) b); } // Function to find a pair in an array with a given sum using sorting void findPair(int nums[], int n, int target) { // sort the array in ascending order qsort (nums, n, sizeof (int), cmpfunc); // maintain two indices pointing to endpoints of the array int low = 0; int high = n - 1; int flag = 0; // reduce the search space `nums[low…high]` at each iteration of the loop // loop till the search space is exhausted while (low < high) { // sum found if (nums[low] + nums[high] == target) { printf("Pair found (%d, %d) \n",nums[low] ,nums[high]); flag=1; } // increment `low` index if the total is less than the desired sum; // decrement `high` index if the total is more than the desired sum (nums[low] + nums[high] < target)? low++: high--; } if(flag==0) { printf("Pair not found"); } // we reach here if the pair is not found } int main() { int nums[] = {5, 2, 3, 4, 1, 6, 7}; int target = 7; int n = sizeof(nums)/sizeof(nums[0]); findPair(nums, n, target); return 0; }
Output
Pair found (1, 6) Pair found (2, 5) Pair found (3, 4)
Conclusion
Finding pairs in an array that add up to a given sum is a common problem that helps you practice your problem-solving and logic-building skills. In this blog, we explored how to approach this task in C, starting from the basic brute-force method to more efficient solutions using hashing or two-pointer techniques.
The brute-force method is simple to understand but not suitable for large arrays because of its time complexity. On the other hand, using a hash map or sorting the array and applying the two-pointer technique gives much better performance in terms of speed.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment