Skip to content

Instantly share code, notes, and snippets.

View haypho's full-sized avatar
💭
Probably thinking about JavaScript

Hayden Phothong haypho

💭
Probably thinking about JavaScript
View GitHub Profile
@haypho
haypho / Problem1.java
Created October 7, 2019 02:23
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
public boolean problem1(List<Integer> numberList, int k) {
Map<Integer, Integer> differenceToNumberMap = new HashMap<>();
for (Integer number : numberList) {
if (number != null) {
if (differenceToNumberMap.containsKey(number)) {
return true;
} else {
differenceToNumberMap.put(k - number, number);
}
}
@haypho
haypho / Problem2.java
Created October 7, 2019 03:04
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6]. Follo…
public int[] problem2WithDivision(int[] integers) {
int product = getProductOfAllNumbers(integers);
int[] newIntegers = new int[integers.length];
for (int i = 0; i < integers.length; i++) {
newIntegers[i] = product / integers[i];
}
return newIntegers;
}
private int getProductOfAllNumbers(int[] numbers) {