🎯TCS National Qualifier Test (TCS NQT) 2024
Location: Across India
Qualification: B.E / B.Tech / M.E / M.Tech / M.Sc / MCA / Any Graduate / Under Graduate / Diploma
Batch: 2018/2019/2020/2021/2022/2023/2024
Apply Now:- www.allcoding1.com
Telegram:- @allcoding1
Location: Across India
Qualification: B.E / B.Tech / M.E / M.Tech / M.Sc / MCA / Any Graduate / Under Graduate / Diploma
Batch: 2018/2019/2020/2021/2022/2023/2024
Apply Now:- www.allcoding1.com
Telegram:- @allcoding1
👍1
#include <iostream>
#include <vector>
#include <unordered_map>
class Main {
public:
static long getZeroBitSubarrays(const std::vector<int>& arr) {
int n = arr.size();
long totalSubarrayCount = static_cast<long>(n) * (n + 1) / 2;
long nonzeroSubarrayCount = 0;
std::unordered_map<int, int> windowBitCounts;
int leftIdx = 0;
for (int rightIdx = 0; rightIdx < n; rightIdx++) {
int rightElement = arr[rightIdx];
if (rightElement == 0) {
windowBitCounts.clear();
leftIdx = rightIdx + 1;
continue;
}
std::vector<int> setBitIndices = getSetBitIndices(rightElement);
for (int index : setBitIndices) {
windowBitCounts[index]++;
}
while (leftIdx < rightIdx && isBitwiseAndZero(rightIdx - leftIdx + 1, windowBitCounts)) {
for (int index : getSetBitIndices(arr[leftIdx])) {
windowBitCounts[index]--;
if (windowBitCounts[index] == 0) {
windowBitCounts.erase(index);
}
}
leftIdx++;
}
nonzeroSubarrayCount += (rightIdx - leftIdx + 1);
}
return totalSubarrayCount - nonzeroSubarrayCount;
}
private:
static std::vector<int> getSetBitIndices(int x) {
std::vector<int> setBits;
int pow2 = 1;
int exponent = 0;
while (pow2 <= x) {
if ((pow2 & x) != 0) {
setBits.push_back(exponent);
}
exponent++;
pow2 *= 2;
}
return setBits;
}
static bool isBitwiseAndZero(int windowLength, const std::unordered_map<int, int>& bitCounts) {
for (const auto& entry : bitCounts) {
if (entry.second >= windowLength) {
return false;
}
}
return true;
}
};
DE Shaw ✅
C++
Telegram:- @allcoding1
#include <vector>
#include <unordered_map>
class Main {
public:
static long getZeroBitSubarrays(const std::vector<int>& arr) {
int n = arr.size();
long totalSubarrayCount = static_cast<long>(n) * (n + 1) / 2;
long nonzeroSubarrayCount = 0;
std::unordered_map<int, int> windowBitCounts;
int leftIdx = 0;
for (int rightIdx = 0; rightIdx < n; rightIdx++) {
int rightElement = arr[rightIdx];
if (rightElement == 0) {
windowBitCounts.clear();
leftIdx = rightIdx + 1;
continue;
}
std::vector<int> setBitIndices = getSetBitIndices(rightElement);
for (int index : setBitIndices) {
windowBitCounts[index]++;
}
while (leftIdx < rightIdx && isBitwiseAndZero(rightIdx - leftIdx + 1, windowBitCounts)) {
for (int index : getSetBitIndices(arr[leftIdx])) {
windowBitCounts[index]--;
if (windowBitCounts[index] == 0) {
windowBitCounts.erase(index);
}
}
leftIdx++;
}
nonzeroSubarrayCount += (rightIdx - leftIdx + 1);
}
return totalSubarrayCount - nonzeroSubarrayCount;
}
private:
static std::vector<int> getSetBitIndices(int x) {
std::vector<int> setBits;
int pow2 = 1;
int exponent = 0;
while (pow2 <= x) {
if ((pow2 & x) != 0) {
setBits.push_back(exponent);
}
exponent++;
pow2 *= 2;
}
return setBits;
}
static bool isBitwiseAndZero(int windowLength, const std::unordered_map<int, int>& bitCounts) {
for (const auto& entry : bitCounts) {
if (entry.second >= windowLength) {
return false;
}
}
return true;
}
};
DE Shaw ✅
C++
Telegram:- @allcoding1
👍3😁2❤1
Saks subarray product✅
long long solve(vector<int>& nums, int k) {
if (k <= 1) return 0;
int n = nums.size();
long long p = 1;
int i = 0, j = 0;
long long ans = 0;
while (j < n) {
p *= nums[j];
while (i <= j && p > k) {
p /= nums[i];
i++;
}
ans += j - i + 1;
j++;
}
return ans;
}
long long solve(vector<int>& nums, int k) {
if (k <= 1) return 0;
int n = nums.size();
long long p = 1;
int i = 0, j = 0;
long long ans = 0;
while (j < n) {
p *= nums[j];
while (i <= j && p > k) {
p /= nums[i];
i++;
}
ans += j - i + 1;
j++;
}
return ans;
}
👍4
allcoding1
Photo
Here's a Python program to simulate the given problem:
`python
def print_terrain(terrain):
for row in terrain:
print(''.join(row))
def flow_water(terrain, n):
water_level = int(terrain[n // 2][n // 2])
terrain[n // 2][n // 2] = 'W'
def can_flow(x, y, direction):
if direction == 'N':
return x > 0 and terrain[x-1][y] != 'W' and int(terrain[x-1][y]) <= water_level
elif direction == 'S':
return x < n - 1 and terrain[x+1][y] != 'W' and int(terrain[x+1][y]) <= water_level
elif direction == 'E':
return y < n - 1 and terrain[x][y+1] != 'W' and int(terrain[x][y+1]) <= water_level
elif direction == 'W':
return y > 0 and terrain[x][y-1] != 'W' and int(terrain[x][y-1]) <= water_level
def flow(x, y):
if can_flow(x, y, 'N'):
terrain[x-1][y] = 'W'
return True
if can_flow(x, y, 'S'):
terrain[x+1][y] = 'W'
return True
if can_flow(x, y, 'E'):
terrain[x][y+1] = 'W'
return True
if can_flow(x, y, 'W'):
terrain[x][y-1] = 'W'
return True
return False
while True:
print_terrain(terrain)
has_flown = False
for i in range(n):
for j in range(n):
if terrain[i][j] == 'W':
if flow(i, j):
has_flown = True
if not has_flown:
water_level += 1
print(f"Cannot flow, increasing water level to {water_level}")
break
if any(cell == 'W' and (i == 0 or j == 0 or i == n - 1 or j == n - 1) for i, row in enumerate(terrain) for j, cell in enumerate(row)):
print("Reached edge, exiting.")
break
n = 7
terrain = [
[494, 88, 89, 778, 984, 726, 587],
[340, 959, 220, 301, 639, 280, 290],
[666, 906, 632, 824, 127, 505, 787],
[673, 499, 843, 172, 193, 613, 154],
[544, 211, 124, 60, 575, 572, 389],
[635, 170, 174, 946, 593, 314, 300],
[620, 167, 931, 780, 416, 954, 275]
]
flow_water(terrain, n)
Python
Telegram:- @allcoding1_official
`python
def print_terrain(terrain):
for row in terrain:
print(''.join(row))
def flow_water(terrain, n):
water_level = int(terrain[n // 2][n // 2])
terrain[n // 2][n // 2] = 'W'
def can_flow(x, y, direction):
if direction == 'N':
return x > 0 and terrain[x-1][y] != 'W' and int(terrain[x-1][y]) <= water_level
elif direction == 'S':
return x < n - 1 and terrain[x+1][y] != 'W' and int(terrain[x+1][y]) <= water_level
elif direction == 'E':
return y < n - 1 and terrain[x][y+1] != 'W' and int(terrain[x][y+1]) <= water_level
elif direction == 'W':
return y > 0 and terrain[x][y-1] != 'W' and int(terrain[x][y-1]) <= water_level
def flow(x, y):
if can_flow(x, y, 'N'):
terrain[x-1][y] = 'W'
return True
if can_flow(x, y, 'S'):
terrain[x+1][y] = 'W'
return True
if can_flow(x, y, 'E'):
terrain[x][y+1] = 'W'
return True
if can_flow(x, y, 'W'):
terrain[x][y-1] = 'W'
return True
return False
while True:
print_terrain(terrain)
has_flown = False
for i in range(n):
for j in range(n):
if terrain[i][j] == 'W':
if flow(i, j):
has_flown = True
if not has_flown:
water_level += 1
print(f"Cannot flow, increasing water level to {water_level}")
break
if any(cell == 'W' and (i == 0 or j == 0 or i == n - 1 or j == n - 1) for i, row in enumerate(terrain) for j, cell in enumerate(row)):
print("Reached edge, exiting.")
break
n = 7
terrain = [
[494, 88, 89, 778, 984, 726, 587],
[340, 959, 220, 301, 639, 280, 290],
[666, 906, 632, 824, 127, 505, 787],
[673, 499, 843, 172, 193, 613, 154],
[544, 211, 124, 60, 575, 572, 389],
[635, 170, 174, 946, 593, 314, 300],
[620, 167, 931, 780, 416, 954, 275]
]
flow_water(terrain, n)
Python
Telegram:- @allcoding1_official
👍10
bool isPal(int n) {
int r, s = 0, t;
t = n;
while (n > 0) {
r = n % 10;
s = (s * 10) + r;
n = n / 10;
}
return (t == s);
}
int firstPal(int n) {
int i = 1;
while (true) {
if (isPal(i)) {
int d = 1 + log10(i);
if (d == n)
return i;
}
i++;
}
}
void login(int d, string u, string p) {
map<string, string> users = {
{"user1", "pass1"},
{"user2", "pass2"},
{"user3", "pass3"},
{"user4", "pass4"},
{"user5", "pass5"}
};
if (users.find(u) != users.end() && users[u] == p) {
int t = firstPal(d);
cout << "Welcome " << u << " and the generated token is: token-" << t << endl;
} else {
cout << "UserId or password is not valid, please try again." << endl;
}
}
IBM✅
Telegram:- @allcoding1
int r, s = 0, t;
t = n;
while (n > 0) {
r = n % 10;
s = (s * 10) + r;
n = n / 10;
}
return (t == s);
}
int firstPal(int n) {
int i = 1;
while (true) {
if (isPal(i)) {
int d = 1 + log10(i);
if (d == n)
return i;
}
i++;
}
}
void login(int d, string u, string p) {
map<string, string> users = {
{"user1", "pass1"},
{"user2", "pass2"},
{"user3", "pass3"},
{"user4", "pass4"},
{"user5", "pass5"}
};
if (users.find(u) != users.end() && users[u] == p) {
int t = firstPal(d);
cout << "Welcome " << u << " and the generated token is: token-" << t << endl;
} else {
cout << "UserId or password is not valid, please try again." << endl;
}
}
IBM✅
Telegram:- @allcoding1
👍9
Odd Even Code
Python 3✅
IBM
Telegram:- @allcoding1
Code 1👆
Code 2👇
vector<int>solve(vector<int> arr, int transform) {
for(int t = 0; t < transform; t++) {
for(int i = 0; i < arr.size(); i++) {
if(arr[i] % 2 == 0) {
arr[i] -= 3;
} else {
arr[i] += 3;
}
}
}
return arr;
}
Odd Even
Python 3✅
IBM
Telegram:- @allcoding1
Code 1👆
Code 2👇
vector<int>solve(vector<int> arr, int transform) {
for(int t = 0; t < transform; t++) {
for(int i = 0; i < arr.size(); i++) {
if(arr[i] % 2 == 0) {
arr[i] -= 3;
} else {
arr[i] += 3;
}
}
}
return arr;
}
Odd Even
👍5
Code 1👆
Code 2👇
string solve(string bs) {
map<string, string> nb = {
{"001", "C"},
{"010", "G"},
{"011", "A"},
{"101", "T"},
{"110", "U"},
{"000", "DNA"},
{"111", "RNA"}
};
string ds = "";
string t = nb[bs.substr(0, 3)];
for(int i = 3; i < bs.length(); i += 3) {
string b = bs.substr(i, 3);
if(nb.find(b) != nb.end()) {
string x = nb[b];
if(t == "DNA" && x == "U") {
x = "T";
}
ds += x;
} else {
ds += "Error";
}
}
return ds;
}
DNA✅
IBM
Telegram:- @allcoding1
Code 2👇
string solve(string bs) {
map<string, string> nb = {
{"001", "C"},
{"010", "G"},
{"011", "A"},
{"101", "T"},
{"110", "U"},
{"000", "DNA"},
{"111", "RNA"}
};
string ds = "";
string t = nb[bs.substr(0, 3)];
for(int i = 3; i < bs.length(); i += 3) {
string b = bs.substr(i, 3);
if(nb.find(b) != nb.end()) {
string x = nb[b];
if(t == "DNA" && x == "U") {
x = "T";
}
ds += x;
} else {
ds += "Error";
}
}
return ds;
}
DNA✅
IBM
Telegram:- @allcoding1
🎯Amazon off Campus Drive 2024 Hiring Freshers As Associate Quality Services | 2.52-4 LPA
Job Role : Associate Quality Services
Qualification : BE/BTech/MCA/BSc/BCA/MSc
Experience : Freshers
Salary CTC : Rs 2.52-4 LPA
Apply Now:- www.allcoding1.com
Telegram:- @allcoding1
Job Role : Associate Quality Services
Qualification : BE/BTech/MCA/BSc/BCA/MSc
Experience : Freshers
Salary CTC : Rs 2.52-4 LPA
Apply Now:- www.allcoding1.com
Telegram:- @allcoding1
❤2👍2
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll solve(ll k, ll s) {
auto comb = [](ll n) {
return n * (n - 1) / 2;
};
if (k > 3 * s) return 0;
ll ans = comb(k + 2);
if (k > s) ans -= 3 * comb(k - s + 1);
if (k - 2 >= 2 * s) ans += 3 * comb(k - 2 * s);
return ans;
}
int main() {
ll n, s;
cin >> n >> s;
cout << solve(s,n) << endl;
return 0;
}
Distribute Car Toy
Service Now ✅
Telegram:- @allcoding1
#define ll long long
using namespace std;
ll solve(ll k, ll s) {
auto comb = [](ll n) {
return n * (n - 1) / 2;
};
if (k > 3 * s) return 0;
ll ans = comb(k + 2);
if (k > s) ans -= 3 * comb(k - s + 1);
if (k - 2 >= 2 * s) ans += 3 * comb(k - 2 * s);
return ans;
}
int main() {
ll n, s;
cin >> n >> s;
cout << solve(s,n) << endl;
return 0;
}
Distribute Car Toy
Service Now ✅
Telegram:- @allcoding1
👍7
int solve(vector<int>& nums) {
vector<int> s(nums);
sort(s.begin(), s.end());
int i = 0, j = nums.size() - 1;
while (i < nums.size() && nums[i] == s[i]) {
i++;
}
while (j > i && nums[j] == s[j]) {
j--;
}
return j - i + 1;
}
Arrange the heights
Apple ✅
Telegram:- @allcoding1
vector<int> s(nums);
sort(s.begin(), s.end());
int i = 0, j = nums.size() - 1;
while (i < nums.size() && nums[i] == s[i]) {
i++;
}
while (j > i && nums[j] == s[j]) {
j--;
}
return j - i + 1;
}
Arrange the heights
Apple ✅
Telegram:- @allcoding1
👍4❤2
#include<bits/stdc++.h>
using namespace std;
void dfs(int node, vector<int>& vis, vector<vector<int>>& adj) {
vis[node] = 1;
for(auto it : adj[node]) {
if(!vis[it]) {
dfs(it, vis, adj);
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int x;
cin >> x;
if(x == 1) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
vector<int> vis(n, 0);
int cc = 0;
for(int i = 0; i < n; i++) {
if(!vis[i]) {
dfs(i, vis, adj);
cc++;
}
}
cout << cc << endl;
return 0;
}
Telegram:- @allcoding1
using namespace std;
void dfs(int node, vector<int>& vis, vector<vector<int>>& adj) {
vis[node] = 1;
for(auto it : adj[node]) {
if(!vis[it]) {
dfs(it, vis, adj);
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int x;
cin >> x;
if(x == 1) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
vector<int> vis(n, 0);
int cc = 0;
for(int i = 0; i < n; i++) {
if(!vis[i]) {
dfs(i, vis, adj);
cc++;
}
}
cout << cc << endl;
return 0;
}
Telegram:- @allcoding1
👍7
def count(a, X):
a.sort()
ans = 0
n = len(a)
for i in range(n):
if (i < n // 2):
ans += max(0, a[i] - X)
elif (i == n // 2):
ans += abs(X - a[i])
else:
ans += max(0, X - a[i]);
return ans
IBM
Telegram:- @allcoding1
a.sort()
ans = 0
n = len(a)
for i in range(n):
if (i < n // 2):
ans += max(0, a[i] - X)
elif (i == n // 2):
ans += abs(X - a[i])
else:
ans += max(0, X - a[i]);
return ans
IBM
Telegram:- @allcoding1
👍3👏1