JAVA Loops for a series
Here's the solution I came up with. It would need some alteration for the output, that's easy enough.
public static List<Integer> generateSequence(int a, int b, int n) {
List<Integer> list = new ArrayList<Integer>();
int currentValue = 0;
for (int i = 0; i < n; i++) {
int value = (int) Math.pow(2, i) * b;
if (i == 0) {
value += a;
}
currentValue += value;
list.add(currentValue);
}
return list;
}
public static void main(String[] args) throws IOException {
List<List<Integer>> lists = new ArrayList<>();
try (Scanner scanner = new Scanner(System.in)) {
int q = scanner.nextInt();
for (int i = 0; i < q; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int n = scanner.nextInt();
lists.add(generateSequence(a, b, n));
}
}
for (List<Integer> list : lists) {
System.out.println(list);
}
}
The code I wrote for this hackerrank test works but is too resource and now I am out of ideas. What am I doing wrong?
The problem with your code is that it uses too many loops.
- Both
Array.drop(Int)
andArray.dropLast(Int)
use a loop to construct aList
, which they return. Iterable<T>.count((T) -> Boolean)
uses a loop to count the number of times the condition specified is true.
To optimize your code, drop these extension functions that use loop.
fun minimumBribes(queue: Array<Int>): Unit {
var bribeCount = 0
for ((index, initialPosition) in queue.withIndex()) {
val currentPosition = index + 1
if ((initialPosition - currentPosition) > 2) {
println("Too chaotic")
return
}
val startingCheckIndex = max(0, initialPosition - 2)
for (checkIndex in startingCheckIndex until index) {
val hasBribed = queue[checkIndex] > initialPosition
if (hasBribed) bribeCount++
}
}
println(bribeCount)
}
Clarification:
For each number in the queue:
1: We check how far a person has moved up in the queue i.e. initialPosition - currentPosition
. If he/she has moved up more than 2 positions then we print "Too chaotic" and return control because it is not possible for the person to bribe more than 2 people ahead of him/her.
For example, if the person's sticker says 6 and his current position is 3 then the person has moved up 3 positions, which is not possible.
2: You mentioned in the question, I count the number of elements that have a bigger value from that element and are standing before it in the queue
.
This is correct as we are counting how many bribes a person has taken but we cannot check right up to the first position. As a person that bribes another person can only move one position ahead of him, we will only check up to 1 position ahead of the initial position of the person. I have called the index for this position startingCheckIndex
in my code.
For example: If the person's sticker says 5 and the current position is 10. then we will check only from position 4 up to 10.
Note that I have used max(0, initialPosition - 2)
.-2
because the array index starts from 0 and initialPosition starts from 1.max(0,..)
because for initialPosition 1, 1 - 2 = -1
so we need to maintain minimum value of 0.
Java Staircase is printing in wrong direction (hackerrank)
You simply have to make two changes:
First you have to add a loop that will print spaces. If you start the loop at n
and loop until more than or equal to i
, while subtracting, this will print the correct amount of spaces.
Second, you need to have the newline at the end of the loop:
static void staircase(int n) {
int counter=0;
for(int i=0; i<n;i++)
{
counter++;
for(int k=n; k>= i;k--)
{
System.out.print(" ");
}
for(int j=0; j<=counter-1;j++)
{
System.out.print("#");
}
System.out.print("\n");
}
}
Output:
#
##
###
####
#####
######
Also just a note on your code. It is bad practice to close System.in
. The general rule is that if you did not open a resource, you should not close it.
Related Topics
Printing a Statement Only Once in a While Loop
How to Create a Spring JPA Repository Findby Query Using a Property That Contains a Keyword
How to Trim White Space from All Elements in Array
Regular Expression for Uppercase Letters in a String
@Responsebody , Responseentity Spring Return Object as Json
How to Find Distinct Rows With Field in List Using JPA and Spring
How to Click an Li Inside a Ul Using Selenium Webdriver
How to Skip the First Line of a CSV in Java
A Method to Check If a Collection or Map Is Empty or Null
How to Disable Log4J Logging from Java Code
How to Connect Java Backend With Html/Css Frontend
Spring @Value Annotation Always Evaluating as Null
How to Get the First Date and Last Date of the Previous Month (Java)
Save Child Objects Automatically Using JPA Hibernate
Write to Text File from Multiple Threads
Generate Bank Account Number With Random
Is There an Invisible Character That Is Not Regarded as Whitespace
How to Modify a Field in Each Element of List in Java 8 Using Stream