Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions