Maximum possible time that can be formed from four digits | (Recursive Approach)
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 formatInput: 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 valid // 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; } |
Java
// Java implementation of the above approach import java.io.*; import java.util.*; class GFG { // Stores all the permutations public static ArrayList<String> res = new ArrayList<>(); // Function to generate valid permutations public static void getMax( int idx, int [] nums, String per) { // All the digits are used if (idx == nums.length) { // If first digit is // not 0, 1 or 2 then it is // invalid, return if (per.charAt( 0 ) != '0' && per.charAt( 0 ) != '1' && per.charAt( 0 ) != '2' ) return ; // Second digit must be less than 7 if (per.charAt( 2 ) >= '6' ) return ; // If first digit is 2 // then second digit must be // smaller than 5 if (per.charAt( 0 ) == '2' && per.charAt( 1 ) >= '4' ) return ; // Push the valid // permutation in result res.add(per); return ; } for ( int i = idx; i < nums.length; i++) { // add num to res per += String.valueOf(nums[i]); // swap and solve // for further permutation int temp = nums[idx]; nums[idx] = nums[i]; nums[i] = temp; // Recur getMax(idx + 1 , nums, per); // Backtrack per = per.substring( 0 , per.length() - 1 ); temp = nums[i]; nums[i] = nums[idx]; nums[idx] = temp; } } // Function to return maximum time // possible in 24-hour format public static String getMaxtime( int [] arr) { String per = "" ; getMax( 0 , arr, per); // No valid permutation // can be generated if (res.isEmpty()) return "" ; // Sort all valid permutations Collections.sort(res); // Largest valid permutation String ans = res.get(res.size() - 1 ); // Resultant String String result = ans.substring( 0 , 2 ) + ":" + ans.substring( 2 , 4 ); return result; } // Driver Code public static void main (String[] args) { int [] arr = { 1 , 2 , 3 , 4 }; int n = arr.length; System.out.println(getMaxtime(arr)); } } // This code is contributed by Shubham Singh |
Python3
# Python implementation of the above approach # Stores all the permutations res = []; # Function to generate valid permutations def getMax(idx, nums, per): # All the digits are used if (idx = = len (nums)) : # If first digit is # not 0, 1 or 2 then it is # invalid, return if (per[ 0 ] ! = '0' and per[ 0 ] ! = '1' and 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 valid # permutation in result res.append(per); return ; for i in range (idx, len (nums)): # add num to res per + = str (nums[i]) ; # swap and solve # for further permutation nums[idx], nums[i] = nums[i], nums[idx]; # Recur getMax(idx + 1 , nums, per); # Backtrack #per.pop(); per = per[: - 1 ]; nums[i], nums[idx] = nums[idx], nums[i]; # Function to return maximum time # possible in 24-hour format def getMaxtime(arr): per = ""; getMax( 0 , arr, per); # No valid permutation # can be generated if ( len (res) = = 0 ): return ""; # Sort all valid permutations res.sort(); # Largest valid permutation ans = res[ len (res) - 1 ]; # Resultant string result = ans[ 0 : 2 ] + ":" + ans[ 2 : 4 ]; return result; # Driver Code arr = [ 1 , 2 , 3 , 4 ]; n = len (arr); print (getMaxtime(arr)); |
C#
// C# code to delete middle of a stack using System; using System.Collections.Generic; class GFG { public static List< string > res = new List< string >(); public static void getMax( int idx, ref int [] nums, string per) { // all the digits are used if (idx == nums.Length) { // 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 musch 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' && per[1] >= '4' ) return ; // push the valid // permutation in result res.Add(per); return ; } for ( int i = idx; i<nums.Length; i++){ // add num to res per += nums[i].ToString(); // swap and solve for further permutation int temp = nums[idx]; nums[idx] = nums[i]; nums[i] = temp; // recur getMax(idx+1, ref nums, per); // backtrack per = per.Substring(0, per.Length-1); temp = nums[idx]; nums[idx] = nums[i]; nums[i] = temp; } } public static string getMaxtime( ref int [] arr){ string per = "" ; getMax(0, ref arr, per); // No Valid permutation // can be generated if (res.Count == 0){ return "" ; } // sort all valid permutations res.Sort(); // largest valid permutation string ans = res[res.Count - 1]; // resultant string string result = ans.Substring(0,2) + ":" + ans.Substring(2,2); return result; } // driver code to test above function public static void Main(){ int [] arr = { 1, 2, 3, 4 }; int n = arr.Length; Console.WriteLine(getMaxtime( ref arr)); } } // THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002) |
Javascript
// Javascript implementation of the above approach // Stores all the permutations let res = [] // Function to generate valid permutations function getMax(idx, nums, per) { // All the digits are used if (idx ==nums.length) { // 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' && per[1] >= '4' ) { return ; } // Push the valid // permutation in result res.push(per); return ; } for (let i = idx; i < nums.length; i++) { // add num to res per += nums[i].toString(); // swap and solve // for further permutation [nums[idx], nums[i]] = [nums[i], nums[idx]] // Recur getMax(idx + 1, nums, per); // Backtrack per = per.substring(0, per.length - 1); [nums[i], nums[idx]] = [nums[idx], nums[i]] } } // Function to return maximum time // possible in 24-hour format function getMaxtime(arr) { let per = "" ; getMax(0, arr, per); // No valid permutation // can be generated if (res.length === 0) { return "" ; } // Sort all valid permutations res.sort(); // Largest valid permutation let ans = res[res.length - 1]; // Resultant string let result = ans.substring(0, 2) + ":" + ans.substring(2, 4); return result; } // Driver Code let arr = [1, 2, 3, 4]; document.write(getMaxtime(arr)); |
23:41
Time Complexity: O(N! * log(N!))
Auxiliary Space: O(N)
Please Login to comment...