1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
12345 [234]
3 years ago
6

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 b

ulb 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.
Computers and Technology
1 answer:
Ksivusya [100]3 years ago
3 0

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

You might be interested in
Insecurely attached infants who are left my their mothers in an unfamiliar setting often will
Temka [501]

Insecurely attached infants who are left my their mothers in an unfamiliar setting often will Hold fast in their mothers in their return.

A. Hold fast in their mothers in their return

<u>Explanation:</u>

It is in the conscience of the infants to have the company of their parents no matter the place or time and do not generally like to be left alone. Moreover, the questions says, insecurely attached infants which further add up to this behavior.

The infant would not explore the surroundings due to lack of confidence in an unfamiliar setting. They would rather be uncomfortable and most probably weep the time until their mother arrives and hold fast on to them on their return.  

8 0
3 years ago
The design strategy that starts with a global view of the entire problem and breaks the problem down into smaller, more manageab
In-s [12.5K]

Answer:

Top down design

Explanation:

Top-down design is an approach that is used to break down the problem into the smaller subpart so that it can be manageable into more clear form.

C programming is the example of a top-down approach while C++ is the example of the bottom-up approach.

The advantages of the top-down design approach are:

1) easy to manage

2) easy to find the error

3) easy to debug

4 0
2 years ago
What is the difference between a learner’s license and a driver’s license?
kirill115 [55]

privilege to operate a motor vehicle-drivers License

Able to get when 15. Valid for 1 yr. Allows you to drive with a parent in the car. $15. Bring social security and birth certificate to the DLD. Have for 6 months (and do 40 hrs) before getting license.-Learner Permit

4 0
2 years ago
During the design phase, development teams translate the requirements into?
Ludmilka [50]
A design for the software application
7 0
3 years ago
When you tried to login to server your trail may faild this failure event is recorded in<br>​
julia-pushkina [17]
Real time not online time...
8 0
2 years ago
Other questions:
  • Array A is not a heap. Clearly explain why does above tree not a heap? b) Using build heap procedure discussed in the class, con
    15·1 answer
  • When might an lcd monitor experience distorted geometry?
    7·1 answer
  • The computer mouse is used to
    11·1 answer
  • Some of the more important __________ include anti-virus (and anti-malware), host-based firewall, system hardening (removing unw
    11·1 answer
  • Creating a chart using a spreadsheet Chart Wizard involves four steps. Which is the last step?
    6·1 answer
  • Apps are designed by___.
    11·2 answers
  • Is Filmora 9 or Final Cut Pro Better for personal use?
    12·1 answer
  • Define management styles
    14·2 answers
  • write a recursive bool valued function containsvowel that accepts a string and returns true if the string contains a vowel
    6·1 answer
  • Data frames can be subset by a chosen value using ==.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!