How Is This Wrong - Hackerrank Loop in Java

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.

  1. Both Array.drop(Int) and Array.dropLast(Int) use a loop to construct a List, which they return.
  2. 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



Leave a reply



Submit