Tuesday, July 4, 2017

CyclicRotation

import java.util.Arrays;
import java.util.stream.Collectors;

public class CyclicRotation {

     public static void main(String[] args) {
          print(solution(3, 1, 2, 3, 4, 5));
     }

     public static int[] solution(int k, int... a) {
          int l = a.length;
          int[] r = new int[l];
          for (int i = 0; i < l; i++) {
               r[(i + k) % l] = a[i];
          }
          return r;
     }

     public static void print(int... a) {
          System.out.println("[" + Arrays.stream(a).boxed().map(x -> x.toString()).collect(Collectors.joining(",")) + "]");
     }
}


Binary Gap

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

import java.util.Arrays;

public class BinaryGap {
     public static void main(String[] args) {
          BinaryGap bg = new BinaryGap();
          for (int i = 1; i < 10; i++) {
               System.out.println(bg.solution(i));
          }
     }

     public int solution(int n) {
          return Arrays.stream(Integer.toBinaryString(n).replaceAll("0+$", "").split("1")).mapToInt(String::length).max().orElse(0);
     }
}


Saturday, July 1, 2017

Wonder-Land Number

This a way to generate the wonderland number.

It has six digits.

If you multiply it by 2,3,4,5, or 6, the resulting number has all the same digits in at as the original number.
The only difference is the position that they are in.


import java.util.ArrayList;
import java.util.List;

public class WonderLandNumber {
     public static void main(String[] args) {         
          for (int i = 100000; i < 1000000; i++) {
               if (isWonderLandNumber(i)) {
                    System.out.println(i);
                    break;
               }
          }
     }

     public static boolean isWonderLandNumber(int number) {
          return containsAll(number, 2 * number) && containsAll(number, 3 * number) && containsAll(number, 4 * number)
                    && containsAll(number, 5 * number) && containsAll(number, 6 * number);
     }

     public static boolean containsAll(int number, int multiple) {
          String one = String.valueOf(number);
          String two = String.valueOf(multiple);
          return one.length() == two.length() && getDigits(one).containsAll(getDigits(two));
     }

     public static List getDigits(String number) {
          List digits = new ArrayList<>();
          for (String digit : number.split("")) {
               digits.add(digit);
          }
          return digits;
     }
}

Output:

142857


Balanced String

A program to determine if the parentheses (), the brackets [], and the braces {}, in a string are balanced.

For example:

{{)(}} is not balanced because ) comes before (

({)} is not balanced because) is not balanced between {} and similarly the { is not balanced between ()

[({})] is balanced

{}([]) is balanced

{()}[[{}]] is balanced


import java.util.Stack;

public class BalancedParenthesis {
     public static void main(String[] args) {
          BalancedParenthesis bp = new BalancedParenthesis();
          System.out.println(bp.isBalanced("{{)(}}"));
          System.out.println(bp.isBalanced("({)}"));
          System.out.println(bp.isBalanced("[({})]"));
          System.out.println(bp.isBalanced("{}([])"));
          System.out.println(bp.isBalanced("{()}[[{}]]"));
     }

     public boolean isBalanced(String string) {
          Stack stack = new Stack<>();
          for (String token : string.split("")) {
               if ("(".equals(token) || "[".equals(token) || "{".equals(token)) {
                    stack.push(token);
               } else if (")".equals(token) || "]".equals(token) || "}".equals(token)) {
                    if (stack.isEmpty()) {
                         return false;
                    }
                    String pop = stack.pop();
                    if ((")".equals(token) && !"(".equals(pop))
                              || ("]".equals(token) && !"[".equals(pop))
                              || ("}".equals(token) && !"{".equals(pop))) {
                         return false;
                    }
               }
          }
          return true;
     }
}

Output:
false
false
true
true
true

Prime Number

import java.util.stream.IntStream;

public class PrimeNumber {
    
     public static void main(String[] args) {
          PrimeNumber primeNumber = new PrimeNumber();
          System.out.println(primeNumber.isPrime(2));
          System.out.println(primeNumber.isPrime(9));
     }
    
     public boolean isPrime(int number) {
          if (number > 1) {
               return !IntStream.rangeClosed(2, (int) Math.sqrt(number)).anyMatch(x -> number % x == 0);
          }
          return false;
     }
}


Output:
true
false