πŸ“ Problem Details

πŸ’­What Were My Initial Thoughts?

- the constraints are a assistive factor to this solving this problem 
- num can only be 4 digits

- convert the integer into a string 
- sort the string - create two separate strings num1 num2 
- insert the first and third element into num1 
- insert the 2nd and 4th element into num2 return the sum of both as integers

βŒ› Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(k) where k is the number of digits in the number (4)
could be considerd constant but we are still using additional auxillary space 

πŸ’» Implementation of Solution

class Solution {
public:
    int minimumSum(int num) {
        // Convert the number to a string
        string numStr = to_string(num);
        
        // Sort the string to arrange digits in ascending order
        sort(numStr.begin(), numStr.end());
        
        // Form two numbers by combining alternate digits
        string num1 = "";
        string num2 = "";
        num1 += numStr[0];
        num1 += numStr[2];
        num2 += numStr[1];
        num2 += numStr[3];
        
        // Convert the strings back to integers and return their sum
        return stoi(num1) + stoi(num2);
    }
};