Find Next number having distinct digits from the given number N
Last Updated :
26 May, 2022
Given a natural number N, the task is to find the next number having distinct digits from the given number.
Examples:
Input: N = 19
Output: 20
Explanation:
Next number to 19 whose digits are different from 19 is 20.
Input: N = 2019
Output: 3333
Explanation:
Next number to 2019 whose digits are different from 2019 is 3333.
Approach: The idea is to use hashing to compute the next number with distinct digit from the given number -
- Create a hash array to store the digits present in the number, store the most significant digit and also store the number of digits present in the number in a count variable
- Find the next digit for the most significant bit which is not present in the number and greater than the current most significant digit.
- If the next most significant digit is not found then increase the number of digits by incrementing the count and find the most significant digit between 1 to 9 which is not present in the number.
- If the next most significant digit is found then find the minimum digit next which is not present in the number between 0 to 9.
- Iterate over the number of digits from 1 to count
- Multiply the most significant digit by 10 and add the next digit which is not present in the given number.
Below is the implementation of the above approach:
C++
// C++ implementation to find
// the next distinct digits number
#include <bits/stdc++.h>
using namespace std;
// Function to find the
// next distinct digits number
void findNextNumber(int n)
{
int h[10] = { 0 };
int i = 0, msb = n, rem = 0;
int next_num = -1, count = 0;
// Loop to find the distinct
// digits using hash array
// and the number of digits
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb /= 10;
count++;
}
h[msb] = 1;
count++;
// Loop to find the most significant
// distinct digit of the next number
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break;
}
}
// Condition to check if the number
// is possible with the same number
// of digits count
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break;
}
}
}
// Condition to check if the desired
// most significant distinct
// digit is found
if (next_num > 0){
// Loop to find the minimum next digit
// which is not present in the number
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break;
}
}
// Computation of the number
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
// Condition to check if the number is
// greater than the given number
if (next_num > n)
cout << next_num << "\n";
else
cout << "Not Possible \n";
}
else{
cout << "Not Possible \n";
}
}
// Driver Code
int main()
{
int n = 2019;
findNextNumber(n);
return 0;
}
C
// C implementation to find
//next distinct digits number
#include <stdio.h>
// Function to find the
// next distinct digits number
void findNextNumber(int n)
{
int h[10] = { 0 };
int i = 0, msb = n, rem = 0;
int next_num = -1, count = 0;
// Loop to find the distinct
// digits using hash array
// and the number of digits
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb /= 10;
count++;
}
h[msb] = 1;
count++;
// Loop to find the most significant
// distinct digit of the next number
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break;
}
}
// Condition to check if the number
// is possible with the same number
// of digits count
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break;
}
}
}
// Condition to check if the desired
// most significant distinct
// digit is found
if (next_num > 0){
// Loop to find the minimum next digit
// which is not present in the number
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break;
}
}
// Computation of the number
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
// Condition to check if the number is
// greater than the given number
if (next_num > n)
printf("%d\n", next_num);
else
printf("\nNot Possible");
}
else{
printf("Not Possible \n");
}
}
// Driver Code
int main()
{
int n = 2019;
findNextNumber(n);
return 0;
}
// This code is contributed by allwink45
Java
// Java implementation to find
// the next distinct digits number
class GFG{
// Function to find the
// next distinct digits number
static void findNextNumber(int n)
{
int h[] = new int[10];
int i = 0, msb = n, rem = 0;
int next_num = -1, count = 0;
// Loop to find the distinct
// digits using hash array
// and the number of digits
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb /= 10;
count++;
}
h[msb] = 1;
count++;
// Loop to find the most significant
// distinct digit of the next number
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break;
}
}
// Condition to check if the number
// is possible with the same number
// of digits count
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break;
}
}
}
// Condition to check if the desired
// most significant distinct
// digit is found
if (next_num > 0){
// Loop to find the minimum next digit
// which is not present in the number
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break;
}
}
// Computation of the number
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
// Condition to check if the number is
// greater than the given number
if (next_num > n)
System.out.print(next_num+ "\n");
else
System.out.print("Not Possible \n");
}
else{
System.out.print("Not Possible \n");
}
}
// Driver Code
public static void main(String[] args)
{
int n = 2019;
findNextNumber(n);
}
}
// This code is contributed by 29AjayKumar
C#
// C# implementation to find
// the next distinct digits number
using System;
class GFG{
// Function to find the
// next distinct digits number
static void findNextNumber(int n)
{
int []h = new int[10];
int i = 0, msb = n, rem = 0;
int next_num = -1, count = 0;
// Loop to find the distinct
// digits using hash array
// and the number of digits
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb /= 10;
count++;
}
h[msb] = 1;
count++;
// Loop to find the most significant
// distinct digit of the next number
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break;
}
}
// Condition to check if the number
// is possible with the same number
// of digits count
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break;
}
}
}
// Condition to check if the desired
// most significant distinct
// digit is found
if (next_num > 0){
// Loop to find the minimum next digit
// which is not present in the number
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break;
}
}
// Computation of the number
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
// Condition to check if the number is
// greater than the given number
if (next_num > n)
Console.WriteLine(next_num);
else
Console.WriteLine("Not Possible");
}
else{
Console.WriteLine("Not Possible");
}
}
// Driver Code
public static void Main(string[] args)
{
int n = 2019;
findNextNumber(n);
}
}
// This code is contributed by Yash_R
Python 3
# Python 3 implementation to find
# the next distinct digits number
# Function to find the
# next distinct digits number
def findNextNumber(n):
h = [0 for i in range(10)]
i = 0
msb = n
rem = 0
next_num = -1
count = 0
# Loop to find the distinct
# digits using hash array
# and the number of digits
while (msb > 9):
rem = msb % 10
h[rem] = 1
msb //= 10
count += 1
h[msb] = 1
count += 1
# Loop to find the most significant
# distinct digit of the next number
for i in range(msb + 1,10,1):
if (h[i] == 0):
next_num = i
break
# Condition to check if the number
# is possible with the same number
# of digits count
if (next_num == -1):
for i in range(1,msb,1):
if (h[i] == 0):
next_num = i
count += 1
break
# Condition to check if the desired
# most significant distinct
# digit is found
if (next_num > 0):
# Loop to find the minimum next digit
# which is not present in the number
for i in range(0,10,1):
if (h[i] == 0):
msb = i
break
# Computation of the number
for i in range(1,count,1):
next_num = ((next_num * 10) + msb)
# Condition to check if the number is
# greater than the given number
if (next_num > n):
print(next_num)
else:
print("Not Possible")
else:
print("Not Possible")
# Driver Code
if __name__ == '__main__':
n = 2019
findNextNumber(n)
# This code is contributed by Surendra_Gangwar
JavaScript
<script>
// Javascript implementation to find
// the next distinct digits number
// Function to find the
// next distinct digits number
function findNextNumber(n)
{
let h = Array.from({length: 10}, (_, i) => 0);
let i = 0, msb = n, rem = 0;
let next_num = -1, count = 0;
// Loop to find the distinct
// digits using hash array
// and the number of digits
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb = Math.floor(msb / 10);
count++;
}
h[msb] = 1;
count++;
// Loop to find the most significant
// distinct digit of the next number
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break;
}
}
// Condition to check if the number
// is possible with the same number
// of digits count
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break;
}
}
}
// Condition to check if the desired
// most significant distinct
// digit is found
if (next_num > 0){
// Loop to find the minimum next digit
// which is not present in the number
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break;
}
}
// Computation of the number
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
// Condition to check if the number is
// greater than the given number
if (next_num > n)
document.write(next_num+ "\n");
else
document.write("Not Possible \n");
}
else{
document.write("Not Possible \n");
}
}
// Driver code
let n = 2019;
findNextNumber(n);
</script>
Time Complexity: O(logN)
Auxiliary Space: O(logN)
Similar Reads
Number of digits in the nth number made of given four digits Given an integer n, find the number of digits in the n-th number formed using only the digits 1, 4, 6, and 9, when the numbers are arranged in ascending order.1, 4, 6, 9, 11, 14, 16, 19, 41, 44, 46, 49, 61, 64, 66, 69, 91, 94, 96, 99, 111, 114, 116, 119, ....Examples: Input: n = 6Output: 2Explanatio
10 min read
Numbers having Unique (or Distinct) digits Given a range, print all numbers having unique digits. Examples : Input : 10 20Output : 10 12 13 14 15 16 17 18 19 20 (Except 11)Input : 1 10Output : 1 2 3 4 5 6 7 8 9 10Approach:As the problem is pretty simple, the only thing to be done is :-1- Find the digits one by one and keep marking visited di
10 min read
Find maximum number that can be formed using digits of a given number Given a number, write a program to find a maximum number that can be formed using all of the digits of this number.Examples: Input : 38293367Output : 98763332Input : 1203465Output: 6543210Simple Approach: The simple method to solve this problem is to extract and store the digits of the given number
6 min read
Count of distinct numbers formed by shuffling the digits of a large number N Given a large number N in the form of a string, the task is to determine the count of distinct numbers that can be formed by shuffling the digits of the number N. Note: N may contain leading zeros. The number itself is also taken into count.Since the answer could be very large, print result modulo 1
11 min read
Count digits in given number N which divide N Given a number N which may be 10^5 digits long, the task is to count all the digits in N which divide N. Divisibility by 0 is not allowed. If any digit in N which is repeated divides N, then all repetitions of that digit should be counted i. e., N = 122324, here 2 divides N and it appears 3 times. S
15+ min read