forked from DSC-Banasthali-Vidyapith/Engineering-Daze
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest_number_formed_from_an_array.cpp
More file actions
99 lines (77 loc) · 1.72 KB
/
Copy pathlargest_number_formed_from_an_array.cpp
File metadata and controls
99 lines (77 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// Making a custom comparator function using struct
// Function will compare any two numbers, say A and B
// Function will accept A and B as integers and combine them in two ways- AB and BA
// if AB > BA then AB is returned, else BA
struct {
bool operator()(int x, int y) const
{
string value1 = to_string(x) + to_string(y);
string value2 = to_string(y) + to_string(x);
return value1 > value2;
}
} compare;
// Function to append and return the result
// Function takes in all the numbers as a vector array
// sort function sorts the array according to the comparator function
// Appends the elements in the sorted vector array to a string and returns the result
string LargestNumber(vector<int> &numberArray)
{
sort(numberArray.begin(), numberArray.end(), compare);
string result;
for (int &i: numberArray) {
result += to_string(i);
}
return result;
}
// Main function
int main()
{
// Declaring and taking in for number of inputs
int n;
cin >> n;
// Declaring the vector array
vector<int> numberArray(n);
// For loop to take in the inputs one by one and adding to the vector array
for(int i=0; i<n; i++) {
cin>>numberArray[i];
}
// Printing out the answer
cout << LargestNumber(numberArray) << endl;
return 0;
}
/*
Test Cases:-
a)Input:-
10
22
33
44
55
66
77
88
99
11
0
Output:-
9988776655443322110
b)Input:-
6
3
45
21
112
369
86
Output:-
8645369321112
*/
/*
Time complexity - O(n(log(n)))
Space complexity - n, where n is the size of the array.
*/