I've been trying to find the right solution for the same question as here but in Java, and slightly modified since the count is supposed to be returned.
I came up with the following solution:
public static int count(int n) {
// check for 0 or smaller
if (n <= 0) {
return -1;
}
// find root of N
int root = (int) Math.ceil(Math.sqrt(n));
int count = 0;
for (int i = 1; i < root; i++) {
if (n % i == 0) {
// calculate bit_rev(i)
int reverse = bit_rev(i);
// calculate bit_rev(N/i)
int reverseDiv = bit_rev((int)Math.floor(n/i));
// check whether i * bit_rev(i) == N or i == bit_rev(N/i)
if (reverse*i == n
|| i == reverseDiv) {
System.out.println(String.format("Found reverse (N = %d, i = %d, bit_rev(i) = %d, bit_rev(i) * i = %d, bit_rev(N/i) = %d)", n, i, reverse, reverse*i, reverseDiv));
count++;
} else {
System.out.println(String.format("N = %d mod i = %d == 0, but no match (bit_rev(i) = %d, bit_rev(i) * i = %d, bit_rev(N/i) = %d)", n, i, reverse, reverse*i, reverseDiv));
}
}
}
if (count == 0) {
// didn't match -> return -1
return -1;
} else {
// return whatever the count was
return count;
}
}
public static int bit_rev(int n) {
String string = Integer.toBinaryString(n);
String reverseString = new StringBuffer(string).reverse().toString();
int reverse = Integer.parseInt(reverseString, 2);
return reverse;
}
In the reference sample the solution for N = 3245 is supposed to be count = 55.
However, my solution finds that count = 1 (i = 55, bit_rev(i) = 59).
The other reference samples were:
-) N = 50 -> count = 1
-) N = 286 -> count = 2
I understand that the solution for finding bit_rev() isn't the greatest (most efficient one), but I don't think it's wrong, is it?
Is there any other mistake in here, or is the reference sample for N = 3245 wrong?
I think you've misread the question, or the reference answer is wrong. If it's anything like the question at http://algorithmsforever.blogspot.com/2011/12/integer-binary-roots.html?m=1 then you need to be returning the smallest binary root, not the number of binary roots.
Regardless, I think your implementation is correctly working them out. 55 is indeed the smallest binary root of 3245 (not the count of roots).
Related
I Find longest sequence of zeros in binary representation of an integer but result incorrect on app.codility.com with n = 6 and n =328
https://imgur.com/rzFsjaY
public int solution(int N) {
Integer result = 1;
StringBuilder modNumber = new StringBuilder();
while (result > 0) {
result = n / 2;
modNumber.append(n % 2 + "");
n = result;
}
int length = modNumber.length();
String modString = modNumber.toString();
Integer binaryGap = 0;
List<Integer> lstResult = new ArrayList<>();
Boolean isBinaryGap = false;
if (modString.charAt(0) == '0') {
binaryGap = 0;
} else {
for (int i = 1; i < length; i++) {
char c = modString.charAt(i);
if (c == '0') {
binaryGap += 1;
isBinaryGap = true;
} else {
isBinaryGap = false;
}
if(!isBinaryGap) {
lstResult.add(binaryGap);
binaryGap = 0;
}
}
}
int max=0;
if(!lstResult.isEmpty()) {
max = lstResult.stream().collect(Collectors.summarizingInt(Integer::intValue)).getMax();
}
return max;
}
The problem is this part:
if (modString.charAt(0) == '0') {
binaryGap = 0;
} else {
I suspect your intention is to handle the case of the input being zero (so why not do that right off the bat, using the original value of N?), but you get many false positives because your technique for forming the string places the bits in order from least-significant to most-significant. The least-significant bit is zero for every even number, and you therefore report a gap of 0 for all of them, but the only even number for which that's the correct result is 0.
I'm sure that with that information you could fix your code to produce the correct results, but the problem would not even have arisen if you had chosen a simpler approach. For example, how about scanning the binary representation just once, keeping track of the current and maximum gap lengths as you go? You already perform the needed scan, but instead of just computing the result directly, you go through all that mess of building and then analyzing a string representation. Simpler code leaves less room for bugs, and when they arise, they are usually easier to spot.
I'm trying to solve the following problem. Given an integer, n, list all n-digits numbers such that each number does not have repeating digits.
For example, if n is 4, then the output is as follows:
0123
0124
0125
...
9875
9876
Total number of 4-digit numbers is 5040
My present approach is by brute-force. I can generate all n-digit numbers, then, using a Set, list all numbers with no repeating digits. However, I'm pretty sure there is a faster, better and more elegant way of doing this.
I'm programming in Java, but I can read source code in C.
Thanks
Mathematically, you have 10 options for the first number, 9 for the second, 8 for the 3rd, and 7 for the 4th. So, 10 * 9 * 8 * 7 = 5040.
Programmatically, you can generate these with some combinations logic. Using a functional approach usually keeps code cleaner; meaning build up a new string recursively as opposed to trying to use a StringBuilder or array to keep modifying your existing string.
Example Code
The following code will generate the permutations, without reusing digits, without any extra set or map/etc.
public class LockerNumberNoRepeats {
public static void main(String[] args) {
System.out.println("Total combinations = " + permutations(4));
}
public static int permutations(int targetLength) {
return permutations("", "0123456789", targetLength);
}
private static int permutations(String c, String r, int targetLength) {
if (c.length() == targetLength) {
System.out.println(c);
return 1;
}
int sum = 0;
for (int i = 0; i < r.length(); ++i) {
sum += permutations(c + r.charAt(i), r.substring(0,i) + r.substring(i + 1), targetLength);
}
return sum;
}
}
Output:
...
9875
9876
Total combinations = 5040
Explanation
Pulling this from a comment by #Rick as it was very well said and helps to clarify the solution.
So to explain what is happening here - it's recursing a function which takes three parameters: a list of digits we've already used (the string we're building - c), a list of digits we haven't used yet (the string r) and the target depth or length. Then when a digit is used, it is added to c and removed from r for subsequent recursive calls, so you don't need to check if it is already used, because you only pass in those which haven't already been used.
it's easy to find a formula. i.e.
if n=1 there are 10 variants.
if n=2 there are 9*10 variants.
if n=3 there are 8*9*10 variants.
if n=4 there are 7*8*9*10 variants.
Note the symmetry here:
0123
0124
...
9875
9876
9876 = 9999 - 123
9875 = 9999 - 124
So for starters you can chop the work in half.
It's possible that you might be able to find a regex which covers scenarios such that if a digit occurs twice in the same string then it matches/fails.
Whether the regex will be faster or not, who knows?
Specifically for four digits you could have nested For loops:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j != i) {
for (int k = 0; k < 10; k++) {
if ((k != j) && (k != i)) {
for (int m = 0; m < 10; m++) {
if ((m != k) && (m != j) && (m != i)) {
someStringCollection.add((((("" + i) + j) + k) + m));
(etc)
Alternatively, for a more generalised solution, this is a good example of the handy-dandy nature of recursion. E.g. you have a function which takes the list of previous digits, and required depth, and if the number of required digits is less than the depth just have a loop of ten iterations (through each value for the digit you're adding), if the digit doesn't exist in the list already then add it to the list and recurse. If you're at the correct depth just concatenate all the digits in the list and add it to the collection of valid strings you have.
Backtracking method is also a brute-force method.
private static int pickAndSet(byte[] used, int last) {
if (last >= 0) used[last] = 0;
int start = (last < 0) ? 0 : last + 1;
for (int i = start; i < used.length; i++) {
if (used[i] == 0) {
used[i] = 1;
return i;
}
}
return -1;
}
public static int get_series(int n) {
if (n < 1 || n > 10) return 0;
byte[] used = new byte[10];
int[] result = new int[n];
char[] output = new char[n];
int idx = 0;
boolean dirForward = true;
int count = 0;
while (true) {
result[idx] = pickAndSet(used, dirForward ? -1 : result[idx]);
if (result[idx] < 0) { //fail, should rewind.
if (idx == 0) break; //the zero index rewind failed, think all over.
dirForward = false;
idx --;
continue;
} else {//forward.
dirForward = true;
}
idx ++;
if (n == idx) {
for (int k = 0; k < result.length; k++) output[k] = (char)('0' + result[k]);
System.out.println(output);
count ++;
dirForward = false;
idx --;
}
}
return count;
}
I'm working on a data structures assignment and my attempt to increment a double hash function is stuck in an infinite loop.
My book defines a strategy to double hash as
h′(k) = q−(k mod q), for some prime number q < N. Also, N
should be a prime.
I've identified that the double hash increment is causing the issue, as switching to linear probing runs fine.
private int findSlot(int h, K k) {
totalProbes = 0;
int avail = -1; // no slot available (thus far)
int j = h; // index while scanning table
do {
totalProbes++;
if (totalProbes > maxProbes) maxProbes = totalProbes;
if (isAvailable(j)) { // may be either empty or defunct
if (avail == -1) avail = j; // this is the first available slot!
if (table[j] == null) {
break;
} // if empty, search fails immediately
} else if (table[j].getKey().equals(k))
return j; // successful match
//j = (j + 1) % capacity; // keep looking (cyclically)
j = hashTwo(k); //increment using double hash
} while (j != h); // stop if we return to the start
return -(avail + 1); // search has failed
}
private int hashTwo(K key) {
String keyString = key.toString(); //convert generic -> string -> int
int keyInt = Integer.parseInt(keyString);
return 7 - (keyInt % 7);
}
There is some ugliness with the hash 2 function - namely converting from a generic to an integer, but besides that it follows the same instructions as the book.
1.error in your code: it has to be "j = hashTwo(j)"
2.error in the formula: for k=q 0> h′(k) = q−(k mod q) = q-0=q=k
(you better look https://en.wikipedia.org/wiki/Double_hashing for a valid formula)
Instead of "Integer.parseInt" you should start iteration with
private int findSlot(K k, int size) {
return findSlot(hashTwo(k.hashCode()), size);
}
private int findSlot(int h, int size) {...}
In an interview I had, I was asked to write a program that prints to the screen the numbers 1,2,3,4,5....until 99999999999999.....?(the last number to print is the digit 9 million times)
You are not allowed to use Big-Integer or any other similar object.
The hint I got is to use modulo and work with strings, I tried to think about it but haven't figured it out.
Thanks in advance
You need an array to store the number, and perform operations on the array.
Here's an example
public class BigNumberTest2 {
public static void main(String[] args) {
/*Array with the digits of the number. 0th index stores the most significant digit*/
//int[] num = new int[1000000];
//Can have a million digits, length is 1 + needed to avoid overflow
int[] num = {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int base = 10;
int step = 1;
String endNum = "100000000000000000000000000000000000000000000000000020";//Can have a million digits
while(true) {
//Increment by step
for(int carry = step, i = num.length - 1; carry != 0 && i >= 0; i--) {
int newDigit = num[i] + carry;
num[i] = newDigit % base;
carry = newDigit / base;
}
//Find the position of most significant digit
int mostSignificantDigitIndex = 0;
while(num[mostSignificantDigitIndex] == 0) {/*No need to check if firstNonZero < num.length, as start num >=0 */
mostSignificantDigitIndex++;
}
StringBuilder strNum = new StringBuilder();
//Concatenate to get actual string
for(int i = mostSignificantDigitIndex; i < num.length; i++) {
strNum.append(num[i]);
}
System.out.println(strNum);
//Check if number current number is greater or equal to endNum
if(strNum.length() > endNum.length() || (strNum.length() == endNum.length() && strNum.toString().compareTo(endNum) >= 0)) {
break;
}
}
}
}
Output
1000000000000000000000000000000000000000000000000000001
1000000000000000000000000000000000000000000000000000002
1000000000000000000000000000000000000000000000000000003
1000000000000000000000000000000000000000000000000000004
1000000000000000000000000000000000000000000000000000005
1000000000000000000000000000000000000000000000000000006
1000000000000000000000000000000000000000000000000000007
1000000000000000000000000000000000000000000000000000008
1000000000000000000000000000000000000000000000000000009
1000000000000000000000000000000000000000000000000000010
1000000000000000000000000000000000000000000000000000011
1000000000000000000000000000000000000000000000000000012
1000000000000000000000000000000000000000000000000000013
1000000000000000000000000000000000000000000000000000014
1000000000000000000000000000000000000000000000000000015
1000000000000000000000000000000000000000000000000000016
1000000000000000000000000000000000000000000000000000017
1000000000000000000000000000000000000000000000000000018
1000000000000000000000000000000000000000000000000000019
1000000000000000000000000000000000000000000000000000020
Something like this:
This is a PHP, but you could transform it to java easy...
Recursion with increasing number through string.
function nextNum($num="", $step=1, $end="999999999999999999") {
$string = "";
$saving = 0;
for($i=strlen($num)-1; $i>=0; $i--) {
$calc = intval(intval($num[$i]) + $saving);
if ($i==(strlen($num)-1)) $calc = $calc + $step;
if (strlen($calc)==2) {
$calc = $calc."";
$saving = intval($calc[0]);
$calc = intval($calc[1]);
}
else {
$calc = intval($calc);
$saving = 0;
}
$string = $calc . $string;
}
if ($saving!=0) $string = $saving.$string;
echo $string." ";
if ($end == $string || strlen($end)<strlen($string)) { return; }
else return nextNum($string, $step, $end);
}
nextNum("0", 1, "999999999999999999");
I didn't test it... but it should work..
I am trying to automate the finding of the closest factor of a number to another number;
Example:
Closest factor of 700 to 30 is 28 (30 does not go into 700, but 28 does).
An obvious solution is just to get all the factors of 700 and do a simple distance calculation to find the nearest factor to 30, but this seems to be inefficient.
Another solution is to find all the base prime factors, like:
private List<Integer> getPrimeFactors(int upTo) {
List<Integer> result = new ArrayList<>();
for (int i = 2; i <= upTo; i++) {
if (upTo % i == 0) {
result.add(i);
}
}
return result;
}
And multiplying each of these numbers together to get all the combinations, and therefore find the closest.
I am trying to programme this so it is automated.
Any better solutions?
You don't need to calculate all factors, but you can go in both directions from the number to find its closest number which is the factor of given number
Pseduo code will be:
n= given number(dividend);
x= second number( whose closest number is required)
i=0;
if(n%x==0) print x;
else
while(true){
if(n%(x-i)==0){
print x-i
break
}
else if(n%(x+i)==0){
print x+i;
break
}
else i=i+1
}
I have my solution wrapped in a small static method:
/**
* #param target the number you want the factor to be close to
* #param number the number you want the result to be a factor of
*/
private static int getClosestFactor(int target, int number) {
for (int i = 0; i < number; i++) {
if (number % (target + i) == 0) {
return target + i;
} else if (number % (target - i) == 0) {
return target - i;
}
}
return number;
}
package dummy;
public class test {
public static void main(String[] args) {
int factorOff = 700;
int factorFrom = 30;
for (int i = 2; i < factorOff; i++) {
if (factorOff % (factorFrom + i) == 0) {
System.out.println(factorFrom + i);
i = factorOff;
} else if (factorFrom - i > 1 && factorOff % (factorFrom - i) == 0) {
System.out.println(factorFrom - i);
i = factorOff;
}
}
}
}
This should be a fast solution:
public static int findClosestFactor(int number, int closeTo) {
int result = 1;
int currentDist = closeTo - 1;
// stop conditions for comparison
boolean compareSmallFactor = true;
boolean compareLargeFactor = true;
for (int factor1 = (int) Math.sqrt(number); factor1 > 0; factor1--) {
if (number % factor1 == 0) {
if (compareSmallFactor) {
int dist1 = Math.abs(closeTo - factor1);
if (dist1 < currentDist) {
result = factor1;
currentDist = dist1;
}
// factor 1 is getting always smaller
// so you need not compare next time, if go away from target (smaller than target)
if (factor1 <= closeTo) {
compareSmallFactor = false;
}
}
if (compareLargeFactor) {
int factor2 = number / factor1;
int dist2 = Math.abs(closeTo - factor2);
if (dist2 < currentDist) {
result = factor2;
currentDist = dist2;
}
// factor 2 is getting always larger
// so you need not compare next time, if go away from target (larger than target)
if (factor2 >= closeTo) {
compareLargeFactor = false;
}
}
// if both factors go away from target, you can cancel
if (!compareSmallFactor && !compareLargeFactor) {
break;
}
}
}
return result;
}
You can factorize the number, then use a powerset generator (for a multiset[1]) to search for the group of factors that get closest to the max without going over.
The powerset generator can be modified to prevent further iteration down branches that exceed the desired value (branch and bound). IE: if factors are (2,5,7,13,19). The number is 80, and you have 2*5*7 = 70. 2 * 5 * 7 * 13 = 910. There is no need to check 2 * 5 * 7 * 19 as it would clearly exceed the max.
[1] It's a good idea to treat the factorization as an multiset. For example in the case of 700, ((2,2),(5,2),(7,1)). You could treat it as (2,2,5,5,7), but it would do extra work since there's no need to find 2 * 5 = 10 more than once, but if it's not treated as a multiset, then that would happen.