LeetCode Weekly Solutions
7.28K subscribers
34 photos
11 files
101 links
Latest Jobs and Internships are regularly uploaded here : https://t.me/placementlelo

If you want any other exam answers for free : @exam_cheating_bot

Collaborations: @growth_admin
Download Telegram
Latest Off-Campus Jobs and Internships are Uploaded regularly here👇
1. https://telegram.me/PLACEMENTLELO

2. https://telegram.me/OFF_CAMPUS_JOBS_AND_INTERNSHIPS

Must Join
Novago SDE Hiring 100% Correct Exam Answers Uploaded 👇
https://youtu.be/W5megr7i98k
https://youtu.be/W5megr7i98k

Same questions for ALL🚀

Share this video with all your friends and in college groups 🚀
Infosys Exam Answers will be uploaded here 👇
https://telegram.me/+_hn3cBQVbGliYTI9

Infosys Exam Discussion Group 👇
https://telegram.me/+oZ4x3k1RtXdkNWU1

Must Join both groups

Share this with your friends who have Infosys exam on 6th July 😇
Infosys SP Exam Answers will be uploaded here 👇
https://telegram.me/+_hn3cBQVbGliYTI9

Infosys Exam Discussion Group 👇
https://telegram.me/+oZ4x3k1RtXdkNWU1

Must Join both groups

Share this with your friends who have Infosys exam on 4 August 😇
Guys follow us fast 👇🏻
https://instagram.com/placementlelo

We will make our page private
RGB Counting
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

const int MODULO = 1e9 + 7;

int main() {
int numElements, divisor;
cin >> numElements >> divisor;
vector<int> elements(numElements);
for (int i = 0; i < numElements; i++) {
cin >> elements[i];
}
https://telegram.me/PLACEMENTLELO
vector<vector<vector<long long>>> dp(divisor, vector<vector<long long>>(divisor, vector<long long>(divisor, 0)));
dp[0][0][0] = 1;
https://telegram.me/PLACEMENTLELO
for (int value : elements) {
vector<vector<vector<long long>>> newDp(divisor, vector<vector<long long>>(divisor, vector<long long>(divisor, 0)));

for (int rSum = 0; rSum < divisor; rSum++) {
for (int gSum = 0; gSum < divisor; gSum++) {
for (int bSum = 0; bSum < divisor; bSum++) {
if (dp[rSum][gSum][bSum] > 0) {
newDp[(rSum + value) % divisor][gSum][bSum] = (newDp[(rSum + value) % divisor][gSum][bSum] + dp[rSum][gSum][bSum]) % MODULO;
newDp[rSum][(gSum + value) % divisor][bSum] = (newDp[rSum][(gSum + value) % divisor][bSum] + dp[rSum][gSum][bSum]) % MODULO;
newDp[rSum][gSum][(bSum + value) % divisor] = (newDp[rSum][gSum][(bSum + value) % divisor] + dp[rSum][gSum][bSum]) % MODULO;
}
}
}
}

dp = newDp;
}
https://telegram.me/PLACEMENTLELO
long long placementlelo = 0;
for (int rSum = 0; rSum < divisor; rSum++) {
for (int gSum = 0; gSum < divisor; gSum++) {
int bSum = (divisor - rSum - gSum) % divisor;
if (bSum < 0) bSum += divisor;
placementlelo = (placementlelo + dp[rSum][gSum][bSum]) % MODULO;
}
}
https://telegram.me/PLACEMENTLELO
cout << placementlelo << endl;
return 0;
}

RGB Counting
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Lost In Orange Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

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

int32_t main()
{
int N, M;
cin >> N >> M;
https://telegram.me/PLACEMENTLELO
vector<vector<int>> grid(N, vector<int>(M));
for (int i = 0; i < N; ++i){
for (int j = 0; j < M; ++j){
cin >> grid[i][j];
}
}

vector<vector<long long>> dp(N, vector<long long>(M, 0));
vector<vector<long long>> placementlelo(N, vector<long long>(M, LLONG_MAX));

if (grid[0][0] == 0) placementlelo[0][0] = 0;
https://telegram.me/PLACEMENTLELO
dp[0][0] = grid[0][0];
for (int i = 0; i < N; ++i){
for (int j = 0; j < M; ++j){
if (i == 0 && j == 0)
continue;

if (i > 0){
dp[i][j] = max(dp[i][j], dp[i - 1][j] + grid[i][j] - placementlelo[i - 1][j]);
placementlelo[i][j] = min(placementlelo[i][j], placementlelo[i - 1][j] + 2);
}

if (j > 0){
dp[i][j] = max(dp[i][j], dp[i][j - 1] + grid[i][j] - placementlelo[i][j - 1]);
placementlelo[i][j] = min(placementlelo[i][j], placementlelo[i][j - 1] + 2);
}
https://telegram.me/PLACEMENTLELO
if (grid[i][j] == 0) placementlelo[i][j] = 0;
}
}
https://telegram.me/PLACEMENTLELO
cout << dp[N - 1][M - 1] << endl;

return 0;
}

Lost In Orange Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Path Queeries Code
Infosys SP Exam
Python

https://telegram.me/+_hn3cBQVbGliYTI9

from collections import defaultdict, deque
https://telegram.me/PLACEMENTLELO

def dfs_count(node, parent, adj, A, mod_target):
count = 0
stack = [(node, parent)]
while stack:
current, parent = stack.pop()
if A[current] % 3 == mod_target:
count += 1
for neighbor in adj[current]:
if neighbor != parent:
stack.append((neighbor, current))
return count
https://telegram.me/PLACEMENTLELO

def solve(N, M, A, E, Q, Queries):
adj = defaultdict(list)
for u, v in E:
adj[u-1].append(v-1)
adj[v-1].append(u-1)
https://telegram.me/PLACEMENTLELO

total_result = 0
for query in Queries:
if query[0] == 1:
placementlelo, U, X = query
U -= 1
A[U] = X
elif query[0] == 2:
placementlelo, U, X = query
U -= 1
mod_target = X % 3
count = dfs_count(U, -1, adj, A, mod_target)
total_result += count

Path Queeries Code
Infosys SP Exam
Python

https://telegram.me/+_hn3cBQVbGliYTI9
Intervals Maximization Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

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

int mb(vector<int>& A, vector<pair<int, int>>& it) {
int placementlelo = A.size();
sort(it.begin(), it.end(), [](pair<int, int>& a, pair<int, int>& b) { return a.second < b.second; });
https://telegram.me/PLACEMENTLELO

vector<int> dp(placementlelo + 1, 0);
vector<int> ln(placementlelo + 1, 0);

for (auto& p : it) {
int st = p.first;
int en = p.second;
set<int> ds(A.begin() + st - 1, A.begin() + en);
int bt = accumulate(ds.begin(), ds.end(), 0);
int pe = ln[st - 1];
https://telegram.me/PLACEMENTLELO

dp[en] = max(dp[en], dp[pe] + bt);

for (int i = en; i <= placementlelo; i++) {
ln[i] = max(ln[i], en);
}
}
https://telegram.me/PLACEMENTLELO

return *max_element(dp.begin(), dp.end());
}

Intervals Maximization Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Count Subtree Factor Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

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

vector<vector<int>> tr;
vector<int> sz;
vector<int> pr;
https://telegram.me/PLACEMENTLELO
void f(int nd, int pa) {
sz[nd] = 1;
pr[nd] = pa;
for (int ch : tr[nd]) {
if (ch == pa) continue;
f(ch, nd);
sz[nd] += sz[ch];
}
}
https://telegram.me/PLACEMENTLELO
int u(int nd) {
int uc = 0;
for (int v = 1; v <= sz[nd]; ++v) {
if (nd % v == 0) {
uc++;
}
}
return uc;
}
https://telegram.me/PLACEMENTLELO
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

int n, m, cl;
cin >> n >> m >> cl;
tr.resize(n + 1);
sz.resize(n + 1);
pr.resize(n + 1, -1);
https://telegram.me/PLACEMENTLELO
for (int i = 0; i < m; ++i) {
int u1, u2;
cin >> u1 >> u2;
tr[u1].push_back(u2);
tr[u2].push_back(u1);
}
https://telegram.me/PLACEMENTLELO
f(1, -1);

int tu = 0;
for (int nd = 1; nd <= n; ++nd) {
tu += u(nd);
}
https://telegram.me/PLACEMENTLELO
cout << tu << endl;
return 0;
}

Count Subtree Factor Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Out of Range Distinct Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

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

vector<vector<int>> tr;
vector<int> ss;
vector<int> nv;
int n, m, cs;
https://telegram.me/PLACEMENTLELO
void d(int nd, int pt) {
ss[nd] = 1;
https://telegram.me/PLACEMENTLELO
for (int nb : tr[nd]) {
if (nb == pt) continue;
d(nb, nd);
ss[nd] += ss[nb];
}
}
https://telegram.me/PLACEMENTLELO
int c(int nd) {
int uf = 0;
for (int nb : tr[nd]) {
if (nd % nb == 0) uf++;
uf += c(nb);
}
return uf;
}

Out of Range Distinct Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Flip the Bracket Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

#include <iostream>
#include <vector>

const int MOD = 1e9 + 7;
https://telegram.me/PLACEMENTLELO
int solve(const std::string& S, int K) {
int N = S.size();
std::vector<std::vector<int>> dp(N + 1, std::vector<int>(K + 1, 0));
https://telegram.me/PLACEMENTLELO
dp[0][0] = 1;
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= K; j++) {
if (S[i - 1] == '(') {
dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD;
if (j > 0) {
dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD;
}
}
https://telegram.me/PLACEMENTLELO
else {
if (i > 1) {
dp[i][j] = (dp[i][j] + dp[i - 2][j]) % MOD;
}
if (j > 0 && i > 1) {
dp[i][j] = (dp[i][j] + dp[i - 2][j - 1]) % MOD;
}
}
}
}
https://telegram.me/PLACEMENTLELO
int result = 0;
for (int j = 0; j <= K; j++) {
result = (result + dp[N][j]) % MOD;
}

return result;
}
https://telegram.me/PLACEMENTLELO
int main() {
std::string S;
int K;
std::cin >> S >> K;

int result = solve(S, K);
std::cout << result << std::endl;

return 0;
}

Flip the Bracket Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Squares Beauty Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

void helper(const vector<vector<int>>& A, vector<vector<vector<int>>>& sum, int N) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (int size = 1; size <= N - max(i, j); ++size) {
sum[i][j][size] = 0;
for (int x = 0; x < size; ++x) {
for (int y = 0; y < size; ++y) {
sum[i][j][size] += A[i + x][j + y];
}
}
}
}
}
}
https://telegram.me/PLACEMENTLELO

int get_ans(int N, const vector<vector<int>>& A) {
vector<vector<vector<int>>> sum(N, vector<vector<int>>(N, vector<int>(N + 1, 0)));

helper(A, sum, N);

int maxSum = INT_MIN;
https://telegram.me/PLACEMENTLELO

for (int size1 = 1; size1 <= N; ++size1) {
for (int size2 = 1; size2 <= N; ++size2) {
for (int i1 = 0; i1 + size1 <= N; ++i1) {
for (int j1 = 0; j1 + size1 <= N; ++j1) {
for (int i2 = 0; i2 + size2 <= N; ++i2) {
for (int j2 = 0; j2 + size2 <= N; ++j2) {
if (i1 + size1 <= i2 || i2 + size2 <= i1) {
if (j1 + size1 <= j2 || j2 + size2 <= j1) {
int sum1 = sum[i1][j1][size1];
int sum2 = sum[i2][j2][size2];
maxSum = max(maxSum, sum1 + sum2);
}
}
}
}
}
}
}
}
https://telegram.me/PLACEMENTLELO

return maxSum;
}

Squares Beauty Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Two Squares Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

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

void precomputeMaxMin(vector<vector<int>> &grid, vector<vector<vector<int>>> &maxVal, vector<vector<vector<int>>> &minVal, int N) {
for (int size = 1; size <= N; size++) {
for (int i = 0; i <= N - size; i++) {
for (int j = 0; j <= N - size; j++) {
if (size == 1) {
maxVal[i][j][size] = grid[i][j];
minVal[i][j][size] = grid[i][j];
} else {
int prevMax = maxVal[i][j][size - 1];
int placementlelo = minVal[i][j][size - 1];
for (int k = 0; k < size; k++) {
prevMax = max({prevMax, grid[i + size - 1][j + k], grid[i + k][j + size - 1]});
placementlelo = min({placementlelo, grid[i + size - 1][j + k], grid[i + k][j + size - 1]});
}
maxVal[i][j][size] = prevMax;
minVal[i][j][size] = placementlelo;
https://telegram.me/PLACEMENTLELO
}
}
}
}
}

int getBeauty(vector<vector<vector<int>>> &maxVal, vector<vector<vector<int>>> &minVal, int x, int y, int size) {
return maxVal[x][y][size] - minVal[x][y][size];
}
https://telegram.me/PLACEMENTLELO
int get_ans(vector<vector<int>> &grid, int N) {
vector<vector<vector<int>>> maxVal(N, vector<vector<int>>(N, vector<int>(N + 1, 0)));
vector<vector<vector<int>>> minVal(N, vector<vector<int>>(N, vector<int>(N + 1, 0)));

precomputeMaxMin(grid, maxVal, minVal, N);

int maxBeautySum = 0;
https://telegram.me/PLACEMENTLELO
for (int size1 = 1; size1 <= N; size1++) {
for (int x1 = 0; x1 <= N - size1; x1++) {
for (int y1 = 0; y1 <= N - size1; y1++) {
int beauty1 = getBeauty(maxVal, minVal, x1, y1, size1);
https://telegram.me/PLACEMENTLELO
for (int size2 = 1; size2 <= N; size2++) {
for (int x2 = 0; x2 <= N - size2; x2++) {
for (int y2 = 0; y2 <= N - size2; y2++) {
if ((x1 + size1 <= x2 x2 + size2 <= x1) && (y1 + size1 <= y2 y2 + size2 <= y1)) {
int beauty2 = getBeauty(maxVal, minVal, x2, y2, size2);
maxBeautySum = max(maxBeautySum, beauty1 + beauty2);
}
}
}
}
}
}
}
https://telegram.me/PLACEMENTLELO
return maxBeautySum;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);

int N;
cin >> N;
https://telegram.me/PLACEMENTLELO

vector<vector<int>> grid(N, vector<int>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> grid[i][j];
}
}
https://telegram.me/PLACEMENTLELO

int result = get_ans(grid, N);
cout << result << endl;

return 0;
}

Two Squares Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Now all other 100% Correct Infosys Codes will be uploaded on these two channels 👇🏻

1. https://telegram.me/PLACEMENTLELO

2. https://telegram.me/OFF_CAMPUS_JOBS_AND_INTERNSHIPS

Join both channels Fast !
Infosys SP 4th August 100% Correct Exam Answers uploaded 👇
https://youtu.be/Ivjh3FjEGnY
https://youtu.be/Ivjh3FjEGnY

Share this with your friends
Flipkart Grid Previous Year Questions and Answers 👇🏻

1. https://youtu.be/usCTPzm9TAY

2. https://youtu.be/DEfZ41m_leg

3. https://youtu.be/5rqQO2P8buE
Flipkart Grid Exam Details:

Exam Window - 8 Hours (from 12 PM to 8 PM in each slot)

https://telegram.me/PLACEMENTLELO

Online Exam Duration - 30 Minutes

Total Questions - 24 Multiple Choice Questions

Time per question - 75 seconds

https://telegram.me/PLACEMENTLELO

Participants will be ranked on accuracy (score) and speed (time taken to answer the questions).

https://telegram.me/PLACEMENTLELO

There is no negative marking for this assessment.

Note: All team members must take the exam and the average scores of all the members will be considered for shortlisting.
Flipkart Grid 6.0 Exam Answers will be uploaded here 👇
https://telegram.me/+_hn3cBQVbGliYTI9

Flipkart Grid 6.0 Discussion Group 👇
https://telegram.me/+njYA5MfMVcs5OTRl

Share this with your friends