Gojek Interview Experience for Internship (On-Campus) 2022
This problem was asked in the OA rounds of Gojek during my On-campus internship in 2022. The problem statement goes as follows:
- There are n processes that need to be assigned to m processors such that the kth processor should have the maximum number of processes (the kth processor is the most efficient one). Also, each processor must be assigned at least one process and the number of processes assigned to the adjacent processors must not exceed 1. You need to find the maximum number of processes that can be assigned to the kth processor following all the given constraints.
This problem is not available anywhere over the internet, so when I solved it, I decided to contribute it here so that others can refer to it and get a glimpse of what kind of questions are asked in the OAs.
At first, this question looked a bit tricky, but after giving it some time, I got the binary search approach to solve the problem.
The C++ code is as follows:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
bool isPossible(ll x, ll n, ll m, ll k){
ll tot = x;
ll temp = x-1;
ll ind = k-1;
while (ind >= 0){
if (temp < 1){
tot += 1;
ind--;
continue ;
}
tot += temp;
ind--;
temp--;
}
temp = x-1;
ind = k+1;
while (ind < m){
if (temp < 1){
tot += 1;
ind++;
continue ;
}
tot += temp;
ind++;
temp--;
}
return tot <= n;
}
int main(){
ll n;
ll m;
ll k;
cin >> n >> m >> k;
ll low = 0, high = n;
while (low <= high){
ll mid = low+(high-low)/2;
if (isPossible(mid, n, m, k)) low = mid+1;
else high = mid-1;
}
cout << high << endl;
return 0;
}
|
Last Updated :
19 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...