class Solution {
public:
void bfs(vector<vector<int>>&vis,vector<vector<char>>&grid,int i,int j,int n,int m)
{
vis[i][j]=1;
queue<pair<int,int>>q;
q.push({i,j});
while(!q.empty())
{
int row=q.front().first;
int col=q.front().second;
q.pop();
int delrow[4]={1,0,-1,0};
int delcol[4]={0,1,0,-1};
for(int k=0;k<=3;k++){
int nrow=row+delrow[k];
int ncol=col+delcol[k];
if(nrow>=0 and nrow<n and ncol>=0 and ncol<m and grid[nrow][ncol]=='1' and !vis[nrow][ncol])
{
vis[nrow][ncol]=1;
q.push({nrow,ncol});
}
}
}
}
int numIslands(vector<vector<char>>& grid) {
int n=grid.size();
int m=grid[0].size();
vector<vector<int>>vis(n,vector<int>(m,0));
int cnt=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(!vis[i][j] and grid[i][j]=='1')
{
cnt++;
bfs(vis,grid,i,j,n,m);
}
}
}
return cnt;
}
};
Zeta β
Telegram:- @allcoding1_official
public:
void bfs(vector<vector<int>>&vis,vector<vector<char>>&grid,int i,int j,int n,int m)
{
vis[i][j]=1;
queue<pair<int,int>>q;
q.push({i,j});
while(!q.empty())
{
int row=q.front().first;
int col=q.front().second;
q.pop();
int delrow[4]={1,0,-1,0};
int delcol[4]={0,1,0,-1};
for(int k=0;k<=3;k++){
int nrow=row+delrow[k];
int ncol=col+delcol[k];
if(nrow>=0 and nrow<n and ncol>=0 and ncol<m and grid[nrow][ncol]=='1' and !vis[nrow][ncol])
{
vis[nrow][ncol]=1;
q.push({nrow,ncol});
}
}
}
}
int numIslands(vector<vector<char>>& grid) {
int n=grid.size();
int m=grid[0].size();
vector<vector<int>>vis(n,vector<int>(m,0));
int cnt=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(!vis[i][j] and grid[i][j]=='1')
{
cnt++;
bfs(vis,grid,i,j,n,m);
}
}
}
return cnt;
}
};
Zeta β
Telegram:- @allcoding1_official
π5
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<int> a, int n, int k) {
vector<int> v;
deque<int> dq;
for (int i = 0; i < n; i++) {
while (!dq.empty() && dq.front() <= i - k)
dq.pop_front();
while (!dq.empty() && a[dq.back()] <= a[i])
dq.pop_back();
dq.push_back(i);
if (i >= k - 1)
v.push_back(a[dq.front()]);
}
return v;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<int> result = solution(a, n, k);
for (int i = 0; i < result.size(); i++)
cout << result[i] << " ";
return 0;
}.
//cricket match β
Zeta
Telegram:- @allcoding1_official
using namespace std;
vector<int> solution(vector<int> a, int n, int k) {
vector<int> v;
deque<int> dq;
for (int i = 0; i < n; i++) {
while (!dq.empty() && dq.front() <= i - k)
dq.pop_front();
while (!dq.empty() && a[dq.back()] <= a[i])
dq.pop_back();
dq.push_back(i);
if (i >= k - 1)
v.push_back(a[dq.front()]);
}
return v;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<int> result = solution(a, n, k);
for (int i = 0; i < result.size(); i++)
cout << result[i] << " ";
return 0;
}.
//cricket match β
Zeta
Telegram:- @allcoding1_official
π4
string make_string_S_to_T(string S) {
string T=βprogrammingβ;
bool possible = false;
int M = T.length();
int N = S.length();
for (int i = 0; i <= M; i++) {
int prefix_length = i;
int suffix_length = M - i;
string prefix = S.substr(0, prefix_length);
string suffix = S.substr(N - suffix_length, suffix_length);
if (prefix + suffix == T) {
possible = true;
break;
}
}
if (possible)
return "YES";
else
return "NO";
}
Deleting substring β
Zeta
Telegram:- @allcoding1_official
string T=βprogrammingβ;
bool possible = false;
int M = T.length();
int N = S.length();
for (int i = 0; i <= M; i++) {
int prefix_length = i;
int suffix_length = M - i;
string prefix = S.substr(0, prefix_length);
string suffix = S.substr(N - suffix_length, suffix_length);
if (prefix + suffix == T) {
possible = true;
break;
}
}
if (possible)
return "YES";
else
return "NO";
}
Deleting substring β
Zeta
Telegram:- @allcoding1_official
π6
π5β€βπ₯1π1
allcoding1_official
https://www.allcoding1.com/2023/12/allcoding1-answers.html?m=1
Copy and paste question
π―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_official
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_official
π6
import java.util.Scanner;
import java.util.*;
public class metting
{
public static void helperFunction()
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] edges = new int[n];
for (int i = 0; i < n; i++)
{
edges[i] = sc.nextInt();
}
int C1 = sc.nextInt();
int C2 = sc.nextInt();
// int ans=minimumWeight(n,edges,C1,C2);
// System.out.println(ans);
// public static int minimumWeight(int n, int[] edges, int C1, int C2) {
List<List<Integer>> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(new ArrayList<Integer>());
}
for (int i = 0; i < n; i++) {
if (edges[i] != -1) {
list.get(i).add(edges[i]);
}
}
long[] array1 = new long[n];
long[] array2 = new long[n];
Arrays.fill(array1, Long.MAX_VALUE);
Arrays.fill(array2, Long.MAX_VALUE);
juspay(C1, list, array1);
juspay(C2, list, array2);
int node = 0;
long dist = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (array1[i] == Long.MAX_VALUE || array2[i] == Long.MAX_VALUE)
continue;
if (dist > array1[i] + array2[i]) {
dist = array1[i] + array2[i];
node = i;
}
}
if (dist == Long.MAX_VALUE)
System.out.print(-1);
//return -1;
// return node;
System.out.print(node);
}
private static void juspay(int start, List<List<Integer>> graph, long[] distances)
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(start);
distances[start] = 0;
while (!pq.isEmpty())
{
int curr = pq.poll();
for (int neighbor : graph.get(curr))
{
long distance = distances[curr] + 1;
if (distance < distances[neighbor])
{
distances[neighbor] = distance;
pq.offer(neighbor);
}
}
}
}
public static void main(String[] args) {
metting m = new metting();
metting.helperFunction();
}
}
Nearest meeting Cell
Juspay β
Telegram:- @allcoding1_official
import java.util.*;
public class metting
{
public static void helperFunction()
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] edges = new int[n];
for (int i = 0; i < n; i++)
{
edges[i] = sc.nextInt();
}
int C1 = sc.nextInt();
int C2 = sc.nextInt();
// int ans=minimumWeight(n,edges,C1,C2);
// System.out.println(ans);
// public static int minimumWeight(int n, int[] edges, int C1, int C2) {
List<List<Integer>> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(new ArrayList<Integer>());
}
for (int i = 0; i < n; i++) {
if (edges[i] != -1) {
list.get(i).add(edges[i]);
}
}
long[] array1 = new long[n];
long[] array2 = new long[n];
Arrays.fill(array1, Long.MAX_VALUE);
Arrays.fill(array2, Long.MAX_VALUE);
juspay(C1, list, array1);
juspay(C2, list, array2);
int node = 0;
long dist = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (array1[i] == Long.MAX_VALUE || array2[i] == Long.MAX_VALUE)
continue;
if (dist > array1[i] + array2[i]) {
dist = array1[i] + array2[i];
node = i;
}
}
if (dist == Long.MAX_VALUE)
System.out.print(-1);
//return -1;
// return node;
System.out.print(node);
}
private static void juspay(int start, List<List<Integer>> graph, long[] distances)
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(start);
distances[start] = 0;
while (!pq.isEmpty())
{
int curr = pq.poll();
for (int neighbor : graph.get(curr))
{
long distance = distances[curr] + 1;
if (distance < distances[neighbor])
{
distances[neighbor] = distance;
pq.offer(neighbor);
}
}
}
}
public static void main(String[] args) {
metting m = new metting();
metting.helperFunction();
}
}
Nearest meeting Cell
Juspay β
Telegram:- @allcoding1_official
π4β€1
package Graph;
import java.util.*;
public class Largest_Sum_Cycle
{
public static int solution(int arr[])
{
ArrayList<Integer>sum=new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
ArrayList<Integer>path=new ArrayList<>();
int j=i;
int t=0;
while(arr[j]<arr.length&&arr[j]!=i&&arr[j]!=-1&&!path.contains(j))
{
path.add(j);
t+=j;
j=arr[j];
if(arr[j]==i)
{
t+=j;
break;
}
}
if(j<arr.length&&i==arr[j])
sum.add(t);
}
if(sum.isEmpty())
return -1;
return Collections.max(sum);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
for(int loop=0;loop<testcases;loop++)
{
int numofBlocks=sc.nextInt();
int arr[]=new int[numofBlocks];
int src,dest;
for(int i=0;i<numofBlocks;i++)
{
arr[i]=sc.nextInt();
}
System.out.println(solution(arr));
}
}
}
Juspay β
Telegram:- @allcoding1_official
import java.util.*;
public class Largest_Sum_Cycle
{
public static int solution(int arr[])
{
ArrayList<Integer>sum=new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
ArrayList<Integer>path=new ArrayList<>();
int j=i;
int t=0;
while(arr[j]<arr.length&&arr[j]!=i&&arr[j]!=-1&&!path.contains(j))
{
path.add(j);
t+=j;
j=arr[j];
if(arr[j]==i)
{
t+=j;
break;
}
}
if(j<arr.length&&i==arr[j])
sum.add(t);
}
if(sum.isEmpty())
return -1;
return Collections.max(sum);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
for(int loop=0;loop<testcases;loop++)
{
int numofBlocks=sc.nextInt();
int arr[]=new int[numofBlocks];
int src,dest;
for(int i=0;i<numofBlocks;i++)
{
arr[i]=sc.nextInt();
}
System.out.println(solution(arr));
}
}
}
Juspay β
Telegram:- @allcoding1_official
π6
π―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_official
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_official
π6
#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_official
#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_official
π12π1
#include<bits/stdc++.h>
using namespace std;
class hm {
unordered_map<int, int> m;
int k = 0, v = 0;
public:
void i(int x, int y) {
x -= k;
y -= v;
m[x] = y;
}
int g(int x) {
x -= k;
if (m.find(x) == m.end()) {
return -1;
}
return m[x] + v;
}
void ak(int x) {
k += x;
}
void av(int y) {
v += y;
}
};
int solve(vector<string>& qt, vector<vector<int>>& q) {
hm h;
int s = 0;
for (int i = 0; i < qt.size(); ++i) {
if (qt[i] == "insert") {
h.i(q[i][0], q[i][1]);
} else if (qt[i] == "addToKey") {
h.ak(q[i][0]);
} else if (qt[i] == "addToValue") {
h.av(q[i][0]);
} else if (qt[i] == "get") {
int v = h.g(q[i][0]);
if (v != -1) {
s += v;
}
}
}
return s;
}
Telegram:- @allcoding1_official
using namespace std;
class hm {
unordered_map<int, int> m;
int k = 0, v = 0;
public:
void i(int x, int y) {
x -= k;
y -= v;
m[x] = y;
}
int g(int x) {
x -= k;
if (m.find(x) == m.end()) {
return -1;
}
return m[x] + v;
}
void ak(int x) {
k += x;
}
void av(int y) {
v += y;
}
};
int solve(vector<string>& qt, vector<vector<int>>& q) {
hm h;
int s = 0;
for (int i = 0; i < qt.size(); ++i) {
if (qt[i] == "insert") {
h.i(q[i][0], q[i][1]);
} else if (qt[i] == "addToKey") {
h.ak(q[i][0]);
} else if (qt[i] == "addToValue") {
h.av(q[i][0]);
} else if (qt[i] == "get") {
int v = h.g(q[i][0]);
if (v != -1) {
s += v;
}
}
}
return s;
}
Telegram:- @allcoding1_official
π16β€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;
}
π6π3π1
allcoding1_official
Photo
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RequestParser {
public static List<string> getResponses(String[] validAuthTokens, String[][] requests) {
List<string> responses = new ArrayList<>();
Set<string> validTokensSet = new HashSet<>(Arrays.asList(validAuthTokens));
for (String[] request : requests) {
String requestType = request[0];
String url = request[1];
// Extract the token and other parameters from the URL
String[] urlParts = url.split("\\?");
String[] params = urlParts[1].split("&");
String token = null;
String csrf = null;
StringBuilder otherParams = new StringBuilder();
for (String param : params) {
String[] keyValue = param.split("=");
if (keyValue[0].equals("token")) {
token = keyValue[1];
} else if (keyValue[0].equals("csrf")) {
csrf = keyValue[1];
} else {
otherParams.append(",").append(keyValue[0]).append(",").append(keyValue[1]);
}
}
// Validate the authentication token
if (!validTokensSet.contains(token)) {
responses.add("INVALID");
continue;
}
// Validate the request type and CSRF token for POST requests
if (requestType.equals("POST")) {
if (csrf == null || !csrf.matches("[a-z0-9]{8,}")) {
responses.add("INVALID");
continue;
}
}
// If all validations pass, construct the response string
if (otherParams.length() > 0) {
responses.add("VALID" + otherParams);
} else {
responses.add("VALID");
}
}
return responses;
}
public static void main(String[] args) {
String[] validAuthTokens = {"ah37j2ha483u", "safh34ywb0p5", "ba34wyi8t902"};
String[][] requests = {
{"GET", "https://example.com/?token=347sd6yk8iu2&name=alex"},
{"GET", "https://example.com/?token=safh34ywb0p5&name=sam"},
{"POST", "https://example.com/?token=safh34ywb0p5&name=alex"},
{"POST", "https://example.com/?token=safh34ywb0p5&csrf=ak2sh32dy&name=chri$"}
};
List<string> responses = getResponses(validAuthTokens, requests);
System.out.println(responses); // Output: [INVALID, VALID,name,sam, INVALID, VALID,name,chri$]
}
}
Telegram:- @allcoding1_official
Java
π8