Find the Smallest Number in Java

The challenge

You have a positive number n consisting of digits. You can do at most one operation: Choosing the index of a digit in the number, remove this digit at that index and insert it back to another or at the same place in the number in order to find the smallest number you can get.

#Task: Return an array or a tuple or a string depending on the language (see “Sample Tests”) with

  • 1) the smallest number you got
  • 2) the index i of the digit d you took, i as small as possible
  • 3) the index j (as small as possible) where you insert this digit d to have the smallest number.

Example:

smallest(261235) --> [126235, 2, 0] or (126235, 2, 0) or "126235, 2, 0"
Code language: JavaScript (javascript)

126235 is the smallest number gotten by taking 1 at index 2 and putting it at index 0

smallest(209917) --> [29917, 0, 1] or ... [29917, 1, 0] could be a solution too but index `i` in [29917, 1, 0] is greater than index `i` in [29917, 0, 1].
Code language: CSS (css)

29917 is the smallest number gotten by taking 2 at index 0 and putting it at index 1 which gave 029917 which is the number 29917.

smallest(1000000) --> [1, 0, 6] or ...
Code language: CSS (css)

The solution in Java code

Option 1:

import java.lang.StringBuilder; public class ToSmallest { public static long[] smallest(long n) { long min = n; int index1 = 0; int index2 = 0; String number = String.valueOf(n); for (int i=0; i<number.length(); i++) { for (int j=0; j<number.length(); j++) { if (i!=j && making(number, i, j) < min) { min = making(number, i, j); index1 = i; index2 = j; } } } return new long[]{min, index1, index2}; } public static long making(String s, int i, int j) { StringBuilder sb = new StringBuilder(s); char c = sb.charAt(i); sb.deleteCharAt(i); sb.insert(j, c); return Long.valueOf(sb.toString()); } }
Code language: Java (java)

Option 2:

public class ToSmallest { private static int smallestPosition; private static int replacedPosition = 0; public static long[] smallest(long n) { String value = String.valueOf(n); String smallestPossibleValue = value; for(int i = 0; i < value.length(); i++){ char currentDigit = value.charAt(i); for(int j = 0; j < value.length(); j++){ StringBuilder sb = new StringBuilder(value); sb.deleteCharAt(i); sb.insert(j, currentDigit); String newValue = sb.toString(); if(Long.parseLong(newValue, 10) < Long.parseLong(smallestPossibleValue, 10)){ smallestPossibleValue = newValue; smallestPosition = i; replacedPosition = j; } } } return new long[]{Long.parseLong(smallestPossibleValue), smallestPosition, replacedPosition}; } }
Code language: Java (java)

Option 3:

import java.util.*; public class ToSmallest { public static long[] smallest(long n) { final String s = ""+n; long[] result = new long[]{Long.MAX_VALUE,0,0}; for (int i=s.length()-1; i>=0; i--) { final String s1=s.substring(0,i)+s.substring(i+1); for (int j=s.length()-1; j>=0; j--) { final long tmp = Long.valueOf(s1.substring(0,j)+s.charAt(i)+s1.substring(j)); if (tmp <= result[0]) result = new long[]{tmp,i,j}; } } return result; } }
Code language: Java (java)

Test cases to validate our solution

import static org.junit.Assert.*; import java.util.Arrays; import org.junit.Test; public class ToSmallestTest { private static void testing(long n, String res) { assertEquals(res, Arrays.toString(ToSmallest.smallest(n))); } @Test public void test() { System.out.println("Basic Tests smallest"); testing(261235, "[126235, 2, 0]"); testing(209917, "[29917, 0, 1]"); testing(285365, "[238565, 3, 1]"); testing(269045, "[26945, 3, 0]"); testing(296837, "[239687, 4, 1]"); } }
Code language: Java (java)

Additional test cases

import static org.junit.Assert.*; import java.util.Arrays; import java.util.Random; import org.junit.Test; public class ToSmallestTest { private static void testing(long n, String res) { assertEquals(res, Arrays.toString(ToSmallest.smallest(n))); } @Test public void test() { System.out.println("Basic Tests smallest"); testing(261235, "[126235, 2, 0]"); testing(209917, "[29917, 0, 1]"); testing(285365, "[238565, 3, 1]"); testing(269045, "[26945, 3, 0]"); testing(296837, "[239687, 4, 1]"); testing(187863002809L, "[18786300289, 10, 0]"); testing(199819884756L, "[119989884756, 4, 0]"); testing(94883608842L, "[9488368842, 6, 0]"); testing(256687587015L, "[25668758715, 9, 0]"); testing(935855753L, "[358557539, 0, 8]"); } //........ private static long[] smallestSol(long n) { String s = Long.toString(n), tmp = s; long [] mem = new long[] {-1, -1, -1}; int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); String str1 = s.substring(0, i) + s.substring(i+1, l); for (int j = 0; j < l; j++) { String str2 = str1.substring(0, j) + c + str1.substring(j, str1.length()); int cmp = str2.compareTo(tmp); if (cmp < 0) { tmp = str2; mem[0] = Long.parseLong(tmp); mem[1] = i; mem[2] = j; } } } return mem; } //........ private static long randint(Random rnd, long min, long max) { long randomNumber = (long)(rnd.nextDouble()*(max - min)); return randomNumber; } private static void wTests1() { for (int i = 0; i < 200; i++) { Random rnd = new Random(); long nmx = randint(rnd, 4000, 1000000000000000000L); String ans = Arrays.toString(smallestSol(nmx)); //System.out.println("number " + nmx + " --> " + ans); testing(nmx, ans); } } @Test public void tests1() { System.out.println("Random Tests smallest ****"); wTests1(); } }
Code language: Java (java)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments