Intuit
Intuit
Intuit
return 0
ix = abs(ix - fx)
jx = abs(iy - fy)
if jx >= ix:
return jx
d = ix - jx
t = (ix + 1) / 2
if t <= d:
return t
return ix - d - d
return max(helper(fx, ix, jx), helper(fy, iy, jy)) > abs(ix - jx) + abs(iy -
jy)
Spreading fire
There is a 2D plane of size NM. There is fire which is there at K different points in the 20 plane.
From each of these K points, the fire is spreading in a circular form with the radius of the fire
increasing by time. So, if at t-1, the radius of fire (represented by R) was 2, at 1-2, it becomes 4, at
t=3, it become 6 and so on. T denotes time here. Help us determine, the number of points (points
are denoted by oxy where both x and y are whole numbers, and are within the plane) which
would not be touched by the fire
Constraints
1 <= N,M <= 1000
1 <= K <= 5
def count_safe_points(N, M, K, fire_points):
count = 0
is_safe = True
is_safe = False
if not is_safe:
break
if is_safe:
count += 1
return count
# Example usage
N = 10
M = 10
K = 2
Robber on Tree
Take min and Max of array
int ans=INT_MIN;
if(src==target){
timeTaken[src]=t;
return true;
for(auto c:graph[src]){
if(src!=par){
if(dfs(c,src,target,t+1,graph,timeTaken)){
timeTaken[c]=t+1;
return true;
return false;
if(t<timeTaken[src]){
sum+=coins[src];
if(t==timeTaken[src]){
sum+=(coins[src]/2);
}
// cout<<src<<' ';
int child=0;
for(auto c:graph[src]){
if(par!=c){
child++;
dfs2(c,src,t+1,sum,graph,timeTaken,coins);
if(child==0){
ans=max(ans,sum);
return ;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int mod=998244353;
int n;
cin>>n;
int R;
cin>>R;
vector<int> graph[n+1];
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
graph[a].push_back(b);
graph[b].push_back(a);
vector<int> coins(n+1);
for(int i=1;i<=n;i++)cin>>coins[i];
vector<int> timeTaken(n+1,INT_MAX);
timeTaken[R]=0;
dfs(R,-1,1,0,graph,timeTaken);
dfs2(1,-1,0,0,graph,timeTaken,coins);
cout<<ans<<endl;
return 0;
N, M, K = map(int, input().split())
R = int(input())
T = int(input())
untouched_count = 0
for x in range(N):
for y in range(M):
is_untouched = True
if dist <= R * T:
is_untouched = False
break
if is_untouched:
untouched_count += 1
print(untouched_count)
__________________________________________________________________________________
__________________________________________________________________________________
Harry potter
map<int, int> m;
m[a[i]] = b[i];
for (auto it : m)
m.erase(it2);
CRED VERIFICATION
int solution(const vector<int> &a, const vector<int> &b, const vector<int> &c) {
vector<int> ind(n);
ind[i] = i;
});
trailing[1 << i] = i;
int main() {
cout << solution({10, 10, 10}, {1, 2, 3}, {2, 2, 2}) << endl;
n = len(a)
ind = list(range(n))
ind.sort(key=lambda x: b[x])
s = 1 << n
t = [0] * s
dp = [0] * s
trailing = [0] * s
for i in range(n):
trailing[1 << i] = i
prev = i & (i - 1)
return max(dp)
# Example usage
if __name__ == "__main__":
b = [1, 2, 3]
c = [2, 2, 2]
result = solution(a, b, c)
print(result)
Smart gardner
__________________________________________________________________________________
class Solution {
public:
int mn = 0;
int reach = 0;
int taps = 0;
taps++;
mn = reach;
return taps;
};
__________________________________________________________________________________
__________________________________________________________________________________
____________________________
Lost in woods
#include <iostream>
#include <string>
int main()
int numElements;
string result;
prevChar = currentChar;
if (currentChar == '*')
continue;
{
if (prevChar == '*')
result.push_back('J');
else
result.push_back('W');
if (numElements == 1)
result = 'W';
# include <bits/stdc++.h>
string res;
int i = 1;
if(board[i] == '_') {
res.push_back('W');
else {
res.push_back('J');
i++;
return res;
int main(){
int n;
cin >> n;
vector<char> board;
char temp;
board.push_back(temp);
return 0;
def solve(board):
res = []
n = len(board)
i = 1
while i < n - 1:
if board[i] == '_':
res.append('W')
else:
res.append('J')
i += 1
i += 1
return ''.join(res)
# Example usage
n = int(input())
board = list(input())
result = solve(board)
print(result)
void solve(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int x,y;
cin>>x>>y;
map<int,int> mp;
mp[0]++;
int sum=0,ans=0;
for(int i=0;i<n;i++){
if(a[i]==0)
sum+=(-y);
else
sum+=x;
if(mp.count(sum))
ans+=mp[sum];
mp[sum]++;
cout<<ans<<endl;
int main() {
int t=1;
// cin>>t;
while(t--){
solve();
return 0;
__________________________________________
def solve():
n = int(input())
a = list(map(int, input().split()))
x, y = map(int, input().split())
mp = {0: 1}
sum_val = 0
ans = 0
for i in range(n):
if a[i] == 0:
sum_val += (-y)
else:
sum_val += x
if sum_val in mp:
ans += mp[sum_val]
mp[sum_val] = mp.get(sum_val, 0) + 1
print(ans)
def main():
t = 1
# t = int(input())
for _ in range(t):
solve()
if __name__ == "__main__":
main()
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
_________________________________________________________________________________
GARDEN
#include <bits/stdc++.h>
int numElements;
minCost[0] = 0;
if (range[i] == 0)
continue;
Dhoni
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
def main():
maximum_value = float('-inf')
result = float('-inf')
for i in range(num_rows):
for j in range(num_cols):
cell_value = int(input())
print(result)
if __name__ == "__main__":
main()
for i in range(n):
return max_points
def main():
a_count = int(input())
a = list(map(int, input().split()))
b_count = int(input())
b = list(map(int, input().split()))
result = solve(a, b)
print(result)
if __name__ == "__main__":
main()
public:
int mn = 0;
int reach = 0;
int taps = 0;
reach = i + ranges[i];
}
if(mn == reach) return -1;
taps++;
mn = reach;
return taps;
};
class Solution:
mn = 0
reach = 0
taps = 0
for i in range(len(ranges)):
reach = i + ranges[i]
if mn == reach:
return -1
taps += 1
mn = reach
return taps
# Example usage
if __name__ == "__main__":
s = Solution()
n = int(input("Enter n: "))
Lazy chef 2
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
_________________________
if (ch == a){
aCount--;
option1 += bCount;
left++;
bCount--;
option2 += aCount;
left++;
}else{
a_count = s.count(a)
b_count = s.count(b)
if ch == a:
a_count -= 1
option1 += b_count
left += 1
elif ch == b:
b_count -= 1
option2 += a_count
left += 1
else:
# Example usage
result = solve(s, a, b)
print("Result:", result)
KINGS DREAMS
S = list(range(1, m + 1))
return S * (N // m) + [S:N % m]
upskilling candidates
def get_skilled(required,already,Books,price):
to_gain=0
to_gain|=(1<<skill)
for x in already:
if to_gain&(1<<x):
to_gain^=(1<<x)
m=len(Books)
@cache
def solve(i,mask):
if mask==0:return 0
if i==m:return float('inf')
res=solve(i+1,mask)
v=0
if mask&(1<<skill):
v|=(1<<skill)
res=min(res,price[i]+solve(i+1,mask^v))
return res
b=solve(0,to_gain)
Lazy chef - 2
https://leetcode.com/discuss/interview-question/2492460/Intuit-India-or-OA-or-
LAZY-CHEF-II
MATCHMAKING - 1
Description
Intuit receives calls from it's customers regarding various topics and in various languages. Intuit
wants to match these customers to experts who have knowledge on exactly the same topic and
can speak in that language. Based on the predictions, Intuit had N experts ready to take the calls,
each expert knows exactly one topic and can speak exactly one language proficiently. On a
particular day, M customers end up calling for their problems. A customer is unhappy if it is not
matched to the expert who know about the topic they wish to enquire about and know the
language they talk in. Can you help the team at Intuit assign the customers to experts, such that
the minimum number of customers return unhappy
Constraints
1<= N <= 10^5
1<= M <= 10^5
Input Format
The first line contains N, the count of experts at Intuit.
Next N line contains 2 space seperated strings, the first denoting the topic the expert knows, and
the second denoting the language they know The next line contains M. the count of customers
calling Intuit.
Next M line contains 2 space seperated strings, the first denoting the topic the customer wants to
enquire about, and the second denoting the language they speak in
Output Format
Return an integer representing the number of
customer which went unhappy with Intuit
Sample Input
4
Tax English
Crypto Hindi
Tax Spanish
SF English
4 Crypto Hindi
Tax Spanish
Crypto Spanish
Tax Spanish
Sample Output
2
Explanation
Crypto Spanish - Intuit does not have a crypt o expert who can work in Spanish.
Tax Spanish - Intuit only has a single expert who can do tax and work in Spanish
Solution :
#include <bits/stdc++.h>
int main()
Solution s = Solution();
int n;
cin>>n;
unordered_map<string,vector<pair<string,int>>> mp;
for(int i=0;i<n;i++){
string a,b;
cin>>a>>b;
//map of pair pushing a new pair with same key and default flag
value 0
mp[a].push_back({b,0});
int m;
cin>>m;
vector<pair<string,string>> v;
for(int i=0;i<m;i++){
string a,b;
cin>>a>>b;
v.push_back({a,b});
}
int ans=0;
for(auto &c:v){
auto it =mp.find(c.first);
if(it!=mp.end()){
int j=0;
for(;j<it->second.size();j++){
it->second[j].second=1;
break;
if(j==it->second.size())ans++;
else
ans++;
//in this case if the lang is not present in the map then the
customer is unsatisfied
// add the ans
cout<<ans<<"/n";
return 0;
class Solution:
def __init__(self):
self.mp = {}
def main(self):
n = int(input())
for _ in range(n):
a, b = input().split()
if a not in self.mp:
self.mp[a] = []
self.mp[a].append((b, 0))
m = int(input())
v = []
for _ in range(m):
a, b = input().split()
v.append((a, b))
ans = 0
for c in v:
if c[0] in self.mp:
found = False
self.mp[c[0]][i] = (c[1], 1)
found = True
break
if not found:
ans += 1
else:
ans += 1
print(ans)
if __name__ == "__main__":
s = Solution()
s.main()
Given a binary array, find the total number of subarrays with the ratio of 0's and 1's equal to x:y.
Constraints:
n = arr.length
2<=n<=1e5
1<=x,y<=n
Output: 4
[0 1] 0 1 --> [0,1]
0 [1 0] 1 --> [1,0]
0 1 [0 1] --> [0,1]
[0 1 0 1] --> [0,1,0,1]
With the given constraints, expecting solution no worse than O(n)
int sum = 0;
map<int,int> mp;
mp[0]++;
int ans = 0;
if(arr[i] == 0)
sum += (-y);
else
sum += (x);
if(mp.count(sum))
ans += mp[sum];
mp[sum]++;
return ans;
sum_val = 0
mp = {0: 1}
ans = 0
for i in range(len(arr)):
if arr[i] == 0:
sum_val += (-y)
else:
sum_val += x
if sum_val in mp:
ans += mp[sum_val]
if sum_val not in mp:
mp[sum_val] = 1
else:
mp[sum_val] += 1
return ans
https://leetcode.ca/all/1788.html
overleaping circles
#include <bits/stdc++.h>
return a.size()<b.size();
int circles(vector<int>&v){
vector<vector<int>>m(n);
for(int i=0;i<n;i++){
m[i].push_back(i);
for(int j = 0;j<n;j++){
m[i].push_back(j);
int ans = 0;
sort(m.begin(),m.end(),fun);
ans++;
for(int i=1;i<m[m.size()-1].size();i++){
int j = m[m.size()-1][i];
m[j].erase(find(m[j].begin(),m[j].end(),m[m.size()-1][0]));
m.pop_back();
sort(m.begin(),m.end(),fun);
return ans;
int main() {
int n;
cin>>n;
vector<vector<int>>v;
int i = n;
while(i--){
int x;
cin>>x;
int y;
cin>>y;
int r;
cin>>r;
v.push_back({x,y,r});
cout<<circles(v);
import math
n = len(v)
for i in range(n):
m[i].append(i)
for j in range(n):
m[i].append(j)
ans = 0
m.sort(key=lambda x: len(x))
ans += 1
j = m[-1][i]
m[j].remove(m[-1][0])
m.pop()
m.sort(key=lambda x: len(x))
return ans
def main():
n = int(input())
v = []
for _ in range(n):
x, y, r = map(int, input().split())
v.append([x, y, r])
print(circles(v))
if __name__ == "__main__":
main()
Dhoni
def maximize_reward(matrix):
n = len(matrix)
m = len(matrix[0])
max_reward = float('-inf')
# Precompute maximum and minimum values for each row from the right
for i in range(n):
max_val = matrix[i][-1]
min_val = matrix[i][-1]
max_row[i][j] = max_val
min_row[i][j] = min_val
# Precompute maximum and minimum values for each column from the bottom
for j in range(m):
max_val = matrix[-1][j]
min_val = matrix[-1][j]
max_col[i][j] = max_val
min_col[i][j] = min_val
# Calculate maximum reward for each cell
for i in range(n):
for j in range(m):
return max_reward
# Read input
n, m = map(int, input().split())
print(maximize_reward(matrix))
There are n servers. Ith server has requests[i] requests to serve but can serve a
maximum of max_req[i] requests.
In order to balance the load, a hackers reed to redirect some requests of Ith
server to some other servers. The latency induced in redirecting the request on
the Ith server to the Jth server is |i-j| where |x| represents the absolute value
of x. The max_latency is defined as the maximum latency Induced amongst all the
redirections.
Given the arrays requests and max_req find the minimum possible max latency if the
requests are redirected optimally such that no server has to serve more than the
maximum requests it can serve. If there is no way to serve all the requests,
report -1 as the answer.
n = 4
maxreq = [2,1,5,3]
req = [1,3,2,4]
ans :1 (max(|2-1|,|3-2|)
curr_req = req[:]
curr_max_req = max_req[:]
for i in range(n):
j = i
curr_req[i] -= allocated
curr_max_req[j] -= allocated
j -= 1
j = i + 1
curr_req[i] -= allocated
curr_max_req[j] -= allocated
j += 1
if curr_req[i] > 0:
return False
return True
low = 0
high = max(max_req) + n
result = -1
result = mid
high = mid - 1
else:
low = mid + 1
return result
n = 4
max_req = [2, 1, 5, 3]
req = [1, 3, 2, 4]
schedule training 2
n,m=map(int,input().split(" "))
taskshour=0
for i in range(n):
taskshour+=sum(list(map(int,input().split((" ")))))
p=int(input())
commit=[]
for i in range(p):
x=list(map(int,input().split((" "))))
commit.append(x)
days=int(input())
perday=taskshour//days
if(taskshour%days>0):
flag=1
else:
flag=0
reserved=[]
for i in commit:
for j in range(i[0]+1,i[1]+1):
reserved.append(j)
free=0
max=0
for i in range(1,25):
max=i
free+=1
if(flag==1):
if(free==perday+1):
break
else:
if(free==perday):
break
else:
print("\n\nResult",-1)
print("\n\nResult",max)
__________________________________________________________________________________
__________________________________________________________________________________
_____________________________
scheduled training
n,m=map(int,input().split(" "))
taskshour=0
for i in range(n):
taskshour+=sum(list(map(int,input().split((" ")))))
p=int(input())
commit=[]
for i in range(p):
x=list(map(int,input().split((" "))))
commit.append(x)
days=int(input())
perday=taskshour//days
if(taskshour%days>0):
flag=1
else:
flag=0
reserved=[]
for i in commit:
for j in range(i[0]+1,i[1]+1):
reserved.append(j)
free=0
max=0
for i in range(1,25):
max=i
free+=1
if(flag==1):
if(free==perday+1):
break
else:
if(free==perday):
break
else:
print("\n\nResult",-1)
print("\n\nResult",max)
STUBBORN FRIENDS
if i == n:
return 0
if dp[i][t] != -1:
return dp[i][t]
for x in v[i]:
if x[0] <= t:
dp[i][t] = temp
return temp
vis[src] = ct
for x in adj[src]:
def solve():
n, t = map(int, input().split())
m = int(input())
for i in range(m):
u, v = map(int, input().split())
adj[u].append(v)
adj[v].append(u)
ct = -1
vis = [0] * (n + 1)
if vis[i] != 0:
continue
ct += 1
if vis[j] == ct:
v[ct].append((ci[j], mi[j]))
n = ct + 1
print(ans)
t = int(input())
while t > 0:
solve()
t -= 1
#include <bits/stdc++.h>
int main()
int numElements;
minCost[0] = 0;
if (range[i] == 0)
continue;
}
}
def main():
num_elements = int(input())
min_cost[0] = 0
range_vals[i] = int(input())
element_cost[i] = int(input())
if range_vals[i] == 0:
continue
prev_element_cost = element_cost[prev_idx]
if __name__ == "__main__":
main()
Garden
Jump kady
dp[p][q] = 0
if i == p and j == q:
continue
if i - k >= 1:
if j - k >= 1:
if dp[u][v] == float('inf'):
return -1
else:
return dp[u][v]
# Read input
m, n, X = map(int, input().split())
p, q = map(int, input().split())
u, v = map(int, input().split())
result = can_reach_destination(m, n, X, p, q, u, v)
print(result)
229 VIEWS
There is a 2D plane of size N* M. There is fire which is there at K different points in the 2D plane.
From each of these K points, the fire is spreading in a circular form with the radius of the fire
increasing by time. So, if at t=1, the radius if fire was 2, at t=2, it becomes 4, at t=3, it become 6
and so on. "t" denotes time here.
Help us determine, the number of points (points are denoted by {x,y}, where both x and y are
whole numbers, and are within the plane) which would not be touched by the fire
Constraints
1 <= N <= 1000
1<= M <= 1000
1<=K<=5
1 <= R <= 10
1 <= T <= 100
Assume:
(1) You meant all xs are in [0, n - 1] and ys are in [0, m - 1].
(2) You are asking for each T = 0,1, 2...t, how many points are unaffected.
This can be preprocessed by a loop of m * n * k which calculates the earliest time a point is on
fire.
Then we do something like merge sort or we can call that 2 pointers....
int sqr(int x) {
return x * x;
vector<int> v;
if (temp <= t) {
v.push_back(temp);
sort(v.begin(), v.end());
vector<int> ans(t + 1, n * m);
ans[i] -= j;
return ans;
import math
rr = r * r
v = []
for i in range(n):
for j in range(m):
temp = math.inf
for point in f:
if temp <= t:
v.append(temp)
v.sort()
ans = [n * m] * (t + 1)
j = 0
j += 1
ans[i] -= j
return ans
# Example usage
if __name__ == "__main__":
n = 5
m = 5
r = 2
t = 3
result = solution(n, m, r, t, f)
print(result)