- 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

# Biggest number by arranging numbers in certain order in C++

In this problem, we are given an array of numbers and we have to find the largest value that can be made by changing them in a certain way. the condition for the arrangement is, the order of even numbers and odd numbers shall remain the same i.e. the order of all even numbers cannot be changed.

let's take an example to understand the concept better,

Input : {17, 80, 99, 27, 14 , 22} Output: 801799271422 Explanation: the order of Even and Odd numbers is : Even : 80 14 22 Odd : 17 99 27

Here 99 is the biggest number but 17 comes before it in the order of odd numbers so we have considered 80 first and then sequentially making the order of arrangement like − **80 17 99 27 14 22**

Since we have understood the problem, let's try to generate a solution for this. Here we can’t go for or classical descending order as a constraints about the sequence of Even and Odd is defined. So we will have to maintain this sequence and check ok the biggest of the first elements of the Even and Odd orders. and then go like that. Let’s see an algorithm that would make this more clear.

## Algorithm

Step 1 : Create two structures, one for even other for odd, this will maintain the sequence. Step 2 : Take one element from each structure and check which combination makes a large number. Example, if E is the even number and O is the odd number which are at the top of the structure. then we will check which one is Greater of EO and OE. Step 3 : Place the greater combination into the final sequence. Step 4 : Print the final sequence.

## Example

Now, let's create a program based on this algorithm.

#include <bits/stdc++.h> using namespace std; string merge(vector<string> arr1, vector<string> arr2) { int n1 = arr1.size(); int n2 = arr2.size(); int i = 0, j = 0; string big = ""; while (i < n1 && j < n2) { if ((arr1[i]+arr2[j]).compare((arr2[j]+arr1[i])) > 0) big += arr1[i++]; else big += arr2[j++]; } while (i < n1) big += arr1[i++]; while (j < n2) big += arr2[j++] ; return big; } string largestNumber(vector<string> arr, int n) { vector<string> even, odd; for (int i=0; i<n; i++) { int lastDigit = arr[i].at(arr[i].size() - 1) - '0'; if (lastDigit % 2 == 0) even.push_back(arr[i]); else odd.push_back(arr[i]); } string biggest = merge(even, odd); return biggest; } int main() { vector<string> arr; arr.push_back("17"); arr.push_back("80"); arr.push_back("99"); arr.push_back("27"); arr.push_back("14"); arr.push_back("22"); int n = arr.size(); cout<<"Biggest possible number from the array is = "<<largestNumber(arr, n); return 0; }

## Output

Biggest possible number from the array is = 801799271422

- Related Articles
- How many numbers can be formed by arranging the digits of the number 211 ?
- Arranging words in Ascending order in a string - JavaScript
- Arrange given numbers to form the biggest number?
- If sum of the number 985 and two other numbers obtained by arranging the digits of 985 in cyclic order is divided by 111, 22 and 37 respectively. Find the quotient in each case.
- How to order by certain part of a string in MySQL?
- MySQL query to order by the first number in a set of numbers?
- Set a certain value first with MySQL ORDER BY?
- Arranging words by their length in a sentence in JavaScript
- Order by number of chars in MySQL?
- How to find numbers that are divisible by a certain number for a range of values in R?
- Arranging Coins in C++
- MySQL order by string with numbers?
- Generate random numbers by giving certain mean and standard deviation in Excel
- ORDER BY alphabet first then follow by number in MySQL?
- Get records in a certain order using MySQL?