- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

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.

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.

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; }

Biggest possible number from the array is = 801799271422

- Related Questions & Answers
- Arranging Coins in C++
- Arranging words in Ascending order in a string - JavaScript
- Arrange given numbers to form the biggest number?
- Set a certain value first with MySQL ORDER BY?
- How to order by certain part of a string in MySQL?
- MySQL order by string with numbers?
- MySQL query to order by the first number in a set of numbers?
- Order by number of chars in MySQL?
- Arranging words by their length in a sentence in JavaScript
- C++ Program to Swap Numbers in Cyclic Order Using Call by Reference
- ORDER BY alphabet first then follow by number in MySQL?
- Order by on a specific number in MySQL?
- Count n digit numbers divisible by given number in C++
- Get records in a certain order using MySQL?
- Alphanumeric Order by in MySQL for strings mixed with numbers

Advertisements