
- 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 largest number smaller than N with same set of digits in C++
In this problem, we are given a string N that represents a number. Our task is to find the largest number smaller than N with the same set of digits.
Problem Description: we need to find a number using all the digits of the given number which is the largest smaller number than N.
Let’s take an example to understand the problem,
Input: N = “54314”
Output: 54341
Solution Approach
A simple solution to the problem is by finding the digit of the number which can be moved to find the largest smaller number. Now, the number should be greater than its right success in order to solve the purpose.
For this we will traverse the number form right to left and find the element greater than the last elements.
Then find the greatest element in the right subarray, and replace it with the current element. After the sort the subarray is a descending order. This will be your greatest smaller element.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; void calcGreatestSmallerElement(string N, int size) { int i, j; for (i = size - 1; i > 0; i--) if (N[i] < N[i - 1]) break; if (i == 0) { cout << "Previous number is not possible"; return; } int x = N[i - 1], greatest = i; for (j = i; j < size; j++) if (N[j] < x && N[j] > N[greatest]) greatest = j; swap(N[greatest], N[i - 1]); sort(N.begin() + i, N.begin() + size, greater<char>()); cout<<"The Greatest smaller number with same set of digits is "<<N; return; } int main() { string N = "654232"; int size = N.length(); cout<<"The number is "<<N<<endl; calcGreatestSmallerElement(N, size); return 0; }
Output
The number is 654232 The Greatest smaller number with same set of digits is 654223
- Related Articles
- Find next greater number with same set of digits in C++
- Largest number smaller than or equal to N divisible by K in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Next greater Number than N with the same quantity of digits A and B in C++
- Largest number with prime digits in C++
- Find the largest number with n set and m unset bits in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Find largest sum of digits in all divisors of n in C++
- Program to find higher number with same number of set bits as n in Python?\n
- Largest even digit number not greater than N in C++
- Just smaller number with monotone digits in JavaScript
- Largest product of n contiguous digits of a number in JavaScript
- Find the largest multiple of 3 from array of digits - Set 2 in C++
- Find the largest number that can be formed with the given digits in C++
