03(March) Largest Number formed from an Array
03. Largest Number formed from an Array
The problem can be found at the following link: Question Link
My Approach
To achieve our objective of forming the largest possible number by concatenating strings from an array, we follow these steps:
Define a Custom Comparison Function: We create a function, let's call it
compareStrings
, which compares two strings (a
andb
) based on their concatenation in different orders (a + b
versusb + a
). This function determines which combination results in a larger number.Sorting the Array: Using the
sort
function, we arrange the strings in the array in descending order. This sorting is done based on our custom comparison functioncompareStrings
. Consequently, the array is ordered in a way that ensures when the strings are concatenated, they form the largest possible number.Concatenation: After sorting, we simply concatenate the strings in the sorted array to form the final result.
Return Output: The concatenated string is then returned as the output of our process.
Time and Auxiliary Space Complexity
Time Complexity:
O(NlogN)
, where N is the number of strings in the array.Auxiliary Space Complexity:
O(N)
, where N is the number of strings in the array.
Code (C++)
class Solution {
public:
static bool comp(string a, string b){
return a + b > b + a;
}
string printLargest(int n, vector<string> &arr) {
sort(arr.begin(), arr.end(), comp);
string ans;
for(string i : arr)
ans += i;
return ans;
}
};
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated