
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Largest Time for Given Digits in C++
Suppose we have an array of 4 digits, we have to find the largest 24-hour time that can be made. We know that the smallest 24-hour time is 00:00, and the largest time is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight. We have to return the answer as a string of length 5. If there is no valid time to be returned, then return an empty string.
So, if the input is like [1,2,3,4], then the output will be "23:41"
To solve this, we will follow these steps −
- Define a function isValid(), this will take string a,
- if a[0] > '2', then −
- return false
- if a[0] is same as '2' and a[1] > '3', then −
- return false
- if a[3] > '5', then −
- return false
- return true
- Define a function dfs(), this will take an array A, res, cur,
- if size of cur is same as 5, then −
- if isValid(cur) and cur > res, then −
- res := cur
- return
- if isValid(cur) and cur > res, then −
- for initialize i := 0, when i < 4, update (increase i by 1), do −
- if A[i] is not equal to -1, then −
- tmp := A[i]
- cur := cur + A[i] + ASCII of '0'
- if size of cur is same as 2, then −
- cur := cur concatenate with ':'
- A[i] := -1
- dfs(A, res, cur)
- A[i] := tmp
- delete last element from cur
- if size of cur is same as 2, then −
- delete last element from cur
- if A[i] is not equal to -1, then −
- From the main method do the following −
- res := empty string, tmp := empty string
- dfs(A, res, tmp)
- return res
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: void dfs(vector<int>& A, string& res, string& cur) { if (cur.size() == 5) { if (isValid(cur) && cur > res) res = cur; return; } for (int i = 0; i < 4; ++i) { if (A[i] != -1) { int tmp = A[i]; cur += A[i] + '0'; if (cur.size() == 2) cur += ':'; A[i] = -1; dfs(A, res, cur); A[i] = tmp; cur.pop_back(); if (cur.size() == 2) cur.pop_back(); } } } bool isValid(const string a) { if (a[0] > '2') return false; if (a[0] == '2' && a[1] > '3') return false; if (a[3] > '5') return false; return true; } string largestTimeFromDigits(vector<int>& A) { string res = "", tmp = ""; dfs(A, res, tmp); return res; } }; main(){ Solution ob; vector<int> v = {1,2,3,4}; cout << (ob.largestTimeFromDigits(v)); }
Input
{1,2,3,4}
Output
23:41
- Related Articles
- Find the Largest number with given number of digits and sum of digits in C++
- Program to find nearest time by reusing same digits of given time in python
- Find the largest number that can be formed with the given digits in C++
- Largest product of contiguous digits in Python
- Largest number with prime digits in C++
- How to get Time in Milliseconds for the Given date and time in Java?
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Largest product of n contiguous digits of a number in JavaScript
- Find largest sum of digits in all divisors of n in C++
- Find the difference between the smallest number of 7 digits and the largest number of digits.
- Maximize the given number by replacing a segment of digits with the alternate digits given in C++
- Find the difference between the smallest number of 7 digits and the largest number of 4 digits.
- Find largest number smaller than N with same set of digits in C++
- Number of digits in the nth number made of given four digits in C++
- How to find the product of given digits by using for loop in C language?

Advertisements