๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.58K subscribers
5.59K photos
3 videos
95 files
10K links
๐ŸšฉMain Group - @SuperExams
๐Ÿ“Job Updates - @FresherEarth

๐Ÿ”ฐAuthentic Coding Solutions(with Outputs)
โš ๏ธDaily Job Updates
โš ๏ธHackathon Updates & Solutions

Buy ads: https://telega.io/c/cs_algo
Download Telegram
int reachNumber(int target) {
    const int newTarget = abs(target);
    int ans = 0;
    int pos = 0;

    while (pos < newTarget)
      pos += ++ans;
    while ((pos - newTarget) & 1)
      pos += ++ans;

    return pos;
}

pos return karo instead of ans

Cross Number Puzzle โœ…
def min_remaining_length(seq):
    stack = []
    for char in seq:
        if char == "A" or char == "B":
            if stack and stack[-1] == "A" and char == "B":
                stack.pop()
            elif stack and stack[-1] == "B" and char == "B":
                stack.pop()
            else:
                stack.append(char)
        else:
            stack.append(char)
   
    return len(stack)

Substring Removal โœ…
JPMC code for good
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
import java.util.*;

public class Main {
    public static String getLargestNumber(String num) {
        StringBuilder ans = new StringBuilder();
        StringBuilder s = new StringBuilder();
       
        for (int i = 0; i < num.length() - 1; i++) {
            if ((num.charAt(i) - '0') % 2 == (num.charAt(i + 1) - '0') % 2) {
                s.append(num.charAt(i));
            } else {
                s.append(num.charAt(i));
                char[] chars = s.toString().toCharArray();
                Arrays.sort(chars);
                ans.append(new StringBuilder(new String(chars)).reverse());
                s.setLength(0);
            }
        }
       
        s.append(num.charAt(num.length() - 1));
        char[] chars = s.toString().toCharArray();
        Arrays.sort(chars);
        ans.append(new StringBuilder(new String(chars)).reverse());
       
        return ans.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        scanner.close();
        System.out.println(getLargestNumber(s));
    }
}

Swap Parity โœ…
JPMC code for good
๐Ÿ‘1
#include <bits/stdc++.h>
using namespace std;

int countWithOddSetBits(int n)
{

if (n % 2 != 0)
  return (n + 1) / 2;

int count = __builtin_popcount(n);

int ans = n / 2;
if (count % 2 != 0)
  ans++;
return ans;
}


int main()
{
int l,r;
cin>>l>>r;
cout << countWithOddSetBits(r)-countWithOddSetBits(l);
return 0;
}

Hackwithinfy โœ…
๐Ÿ‘1
def count_good_numbers(L, R):
    count = 0
    for num in range(L, R+1):
        binary = bin(num)[2:]
        set_bits = binary.count('1')
        if set_bits % 2 == 1:
            count += 1
    return count

L = int(input())
R = int(input())

print(count_good_numbers(L, R))
int countWays(int N, int M, int P) {
    vector<vector<int>> dp(N + 1, vector<int>(1 << M));
    dp[0][0] = 1;

    for (int i = 1; i <= N; i++) {
        for (int mask = 0; mask < (1 << M); mask++) {
            for (int prevMask = 0; prevMask < (1 << M); prevMask++) {
                if ((mask & prevMask) == 0 && (mask & (prevMask << 1)) == 0 && (prevMask & (mask << 1)) == 0) {
                    dp[i][mask] = (dp[i][mask] + dp[i - 1][prevMask]) % P;
                }
            }
        }
    }

    int totalWays = 0;
    for (int mask = 0; mask < (1 << M); mask++) {
        totalWays = (totalWays + dp[N][mask]) % P;
    }

    return totalWays;
}

Lonely pieces
Hackwithinfy โœ…
๐Ÿ‘Ž1
vector<ll> solve(vector<int>& p, vector<int>& q) {
    sort(p.begin(), p.end());
    vector<ll> pre(p.size() + 1);
    for (int i = 0; i < p.size(); ++i) {
        pre[i + 1] = pre[i] + p[i];
    }
    vector<ll> res;
    for (int x : q) {
        int idx = lower_bound(p.begin(), p.end(), x) - p.begin();
        ll total = pre[p.size()] - 2 * pre[idx] + (ll)x * (2 * idx - p.size());
        res.push_back(total);
    }
    return res;
}

Equal Price โœ…
#include <bits/stdc++.h>
using namespace std;

bool isPalindrome(string s) {
    int i = 0, j = s.size() - 1;
    while (i < j) {
        if (s[i] != s[j]) {
            return false;
        }
        i++;
        j--;
    }
    return true;
}

int getScore(string s, int L, int R) {
    int freq[26] = {0};
    for (int i = L; i < R; i++) {
        freq[s[i] - 'a']++;
    }
    int oddCount = 0;
    for (int i = 0; i < 26; i++) {
        if (freq[i] % 2 != 0) {
            oddCount++;
        }
    }
    return max(0, oddCount - 1);
}

int main() {
    string S;
    cin >> S;

    int Q;
    cin >> Q;

    int sum = 0;
    for (int q = 0; q < Q; q++) {
        int type;
        cin >> type;

        if (type == 1) {
            int i, j;
            cin >> i >> j;
            S[i - 1] = 'a' + j - 1;
        } else if (type == 2) {
            int L, R;
            cin >> L >> R;
            sum += getScore(S, L - 1, R);
        }
    }

    cout << sum % (int)(1e9 + 7) << endl;
    return 0;
}



//Score of stringโœ…
๐Ÿ‘2
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include<bits/stdc++.h>
using namespace std;

vector<string> getResponses(vector<string> valid_auth_tokens, vector<pair<string, string>> requests) {
    vector<string> res;
    for(auto request : requests) {
        string rt = request.first;
        string url = request.second;
        string params = url.substr(url.find('?') + 1);
        stringstream ss(params);
        string token;
        string elon;
        string au = "";
        string csr = "";
        while(getline(ss, elon, '&')) {
            string key = elon.substr(0, elon.find('='));
            string value = elon.substr(elon.find('=') + 1);
            if(key == "token") {
                au = value;
            } else if(key == "csrf") {
                csr = value;
            }
        }
        if(find(valid_auth_tokens.begin(), valid_auth_tokens.end(), au) == valid_auth_tokens.end()) {
            res.push_back("INVALID");
        } else if(rt == "POST" && (csr.empty() !all_of(csr.begin(), csr.end(), ::isalnum) csr.size() < 8)) {
            res.push_back("INVALID");
        } else {
            string rss = "VALID";
            stringstream ss(params);
            while(getline(ss, elon, '&')) {
                string key = elon.substr(0, elon.find('='));
                string value = elon.substr(elon.find('=') + 1);
                if(key != "token" && key != "csrf") {
                    rss += "," + key + "," + value;
                }
            }
            res.push_back(rss);
        }
    }
    return res;
}

Request Parser โœ…
Source : Hola
๐Ÿ‘Ž4โค2๐Ÿ‘1
def remove_duplicate_words(sentence):
    words = sentence.split()
    unique_words = []
    seen = set()
    for word in words:
        if word not in seen:
            unique_words.append(word)
            seen.add(word)
    return ' '.join(unique_words)

T = int(input())
for _ in range(T):
    N = int(input())
    sentence = input()

    print(remove_duplicate_words(sentence))

GWC โœ