I guess this can be treated as "without sorting" :)

Code
Diff
  • import static java.util.stream.IntStream.of;
    
    interface HighLow {
      static int[] printLargestAndSmallest(int[] nums) {
        var stats = of(nums).summaryStatistics();
        return new int[]{stats.getMin(), stats.getMax()};
      }
    }
    
  • 1
    public class HighLow{
    
    2
      public static int[] printLargestAndSmallest(int[] nums){
    
    3
    		int low[] = nums;
    
    4
    		int high[] = nums;
    
    5
    		int lowest = nums[0];
    
    6
    		int highest = nums[0];
    
    7
    		int count = 0;
    
    8
    		
    
    9
    		for(int i = 0; i < nums.length;i+=2){
    
    10
    			int num1 = nums[i];
    
    11
    			int num2 = nums[i+1];
    
    12
    			
    
    13
    			if(num1<num2){
    
    14
    				low[count] = num1;
    
    15
    				high[count] = num2;
    
    16
    				count++;
    
    17
    			}else{
    
    18
    				low[count] = num2;
    
    19
    				high[count] = num1;
    
    20
    				count++;
    
    21
    			}
    
    22
    			
    
    23
    			lowest = low[0];
    
    24
    			highest = high[0];
    
    25
    			
    
    26
    			for(int j = 1; j < low.length; j++){
    
    27
    				if(low[j] < lowest){
    
    28
    					lowest = low[j];			
    
    29
    				}
    
    30
    			}
    
    31
    			
    
    32
    			for(int j = 1; j < high.length; j++){
    
    33
    				if(high[j] > highest){
    
    34
    					highest = high[j];
    
    35
    				}
    
    36
    			}
    
    1+
    import static java.util.stream.IntStream.of;
    
    3737
    38
    		}
    
    39
        int[] finalHighLow = new int[2];
    
    40
        finalHighLow[0] = lowest;
    
    41
        finalHighLow[1] = highest;
    
    42
        return finalHighLow;
    
    43
    	}
    
    3+
    interface HighLow {
    
    4+
      static int[] printLargestAndSmallest(int[] nums) {
    
    5+
        var stats = of(nums).summaryStatistics();
    
    6+
        return new int[]{stats.getMin(), stats.getMax()};
    
    7+
      }
    
    4444
    }
    

A code-golfed variation of @rowcased authored solution.

Code
Diff
  • interface Calculator {
      static int calculate(int a, int b, char ops) {
        return new int[]{a * b, a + b, 0, a - b}[ops - 42];
      }
    }
    
  • 11
    interface Calculator {
    
    22
      static int calculate(int a, int b, char ops) {
    
    3
        int[] calcs = {a * b, a + b, 0, a - b, 0, a / b};
    
    4
        return calcs[ops - 42];
    
    3+
        return new int[]{a * b, a + b, 0, a - b}[ops - 42];
    
    55
      }
    
    66
    }
    

Create a simple calculator that given two numbers and an operator returns the value of the arithmetic operation. Here are the operations to support:

  • + - addition
  • - - subtraction
  • * - multiplication

Example:

Calculator.calculate(2, 2, '+') // => 4
Calculator.calculate(5, 5, '-') // => 0
Calculator.calculate(1, 1, '*') // => 1
Code
Diff
  • import javax.script.ScriptEngineManager;
    
    interface Calculator {
      static int calculate(int a, int b, char operation) {
        var engine = new ScriptEngineManager().getEngineByName("JavaScript");
        try {
          return Integer.parseInt(String.valueOf(engine.eval(a + " " + operation + " " + b)));
        } catch (Exception cause) {
          return 0;
        }
      }
    }
    
  • 1
    public class calculator {
    
    2
      public static int calculate(int n, int m, char symbol) {
    
    3
        // do your best :)
    
    4
        int result = 0;
    
    5
        switch(symbol) {
    
    6
            case '+':
    
    7
          result = n + m;
    
    8
          break;
    
    9
            case '-':
    
    10
          result = n - m;
    
    11
          break;
    
    12
            case '*':
    
    13
          result = n * m;
    
    14
          break;
    
    1+
    import javax.script.ScriptEngineManager;
    
    2+
    3+
    interface Calculator {
    
    4+
      static int calculate(int a, int b, char operation) {
    
    5+
        var engine = new ScriptEngineManager().getEngineByName("JavaScript");
    
    6+
        try {
    
    7+
          return Integer.parseInt(String.valueOf(engine.eval(a + " " + operation + " " + b)));
    
    8+
        } catch (Exception cause) {
    
    9+
          return 0;
    
    1515
        }
    
    16
      return result;
    
    1717
      }
    
    1818
    }
    
Fundamentals
Numbers
Basic Language Features
Arrays

Given an array of integers and a number. The task is to find the maximum product that can be formed by taking any array element multiplied by the number. If array is empty return zero.

Examples:

solve( [1, 2, 3], 3 ) = 9. The max product is given by 3 * 3
solve( [-4, 70], -5 ) = 20, given by (-4) * (-5)
solve( [-9, 9], 0 ) = 0, given by 9 * 0 OR (-9) * 0

More examples in test cases.

Code
Diff
  • import static java.util.stream.IntStream.of;
    
    interface MaxProduct {
      static int solve(int[] arr, int num) {
        return of(arr).map(e -> e * num).max().orElse(0);
      }
    }
  • 1
    class Kata1 {
    
    2
      public static int maxPairProduct(int[] arr, int num) {
    
    3
        //Never Gonna Give You Up, Never Gonna Let You Down, Never Gonna Run Around And Desert You!
    
    4
        
    
    1+
    import static java.util.stream.IntStream.of;
    
    2+
    3+
    interface MaxProduct {
    
    4+
      static int solve(int[] arr, int num) {
    
    5+
        return of(arr).map(e -> e * num).max().orElse(0);
    
    55
      }
    
    66
    }
    

Less accurate, but still acceptable :)

Code
Diff
  • interface NthFib {
      static int fib(int n) {
        return (int) Math.round(Math.pow(1.61803, n) / 2.236);
      }
    }
  • 1
    public class NthFib {
    
    2
      public static final double phi = (1 + Math.sqrt(5))/2.0;
    
    3
      
    
    4
      public static int fib(int n) {
    
    5
        return (int)Math.round(Math.pow(phi, n)/Math.sqrt(5));
    
    1+
    interface NthFib {
    
    2+
      static int fib(int n) {
    
    3+
        return (int) Math.round(Math.pow(1.61803, n) / 2.236);
    
    66
      }
    
    77
    }
    
Code
Diff
  • class Loan {
      private final double monthlyPayment;
    
      Loan(int amount, int termYears, int termMonths, double annualInterest) {
        monthlyPayment = amount * (annualInterest /= 1200) / (1 - Math.pow(++annualInterest, -12 * termYears - termMonths));
      }
    
      double getMonthlyPayment() {
        return monthlyPayment;
      }
    }
  • 1
    public class Loan {
    
    2
      private int amount;
    
    3
      private int termYears;
    
    4
      private int termMonths;
    
    5
      private double annualInterest;
    
    6
      
    
    7
      public Loan(int amount, int termYears, int termMonths, double annualInterest){
    
    8
        this.amount = amount;
    
    9
        this.termYears = termYears;
    
    10
        this.termMonths = termMonths;
    
    11
        this.annualInterest = annualInterest;
    
    1+
    class Loan {
    
    2+
      private final double monthlyPayment;
    
    3+
    4+
      Loan(int amount, int termYears, int termMonths, double annualInterest) {
    
    5+
        monthlyPayment = amount * (annualInterest /= 1200) / (1 - Math.pow(++annualInterest, -12 * termYears - termMonths));
    
    1212
      }
    
    13
      
    
    14
      public double getMonthlyPayment(){
    
    15
        return amount*this.getMonthlyRate()/
    
    16
        (1-Math.pow(1+getMonthlyRate(), -getTermInMonths()));
    
    17
      }
    
    18
      
    
    19
      public double getMonthlyRate(){
    
    20
        return this.annualInterest/1200.0;
    
    21
      }
    
    22
      
    
    23
      public int getTermInMonths(){
    
    24
        return termYears*12+termMonths;
    
    25
      }
    
    26
      
    
    27
      public double riskFactor(int anualIncome){
    
    28
        return 0;
    
    7+
    8+
      double getMonthlyPayment() {
    
    9+
        return monthlyPayment;
    
    2929
      }
    
    3030
    }
    
Code
Diff
  • package runtime;
    
    public interface Solution {
      static long sum(int... nums) {
        return nums.length * (nums.length + 1) / 2;
      }
    }
  • 11
    package runtime;
    
    22
    3
    public class Solution {
    
    4
      public static long sum(int... nums) {
    
    5
        long sum = 0;
    
    6
        for (long l : nums) {
    
    7
          sum += l;
    
    8
        }
    
    9
        return sum;
    
    3+
    public interface Solution {
    
    4+
      static long sum(int... nums) {
    
    5+
        return nums.length * (nums.length + 1) / 2;
    
    1010
      }
    
    1111
    }
    
Code
Diff
  • class s{}
  • 1
    public class s {
    
    2
    }
    
    1+
    class s{}
    

Nothing special, just a bit more compact version using Java stream API

Code
Diff
  • import static java.util.stream.Collectors.joining;
    
    class HeLlOwOrLddddd {
      static String convert(String input) {
        boolean[] up = {true};
        return input.chars().filter(Character::isLetter).mapToObj(c -> "" + (char) ((up[0] = !up[0]) ? Character.toLowerCase(c) : Character.toUpperCase(c))).collect(joining());
      }
    }
    
  • 1
    public class HeLlOwOrLddddd {
    
    2
      public static String convert(String s) {
    
    3
        StringBuilder sb = new StringBuilder();
    
    4
        boolean f = false;
    
    5
        for (char x : s.toCharArray())
    
    6
          if (Character.isLetter(x))
    
    7
            sb.append((f = !f) ? Character.toUpperCase(x) : Character.toLowerCase(x));
    
    8
        return sb.toString();
    
    1+
    import static java.util.stream.Collectors.joining;
    
    2+
    3+
    class HeLlOwOrLddddd {
    
    4+
      static String convert(String input) {
    
    5+
        boolean[] up = {true};
    
    6+
        return input.chars().filter(Character::isLetter).mapToObj(c -> "" + (char) ((up[0] = !up[0]) ? Character.toLowerCase(c) : Character.toUpperCase(c))).collect(joining());
    
    99
      }
    
    1010
    }
    

A short cut.

Code
Diff
  • class FirstClass {
      static byte sum(byte a, byte b) {
        return a += b;
      }
    }
    
  • 1
    public class FirstClass {
    
    2
        public static byte sum (byte a, byte b) {
    
    3
            return (byte)(a + b);
    
    4
        }
    
    1+
    class FirstClass {
    
    2+
      static byte sum(byte a, byte b) {
    
    3+
        return a += b;
    
    4+
      }
    
    55
    }
    

A bit ugly, but an efficient way of doing this :)

Complexity: O(n)

A few optimizations here:

  1. Ignores extra spaces between words;
  2. If the shortest word is found (a single character), stop searching;
Code
Diff
  • class Kata {
      static int findShort(String s) {
        int min = -1;
        for (int i = 0, l = 0; i < s.length(); i++, l++) {
          if (s.charAt(i) == ' ') {
            if (l > 0 && (min < 0 || l < min)) {
              if ((min = l) == 1) {
                break;
              }
            }
            l = -1;
          }
        }
        return min;
      }
    }
    
  • 1
    import java.util.stream.*;
    
    2
    public class Kata {
    
    3
        public static int findShort(String s) {
    
    4
            return Stream.of(s.split(" "))
    
    5
              .mapToInt(String::length)
    
    6
              .min()
    
    7
              .getAsInt();
    
    1+
    class Kata {
    
    2+
      static int findShort(String s) {
    
    3+
        int min = -1;
    
    4+
        for (int i = 0, l = 0; i < s.length(); i++, l++) {
    
    5+
          if (s.charAt(i) == ' ') {
    
    6+
            if (l > 0 && (min < 0 || l < min)) {
    
    7+
              if ((min = l) == 1) {
    
    8+
                break;
    
    9+
              }
    
    10+
            }
    
    11+
            l = -1;
    
    12+
          }
    
    88
        }
    
    14+
        return min;
    
    15+
      }
    
    99
    }
    
Code
Diff
  • class NthFib {
      static int fib(int n) {
        return n > 1 ? fib(--n) + fib(--n) : n;
      }
    }
    
  • 1
    public class nthFib{
    
    2
    3
        public static int fib(int n){
    
    4
          return n == 1 ? 1 : n == 0 ? 0 : (fib(n - 1) + fib(n - 2));
    
    5
        }
    
    1+
    class NthFib {
    
    2+
      static int fib(int n) {
    
    3+
        return n > 1 ? fib(--n) + fib(--n) : n;
    
    4+
      }
    
    66
    }