Find Pairs in Array with Given Sum in C++

Pairs in Array with given Sum

On this page, we will look into a coding question where we will learn how to Find Pairs in Array with 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.

Pairs in Array with given Sum in cpp

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

  1. Brute Force (Time complexity: O(n^2) )
  2. Using Sorting (Time complexity: O(n log n) )
Both methods have their own pros and cons. The brute force method can be slow for large arrays but it is very simple and easy to implement . The sorting method is faster for large arrays, but requires extra space for sorting and modifying the original array.
Find Pairs in Array with given Sum in C

Method 1 : Brute Force Method

  • In this approach, we check every pair of elements in the array to see if their sum is equal to the given target sum.
  • This method has a time complexity of O(n^2) as we have to compare each element with every other element

C++ code for brute force

Run
#include <iostream>
using namespace std;

void findPairs (int arr[], int n, int targetSum)
{
  for (int i = 0; i < n - 1; i++)
    {
      for (int j = i + 1; j < n; j++)
	{
	  if (arr[i] + arr[j] == targetSum)
	    {
	      cout << "Pair found at index " << i << " and " << j << endl;
	    }
	}
    }
}

int main ()
{
  int arr[] = { 5, 8, 1, 4, 6, 3, 2, 7 };
  int n = sizeof (arr) / sizeof (arr[0]);
  int targetSum = 10;
  findPairs (arr, n, targetSum);
  return 0;
}

Output

Pair found at index 1 and 6
Pair found at index 3 and 4
Pair found at index 5 and 7

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

Run
#include <iostream>
#include <algorithm>
using namespace std;

void findPairs (int arr[], int n, int targetSum)
{
  sort (arr, arr + n);

  int left = 0;
  int right = n - 1;

  while (left < right)
    {
      int currSum = arr[left] + arr[right];

      if (currSum == targetSum)
	{
	  cout << "Pair found: " << arr[left] << " and " << arr[right] <<
	    endl;
	  left++;
	  right--;
	}
      else if (currSum < targetSum)
	{
	  left++;
	}
      else
	{
	  right--;
	}
    }
}


int main ()
{
  int arr[] = { 5, 8, 1, 4, 6, 3, 2, 7 };
  int n = sizeof (arr) / sizeof (arr[0]);
  int targetSum = 10;
  findPairs (arr, n, targetSum);
  return 0;
}

Output

Pair found: 2 and 8
Pair found: 3 and 7
Pair found: 4 and 6

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java