- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Arrange given numbers to form the biggest number?
Here we will see how to generate the biggest number by rearranging the given numbers. Suppose there are {45, 74, 23} is given, the program will find the largest number, that is 744523. So each digit will not be arranged. but the whole number will be placed to make largest number.
To solve this problem, we will use the string sorting. But the comparison logic is different. The comparing function will take two numbers a and b, then concatenate them to form ab and ba. Among them which one is bigger, that is considered.
Algorithm
compareStrings(a, b)
begin ab := concatenate b with a ba := concatenate a with b compare ba with ab, then return 1 if ba is bigger, otherwise return 0 end getLargest(arr): begin sort the arr with the comparison logic using compareString() for each string s in arr, do print s done end
Example
#include<iostream> #include <string> #include &t;vector> #include <algorithm> using namespace std; int stringCompare(string a, string b) { string ab = a.append(b); string ba = b.append(a); return ab.compare(ba) > 0 ? 1: 0; } void getLargest(vector<string> arr) { sort(arr.begin(), arr.end(), stringCompare); //sort the array for (int i =0; i < arr.size() ; i++ ) cout << arr[i]; } int main() { vector<string> arr; arr.push_back("45"); arr.push_back("74"); arr.push_back("23"); getLargest(arr); }
Output
744523
- Related Articles
- Biggest number by arranging numbers in certain order in C++
- Write the numbers given in the following place value table in decimal form."
- Arrange the sentences given below in order to describe the process of fertilization.
- How to find the biggest number in an array around undefined elements? - JavaScript
- Python - Find the number of prime numbers within a given range of numbers
- Arrange the given rational number in ascending order.( frac{4}{-9}, frac{-5}{12} ) and ( frac{2}{-3} )
- Arrange the following numbers in ascending order:$9801, 25751, 36501, 38802$
- Four numbers are inserted between the numbers 4 and 39 such that an AP. results. Find the biggest of these four numbers.
- Program to find the minimum cost to arrange the numbers in ascending or descending order in Python
- In the boxes of column I the letters of some words got jumbled. Arrange them in proper form in the boxes given in column II.
- Rotate number to form the maximum number using JavaScript
- Which is the biggest number by which 6 , 9 and 12 divisible
- Finding closest pair sum of numbers to a given number in JavaScript
- Average of odd numbers till a given odd number?
- Average of even numbers till a given even number?

Advertisements