CSES Problem Set Factory Machines Solution



 Problem Link:  https://cses.fi/problemset/task/1620

Solution in C++:

/// Author : AH_Tonmoy
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int low = 0, high = 1e18;
int ans = 0, cnt = 0;
while (low <= high) {
int mid = (high + low) / 2;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += (mid / a[i]);
if (sum >= k) break;
}
if (sum >= k) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
cout << ans << '\n';
return 0;
}

No comments

Most View Post

Recent post

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.