Skip to content

Commit

Permalink
Google Drive 23.07.2022
Browse files Browse the repository at this point in the history
  • Loading branch information
satyajeetramnit committed Jul 23, 2022
1 parent 4d3b777 commit 5c6423c
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Google/Drive 23.07.2022/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/MinGW/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
59 changes: 59 additions & 0 deletions Google/Drive 23.07.2022/slots.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Slots

// You are given an array A of N non-negative integers. There are K empty slots from 1 to K.
// You have to arrange these N numbers Into K slots (2 K>= M). Each slot can contain at most two numbers filled into it.
// After all the integers have been placed into these slots. find the sum of Bitwise AND of all the numbers with their respective slot number.

// Task

// Determine the maximum sum possible.

// Notes

// • Some slots may remain empty.
// • The bitwise AND operator (&) compares each bit of the first operend to the corresponding bit of the second operand.
// If both bits are 1, the corresponding result bit is set to 1 Otherwise, the corresponding result bit is set to 0.

#include<bits/stdc++.h>
using namespace std;

int MaxSum(int N, int K, vector<int> A){
int mask = pow(3, K) - 1;
vector<int> memo(mask + 1, 0);

function<int(int, int)> dp = [&](int i, int mask) {
int& res = memo[mask];
if (res > 0) return res;
if (i < 0) return 0;
for (int slot = 1, bit = 1; slot <= K; ++slot, bit *= 3)
if (mask / bit % 3 > 0)
res = max(res, (A[i] & slot) + dp(i - 1, mask - bit));
return res;
};

return dp(A.size() - 1, mask);
}

int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
for(int t_i = 0; t_i < T; t_i++){
int N, K;
cin >> N >> K;
vector<int> A(N);
for(int a_i = 0; a_i < N; a_i++){
cin >> A[a_i];
}
cout << MaxSum(N, K, A) << endl;
}
return 0;
}

// in python3
// from scipy.optimize import linear_sum_assignment as LA

// def maximumANDSum(n,t,nums):
// cost = [[-(x&y) for y in list(range(1, t+1)) * 2] for x in nums + [0]*(2*t - len(nums))]
// return -sum(cost[r][c] for r, c in zip(*LA(cost)))
Binary file added Google/Drive 23.07.2022/slots.exe
Binary file not shown.
85 changes: 85 additions & 0 deletions Google/Drive 23.07.2022/swappingPairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Swapping pairs

// You are given N pairs (a_{1}, b_{1}).(a 2 ,b 2 )......(a N ,b N ) . You are also given an integer M.
// You can swap any pair, that is, for the i ^ (th) pair, alpha_{i} becomes b_{i} and b_{i} becomes a_{i}
// You have to apply the operation in such way such that

// sum (i = 1 to N) a[i] = M,

// Your task is to determine whether the conditions can be satisfied or not. If the condition cannot be satisfied,
// then print 'NO' (without quotes). If the condition can be satisfied, then print "YES" (without quotes) and in the next line,
// print a lexicographically-smallest binary string of length N. Here, if the i ^ U_{i} character is '1',
// then it means that you are swapping the i ^ (th) pair. If the i ^ (th) character is 'O' then it means that you are not swapping the i ^ (th) pair.

// In C
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<malloc.h>

char** solve(int N, int M, int** arr, int* out_n){

}

int main(){
int out_n;
int T;
scanf("%d", &T);
for(int t_i = 0; t_i < T; t_i++){
int N;
scanf("%d", &N);
int M;
scanf("%d", &M);
int i_arr, j_arr;
int** arr = (int**)malloc(sizeof(int*) * N);
for(int i_arr = 0; i_arr < N; i_arr++){
arr[i_arr] = (int*)malloc(sizeof(int) * M);
for(int j_arr = 0; j_arr < M; j_arr++){
scanf("%d", &arr[i_arr][j_arr]);
}
}
char** out = solve(N, M, arr, &out_n);
for(int i_out = 0; i_out < out_n; i_out++){
printf("%s\n", out[i_out]);
}
for(int i_arr = 0; i_arr < N; i_arr++){
free(arr[i_arr]);
}
free(arr);
}
return 0;
}

// In C++

// #include<bits/stdc++.h>
// using namespace std;

// vector<vector<int>> solve(int N,int M, vector<vector<int>> arr){

// }

// int main(){
// int T;
// scanf("%d", &T);
// for(int t_i = 0; t_i < T; t_i++){
// int N;
// scanf("%d", &N);
// int M;
// scanf("%d", &M);
// vector<vector<int>> arr(N, vector<int>(M));
// for(int i_arr = 0; i_arr < N; i_arr++){
// for(int j_arr = 0; j_arr < M; j_arr++){
// scanf("%d", &arr[i_arr][j_arr]);
// }
// }
// vector<vector<int>> out = solve(N, M, arr);
// for(int i_out = 0; i_out < out.size(); i_out++){
// for(int j_out = 0; j_out < out[i_out].size(); j_out++){
// printf("%d ", out[i_out][j_out]);
// }
// printf("\n");
// }
// }
// return 0;
// }

0 comments on commit 5c6423c

Please sign in to comment.