Automata Question – 2

Q2. You are required to correct the syntax of the given code without changing its logic. You
can click on Compile & Run anytime to check the compilation/execution status of the program.
You can use System.out.printin to debug your code. The submitted code should be
logically/syntactically correct and pass all testcases. Do not write the main() function as it is not
required.
Code Approach: For this question, you will need to correct the given implementation. We do
not expect you to modify the approach or incorporate any additional library methods.
The method maxReplace(int arr[]) of class MaxArray is supposed to replace every element of
the input array arr with the maximum element of arr.
The method looks fine but gives a compilation error.
Your task is to fix the program so that it passes all test cases.
11 int max = arr[0];
12
12
14 if(len=0)
15
16 {
17
18
19 for (i=0; i<len; i++)
20
21 {
22
23if (max<arr[i])
24
25 {
26
27 max = arr[i];
28
29 }
30
31 }
32
33 }
34
35 for (i=0; i<len; i++)
36 arr[i]= max;
37 return arr;
38
39 }
40
Output: TestCase 1:
Status:
Correct
Expected:
[11, 11, 11, 11,11]
Returned:
[11, 11, 11, 11, 11]
TestCase 2:
Ststus:
Correct
Expected:
[63, 63, 63, 63, 63, 63, 63, 63, 63]
Returned:
[63, 63, 63, 63, 63, 63, 63, 63, 63]
This program was also checked on another testcases. 5 out of 6 passed.
11 int max = arr[0];
12
12
14 if(len=0)
15
16 {
17
18
19 for (i=0; i<len; i++)
20
21 {
22
23 if (max<arr[i])
24
25 {
26
27 max = arr[i];
28
29 }
30
31 }
32
33 }
34
35 for (i=0; i<len; i++)
36 arr[i]= max;
37 return arr;
38
39 }
40