
- 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
Find smallest permutation of given number in C++
In this problem, we are given a large number N. Our task is to find the smallest permutation of a given number.
Let’s take an example to understand the problem,
Input
N = 4529016
Output
1024569
Solution Approach
A simple solution to the problem is by storing the long integer value to a string. Then we will sort the string which is our result. But if there are any leading zeros, we will shift them after the first non zero value.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; string smallestNumPer(string s) { int len = s.length(); sort(s.begin(), s.end()); int i = 0; while (s[i] == '0') i++; swap(s[0], s[i]); return s; } int main() { string s = "4529016"; cout<<"The number is "<<s<<endl; cout<<"The smallest permutation of the number is "<<smallestNumPer(s); return 0; }
Output
The number is 4529016 The smallest permutation of the number is 1024569
- Related Articles
- Find smallest number with given number of digits and sum of digits in C++
- Program to find number of elements in all permutation which are following given conditions in Python
- C++ Program to find the smallest digit in a given number
- Count number of smallest elements in given range in C++
- Program to find nth smallest number from a given matrix in Python
- Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
- How to create permutation of array with the given number of elements in JavaScript
- Find the Number of permutation with K inversions using C++
- Program to reduce list by given operation and find smallest remaining number in Python
- Find permutation of first N natural numbers that satisfies the given condition in C++
- Find Permutation in C++
- Find the Smallest Divisor Given a Threshold in C++
- Find the smallest number in a Java array.
- Find smallest number n such that n XOR n+1 equals to given k in C++
- 8085 Program to find the smallest number

Advertisements