Question: Goldman Sachs OA | Bird Singing | Number Factors and Permutations | Summer Analyst Intern | SGSITS Indore | 2023
0
Entering edit mode

ADD COMMENTlink 2.2 years ago Delta 3.0k
Entering edit mode
0

// Solution for first problem

 

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 7 * 24 * 60 ;
int mini(string &st1) {
    int t1 = (st1[0] - '0') * 10 * 60 + (st1[1] - '0') * 60 + (st1[3] - '0') * 10 + (st1[4] - '0');
    return t1;
}
string d(int day) {
    switch (day) {
    case 0:
        return "Saturday";
    case 1:
        return "Sunday";
    case 2:
        return "Monday";
    case 3:
        return "Tuesday";
    case 4:
        return "Wednesday";
    case 5:
        return "Thursday";
    case 6:
        return "Friday";

    default:
        return "";
    }
}
void solve() {
    string st1, st2, it1, it2;
    cin >> st1 >> st2 >> it1 >> it2;

    int t1_start = mini(st1);
    int t2_start = mini(st2);
    int inv1 = mini(it1);
    int inv2 = mini(it2);

    // Case 1: They start at the same time.
    if (t1_start == t2_start) {
        cout << "Saturday" << endl;
        cout << st1 << endl;
        return;
    }

    // Case 2: They have the same interval but different start times. They will never meet.
    if (inv1 == inv2) {
        cout << "Never" << endl;
        return;
    }

    // Use these variables for the simulation
    int next_t1 = t1_start;
    int next_t2 = t2_start;

    // A safety limit to prevent infinite loops.
    // 200,000 is a safe, large number for this type of problem.
    int limit = 200000;
    while (limit--) {
        if (next_t1 == next_t2) {
            // Found the meeting time. Now format and print it.
            int total_minutes = next_t1;
            int week_minutes = 7 * 24 * 60;

            // The day of the week (0=Sat, 1=Sun, ...)
            int day_index = (total_minutes / (24 * 60)) % 7;
            cout << d(day_index) << endl;

            // The time within that day
            int minutes_in_day = total_minutes % (24 * 60);
            int h = minutes_in_day / 60;
            int m = minutes_in_day % 60;

            // Print with leading zeros if necessary
            if (h < 10) cout << "0";
            cout << h << ":";
            if (m < 10) cout << "0";
            cout << m << endl;

            return;
        }

        // Advance the time of the bird that sings earlier
        if (next_t1 < next_t2) {
            next_t1 += inv1;
        } else {
            next_t2 += inv2;
        }
    }

    // If the loop finishes without finding a match, they will never meet.
    cout << "Never" << endl;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("debug.txt", "w", stderr);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
 
ADD REPLYlink 5 days ago
Vishal NA
• 0

Login before adding your answer.

Similar Posts
Loading Similar Posts