Straight to the Point !
Are you landed to this page and rushing for the immediate solution in javascript. here you go !!
- Step 1: goes_here
- Step 2: goes_here
- 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
- Constraints
- Expected
- Test Cases
- Foot Note
- Solution Intro
- Solutions
- Other Possible Questions
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
- n is in the range [1, 100,000].
- Each value arr[i] is in the range [1, 1,000,000,000].
- k is in the range [1, 1,000,000,000].
Expected
- Return the number of different pairs of elements which sum to k.
Test Cases
- n = 5, k = 6 and arr = [1, 2, 3, 4, 3], so output = 2
- n = 5, k = 6 and arr = [1, 5, 3, 3, 3], so output = 4
Foot Note
- 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.
Approach | Time Complexity | Space Complexity | |
---|---|---|---|
1 | Brute Force | O(n+m | O(m+n) |
2 | $Approach 1$ | O(n) | O(n) |
3 | $Approach 2$ | O(n) | O(n) |
4 | Time Optimized | O(n) | O(n) |
5 | Memory Optimized | O(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...
1Code goes here...