answer.
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
SashulF
1 month 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:
ivann1987 [985]1 month ago
3 0

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

You might be interested in
Suppose we are sorting an array of eight integers using quicksort, and we have just finished the first partitioning with the arr
Rzqust [982]

Answer:

c. The pivot could either be 7 or 9.

Explanation:

When sorting an array of eight integers through quicksort, the first partitioning indicates that either 7 or 9 may serve as the pivot. Observing the array, it is specifically 7 and 9 that occupy their correct positions within the ordered array. All integers preceding 7 and 9 are lesser, and all numbers following them are greater. Therefore, it suggests that the pivot is located between 7 and 9.

6 0
24 days ago
Choose the person responsible for each innovation.
zubka84 [1016]

Response:

John Blankenbaker

Reasoning:

5 0
1 month ago
During a move, employee workstations were disconnected from the network and reconnected in new offices. However, after the move
Natasha_Volkova [964]
Ensure the cables are operational and correctly connected. Given that everything functioned well prior to the relocation, but post-move some workstations are unable to receive a valid IP, the first action should be to confirm that the cables are securely connected. If they are, also test the cables for functionality. If issues persist after this, then additional troubleshooting would be necessary.
5 0
22 hours ago
Brendan is examining a report using the Design view. Which section in the Design view is going to appear only once on the first
maria [999]

The Report Header Section is the part in Design view that appears only on the first page and may include logos and title details.

Clarification:

In Design view, the Create tab allows for report creation with just a click. The Report Wizard can also assist in generating reports with various options.

After initiating a report, you will realize that it contains multiple sections, requiring decisions on the data to be included in each section.

The Report Header Section shows at the top of the first page and only once during the report generation. It features elements like the logo, report title, and current date.

3 0
1 month ago
The level of the computer hierarchy where an operating system functions is the ______. digital logic level system software level
Natasha_Volkova [964]
I hold the top position in the hierarchy!
6 0
25 days ago
Read 2 more answers
Other questions:
  • Write a loop that reads strings from standard input, where the string is either duck or goose. the loop terminates when goose is
    7·1 answer
  • Users report that the network access is slow. After questioning the employees, the network administrator learned that one employ
    7·1 answer
  • U.S. industries like steel, computers, and energy need to be protected from foreign competition to ensure which of the following
    6·2 answers
  • An Internet user has a need to send private data to another user. Which of the following provides the most security when transmi
    14·2 answers
  • Consider a load-balancing algorithm that ensures that each queue has approximately the same number of threads, independent of pr
    14·1 answer
  • The table is an excerpt from an interviewer’s notes about three applicants applying to the IT department.                
    15·1 answer
  • If a database named Contacts has a table named tblEmployees and another database named Orders links to that tblEmployees table,
    8·1 answer
  • An administrator has been working within an organization for over 10 years. He has moved between different IT divisions within t
    8·1 answer
  • PLEASE HELP PROGRAMMING WILL GIVE BRAINLIEST
    6·1 answer
  • Write lines of verse that rhyme to remember the following information:
    16·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!