Pair Sums

Feb 7, 202010 mins read

Straight to the Point !

Are you landed to this page and rushing for the immediate solution in javascript. here you go !!

  1. Step 1: goes_here
  2. Step 2: goes_here
  3. Step 3: goes_here
1Code Solution Goes here.

On High level note, It worth to read in detail for a better understanding. This is one of the Facebook interview question and Leet code problem as well - 1460. leetCodetitle

Table of Contents

Problem Statement

Given a list of n integers arr[0..(n-1)], determine the number of different pairs of elements within it which sum to k.

If an integer appears in the list multiple times, each copy is considered to be different; that is, two pairs are considered different if one pair includes at least one array index which the other doesn't, even if they include the same values.

1int numberOfWays(int[] arr, int k)

Constraints

  1. n is in the range [1, 100,000].
  2. Each value arr[i] is in the range [1, 1,000,000,000].
  3. k is in the range [1, 1,000,000,000].

Expected

  1. Return the number of different pairs of elements which sum to k.

Test Cases

  1. n = 5, k = 6 and arr = [1, 2, 3, 4, 3], so output = 2
  2. n = 5, k = 6 and arr = [1, 5, 3, 3, 3], so output = 4

Foot Note

  1. There's one valid pair 1+5, and three different valid pairs 3+3 (the 3rd and 4th elements, 3rd and 5th elements, and 4th and 5th elements).

Solution Intro

Lets see the list of approaches and their complexities.


ApproachTime ComplexitySpace Complexity
1Brute ForceO(n+mO(m+n)
2$Approach 1$O(n)O(n)
3$Approach 2$O(n)O(n)
4Time OptimizedO(n)O(n)
5Memory OptimizedO(n)O(n)

Solutions

With no further due, lets take a example of code solutions.

Brute Force

Description

1Code goes here...

$Approach-1$

Description

1Code goes here...

$Approach-2$

Description

1Code goes here...

$Time-Optimized$

Description

1Code goes here...
### $Space-Optimized$Description
1Code goes here...