Find Next Decimal With Same Digits and Same number of Digits (i.e. input 112 output will be 151)

Sudarshan Kasar
2 min readDec 29, 2020

--

/***
* Following Integer
*
* You are writing out a list of numbers. Your list contains all numbers with exactly i digits in its decimal representation which are equal to i,
* for each i between 1 and 9, inclusive. You are writing them out in ascending order. For example, you might be writing every number with two '1's and one '5'.
* Your list would begin 115, 151, 511, 1015, 1051. Given N, the last number you wrote, compute what the next number in the list will be.
* The number of 1s, 2s, ..., 9s is fixed but the number of 0s is arbitrary.
*
* Input:
* Your program should read lines of text from standard input. Each line will contain a single integer n < 10^6.
* Output:
* For each input ingter n, print to standard output the next integer in the list, one integer per line.
* */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;


public class Main {

public static void main(String[] args) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader);
String line;

while ((line = in.readLine()) != null) {
int inputNumber = Integer.parseInt(line);

HashMap<Integer, Integer> digitWithOccurance =countDigitAndOccurance(inputNumber);
digitWithOccurance.remove(0);//remove orbitrary digit

int nextDecimal = nextDecimalRepresentationSameDigits(inputNumber, digitWithOccurance);

System.out.println(nextDecimal);
}
}

private static int nextDecimalRepresentationSameDigits(
int inputNumber, HashMap<Integer, Integer> digitWithOccurance){
int returnNumber = inputNumber;
int decimalRepresentation = inputNumber + 1;
boolean condition = true;

while(condition){
HashMap<Integer, Integer> newNumberDigitWithOccurance = countDigitAndOccurance(decimalRepresentation);
newNumberDigitWithOccurance.remove(0);

if(digitWithOccurance.equals(newNumberDigitWithOccurance)){
returnNumber = decimalRepresentation;
condition = false;
} else {
++decimalRepresentation;
}
}

return returnNumber;
}

private static HashMap<Integer, Integer> countDigitAndOccurance(int inputNumber) {
String temp = "" + inputNumber;

HashMap<Integer, Integer> digitOccurance = new HashMap();

for (int i = 0; i < temp.length(); i++)
{
int key = Integer.parseInt(String.valueOf(temp.charAt(i)));

if(digitOccurance.keySet().contains(key)) {
int digitCount = digitOccurance.get(key) + 1;

digitOccurance.put(key, digitCount);
} else {
digitOccurance.put(key, 1);
}
}

return digitOccurance;
}
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sudarshan Kasar
Sudarshan Kasar

No responses yet

Write a response