Question
What does Alg1 and Alg2 compute respectively? Also give a more efficient algorithm that solves the same problem as Alg1. ------------------------------------------------------------------------------------ public static void main(String[]
What does Alg1 and Alg2 compute respectively? Also give a more efficient algorithm that solves the same problem as Alg1.
------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException { int n = 8; int[] a = {3,2,2,2,4,5,5,3}; System.out.println(Alg1(a, n)); }
private static int Alg1(int[] a, int n) {
int l = 0;
for (int i = 0; i < a.length; i++) { for (int j = i; j < a.length; j++) { if (Alg2(a, i, j) && j - i + 1 > l) { l = j - i + 1;
}
}
}
return l;
}
private static boolean Alg2(int[] a, int i, int j) {
if (i == j) { return true;
}
for (int k = i; k < j - 1; k++) {
if (a[k] != a[k + 1]) {
return false;
}
}
return true;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started