Answer:
The code is given below with appropriate comments
Explanation:
// TestSolution class implementation
import java.util.Arrays;
public class TestSolution
{
// solution function implementation
public static int solution(int[] arr)
{
// declare the local variables
int i, j, count = 0;
boolean shines;
// use the nested loops to count the number of moments for which every turned on bulb shines
for (i = 0; i < arr.length; i++)
{
shines = true;
for (j = i + 1; j < arr.length && shines; j++)
{
if (arr[i] > arr[j])
shines = false;
}
if (shines)
count++;
}
// return the number of moments for which every turned on bulb shines
return count;
} // end of solution function
// start main function
public static void main(String[] args)
{
// create three arrays named A, B, and C
int[] A = {2, 1, 3, 5, 4};
int[] B = {2, 3, 4, 1, 5};
int[] C = {1, 3, 4, 2, 5};
// generate a random number N within the range range[1..100000]
int N = 1 + (int)(Math.random() * 100000);
// create an array named D of size N
int[] D = new int[N];
// fill the array D with the distinct random numbers within the range [1..N]
int i = 0;
while(i < N)
{
int num = 1 + (int)(Math.random() * N);
boolean found = false;
for(int j = 0; j < i && !found; j++)
{
if(D[j] == num)
found = true;
}
if(!found)
{
D[i] = num;
i++;
}
}
// print the elements and number of moments of the arrays A, B, and C
System.out.println("Array A: " + Arrays.toString(A) + " and Moments: " + solution(A));
System.out.println("Array B: " + Arrays.toString(B) + " and Moments: " + solution(B));
System.out.println("Array C: " + Arrays.toString(C) + " and Moments: " + solution(C));
// print the size and number of moments of the array D
System.out.println("Size(N) of Array D: " + N + " and Moments: " + solution(D));
} // end of main function
} // end of TestSolution class