diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..da47135 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Q3"] + path = Q3 + url = https://github.com/qyyao/Q3 diff --git a/Q1.java b/Q1.java new file mode 100644 index 0000000..b9bb780 --- /dev/null +++ b/Q1.java @@ -0,0 +1,51 @@ +import java.util.Arrays; + +public class Q1 { + + public static void findPairs(int[] testArray, int targetSum) { + + //sort the array + Arrays.sort(testArray); + + //initialize an index to keep track of placement + int i = 0; + int j; + + //find the length of the array + int length = testArray.length; + + //we only have to check numbers up to half of the target sum + while (testArray[i] <= targetSum / 2) { + //make sure we do not calculate duplicates by not testing cases where testArray[i] is identical to the one previous + + if (i > 0 ? testArray[i] != testArray[i - 1] : testArray[i] <= targetSum / 2) { + + for (j = i + 1; j < length; j++) { + + if (testArray[j] == targetSum - testArray[i]) { + System.out.println("(" + testArray[i] + "," + testArray[j] + ")"); + i++; + break; + } + + if (j==length-1){ //when you reach the end of the array without finding a pair, accumulate i + i++; + } + } + } + + else{ + i++; + } + } + } + + + public static void main(String[] args) { + int[] testArray = {2, 4, 3, 3, 3, 5, 5, 5, 5, 2, 6, 8, }; + int targetSum = 10; + + findPairs(testArray, targetSum); + } + +} \ No newline at end of file diff --git a/Q2.java b/Q2.java new file mode 100644 index 0000000..875a3ef --- /dev/null +++ b/Q2.java @@ -0,0 +1,33 @@ +public class Q2{ + + public static boolean isPalindrome(String testString) { + + + //find the length of the string + int length = testString.length(); + + //compare each letter to the same letter in the reverse position + for (int i = 0; i < length/2; i++){ + //subtract one more from the length as length is always one more than the last index + if (testString.charAt(i) != testString.charAt(length-i-1)){ + return false; + } + } + + return true; + } + + public static void main(String[] args) { + + boolean string1 = isPalindrome("remembertobuyeggs"); + boolean string2 = isPalindrome("radar"); + boolean string3 = isPalindrome("asdffdsa"); + boolean string4 = isPalindrome("thisisnotapalindrome"); + + + System.out.println(string1); + System.out.println(string2); + System.out.println(string3); + System.out.println(string4); + } +} \ No newline at end of file diff --git a/Q3 b/Q3 new file mode 160000 index 0000000..03c1131 --- /dev/null +++ b/Q3 @@ -0,0 +1 @@ +Subproject commit 03c1131caa4c9bbf0b67913fe45cc86aaff5294a