Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions challenge1/Question1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.example.challenge1;

import java.util.HashMap;
import java.util.Map;

public class Question1 {

private static void findPairs(int[] testArray, int targetSum) {
// ASSUMPTIONS:
// 1. values in testArray range from 1 - (targetSum - 1)


// The first attempt I made involved nested loops.
// I suspected it would be faster to use a dictionary of some kind so I researched
// the question and found out
// 1. it is
// 2. the Java name for a dict is a map

HashMap<Integer, Integer> pairMap = new HashMap<>();

// variable used to handle values that are half of targetSum
int duplicate = 0;

for (int i : testArray) {
// count number of values that are exactly half of targetSum
if (i == targetSum - i) { duplicate++; }

// every possible pair is added to map
pairMap.put(i, targetSum - i);
}

for (int i = 0; i < targetSum; i++) {
// removes duplicates from map as well as pairs that don't exist
// ex: { 5 } will not print out (5, 1) since 1 is not in array

// half of targetSum is dealt with later
if (pairMap.containsValue(i) && pairMap.containsKey(i) && i != targetSum - i) {
pairMap.remove(i);
}
}

// remove half of targetSum from map if there isn't more than one in array
if (duplicate < 2) {
pairMap.remove(targetSum / 2);
}

for (Map.Entry<Integer, Integer> i : pairMap.entrySet()) {
System.out.println("(" + i.getKey() + ", " + i.getValue() + ")");

}

}

public static void main(String[] args) {
int[] testArray = {2, 4, 5, 1, 3, 5, 4};
int targetSum = 6;

findPairs(testArray, targetSum);
}
}