Computer Science, asked by amitcool8352, 1 year ago

Write a program to arrange the number such that they form largest number

Answers

Answered by swapnil16
0
In c++ Language:-

// Given an array of numbers, program to arrange the numbers to form the

// largest number

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;

 

// A comparison function which is used by sort() in printLargest()

int myCompare(string X, string Y)

{

    // first append Y at the end of X

    string XY = X.append(Y);

 

    // then append X at the end of Y

    string YX = Y.append(X);

 

    // Now see which of the two formed numbers is greater

    return XY.compare(YX) > 0 ? 1: 0;

}

 

// The main function that prints the arrangement with the largest value.

// The function accepts a vector of strings

void printLargest(vector<string> arr)

{

    // Sort the numbers using library sort funtion. The function uses

    // our comparison function myCompare() to compare two strings.

    // See http://www.cplusplus.com/reference/algorithm/sort/ for details

    sort(arr.begin(), arr.end(), myCompare);

 

    for (int i=0; i < arr.size() ; i++ )

        cout << arr[i];

}

 

// driverr program to test above functions

int main()

{

    vector<string> arr;

 

    //output should be 6054854654

    arr.push_back("54");

    arr.push_back("546");

    arr.push_back("548");

    arr.push_back("60");

    printLargest(arr);

 

    // output should be 777776

    /*arr.push_back("7");

    arr.push_back("776");

    arr.push_back("7");


In JAVA language:-

// Given an array of numbers, program to

// arrange the numbers to form the

// largest number

import java.util.*;

 

class GFG {

 

    // The main function that prints the

    // arrangement with the largest value.

    // The function accepts a vector of strings   

    static void printLargest(Vector<String> arr){

     

        Collections.sort(arr, new Comparator<String>(){

 

        // A comparison function which is used by

        // sort() in printLargest()

        @Override

        public int compare(String X, String Y) {

         

        // first append Y at the end of X

        String XY=X + Y;

         

        // then append X at the end of Y

        String YX=Y + X;

         

        // Now see which of the two formed numbers

        // is greater

        return XY.compareTo(YX) > 0 ? -1:1;

    }

    });

         

    Iterator it = arr.iterator();

 

    while(it.hasNext())

        System.out.print(it.next());

     

    }

     

    // driver program

    public static void main (String[] args) {

         

        Vector<String> arr;

        arr = new Vector<>();

         

        //output should be 6054854654

        

In PYTHON3 language:-


# Python Program to get the maximum

# possible integer from given array

# of integers...

 

# custom comparator to sort according

# to the ab, ba as mentioned in description

def comparator(a, b):

    ab = str(a)+str(b)

    ba = str(b)+str(a)

    return cmp(int(ba), int(ab))

 

# driver code

a = [54, 546, 548, 60,]

sorted_array = sorted(a, cmp=comparator)

number = "".join([str(i) for i in sorted_array])

print(number)




Similar questions