
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print a number strictly less than a given number such that all its digits are distinct in C++
In this problem, we are given a number n. Our task is to print the largest number less than n such that all its digits are distinct.
Let’s take an example to understand the problem
Input: n = 2332 Output: 2319
To solve this problem, we reverse the count of the numbers i.e. from n to 0. And check for number with distinct digits, if the current count values satisfy the condition print it and end the loop. Otherwise continue to loop. The max number of times the loop will run is always less than n.
Example
Program to implement our solutions,
#include <bits/stdc++.h> using namespace std; int findDistinctDigitNumber(int n) { for (int i = n - 1; i>=0 ; i--) { int count[10] = { 0 }; int x = i; int count1 = 0, count2 = 0; while (x) { count[x % 10]++; x /= 10; count1++; } for (int j = 0; j < 10; j++) { if (count[j] == 1) count2++; } if (count1 == count2) return i; } } int main() { int n = 44324; cout<<"Number less than "<<n<<" with all digits distinct are : "<<findDistinctDigitNumber(n); return 0; }
Output
Number less than 44324 with all digits distinct are : 43987
- Related Articles
- Print all Prime Quadruplet of a number less than it in C++
- Count of Numbers such that difference between the number and sum of its digits not less than L in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Number of elements less than or equal to a given number in a given subarray in C++
- Java Program to display a prime number less than the given number
- Find a number x such that sum of x and its digits is equal to given n using C++.
- Program to find number of elements in A are strictly less than at least k elements in B in Python
- Print all combinations of points that can compose a given number in C++
- Python Program to print all distinct uncommon digits present in two given numbers
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
- Print all numbers less than N with at-most 2 unique digits in C++
- Nearest prime less than given number n C++
- C++ Program to check if a given number is Lucky (all digits are different)
- A two-digit number is such that the product of its digits is 20. If 9 is added to the number, the digits interchange their places. Find the number.
- A two-digit number is such that the product of its digits is 8. When 18 is subtracted from the number, the digits interchange their places. Find the number.

Advertisements