
- 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
C++ program to find number in given range where each digit is distinct
Suppose we have two numbers l and r. We have to find an integer x, which is in between l and r (both inclusive) and all digits in x are distinct.
So, if the input is like l = 211; r = 230, then the output will be 213.
Steps
To solve this, we will follow these steps −
for initialize k := l, when k <= r, update (increase k by 1), do: h := convert k to string Define one set s for initialize i := 0, when i < size of h, update (increase i by 1), do: insert h[i] into s if size of s is same as size of h, then: return h return "-1"
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; string solve(int l, int r) { for (int k = l; k <= r; k++) { string h = to_string(k); set<char> s; for (int i = 0; i < h.size(); i++) s.insert(h[i]); if (s.size() == h.size()) { return h; } } return "-1"; } int main() { int l = 211; int r = 230; cout << solve(l, r) << endl; }
Input
211, 230
Output
213
- Related Articles
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- Program to find number of pairs where elements square is within the given range in Python
- C++ Program to find the smallest digit in a given number
- Queries to check whether a given digit is present in the given Range in C++
- Program to Find Out the Occurrence of a Digit from a Given Range in Python
- C program to find frequency of each digit in a string
- Program to find count of numbers having odd number of divisors in given range in C++
- Program to find out number of distinct substrings in a given string in python
- C++ Program to find out the distinct elements in a given sequence
- C++ program to find first digit in factorial of a number
- Program to find last digit of n’th Fibonnaci Number in C++
- Print multiples of Unit Digit of Given Number in C Program
- Program to find number of distinct island shapes from a given matrix in Python
- Count numbers with unit digit k in given range in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++

Advertisements