Project 1
1. public static int findEquilibriumPoint(int[] A) {
int n = A.length;
int sum = 0;
int leftSum = 0;
// Calculate the total sum of the array
for (int i = 0; i < n; i++) { sum += A[i];
}
// Check each element in the array for (int i = 0; i < n; i++) {
// Calculate the sum of elements before the current element leftSum += A[i];
// Check if the sum of elements before the current element is equal to the sum of elements after it
if (leftSum == sum - leftSum) {
return i+1;
// Return the position of the equilibrium point
}
}
// If no equilibrium point was found,
return -1 return -1;
}
2. public class ReverseWords {
public static String reverseWords(String s) {
// split the string into an array of words
String[] words = s.split("\\.");
// reverse the array of words for (int i = 0; i < words.length / 2; i++) {
String temp = words[i];
words[i] = words[words.length - i - 1];
words[words.length - i - 1] = temp;
}
// join the reversed array of words into a single string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < words.length; i++) {
sb.append(words[i]);
if (i < words.length - 1) { sb.append(".");
}
}
return sb.toString();
}
public static void main(String[] args) {
String s = "i.like.this.program.very.much";
System.out.println(reverseWords(s))
// should print "much.very.program.this.like.i"
}
}
3. public class Main {
public static String removeDuplicates(String s) {
// Create a boolean array to store characters that have been encountered boolean[]
encountered = new boolean[256];
// Create a string builder to store the modified string StringBuilder
modifiedString = new StringBuilder();
// Iterate through the string
for (int i = 0; i < s.length(); i++) {
// Get the character at the current position
char c = s.charAt(i);
// If the character has not been encountered before, add it to the modified string
if (!encountered[c]) {
modifiedString.append(c);
encountered[c] = true;
}
}
// Return the modified string return modifiedString.toString();
}
public static void main(String[] args) {
String s1 = "skilllync";
String s2 = "skill lync course";
System.out.println(removeDuplicates(s1));
System.out.println(removeDuplicates(s2));
}
}
4.