Answers for "2 sum problem"

1

two sum

class Solution {
    public int[] twoSum(int[] nums, int target) {
      int[] arr = new int[2];
        
        HashSet<Integer> hs = new HashSet<>();
        
        for(int i=0; i<nums.length; i++){
            if(hs.contains(target-nums[i])){
                arr[1] = i;
                break;
            }
            hs.add(nums[i]);
        }
        
        for(int i=0; i<nums.length; i++){
            if(nums[i] == target - nums[arr[1]] && arr[1] != i)
                arr[0] = i;
        }
        return arr;
    }
}
Posted by: Guest on September-02-2021
-1

Sum of all the multiples of 3 or 5

const findSum = n => {
  let countArr = []
  
  for(let i = 0; i <= n; i++) if(i % 3 === 0 || i % 5 === 0) countArr.push(i) 
  return countArr.reduce((acc , curr) => acc + curr)
}
console.log(findSum(5))
Posted by: Guest on June-18-2020

Browse Popular Code Answers by Language