Answer:
The code example provided has relevant comments included.
Explanation:
// Implementation of the TestSolution class
import java.util.Arrays;
public class TestSolution
{
// Implementing the solution function
public static int solution(int[] arr)
{
// Define local variables
int i, j, count = 0;
boolean shines;
// Using nested loops to tally the instances where every lit 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 count of instances where every lit bulb shines
return count;
} // End of solution function
// Main function begins
public static void main(String[] args)
{
// Arrays A, B, and C are created
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 in the range of [1..100000]
int N = 1 + (int)(Math.random() * 100000);
// Create an array D of size N
int[] D = new int[N];
// Fill array D with distinct random numbers in the [1..N] range
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++;
}
}
// Display elements and moments of 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));
// Output the size and moments of array D
System.out.println("Size(N) of Array D: " + N + " and Moments: " + solution(D));
} // End of main function
} // End of TestSolution class