We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Function to convert HH:MM:SS to total seconds
int timeToSeconds(const string& time) {
int h = stoi(time.substr(0, 2));
int m = stoi(time.substr(3, 2));
int s = stoi(time.substr(6, 2));
return h * 3600 + m * 60 + s;
}
// Function to convert seconds to HH:MM:SS
string secondsToTime(int seconds) {
int h = seconds / 3600;
seconds %= 3600;
int m = seconds / 60;
int s = seconds % 60;
ostringstream oss;
oss << setw(2) << setfill('0') << h << ":"
<< setw(2) << setfill('0') << m << ":"
<< setw(2) << setfill('0') << s;
return oss.str();
}
int main() {
int n;
cin >> n;
vector times(n);
vector> converted; // (seconds, original_string)
for (int i = 0; i < n; ++i) {
cin >> times[i];
converted.push_back({timeToSeconds(times[i]), times[i]});
}
// Sort by seconds
sort(converted.begin(), converted.end());
int min_diff = INT_MAX;
vector<pair<string, string>> result;
for (int i = 1; i < n; ++i) {
int diff = converted[i].first - converted[i - 1].first;
if (diff < min_diff) {
min_diff = diff;
result.clear();
result.push_back({converted[i - 1].second, converted[i].second});
} else if (diff == min_diff) {
result.push_back({converted[i - 1].second, converted[i].second});
}
}
// Print results
for (auto& interval : result) {
cout << interval.first << "->" << interval.second << endl;
}
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
A Very Big Sum
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
using namespace std;
// Function to convert HH:MM:SS to total seconds int timeToSeconds(const string& time) { int h = stoi(time.substr(0, 2)); int m = stoi(time.substr(3, 2)); int s = stoi(time.substr(6, 2)); return h * 3600 + m * 60 + s; }
// Function to convert seconds to HH:MM:SS string secondsToTime(int seconds) { int h = seconds / 3600; seconds %= 3600; int m = seconds / 60; int s = seconds % 60; ostringstream oss; oss << setw(2) << setfill('0') << h << ":" << setw(2) << setfill('0') << m << ":" << setw(2) << setfill('0') << s; return oss.str(); }
int main() { int n; cin >> n; vector times(n); vector> converted; // (seconds, original_string)
}