
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Form the Smallest Number Using At Most One Swap Operation in C++
In this problem, we are given a positive integer. Our task is to create a program to form the smaller number using at most one swap operation.
We will be creating a new number using the digits of the existing number. The smallest number formed can have only one digit swapped from the existing number.
Let’s take an example to understand the problem
Input: n = 63519 Output: 36519
Solution Approach
One method to solve the problem is by finding all the numbers created by swapping pair of digits of the given number. Out of all these swapped digit numbers, the smallest one is returned. For this, we will convert the number to string and swap positions.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findSmallestNumSwapDig(int N){ string strNum = to_string(N); string temp = strNum; for (int i = 0; i < strNum.size(); i++) { for (int j = i + 1; j < strNum.size(); j++) { swap(strNum[i], strNum[j]); if (stoi(strNum) < stoi(temp)) temp = strNum; swap(strNum[i], strNum[j]); } } return stoi(temp); } int main(){ int num = 792156; cout<<"The number is "<<num<<endl; cout<<"The smallest number created by swapping one digit is "<<findSmallestNumSwapDig(num) << endl; return 0; }
Output
The number is 792156 The smallest number created by swapping one digit is192756
Another approach
One more approach to solve the problem is by using an extra auxiliary array aux[]. This array is used to store the index of the smallest digit at the right (greater index) of the current index i.e. aux[i] is the index of the smallest digit at the right side of the number’s digit. Initialise aux[i] = -1, if no such digit exists. Now, traverse the number from index 0 to n-1 and find the number in the aux[] array which is smaller than the current value. For index 0 value, check for non-zero element, if 0 occurs discard the value. If any element is found, swap arr[i] & arr[aux[i]] and return the created number.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; int findSmallestNumSwapDig(int N){ string num = to_string(N); int n = num.size(); int auxArr[n], right; auxArr[n - 1] = -1; right = n - 1; for (int i = n - 2; i >= 1; i--) { if (num[i] >= num[right]) auxArr[i] = right; else { if (num[i] == num[i + 1]) auxArr[i] = right; else { auxArr[i] = -1; right = i; } } } int small = -1; for (int i = 1; i < n; i++) if (num[i] != '0') { if (small == -1) { if (num[i] < num[0]) small = i; } else if (num[i] <= num[small]) small = i; } if (small != -1) swap(num[0], num[small]); else { for (int i = 1; i < n; i++) { if (auxArr[i] != -1 && num[i] != num[auxArr[i]]) { swap(num[i], num[auxArr[i]]); break; } } } return stoi(num); } int main(){ int num = 792156; cout<<"The number is "<<num<<endl; cout<<"The smallest number created by swapping one digit is" <<findSmallestNumSwapDig(num)<< endl; return 0; }
Output
The number is 792156 The smallest number created by swapping one digit is192756