Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Q3"]
path = Q3
url = https://github.com/qyyao/Q3
51 changes: 51 additions & 0 deletions Q1.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
33 changes: 33 additions & 0 deletions Q2.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions Q3
Submodule Q3 added at 03c113