Today is Sagar's birthday. He got an array of numbers as a gift from his father. His father told him that he has another gift if Sagar is able to make the largest number by concatenating the numbers and swapping the digits of numbers. Help Sagar to make the largest number from the given numbers.
Example:
Input: n = 5, a = {1, 2, 20, 31, 5}
Output: 5322110
Approach
C++
#include <bits/stdc++.h>using namespace std;string gift(int n, int a[]){vector<int> v;for (int i = 0; i < n; i++){int j = a[i];if (j == 0)v.push_back(0);while (j){v.push_back(j % 10);j = j / 10;}}string str = "";sort(v.begin(), v.end());for (int i = v.size() - 1; i >= 0; i--)str += to_string(v[i]);return str;}int main(){int n = 5;int a[n] = {1, 2, 20, 31, 5};cout << gift(n, a) << "\n";return 0;}
No comments:
Post a Comment