-
Code import java.util.*; class Solution { public static int retSmallestPositiveInteger() { int number = 9; // for sure doesnt exists before 10 XD boolean smallest = false; do { smallest = isSmallestPositiveInteget(++number, 2); } while (!smallest); return number; } private static boolean isSmallestPositiveInteget(int i, int factor) { if (factor > 6) { return true; } if (hasSameDigits(i, i*factor)) { return isSmallestPositiveInteget(i, ++factor); } return false; } private static boolean hasSameDigits(int x, int y) { char[] xdigits = Integer.toString(x).toCharArray(); char[] ydigits = Integer.toString(y).toCharArray(); Arrays.sort(xdigits); Arrays.sort(ydigits); int hashX = Arrays.hashCode(xdigits); int hashY = Arrays.hashCode(ydigits); return hashX == hashY; } }
Test Cases import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; // TODO: Replace examples and use TDD development by writing your own tests public class SolutionTest { public void testSolution() { assertEquals(142857, new Solution().retSmallestPositiveInteger()); } }
Output:
-
Code - import java.util.*;
- class Solution {
public static int retSmallestPositiveInteger() {for(int i=1; ; i++) {if(hasSameDigits(i, i*2) && hasSameDigits(i, i*3) && hasSameDigits(i, i*4) && hasSameDigits(i, i*5) && hasSameDigits(i, i*6))return i;}}- public static int retSmallestPositiveInteger() {
- int number = 9; // for sure doesnt exists before 10 XD
- boolean smallest = false;
- do {
- smallest = isSmallestPositiveInteget(++number, 2);
- }
- while (!smallest);
- return number;
- }
private static boolean hasSameDigits(int x, int y) {char[] xdigits = Integer.toString(x).toCharArray();char[] ydigits = Integer.toString(y).toCharArray();Arrays.sort(xdigits);Arrays.sort(ydigits);return Arrays.equals(xdigits, ydigits);}- private static boolean isSmallestPositiveInteget(int i, int factor) {
- if (factor > 6) {
- return true;
- }
- if (hasSameDigits(i, i*factor)) {
- return isSmallestPositiveInteget(i, ++factor);
- }
- return false;
- }
- private static boolean hasSameDigits(int x, int y) {
- char[] xdigits = Integer.toString(x).toCharArray();
- char[] ydigits = Integer.toString(y).toCharArray();
- Arrays.sort(xdigits);
- Arrays.sort(ydigits);
- int hashX = Arrays.hashCode(xdigits);
- int hashY = Arrays.hashCode(ydigits);
- return hashX == hashY;
- }
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}