Ad

You can eliminate the need for the second fact function by providing a default for the accumulator.

Code
Diff
  • tailrec fun fact(n: Int, accumulator: Int = 1): Int = if (n == 0) accumulator else fact(n - 1, n * accumulator)
    
    • tailrec fun fact(n: Int, accumulator: Int): Int = if (n == 0) accumulator else fact(n - 1, n * accumulator)
    • fun fact(n: Int): Int = fact(n, 1)
    • tailrec fun fact(n: Int, accumulator: Int = 1): Int = if (n == 0) accumulator else fact(n - 1, n * accumulator)

Write a method that returns the largest integer in the list.
You can assume that the list has at least one element.

.... sorting the array introduced unnecessary time complexity.

Code
Diff
  • import java.util.Arrays;
    
    public class Kata {
      
      public static int findMax(int[] intArray) {
    
        return Arrays.stream(intArray).reduce(Math::max).orElseThrow();
      }
      
    }
    
    • import java.util.*;
    • import java.util.Arrays;
    • public class Kata {
    • public static int findMax(int[] my_array) {
    • // Write a method that returns the largest integer in the list.
    • // You can assume that the list has at least one element.
    • Arrays.sort(my_array);
    • return (my_array[my_array.length-1]);
    • }
    • }
    • public static int findMax(int[] intArray) {
    • return Arrays.stream(intArray).reduce(Math::max).orElseThrow();
    • }
    • }

I was going to use an IntStream....

IntStream.range(0,numOfPeople).forEach(i -> urinals[i*(urinals.length-1)/(numOfPeople-1)] = true);

.... but decided for something this simple, maybe doing it without importing anything would be better.

Code
Diff
  • public class Kata {
      // Implement the urinal problem, described as "given x number of people walking 
      // into a bathroom that contains y number of urinals, describe the optimate
      // placement of people so that none is standing next to the other"
      // In this case, the urinals are represented by a boolean array with 
      // true representing a person at a urinal, and false meaning the urinal is empty
      public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
        for (int i = 0; i < numOfPeople; i++) {
          urinals[i * (urinals.length - 1) / (numOfPeople - 1)] = true;
        }
        return urinals;
      }
    }
    • public class Kata {
    • // Implement the urinal problem, described as "given x number of people walking
    • // into a bathroom that contains y number of urinals, describe the optimate
    • // placement of people so that none is standing next to the other"
    • // In this case, the urinals are represented by a boolean array with
    • // true representing a person at a urinal, and false meaning the urinal is empty
    • public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
    • // Implement the urinal problem, described as "given x number of people walking
    • // into a bathroom that contains y number of urinals, describe the optimate
    • // placement of people so that none is standing next to the other"
    • // In this case, the urinals are represented by a boolean array with
    • // true representing a person at a urinal, and false meaning the urinal is empty
    • int pointer = 0;
    • for (int i = numOfPeople; i > 0; i--) {
    • if (i == 1) { urinals[urinals.length-1] = true; }
    • else {
    • urinals[pointer] = true;
    • pointer += 1 + (urinals.length - pointer) / i;
    • }
    • for (int i = 0; i < numOfPeople; i++) {
    • urinals[i * (urinals.length - 1) / (numOfPeople - 1)] = true;
    • }
    • return urinals;
    • }
    • }

Not sure what this space is for (first time doing this), so I'll explain that the array is a bit pattern. Specifically, it is a pattern of the least significant 0 bit in the sequence. You can get the least significant 1 bit by doing N & -N. So I inverted N by using N ^ -1, resulting in (N^-1)&-(N^-1).

Code
Diff
  • import java.util.stream.IntStream;
    
    public class PowerfulArrays {
        
      public static int[] powerfulArray(int n) {
        return IntStream.range(0, (1<<n)-1).map(i -> (i ^ -1) & -(i ^ -1)).toArray();
      }
    }
    • import java.util.Arrays;
    • import java.util.stream.IntStream;
    • public class PowerfulArrays {
    • public static int[] powerfulArray(int n) {
    • if (n == 0) {
    • return new int[0];
    • } else {
    • // if n > 0
    • int[] subArray = powerfulArray(n-1); // recursively get subArray for n-1
    • int k = (int) Math.pow(2, n);
    • int[] result = new int[k - 1]; // create new int array of size k-1
    • System.arraycopy(subArray, 0, result, 0, subArray.length); // copy subArray on left side
    • result[subArray.length] = k / 2; // put k / 2 in middle
    • System.arraycopy(subArray, 0, result, subArray.length + 1, subArray.length); // copy subArray on right side
    • return result;
    • }
    • }
    • public static int[] powerfulArray(int n) {
    • return IntStream.range(0, (1<<n)-1).map(i -> (i ^ -1) & -(i ^ -1)).toArray();
    • }
    • }