# Solving the “Double Cola” Challenge Using Java

## The challenge

Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a “Double Cola” drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on.

For example, Penny drinks the third can of cola and the queue will look like this:

``````Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny
``````

Write a program that will return the name of the person who will drink the `n`-th cola.

### Input:

The input data consist of an array which contains at least 1 name, and single integer `n` which may go as high as the biggest number your language of choice supports (if there’s such limit, of course).

### Output / Examples:

Return the single line — the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1.

``````let names = &[Name::Sheldon, Name::Leonard, Name::Penny, Name::Rajesh, Name::Howard];
assert_eq!(who_is_next(names, 1), Name::Sheldon);
assert_eq!(who_is_next(names, 6), Name::Sheldon);
assert_eq!(who_is_next(names, 52), Name::Penny);
assert_eq!(who_is_next(names, 7230702951), Name::Leonard);
``````

Courtesy of CodeForces: https://codeforces.com/problemset/problem/82/A

## Test cases

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class ListTests {
@Test
public void test1() {
String[] names = new String[] { "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" };
int n = 1;
assertEquals("Sheldon", new Line().WhoIsNext(names, n));
}
@Test
public void test2() {
String[] names = new String[] { "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" };
int n = 6;
assertEquals("Sheldon", new Line().WhoIsNext(names, n));
}
}
``````

## The solution using Java

``````public class Line {
public static String WhoIsNext(String[] names, int n) {
n = n - 1;
int len = names.length;

while(n >= len) {
int div = n-len;
n = (int)Math.floor(div/2);
}
return names[n];
}
}
``````