Maximum possible time that can be formed from four digits | (Recursive Approach)
Last Updated :
14 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
vector<string> res;
void getMax( int idx, vector< int >& nums, string per)
{
if (idx == nums.size()) {
if (per[0] != '0'
&& per[0] != '1'
&& per[0] != '2' )
return ;
if (per[2] >= '6' )
return ;
if (per[0] == '2'
and per[1] >= '4' )
return ;
res.push_back(per);
return ;
}
for ( int i = idx; i < nums.size(); i++) {
per += nums[i] + '0' ;
swap(nums[idx], nums[i]);
getMax(idx + 1, nums, per);
per.pop_back();
swap(nums[i], nums[idx]);
}
}
string getMaxtime(vector< int >& arr)
{
string per = "" ;
getMax(0, arr, per);
if (res.empty())
return "" ;
sort(res.begin(), res.end());
string ans = res[res.size() - 1];
string result
= ans.substr(0, 2)
+ ":"
+ ans.substr(2, 2);
return result;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4 };
int n = sizeof (arr) / sizeof ( int );
cout << (getMaxtime(arr));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static ArrayList<String> res = new ArrayList<>();
public static void getMax( int idx, int [] nums, String per)
{
if (idx == nums.length) {
if (per.charAt( 0 ) != '0' &&
per.charAt( 0 ) != '1' &&
per.charAt( 0 ) != '2' )
return ;
if (per.charAt( 2 ) >= '6' )
return ;
if (per.charAt( 0 ) == '2' && per.charAt( 1 ) >= '4' )
return ;
res.add(per);
return ;
}
for ( int i = idx; i < nums.length; i++) {
per += String.valueOf(nums[i]);
int temp = nums[idx];
nums[idx] = nums[i];
nums[i] = temp;
getMax(idx + 1 , nums, per);
per = per.substring( 0 , per.length() - 1 );
temp = nums[i];
nums[i] = nums[idx];
nums[idx] = temp;
}
}
public static String getMaxtime( int [] arr)
{
String per = "" ;
getMax( 0 , arr, per);
if (res.isEmpty())
return "" ;
Collections.sort(res);
String ans = res.get(res.size() - 1 );
String result
= ans.substring( 0 , 2 )
+ ":"
+ ans.substring( 2 , 4 );
return result;
}
public static void main (String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 };
int n = arr.length;
System.out.println(getMaxtime(arr));
}
}
|
Python3
res = [];
def getMax(idx, nums, per):
if (idx = = len (nums)) :
if (per[ 0 ] ! = '0' and per[ 0 ] ! = '1' and per[ 0 ] ! = '2' ):
return ;
if (per[ 2 ] > = '6' ):
return ;
if (per[ 0 ] = = '2' and per[ 1 ] > = '4' ):
return ;
res.append(per);
return ;
for i in range (idx, len (nums)):
per + = str (nums[i]) ;
nums[idx], nums[i] = nums[i], nums[idx];
getMax(idx + 1 , nums, per);
per = per[: - 1 ];
nums[i], nums[idx] = nums[idx], nums[i];
def getMaxtime(arr):
per = "";
getMax( 0 , arr, per);
if ( len (res) = = 0 ):
return "";
res.sort();
ans = res[ len (res) - 1 ];
result = ans[ 0 : 2 ] + ":" + ans[ 2 : 4 ];
return result;
arr = [ 1 , 2 , 3 , 4 ];
n = len (arr);
print (getMaxtime(arr));
|
C#
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)
{
if (idx == nums.Length)
{
if (per[0] != '0' && per[0] != '1' && per[0] != '2' ) return ;
if (per[2] >= '6' ) return ;
if (per[0] == '2' && per[1] >= '4' ) return ;
res.Add(per);
return ;
}
for ( int i = idx; i<nums.Length; i++){
per += nums[i].ToString();
int temp = nums[idx];
nums[idx] = nums[i];
nums[i] = temp;
getMax(idx+1, ref nums, per);
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);
if (res.Count == 0){
return "" ;
}
res.Sort();
string ans = res[res.Count - 1];
string result = ans.Substring(0,2) + ":" + ans.Substring(2,2);
return result;
}
public static void Main(){
int [] arr = { 1, 2, 3, 4 };
int n = arr.Length;
Console.WriteLine(getMaxtime( ref arr));
}
}
|
Javascript
let res = []
function getMax(idx, nums, per) {
if (idx ==nums.length) {
if (per[0] != '0' && per[0] != '1' && per[0] != '2' ) {
return ;
}
if (per[2] >= '6' ) {
return ;
}
if (per[0] === '2' && per[1] >= '4' ) {
return ;
}
res.push(per);
return ;
}
for (let i = idx; i < nums.length; i++) {
per += nums[i].toString();
[nums[idx], nums[i]] = [nums[i], nums[idx]]
getMax(idx + 1, nums, per);
per = per.substring(0, per.length - 1);
[nums[i], nums[idx]] = [nums[idx], nums[i]]
}
}
function getMaxtime(arr) {
let per = "" ;
getMax(0, arr, per);
if (res.length === 0) {
return "" ;
}
res.sort();
let ans = res[res.length - 1];
let result = ans.substring(0, 2) + ":" + ans.substring(2, 4);
return result;
}
let arr = [1, 2, 3, 4];
document.write(getMaxtime(arr));
|
Time Complexity: O(N! * log(N!))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...