Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximum possible time that can be formed from four digits | (Recursive Approach)

  • Last Updated : 12 Dec, 2021

Given an array arr[] with 4 integers. The task is to return the maximum 24 hour time that can be formed using the digits from the array. Note that the minimum time in 24-hour format is 00:00, and the maximum is 23:59. If a valid time cannot be formed then return an empty string.
Examples:

Input: arr[] = {1, 2, 3, 4}  
Output: 23:41
Explanation: 23:41 is the maximum time possible in 24-hour format

Input: arr[] = {5, 5, 6, 6}  
Output: “”
Explanation: No valid time can be formed from the given digits

HashMap Approach:- One approach to this problem is already discussed  https://www.geeksforgeeks.org/maximum-possible-time-that-can-be-formed-from-four-digits/ 

Recursive Approach: The task can also be solved using recursion. Try generating all possible permutations using recursion and then discard the invalid permutation and return the permutation with the largest possible value, after sorting all the permutations. If no valid permutation exists, return an empty string. Condition for the valid permutation.

  • The first digit of hours must be from the range [0, 2].
  • The second digit of hours must be from the range [0, 3] if the first digit was chosen as 2 else [0, 9].
  • The first digit of minutes must be from the range [0, 5] and the second digit of minutes must be from the range [0, 9]

Below is the implementation of the above code:

C++




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Stores all the permutations
vector<string> res;
 
// Function to generate valid permutations
void getMax(int idx, vector<int>& nums, string per)
{
    // All the digits are used
    if (idx == nums.size()) {
 
        // If first digit is
        // not 0, 1 or 2 then it is
        // invalid, return
        if (per[0] != '0'
            && per[0] != '1'
            && per[0] != '2')
            return;
 
        // Second digit must be less than 7
        if (per[2] >= '6')
            return;
 
        // If first digit is 2
        // then second digit must be
        // smaller than 5
        if (per[0] == '2'
            and per[1] >= '4')
            return;
 
        // Push the vali
        // d permutation in result
        res.push_back(per);
        return;
    }
 
    for (int i = idx; i < nums.size(); i++) {
 
        // add num to res
        per += nums[i] + '0';
 
        // swap and solve
        // for further permutation
        swap(nums[idx], nums[i]);
 
        // Recur
        getMax(idx + 1, nums, per);
 
        // Backtrack
        per.pop_back();
 
        swap(nums[i], nums[idx]);
    }
}
 
// Function to return maximum time
// possible in 24-hour format
string getMaxtime(vector<int>& arr)
{
    string per = "";
    getMax(0, arr, per);
 
    // No valid permutation
    // can be generated
    if (res.empty())
        return "";
 
    // Sort all valid permutations
    sort(res.begin(), res.end());
 
    // Largest valid permutation
    string ans = res[res.size() - 1];
 
    // Resultant string
    string result
        = ans.substr(0, 2)
          + ":"
          + ans.substr(2, 2);
    return result;
}
 
// Driver Code
int main()
{
    vector<int> arr = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(int);
    cout << (getMaxtime(arr));
    return 0;
}
Output
23:41

Time Complexity: O(N! * log(N!)) 
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!