0% found this document useful (0 votes)
4 views11 pages

DEShaw

Uploaded by

musirikechandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

DEShaw

Uploaded by

musirikechandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

DEShaw

a)Given three coding questions 1:moderate 2:moderate 3:Hard


(1,2 done,3 partial (brute force))
b)separate time slots for each question(25min,35min,35 min)

1)Given an array arr of n integers, the index weighted sum of a subsequence of the
array formed by choosing indices [ i 1,i2,...,ik] is defined s( 1/1 * K a[i1])+(i2k+a[i2])... +
(ikk+a[ik]) assuming indexing starts from 1, where k is the length of the subsequence.
For example, for the array arr = [1, 10. 100]the index weighted sum of the subsequence
formed by indices [1, 3] is (1^ * 2+1)+ (3 * 2 + 100) = 3 +106=109.

Given ag and an integer max sum, find the length of the longest subsequence with an
index weighted sum less than or equal to max sum.
Example

Suppose n = 4 arr = [4, 3, 2, 1], max_sum = 33.

The optimal subsequence is formed by indices [1, 2, 4] i.e. values [4, 3, 1] with the index
weighted sum ( 1 * 3+4)+(2^ * 3+3)+(4^ * 3+1)= 7 + 9 + 13 = 29 Any subsequence of
length greater than 3 will have an index weighted sum greater than max_sum. Hence
the answer is .
Constraints

• 1<=n<=2*105

• 1 <= arr[i] s<=104

1 <=max_sum≤ 1015

It is guaranteed that at least one of the elements of arr is less than max_sum
Code: #include<bits/stdc++.h> // use binary search over length and proceed
using namespace std;
#define ll long long
int solve(int n,vector<int> &a,int max_sum){

int low =0;


int high = n;
int mid;
priority_queue<int> pq;
int ans=0;
while(low<=high){
while(!pq.empty()) pq.pop();
int mid = low + (high-low)/2;
int sum=0;
for(int i=1;i<=n;i++){
if(sum+(i*mid + a[i-1]) <= max_sum){
pq.push(i*mid+a[i-1]);
sum+=(i*mid + a[i-1]);
}
else {
if(i*mid + a[i-1]<pq.top()){
sum-=pq.top();
pq.pop();
sum+=(i*mid + a[i-1]);
pq.push(i*mid + a[i-1]);
}
}
}
if(pq.size()>=mid){
ans = mid;
low = mid+1;
}
else {
high = mid-1;
}
}
}

2)A tree is represented as an undirected connected graph of n nodes numbered from 1


to n and n- 1 edges. The ith edge connects the nodes numbered from edges[i][0] and
edges[0][1] and each node / is associated with a value val

In a special move, any node can be visited from any other node directly via
teleportation.

Given val and edges, find the maximum sum of values of the nodes visited on a path
starting and ending at node number 1 by using the special move at most once and
visiting any edge at most once,
Code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
int dfs(int x,vector<int> &val,vector<vector<int>> &adj,vector<int> &vis){
vis[x]=1; // just find max val from left subtree of root node and maxvalue
int maxi = 0; // from right sub tree of root node
for(auto &z:adj[x]){
if(vis[z]==0){
maxi = max(maxi,dfs(z,val,adj,vis));
}
}
return maxi+val[x-1];

}
int getSum(vector<int> val,vector<vector<int>> edges){
int n = val.size();
int m = edges.size();
vector<vector<int>> adj(n+1);
int l,r;
for(int i=0;i<m;i++){
l = edges[i][0];
r = edges[i][1];
adj[l].push_back(r);
adj[r].push_back(l);
}
vector<int> vis(n+1,0);
vector<int> sum(n+1,0);
vis[1]=0;
for(auto &x:adj[1]){
if(vis[x]==0){
sum[x] = dfs(x,val,adj,vis);
}
}
sort(sum.begin(),sum.end(),greater<int>());
return (sum[0]+sum[1]);

}
3) An array arr is to be divided into multiple non- empty subarrays such that the total
sum of costs of division is minimized. Each element of the array arr[i] is associated with
a cost cost[i].

The subarrays after division are numbered 1 to m where m is the number of subarrays.
For example, if arr=[1, 2, 3, 4] is divided into two subarrays [1, 2] and [3,4], the subarray
[1, 2] is numbered 1 and [3, 4] is numbered 2.

The cost of a subarray starting from index / and ending at index/is defined as the
product of value (arr[0]+ arr[1]... + arr[j])+(k * subarray_number) and the sum of cost of
the

subarray i.e. cost[i]+ cost[i+1].. + cost). Thus the

cost of subarray from index i to j is (arr[0]+ arr[1]...

+ arr[j]+k subarray_number) * (cost[i] + ... +

cost[j]). Here k is the subarray number factor incured in cost.

Given arr, cost of n integers each and the subarray number factor k incurred in cost, find
the minimunm total cost of the subarrays after optimal division.
Code:
(Brute force one not optimal )(10/14)

long long solve(int i, int num, int n, int k, vector<int> &arr, vector<long long> &cost,
vector<vector<long long>> &dp, vector<long long> &pf)
{
if (i == n)
{
return 0;
}
if (dp[i][num] != -1)
{
return dp[i][num];
}
long long ans = infinity;
for (int j = i; j < n; j++)
{
long long sum = arr[j];
sum += k * num;
sum *= (pf[j + 1] - pf[i]);
ans = min(ans, sum + solve(j + 1, num + 1, n, k, arr, cost, dp, pf));
}
return dp[i][num] = ans;
}
long getMinCost(vector<int> arr, vector<int> cost, int k)
{
int n = arr.size();
for (int i = 1; i < n; i++)
{
arr[i] += arr[i - 1];
}
vector<long long> pf(n + 1);
for (int i = 1; i <= n; i++)
{
pf[i] = pf[i - 1] + cost[i - 1];
}
vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, -1));
return solve(0, 1, n, k, arr, cost, dp, pf);
}

You might also like