Ask Question
10 September, 02:24

There are N bulbs numbered from 1 to N, arranged in a row. The first bulb is plugged into the power socket and each successive bulb is connected to the previous one. (Second bulb to first, third to second) Initially, all the bulbs are turned off. A moment K (for K from 0 to N-1), the A[K]-th bulb is turned on. A bulbs shines if it is one and all the previous bulbs are turned on too. Write a function in java that given an Array A of N different integers from 1 to N, returns the number of moments for which every turned on bulb shines.

+4
Answers (1)
  1. 10 September, 05:03
    0
    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
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “There are N bulbs numbered from 1 to N, arranged in a row. The first bulb is plugged into the power socket and each successive bulb is ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers