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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!