 Open in App
Not now

# Biggest number by arranging numbers in certain order

• Difficulty Level : Easy
• Last Updated : 22 Dec, 2022

Given an array of n numbers. Arrange them in a way that yields the largest value. While arranging the order of even numbers with respect to each other and the order of odd numbers with respect to each other should be maintained respectively.

Examples:

```Input : {78, 81, 88, 79, 117, 56}
Output : 8179788856117
The numbers are arranged in the order:
81 79 78 88 56 117 and then
concatenated.
The odd numbers 81 79 117 and
The even numbers 78 88 56 maintain
their orders as in the original array.

Input : {400, 99, 76, 331, 65, 18}
Output : 99400763316518```

This problem is a variation to the problem Arrange the given numbers to form the biggest number . In this problem, we find the biggest number with certain restrictions which is that the order of odd and even numbers needs to be maintained in the final result and thus aims to have a solution of O(n) time complexity.

The following are steps to find the biggest number maintaining order of odd and even numbers.

1. Divide the numbers of original array into 2 arrays even[] and odd[]. While dividing the order of numbers should be maintained.
2. Merge even and odd arrays and while merging follow the condition. Let X be an element of one array and Y be an element of another array. Compare XY(Y appended to X) and YX(X appended to Y). If XY is larger then add X to final result else add Y to final result.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to form the biggest number``// by arranging numbers in certain order``#include ``using` `namespace` `std;` `// function to merge the even and odd list``// to form the biggest number``string merge(vector arr1, vector arr2)``{``    ``int` `n1 = arr1.size();``    ``int` `n2 = arr2.size();``    ``int` `i = 0, j = 0;` `    ``// to store the final biggest number``    ``string big = ``""``;` `    ``while` `(i < n1 && j < n2)``    ``{``        ``// if true then add arr1[i] to big``        ``if` `((arr1[i]+arr2[j]).compare((arr2[j]+arr1[i])) > 0)``            ``big += arr1[i++];` `        ``// else add arr2[j] to big``        ``else``            ``big += arr2[j++];``    ``}` `    ``// add remaining elements``    ``// of arr1 to big``    ``while` `(i < n1)``        ``big += arr1[i++];` `    ``// add remaining elements``    ``// of arr2 to big``    ``while` `(j < n2)``        ``big += arr2[j++] ;` `    ``return` `big;``}` `// function to find the biggest number``string printLargest(vector arr, ``int` `n)``{``    ``vector even, odd;` `    ``for` `(``int` `i=0; i arr;``    ``arr.push_back(``"78"``);``    ``arr.push_back(``"81"``);``    ``arr.push_back(``"88"``);``    ``arr.push_back(``"79"``);``    ``arr.push_back(``"117"``);``    ``arr.push_back(``"56"``);` `    ``int` `n = arr.size();``    ``cout << ``"Biggest number = "``         ``<< printLargest(arr, n);``    ``return` `0;``}`

## Java

 `import` `java.util.Vector;` `// Java implementation to form the biggest number``// by arranging numbers in certain order``class` `GFG``{` `    ``// function to merge the even and odd list``    ``// to form the biggest number``    ``static` `String merge(Vector arr1,``                        ``Vector arr2)``    ``{``        ``int` `n1 = arr1.size();``        ``int` `n2 = arr2.size();``        ``int` `i = ``0``, j = ``0``;` `        ``// to store the final biggest number``        ``String big = ``""``;` `        ``while` `(i < n1 && j < n2)``        ``{``            ` `            ``// if true then add arr1[i] to big``            ``if` `((arr1.get(i) + arr2.get(j)).``                    ``compareTo((arr2.get(j) + arr1.get(i))) > ``0``)``            ``{``                ``big += arr1.get(i++);``            ``}``            ` `            ``// else add arr2[j] to big``            ``else``            ``{``                ``big += arr2.get(j++);``            ``}``        ``}` `        ``// add remaining elements``        ``// of arr1 to big``        ``while` `(i < n1)``        ``{``            ``big += arr1.get(i++);``        ``}` `        ``// add remaining elements``        ``// of arr2 to big``        ``while` `(j < n2)``        ``{``            ``big += arr2.get(j++);``        ``}``        ``return` `big;``    ``}` `    ``// function to find the biggest number``    ``static` `String printLargest(Vector arr, ``int` `n)``    ``{``        ``Vector even = ``new` `Vector(),``                        ``odd = ``new` `Vector();` `        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``int` `lastDigit = arr.get(i).``                ``charAt(arr.get(i).length() - ``1``) - ``'0'``;` `            ``// inserting even numbers``            ``if` `(lastDigit % ``2` `== ``0``)``            ``{``                ``even.add(arr.get(i));``            ``}``            ` `            ``// inserting odd numbers``            ``else``            ``{``                ``odd.add(arr.get(i));``            ``}``        ``}` `        ``// merging both the array``        ``String biggest = merge(even, odd);` `        ``// final required biggest number``        ``return` `biggest;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``Vector arr = ``new` `Vector();``        ``arr.add(``"78"``);``        ``arr.add(``"81"``);``        ``arr.add(``"88"``);``        ``arr.add(``"79"``);``        ``arr.add(``"117"``);``        ``arr.add(``"56"``);` `        ``int` `n = arr.size();``        ``System.out.println(``"Biggest number = "` `+``                            ``printLargest(arr, n));``    ``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 implementation to form the biggest number``# by arranging numbers in certain order` `# function to merge the even and odd list``# to form the biggest number``def` `merge(arr1, arr2):` `    ``n1 ``=` `len``(arr1)``    ``n2 ``=` `len``(arr2)` `    ``i, j ``=` `0``, ``0` `    ``# to store the final biggest number``    ``big ``=` `""` `    ``while` `(i < n1 ``and` `j < n2):` `        ``# if true then add arr1[i] to big``        ``if` `(``int``(arr1[i]``+``arr2[j]) ``-` `(``int``(arr2[j]``+``arr1[i])) > ``0``):``            ``big ``+``=` `arr1[i]``            ``i ``+``=` `1` `        ``# else add arr2[j] to big``        ``else``:``            ``big ``+``=` `arr2[j]``            ``j ``+``=` `1` `    ``# add remaining elements``    ``# of arr1 to big``    ``while` `(i < n1):``        ``big ``+``=` `arr1[i]``        ``i ``+``=` `1` `    ``# add remaining elements``    ``# of arr2 to big``    ``while` `(j < n2):``        ``big ``+``=` `arr2[j]``        ``j ``+``=` `1` `    ``return` `big`  `# function to find the biggest number``def` `printLargest(arr, n):` `    ``even ``=` `[]``    ``odd ``=` `[]` `    ``for` `i ``in` `range``(n):``        ``lastDigit ``=` `int``(arr[i][``-``1``])` `        ``# inserting even numbers``        ``if` `(lastDigit ``%` `2` `=``=` `0``):``            ``even.append(arr[i])` `        ``# inserting odd numbers``        ``else``:``            ``odd.append(arr[i])` `    ``# merging both the array``    ``biggest ``=` `merge(even, odd)` `    ``# final required biggest number``    ``return` `biggest` `# Driver program to test above``arr ``=` `[``'78'``, ``'81'``, ``'88'``, ``'79'``, ``'117'``, ``'56'``]` `n ``=` `len``(arr)``print``(``"Biggest number ="``, printLargest(arr, n))` `# This code is contributed by phasing17`

## C#

 `// C# implementation to form the biggest number``// by arranging numbers in certain order``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `    ``// function to merge the even and odd list``    ``// to form the biggest number``    ``static` `String merge(List arr1,``                        ``List arr2)``    ``{``        ``int` `n1 = arr1.Count;``        ``int` `n2 = arr2.Count;``        ``int` `i = 0, j = 0;` `        ``// to store the final biggest number``        ``String big = ``""``;` `        ``while` `(i < n1 && j < n2)``        ``{``            ` `            ``// if true then Add arr1[i] to big``            ``if` `((arr1[i] + arr2[j]).CompareTo((arr2[j] +``                                               ``arr1[i])) > 0)``            ``{``                ``big += arr1[i++];``            ``}``            ` `            ``// else Add arr2[j] to big``            ``else``            ``{``                ``big += arr2[j++];``            ``}``        ``}` `        ``// Add remaining elements``        ``// of arr1 to big``        ``while` `(i < n1)``        ``{``            ``big += arr1[i++];``        ``}` `        ``// Add remaining elements``        ``// of arr2 to big``        ``while` `(j < n2)``        ``{``            ``big += arr2[j++];``        ``}``        ``return` `big;``    ``}` `    ``// function to find the biggest number``    ``static` `String printLargest(List arr, ``int` `n)``    ``{``        ``List even = ``new` `List(),``                     ``odd = ``new` `List();` `        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``int` `lastDigit = arr[i][arr[i].Length - 1] - ``'0'``;` `            ``// inserting even numbers``            ``if` `(lastDigit % 2 == 0)``            ``{``                ``even.Add(arr[i]);``            ``}``            ` `            ``// inserting odd numbers``            ``else``            ``{``                ``odd.Add(arr[i]);``            ``}``        ``}` `        ``// merging both the array``        ``String biggest = merge(even, odd);` `        ``// final required biggest number``        ``return` `biggest;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``List arr = ``new` `List();``        ``arr.Add(``"78"``);``        ``arr.Add(``"81"``);``        ``arr.Add(``"88"``);``        ``arr.Add(``"79"``);``        ``arr.Add(``"117"``);``        ``arr.Add(``"56"``);` `        ``int` `n = arr.Count;``        ``Console.WriteLine(``"Biggest number = "` `+``                           ``printLargest(arr, n));``    ``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output

`Biggest number = 8179788856117`

Time Complexity: O(n)
Auxiliary Space: O(n)

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.