Interview Questions

Generate Maximum HH:MM from a list of 4 integers.

For example: a=9, b=4, c=3, d=1, would yield 19:43

Approach:

Sort the array from greatest to least O(logN)

Each position will have conditions:

  • pos[0] can only be a value <= 2
  • if pos[0] == 2
    • pos[1] must be a value <= 1
  • pos[2] must be a value < 6
struct {
    bool operator()(int a, int b) {
         return a > b;   
    }
} greater;

vector<int> maxHH(vector<int> arr) {
    sort(arr.begin(), arr.end(), greater);

    if(arr[0] > 2) {
        bool found = false;
        if(arr[arr.size() - 1] <= 2) {
            swap(arr[0], arr[i];
            found = true;
        }
        if(!found) {
            return nullptr;
        }
    }

    if(arr[
}

results matching ""

    No results matching ""