System.out.println(s1.indexOf('a'));
System.out.println(s1.equals(s2));
int sum = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j+=2) sum += (i+j);
int sum = 0; for (int i = 1; i <= 50; i+=2) for (int j = 1; j <= n; j+=3) sum += (i+j);
int sum = 0; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j*=2) sum += (i+j);
int sum1 = 0; for (int i = 1; i <= m*m; i++) sum1 += i; int sum2 = 0; for (int j = 1; j <= m; j++) sum2 += j;
public static boolean adjacentDuplicates(int[] a) { for (int i = 0; i < a.length-1; i++) if (a[i] == a[i+1]) return true; return false; }
public static boolean sumInArray(int[] a, int sum) { //finds sum of all pairs in the array for (int i = 1; i < a.length; i++) for (int j = 0; j < i; j++) if (a[i] + a[j] == sum) return true; return false; }
Let A be an array of integers (some integers can be negative). Assume that the array is of size n. The subarray A[i..j] is the part of the array that starts at index i and ends at index j, where 0 ≤ i ≤ j ≤ n-1. Let sij equal the sum of the integers in A[i..j].
We wish to solve the following problem:
Find the maximum value for sij over all subarrays in array A, where 0 ≤ i ≤ j ≤ n-1.
The three algorithms given below solve this problem. NOTE: If all of the values in the array are negative, then the maximum value for sij is 0 by default.
Example: If the array contains the values { -1, 12, -3, 14, -4, 3 }, then the maximum sum over all subarrays is 23 (for the subarray {12, -3, 14}). If the array contains the values { 2, -3, 5, -1, 0, 7}, then the maximum sum over all subarrays is 11 (for the subarray {5, -1, 0, 7}).
ALGORITHM 1
Start with a maximum sum of 0. Compute the sum of each 1-element subarray, then compute the sum of each 2-element subarray, then compute the sum of each 3-element subarray, etc. For each sum you compute, if it is larger than the maximum sum you've seen, then it becomes the maximum sum.
ALGORITHM 2
Start with a maximum sum of 0. Compute the sum of each subarray that starts at index 0, then compute the sum of each subarray that starts at index 1, then compute the sum of each subarray that starts at index 2, etc. For each sum you compute, if it is larger than the maximum sum you've seen, then it becomes the maximum sum.
EFFICIENCY NOTE: Once you compute the sum of the subarray from A[i] to A[j], the sum of the subarray from A[i] to A[j+1] is just the previous sum you computed plus A[j+1]. Don't add up all of the previous values all over again.
ALGORITHM 3
Start with a maximum sum of 0. Compute the sum of all subarrays starting from index 0. Use the efficiency note from Algorithm 2. If the sum you compute is negative, start computing the sum of all subarrays starting from the next index in the array. Repeat this special rule if necessary. For each sum you compute, if it is larger than the maximum sum you've seen, then it becomes the maximum sum.
For example, as you compute the sum of all subarrays starting from index 0, if you find the sum of the integers in A[0..5] is negative, then compute the sum of all subarrays starting from index 6 next. If the sum of the integers from A[6..14] is negative, then compute the sum of all subarrays starting from index 15, Etc.
You are to write a Java program that determines the amount of work each of these algorithms does to compute its answer for arrays of various sizes. Using this data, you are to determine the runtime complexity of each algorithm.
NOTE CORRECTION IN RED: Write your main method for this class so it will run your algorithms for randomly generated arrays of size 5, 10, 15, ..., up to 50. Each array should have random values between -10 and 99, inclusive, and you can have duplicates. (To generate a random number between x and y, use the following Java expression: (int)(Math.random()*(y-x+1) + x).) For each array size, generate 2000 arrays (one at a time) and run the algorithms with each array, averaging the returned number of assignment statements executed for each algorithm separately.
Your program should output your results in tabular format, as shown below (XXXX indicates where your results will be, display integers only, no floating point values):
AVERAGE NUMBER OF ASSIGNMENT STATEMENTS EXECUTED OVER 2000 TRIALS: size Alg1 Alg2 Alg3 5 XXXX XXXX XXXX 10 XXXX XXXX XXXX 15 XXXX XXXX XXXX 20 XXXX XXXX XXXX 25 XXXX XXXX XXXX 30 XXXX XXXX XXXX 35 XXXX XXXX XXXX 40 XXXX XXXX XXXX 45 XXXX XXXX XXXX 50 XXXX XXXX XXXX
Based on the curves, determine the runtime complexity (in big O notation) of each algorithm. Enter your answers in a comment at the beginning of each helper method of the RuntimeAnalyzer class. Your answer should include the runtime complexity and a short explanation of why you believe your answer is correct based on the algorithm given.
Your submitted zip file should include your Java source code, and an Excel spreadsheet that includes the data you collected when you ran the program along with the associated graphs.
See the course website for instructions on how to hand in your program and the policy for late submissions.