Skip to content

Instantly share code, notes, and snippets.

@MelulekiDube
Created September 25, 2018 20:13
Show Gist options
  • Save MelulekiDube/ee23149ffc451321b31b82c55b99ed3d to your computer and use it in GitHub Desktop.
Save MelulekiDube/ee23149ffc451321b31b82c55b99ed3d to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author dube_
*/
public class MultipleOfThree {
static int getNumber(String binary_number) {
int number = 0;
for (int i = binary_number.length() - 1; i >= 0; i--) {
number += Integer.parseInt("" + binary_number.charAt(i)) * Math.pow(2, binary_number.length() - i - 1);
}
return number;
}
static boolean is_multiple(String binary_number) {
// 100000
int number = getNumber(binary_number);
return number % 3 == 0;
}
static String reverseString(String string) {
StringBuilder b = new StringBuilder(string);
return b.reverse().toString();
}
static int performCut(String binary_number) {
int multiple_of_three = 0;
String longest_string = "";
int number_cuts = 0;
//1000000011
for (int i = 0; i < binary_number.length(); i++) {
for (int j = i + 1; j < binary_number.length(); j++) {
if (is_multiple((binary_number.substring(i, j)))) {
longest_string = binary_number.substring(i, j);
}
}
binary_number = binary_number.replace(longest_string, "");
if (!longest_string.isEmpty()) {
number_cuts++;
System.out.println(longest_string);
}
longest_string = "";
}
return number_cuts;
}
public static void main(String[] args) {
System.out.println(performCut("1010001111001"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment