
- 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
Maximum possible time that can be formed from four digits in C++
In this tutorial, we will be discussing a program to find maximum possible time that can be formed from four digits.
For this we will be provided with an array consisting 4 digits. Our task is to find the maximum time (24 hour format) that can formed using those four digits.
Example
#include <bits/stdc++.h> using namespace std; //returning updated frequency map map<int, int> getFrequencyMap(int arr[], int n) { map<int, int> hashMap; for (int i = 0; i < n; i++) { hashMap[arr[i]]++; } return hashMap; } //checking if the digit is present in frequency map bool hasDigit(map<int, int>* hashMap, int digit) { if ((*hashMap)[digit]) { (*hashMap)[digit]--; return true; } return false; } //returning maximum time in 24 hour format string getMaxtime_value(int arr[], int n) { map<int, int> hashMap = getFrequencyMap(arr, n); int i; bool flag; string time_value = ""; flag = false; for (i = 2; i >= 0; i--) { if (hasDigit(&hashMap, i)) { flag = true; time_value += (char)i + 48; break; } } if (!flag) return "-1"; flag = false; if (time_value[0] == '2') { for (i = 3; i >= 0; i--) { if (hasDigit(&hashMap, i)) { flag = true; time_value += (char)i + 48; break; } } } else { for (i = 9; i >= 0; i--) { if (hasDigit(&hashMap, i)) { flag = true; time_value += (char)i + 48; break; } } } if (!flag) return "-1"; time_value += ":"; flag = false; for (i = 5; i >= 0; i--) { if (hasDigit(&hashMap, i)) { flag = true; time_value += (char)i + 48; break; } } if (!flag) return "-1"; flag = false; for (i = 9; i >= 0; i--) { if (hasDigit(&hashMap, i)) { flag = true; time_value += (char)i + 48; break; } } if (!flag) return "-1"; return time_value; } int main() { int arr[] = { 0, 0, 0, 9 }; int n = sizeof(arr) / sizeof(int); cout << (getMaxtime_value(arr, n)); return 0; }
Output
09:00
- Related Articles
- Find maximum number that can be formed using digits of a given number in C++
- All possible strings of any length that can be formed from a given string?
- Find the largest number that can be formed with the given digits in C++
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Maximum length cycle that can be formed by joining two nodes of a binary tree in C++
- Python - Find the latest valid time that can be obtained by replacing the unknown/hidden digits
- Recursively print all sentences that can be formed from list of word lists in C++
- Program to find length of longest word that can be formed from given letters in python
- Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- How many numbers can be formed by arranging the digits of the number 211 ?
- maximum length of continuous silk thread that can be obtained from a cocoon."\n
- Maximum money that can be withdrawn in two steps in C
- Count of alphabets whose ASCII values can be formed with the digits of N in C++

Advertisements