allcoding1_official
106K subscribers
765 photos
2 videos
73 files
757 links
Download Telegram
🎯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
👍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
👍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
👍1621
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;
}
👍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&lt;&gt;();

        Set<string> validTokensSet = new HashSet&lt;&gt;(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("&amp;");
           
            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() &gt; 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&amp;name=alex"},
            {"GET", "https://example.com/?token=safh34ywb0p5&amp;name=sam"},
            {"POST", "https://example.com/?token=safh34ywb0p5&amp;name=alex"},
            {"POST", "https://example.com/?token=safh34ywb0p5&amp;csrf=ak2sh32dy&amp;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
👍

Done
👍2
allcoding1_official
Photo
import java.util.Scanner;

public class SubstringMinimization {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
       
        int N = Integer.parseInt(scanner.nextLine());
        String s = scanner.nextLine();
        int q = Integer.parseInt(scanner.nextLine());

        int[][] queries = new int[q][2];
        char[] characters = new char[q];

        for (int i = 0; i &lt; q; i++) {
            String[] queryStr = scanner.nextLine().split(" ");
            queries[i][0] = Integer.parseInt(queryStr[0]);
            queries[i][1] = Integer.parseInt(queryStr[1]);
        }

        String[] charStr = scanner.nextLine().split(" ");
        for (int i = 0; i &lt; q; i++) {
            characters[i] = charStr[i].charAt(0);
        }
       
        int[] result = solve(N, s, q, queries, characters);

        for (int res : result) {
            System.out.print(res + " ");
        }
    }

    public static int[] solve(int N, String s, int q, int[][] queries, char[] characters) {
        int[] ans = new int[q];

        for (int i = 0; i &lt; q; i++) {
            int left = queries[i][0] - 1;
            int right = queries[i][1] - 1;
            char x = characters[i];
            ans[i] = getMinLength(s, left, right, x);
        }

        return ans;
    }

    private static int getMinLength(String s, int left, int right, char x) {
        StringBuilder sb = new StringBuilder(s.substring(left, right + 1));
        boolean changed = true;

        while (changed) {
            changed = false;
            for (int i = 0; i &lt; sb.length() - 1; i++) {
                if (sb.charAt(i) == x &amp;&amp; sb.charAt(i + 1) == x) {
                    sb.delete(i, i + 2);
                    changed = true;
                    break;
                }
            }
        }

        return sb.length();
    }
}


Telegram:- @allcoding1_official
👍142👎1
Questions....
public class Main {
public static void main(String[] args) {
int[] arr = {3, 3, 4, 7, 8};
int d = 5;
System.out.println(getTripletCount(arr, d));
}

public static int getTripletCount(int[] arr, int d) {
int count = 0;
for (int i = 0; i &lt; arr.length - 2; i++) {
for (int j = i + 1; j &lt; arr.length - 1; j++) {
for (int k = j + 1; k &lt; arr.length; k++) {
if ((arr[i] + arr[j] + arr[k]) % d == 0) {
count++;
}
}
}
}
return count;
}
}

Telegram:- @allcoding1_official
Java
👍41💯1