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
What type of printer heats toner to adhere it to the paper? inkjet . impact. 3D. laser.
Luden [163]

Answer:

laser

Explanation:

These are well known printer types now.  Let's review how they work to determine in which does heat plays a role.

inkjet: inkjet printers are printer throwing ink at the paper in the form of little drops.  No heat involved here.

impact: That's the oldest technology, where a printing head is moving left and right to punch a printing ribbon and transfer ink onto the paper.  No heat involved.

3D: 3D printers don't usually use paper... as they use other materials to create a 3D representation of a model.  Heat is involved in the melting of the material before it's placed on the building model.  But no paper involved here.

laser: laser printers are working very much like a photocopier machine... memorizing the pattern to be printed, then transferring it onto paper... using heat.

8 0
3 years ago
What can be designed to create annoying glitches or destroy data
Yanka [14]
Viruses can cause this.
3 0
3 years ago
Aaron bought a photo-editing software package for personal use. He makes two copies of the software, in case the original softwa
Korvikt [17]

Aaron's action is considered legal, as you are allowed to make a backup copy of a legal copy of a software, but it can only be used in case the original software is destroyed or unusable.

5 0
3 years ago
A a a a I don't need help!?
Arlecino [84]

Answer:

ok

Explanation:

ok

6 0
2 years ago
Why is it important to install updates? Why should you use the Power button on the Start menu instead of the Power button on you
IrinaK [193]

Answer:

Software updates do a lot of things. Software updates offer plenty of benefits. ...

Updates help patch security flaws. Hackers love security flaws, also known as software vulnerabilities. ...

Software updates help protect your data. ...

It’s not all about you. ...

You deserve the latest and greatest

Explanation:

hope that worked

3 0
3 years ago
Other questions:
  • Wich of these is an example of magnetic storage
    11·1 answer
  • Which of the following is a key component to your individual internet safety? government regulations
    13·1 answer
  • 286795991 número pra convidar no tiktok​
    11·2 answers
  • To what are multiple servers arranged in racks related
    14·2 answers
  • All computer systems have
    14·2 answers
  • What code do I have to use in my php if i don’t want my $_SESSION[“variablex”] to destroy? But i already have a session_destroy(
    6·1 answer
  • What is a file type and why is it important? Give at least three examples of file
    7·1 answer
  • Which of the following statements are true about file naming conventions? Check all of the boxes that apply.
    7·2 answers
  • Provide the user with a menu for the following 10 algorithms. Then ask which algorithm the user decides she/he wants to use. Fin
    9·1 answer
  • Which of the expressions is false? when a = 10 and b = 4
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!