Last active
March 7, 2019 09:43
-
-
Save devarajchidambaram/da1f98aaaee3f3a736afeba16040b42f to your computer and use it in GitHub Desktop.
problem solving
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Three Sum | |
* | |
* Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique | |
* triplets in the array which gives the sum of zero. | |
* | |
* Note: | |
* | |
* The solution set must not contain duplicate triplets. | |
* | |
* Example: | |
* | |
* Given array nums = [-1, 0, 1, 2, -1, -4], | |
* | |
* A solution set is: | |
* [ | |
* [-1, 0, 1], | |
* [-1, -1, 2] | |
* ] | |
*/ | |
/** | |
* triplets in the array which gives the sum of zero | |
* @param {number[]} nums | |
* @returns {[[number]]} array of arrays | |
*/ | |
function threeSum(nums) { | |
//Corner case | |
if(!nums || nums.length<3) return ret; | |
var returned = [] | |
var n = nums.length | |
nums = nums.sort(function (a, b) { | |
return a - b | |
}) | |
for (let i = 0; i < n - 2; i++) { | |
//Check if value is sorted | |
if(i !== 0 && nums[i] === nums[i-1]){ | |
continue | |
} | |
let j = i + 1 //next pos | |
let k = n - 1 //last pos | |
while (j < k) { | |
let sum = nums[i] + nums[j] + nums[k] | |
if (sum === 0) { | |
returned.push([nums[i], nums[j], nums[k]]) | |
j++ | |
k-- | |
//skip duplicates from j iterator | |
while (j < k && nums[j] == nums[j - 1]) | |
j++; | |
//skip duplicates from k iterator | |
while (j < k && nums[k] == nums[k + 1]) | |
k--; | |
} else if (sum < 0) { //remember array is sorted if sum is less then increment j else decrement j | |
j++ | |
} else { | |
k-- | |
} | |
} | |
} | |
return returned | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Problem : Two Sum | |
* | |
* Given an array of integers, return indices of the two numbers such that they add up to a specific target. | |
* | |
* You may assume that each input would have exactly one solution, and you may not use the same element twice. | |
* | |
* Example: | |
* | |
* Given nums = [2, 7, 11, 15], target = 9, | |
* | |
* Because nums[0] + nums[1] = 2 + 7 = 9, | |
* return [0, 1]. | |
*/ | |
/** | |
* | |
* @param {number[]} nums | |
* @param {number} target | |
* @return {number[]} indices | |
*/ | |
function twoSum(nums , target){ | |
//To avoid inner forloop we can use map to store the values | |
const map = {} | |
for (let i = 0; i < nums.length; i++) { | |
var diff = target-nums[i] | |
//Check difference is in the | |
if(diff in map){ | |
return [map[diff] , i ] | |
} | |
map[nums[i]] = i | |
} | |
} | |
var nums = [2, 7, 11, 15, 10] | |
var target = 17 | |
console.log(twoSum(nums , target)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment