.NET Developer - Coding Fountains Are Installed...

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

  Home Study tools

 My courses
 My books My folder Career Life 

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / 1. .net developer - coding fountains are installed at ev…

Question: 1. .NET Developer - Coding Fountains are installed at every po… Post a question
Answers from our experts for your tough
homework questions

Enter question

Continue to post
20 questions remaining

My Textbook Solutions

Steel Design Steel Design Econom

5th Edition 4th Edition 6th Editio

View all solutions


  Home Study tools
 My courses
 My books My folder Career Life 
  Home Study tools
 My courses
 My books My folder Career Life 
  Home Study tools
 My courses
 My books My folder Career Life 
  Home Study tools
 My courses
 My books My folder Career Life 

Show transcribed image text

View comments (1)




Expert Answer

Anonymous answered this


Was this answer helpful? 0 1
2,254 answers

Here is the answer,

Please do upvote the


solution.

ANS:

CODE:

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

import java.util.*;

import static java.util.stream.Collectors.*;

import java.util.stream.IntStream;

//Implementation of Fountain class

public class Fountain {

//Declare max as type of static int

public static int max = 100000;

 
//Implementation
Homeof fountainActivation
Study tools
function

 My courses
 My books My folder Career Life 
//with parameter as a[a[0],...a[n-1]]

public static int fountainActivation(List<Integer> a){

//Declare activate,coversWholeGarden as type of List<Boolean>

List<Boolean> activate,coversWholeGarden;

//assign ArrayListto coversWholeGarden as type of List<Boolean>

coversWholeGarden = new ArrayList<>();

//assign ArrayListto activate as type of List<Boolean>

activate = new ArrayList<>();

//Declare i as type of integer

int i = 0;

//Declare numberofFountains,minFountains,n as type of integer

int numberofFountains,minFountains ,n;

//assign max to minFountains

minFountains = max;

//assign whole length of garden to n

n = a.size();

//Iterate the loop

while(i<n)

//add false to coversWholeGarden

coversWholeGarden.add(false);

//add false to activate means

//fountains are switched off

activate.add(false);

//increment i

i++;

//Initialize i with value 0

i = 0;

//Iterate the loop

while( i<n)

numberofFountains = minimumFountains(a, coversWholeGarden, activate, i);

//check numberofFountains is less than minFountains

if(numberofFountains<minFountains)

//assign numberofFountains to minFountains

minFountains = numberofFountains;

//increment the i

i++;

//return the minFountains

return minFountains;

//Implementation of minimumFountains function

//that accepts parameters a,coversWholeGarden ,activate, index as type of List

private static int minimumFountains(List<Integer> a, List<Boolean> coversWholeGarden,

List<Boolean> activate, int arrayindex)

//set garden in activate state

activate.set(arrayindex, true);

//Declare coverStartGarden,coverEndGarden ,n,i as type of integer

int coverStartGarden ,coverEndGarden,n,i;

//Declare changedState as type of List<Integer>

List<Integer> changedState;

//Declare min,coversGardenCount,numberofFountains,numberOfPositions as type of integer

int min,coversGardenCount,numberofFountains,numberOfPositions;

//calculate the size of the array a and assign to n

n = a.size();

//calculation of coverStartGarden value

coverStartGarden = arrayindex - a.get(arrayindex);

//calculation of coverEndGarden value

coverEndGarden = arrayindex + a.get(arrayindex) + 1;

//check coverEndGarden is greater than n

if(coverEndGarden > n)

//assing n to coverEndGarden

coverEndGarden = n;

//check coverStartGarden is less than zero

if(coverStartGarden < 0)

//assing 0 to coverStartGarden

coverStartGarden = 0;

//Declare changedState as type of List<Integer>

changedState = new ArrayList<Integer>();

//assign coverStartGarden to i

i = coverStartGarden;

//Iterate the loop

while(i<coverEndGarden)

 
//check coversWholeGarden.get(i)
Home Study tools
value
 My courses
 My books My folder Career Life 
if(!coversWholeGarden.get(i))

//set coversWholeGardern as true

coversWholeGarden.set(i, true);

//add each position to changedState

changedState.add(i);

//increment the i

i++;

//initailize coversGardenCount with 0

coversGardenCount = 0;

//initilze i with 0

i = 0;

//Iterate the loop

while(i<n)

//check coversWholeGarden.get(i) value

if(coversWholeGarden.get(i))

//add 1 to coversGardenCount

coversGardenCount = coversGardenCount + 1;

//increment the i

i++;

//check whether coversGardenCount is equal to n

if(coversGardenCount == n)

//Declare numberOfPositions as integer

//and assign with 0

numberOfPositions = 0;

//assign 0 to i

i = 0;

//Iterate the loop

while(i<n)

//check activate.get(i) value

if(activate.get(i))

//increment the numberOfPositions

numberOfPositions++;

//increment i

i++;

//Iterate the loop

for(i= 0;i<changedState.size();i++)

//set coversWholeGardern as true

coversWholeGarden.set(changedState.get(i), false);

//set activate as false

activate.set(arrayindex, false);

//return the numberOfPositions

return numberOfPositions;

//assign max to min

min = max;

//initialize i with 0

i = 0;

//Iterate the loop

while(i<n)

//check activate.get(i) value

if(!activate.get(i))

//store the return value of minimumFountains numberofFountains

numberofFountains = minimumFountains(a, coversWholeGarden, activate, i);

//check numberofFountains is less than min

if(numberofFountains<min)

//assign numberofFountains to min

min = numberofFountains;

}
}

//increment i

i++;

//Iterate the loop

for(i= 0;i<changedState.size();i++)

//set coversWholeGardern as true

coversWholeGarden.set(changedState.get(i), false);

 //set activate as false


Study tools

Home  My courses
 My books My folder Career Life 
activate.set(arrayindex, false);

//return the min value

return min;

//Implementation of main function

public static void main(String[] args) throws IOException

//Declare inputstrObject as type of InputStreamReader

InputStreamReader inputstrObject = new InputStreamReader(System.in);

//Create an object for BufferedReader class

BufferedReader bufferedObject1 = new BufferedReader(inputstrObject);

//Declare numberOfPositions and output as type of integer

int numberOfPositions,output;

//Declare tempLine as type of String

String tempLine;

//get the resultant value after applied trim() on

//return value of bufferedObject1.readLine() function

tempLine = bufferedObject1.readLine().trim();

//get the converted value of in the form of integer and assign to numberOfPositions

numberOfPositions= Integer.parseInt(tempLine);

//Create an array a as type of List<Integer>

List<Integer> a = IntStream.range(0, numberOfPositions)

.mapToObj(i -> {

//handle the exceptions through try catch block

try

//Declare tempstras type of string and it is

//local to try block

String tempstr = bufferedObject1.readLine();

//Declare replacedline as type of string and it is

//local to try block

String replacedline = tempstr.replaceAll("\\s+$", "");

//return the replacedline

return replacedline;

//handle IOException

catch (IOException execp)

//throw runtime exception

throw new RuntimeException(execp);

}).map(String::trim).map(Integer::parseInt)

//call collect function

.collect(toList());

//Get the return value from fountainActivation function

output = Fountain.fountainActivation(a);

//Display statement

System.out.println(String.valueOf(output));

Input:

OUTPUT:

If you have any doubts then put them in the comments.


Also, don't forget to upvote
the solution.

Thank you

View comments (30)




Questions viewed by other students

Q: Hello, I'm having trouble with this problem. I would prefer the
solution to either be in java or javaScript. Thank you.
Another Test Case:
  Home Study tools
 My courses
 My books My folder Career Life 

A: See answer

Q:
1. JavaServer Faces - Java View Debug Terminal Run EXPLORER V PROJECTS wy. Hi! org... In this scenario, you have to
code the hello.xhtml and its bean class named "HelloBean.java". src m... In the hello.xhtml: HelloBean.java 5 hello.xhtml x
@test.py 1 2

A: See answer 100% (1 rating)

Show more 

COMPANY

LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK

CUSTOMER SERVICE

© 2003-2021 Chegg Inc. All rights reserved.

You might also like