Smallest number possible by swapping adjacent even-odd pairs in C++14 May 2025 | 4 min read The objective is to rearrange the digits of a given number to generate the smallest possible number while ensuring that adjacent digits represent even-odd pairs. It is known as the "swapping adjacent even-odd pairs" problem in C++.
1. Bubble Sort ApproachA popular and easy-to-understand sorting algorithm for effectively resolving a variety of sorting problems is bubble sort. The Bubble Sort method provides a simple solution when used to identify the smallest number that can be obtained by swapping adjacent even-odd pairs. Using this method, we start by looking at the number's digits from left to right. Iteratively going over the digits, we compare adjacent pairs. If an adjacent pair (an even digit followed by an odd digit) is in the wrong order, we swap them to correct the order. This process is repeated until no more swaps are required to ensure that adjacent digits form even-odd pairs. Algorithm steps:Step-1: Enter an array of numbers that have not been sorted. Step-2: Create a loop that iterates over the array repeatedly until every element is sorted. Step-3: Iterate over every pair of adjacent elements in the array within the loop. Step-4: Verify that for every pair, the first element is odd and the second element is even. If true, swap the elements. Step-5: Compare the element that was just swapped with the element in the array that appeared preceding it. Replace the previously swapped element with the smaller one if necessary. Step-6: Iteratively repeat the procedure until each pair has been validated and sorted. Step-7: If no swaps are performed during any iterations, the array is sorted, and the loop can be terminated. Step-8: Print the sorted array after the sorting process is complete. Example:Let us take an example to illustrate swapping adjacent even-odd pairs problem in C++ using the Bubble sort approach. Output: Please enter the number of elements: 8 Enter the elements separated by space: 7 2 5 9 10 12 8 1 1 5 7 9 2 8 10 12 Explanation:This C++ code sorts an array of integers using the Bubble Sort method, which makes sure that integers with the same parity are put in the same relative order and that even-odd pairs are sorted in ascending order. The BubbleSort function swaps adjacent elements in the array iteratively according to specified conditions. The method executes swaps based on whether adjacent elements form even-odd pairs or have the same parity but are out of order in each iteration. If no swaps are done during an iteration, the swapped flag indicates that the array has already been sorted, which optimises the sorting process by breaking out of the loop. After that, the main function asks the user to enter the array's elements and the number of elements, sorts the array using BubbleSort, and prints the sorted array. 2. Greedy Approach
Example:Let us take an example to illustrate swapping adjacent even-odd pairs problem in C++ using the Greedy approach. Output: Please enter the number: 2145367 The smallest number after swapping the adjacent even-odd pairs is: 2145637 Explanation:This C++ code swaps adjacent even-odd pairs in a given number expressed as a string in an attempt to discover the smallest possible number. Iterating through the string, the smallestNumberSwappingPairs function finds even digits at even indices and odd digits at odd indices. In order to keep the even-odd pairing maintained, it swaps the numbers if such a pair is found. After that, the main function prompts the user to enter a number; it runs the function smallestNumberSwappingPairs to swap adjacent even-odd pairs in order to get the smallest possible number. After that, the result is printed. Overall, the code effectively uses a swapping method to minimize the number while maintaining the order of even-odd pairs. Next TopicCustom Jumble Word Game in C++ |
Lattice Reduction Algorithm in C++
A mathematical technique called lattice reduction that is used in numerical analysis, computational geometry, and cryptography to work with lattices in high-dimensional settings. A lattice is a Euclidean space grid-like structure composed of integer combinations of a set of basis vectors in mathematics. A reduced lattice's...
7 min read
Negative Infinity in C++
Introduction Negative infinity is substantially an uncommon value in C++ that represents an amount that's considerably smaller compared to any additional real number. This idea becomes crucial in many computational contexts, especially when dealing with edge cases in floating-point arithmetic, designing algorithms, and carrying out numerical analysis....
5 min read
Difference between std::lock_guard and std::unique_lock in C++
Simple RAII-based mutex locker std::lock_guard locks the mutex at construction and releases it upon destruction without providing user control. On the other hand, the std::unique_lock function is more flexible because it allows ownership transfer, timed locking, manual unlocking, and deferred locking. Use the std::lock_guard function for...
10 min read
exit(1) in C++
Introduction: The 'exit()' function in C++ is utilized to end a program execution. It enables you to stop the program at any moment during its operation, regardless of where it is invoked within the code. The main objective of using the 'exit()' function is to conclude the...
9 min read
Exception Handling and Object Destruction in C++
In this article, you will learn about exception handling and object destruction in C++ with their examples. Introduction Exception Handling: C++'s exception handling feature enables programs to respond gracefully to unusual or unexpected circumstances. C++ provides the ability to handle errors and unusual conditions that arise during the...
4 min read
Hendecagonal Number in C++
Introduction Numbers have interested mathematicians and programmers since time eternal. One of several interesting sequences is hendecagonal numbers, which are notable due to their geometric significance. The numbers represent an 11-gon or an 11-sided figure (a hendecagon) and can be described as a generalization of triangular,...
4 min read
Look-and-Say Sequence in C++
In this article, we will discuss the Look-and-Say Sequence with its different methods, examples, time complexity, and space complexity. What is the Look-and-Say Sequence? The Look-and-say Sequence (also called the "Count-and-say Sequence") is an integer sequence in which each term that follows the initial one uses a...
10 min read
Distance Traveled by Hour and Minute Hand in a Given Time Interval in C++
In this article, we will discuss how to find the distance traveled by hour and minute hand in a given time interval in C++. Understanding the Problem A traditional analog clock has two major hands: the hour hand and the minute hand. Both of these hands turn...
4 min read
Interesting or Fun Facts about Fibonacci Sequence in C++
The Fibonacci sequence is a mathematical concept that permeates mathematics, computer science, biology, and art. Here are some interesting and fun facts about the Fibonacci sequence in C++ programming. 1. Definition of the Fibonacci Sequence The Fibonacci sequence can be understood in the following way: F(0)= 0 F(1)= 1 F(n)= F(n-1)...
4 min read
Validate localhost API request processed by POSTMAN using Regular Expression in C++
When developing web applications, testing API endpoints locally is a common practice for ensuring functionality and debugging. Tools like Postman facilitate this process by allowing developers to send HTTP requests to API endpoints hosted on localhost. Localhost API requests are those made to endpoints on...
16 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India