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
Internally, computers are constructed from circuitry that consists of small on/off switches. What is the most basic circuitry-le
attashe74 [19]

Answer:

Machine Language.

Explanation:

The most basic language that is used by computers so that they can control the operation of the on/off switches  in the circuitry is Machine language.

Machine Language is a low level language is a collection of binary digits or bits that is understood by the computers.Computers are capable of understanding only machine language.

3 0
3 years ago
Suppose that Alice wants to send Bob a 50 kilobyte message over a 1 Gbps link. The total time required to transmit the message (
svlad2 [7]

Answer:

170 Mbps

Explanation:

Time taken to put data on to the link T1 = 50kB / 1 Gbps = 0.0004096

Time taken for transmission T2 = 2 milliseconds = 2 * 10-3

--> throughput = ( 1 / (1+T2/T1) ) * bandwidth of link

= ( 1 / (1 + 4.8828125) ) * 1 Gbps

= 0.16998672 * 1 Gbps

= 169.98672 Mbps

= 170 Mbps.

8 0
3 years ago
A Hierarchy Custom Setting stores a specific URL for each profile in Salesforce. Which statement can a developer use to retrieve
Mariana [72]

Answer:

The correct answer is option (B) "{!$Setup.Url_Settings__C.URL__c}".

Explanation:

In computer programing using Visualforce, "$Setup" refers to a global merge field that gives the user access to hierarchical custom settings. In this case, the developer needs to retrieve the correct URL to access the current user's profile and display this on a Visualforce Page. "$Setup" will give the developer access to the hierarchical custom settings in Visualforce, and the command "Url_Settings__C.URL__c" will provide the root access to the current user's profile.

4 0
3 years ago
Part 1 of 4 parts for this set of problems: Given an 4777 byte IP datagram (including IP header and IP data, no options) which i
LekaFEV [45]

Answer:

Fragment 1: size (1332), offset value (0), flag (1)

Fragment 2: size (1332), offset value (164), flag (1)

Fragment 3: size (1332), offset value (328), flag (1)

Fragment 4: size (781), offset value (492), flag (1)

Explanation:

The maximum = 1333 B

the datagram contains a header of 20 bytes and a payload of 8 bits( that is 1 byte)

The data allowed = 1333 - 20 - 1 = 1312 B

The segment also has a header of 20 bytes

the data size = 4777 -20 = 4757 B

Therefore, the total datagram = 4757 / 1312 = 4

6 0
2 years ago
Write a program using Python that prompts for an integer and prints the integer, but if something other than an integer is input
Tcecarenko [31]

Answer:

#program in Python

#read until user Enter an integer

while True:

   #try block to check integer

   try:

       #read input from user

       inp = int(input("Enter an integer: "))

       #print input

       print("The integer is: ",inp)

       break

   #if input is not integer

   except ValueError:

       #print message

       print("Wrong: try again.")

Explanation:

In try block, read input from user.If the input is not integer the print a message  in except block.Read the input until user enter an integer. When user enter an  integer then print the integer and break the loop.

Output:

Enter an integer: acs                                                                                                      

Wrong: try again.                                                                                                          

Enter an integer: 4a                                                                                                      

Wrong: try again.                                                                                                          

Enter an integer: 2.2                                                                                                      

Wrong: try again.                                                                                                          

Enter an integer: 12                                                                                                      

The integer is:  12  

7 0
3 years ago
Read 2 more answers
Other questions:
  • The 2 main types of copyright relevant to the recording industry?
    5·2 answers
  • Which of these sites would need additional investigation to check for reliability?
    8·2 answers
  • What are the features of the Outline view in Word? Select three options.
    12·2 answers
  • Dang was accepted to a biology program with a rigorous schedule and a high tuition, but good professors. What would be a benefit
    10·2 answers
  • A production house needs an operating system that captures saves and generates information within a specific time. Which type of
    11·1 answer
  • Ok.,so i have a sopitify account and by accident i pressed the downlaod on button and it says start you free trial i pressed tha
    11·2 answers
  • Can someone please find the IP address of a 3630 hp printer?
    8·1 answer
  • Pls help I will give lots of points
    9·1 answer
  • Which symbol is at the beginning and end of a multiline comment block? ### &amp;&amp;&amp; %%% """
    14·1 answer
  • Why must web designers select a common font?​
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!