๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.57K 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
#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
    ll N;
    cin >> N;
    vector<ll> H(N);
    for(ll i = 0; i < N; i++) {
        cin >> H[i];
    }
    sort(H.begin(), H.end());
    ll min = 0, maxxi = 0;
    for(ll i = 0; i < N-1; i++) {
        min += abs(H[i] - H[i+1]);
        maxxi += abs(H[i] - H[N-1]);
    }
    cout << min << " " << maxxi << endl;
    return 0;
}

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

int main() {
    long long P;
    cin >> P;
    vector<long long> coins = {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
    int count = 0;
    for(int i = coins.size() - 1; i >= 0; i--) {
        while(P >= coins[i]) {
            P -= coins[i];
            count++;
        }
    }
    cout << count << endl;
    return 0;
}

GWC โœ…
๐Ÿ‘1
ll solve(vector<ll>&a,ll k)
{
    ll n=a.size();
    vector<ll>dp(n);
    for(ll i=n-1;i>=0;i--)
    {
        if(i+k<n) dp[i]=a[i]+dp[i+k];
        else dp[i]=a[i];
    }
    ll ans=INT_MIN;
   // for(auto it:dp) cout<<it<<" ";
    for(auto it:dp) ans=max(ans,it);
    return ans;
}



maximum information โœ…
c++20
๐Ÿ‘1
int findTotalCost(vector<int>& arr) {
    multiset<long> st(arr.begin(), arr.end());
    long cost = 0;
    while (st.size() > 1) {
        long t1 = *st.begin();
        st.erase(st.begin());
        long t2 = *st.rbegin();
        st.erase(prev(st.end()));
        long new_element = t1 + t2;
        st.insert(new_element);
        cost += ceil((double)new_element / (t2 - t1 + 1));
    }
    return (int)cost;
}


Array reduction 3 โœ…
c++
def mysterious_third_number(A, B, C):
    if A == B:
        return C
    elif A == C:
        return B
    elif B == C:
        return A
    else:
        return 0


A, B, C = map(int, input().split())

print(mysterious_third_number(A, B, C))

GWC โœ…
set<int> y;
cin >> n;
for (int i = 0; i < n; ++i)
{
  cin >> a;
  y.insert(a);
}
while(1){
  int p = *(y.rbegin());
  int m = p;
  while(p && y.count(p)){
   p/=2;
  }
 
  if(!p){
   break;
  }
  y.erase(m);
  y.insert(p);
}
Int count;
If(!y.empty)
{
Count=*(y.rbegin());
}
Return count

Distinct sets โœ…
๐Ÿ‘2
#include <iostream>
#include <string>

using namespace std;

bool isGoodString(string s) {
    int count = 0;
    for (char c : s) {
        if (c == 'a') {
            count++;
        } else {
            count--;
        }
        if (count < 0) {
            return false;
        }
    }
    return count == 0;
}

int main() {
    string s = "aaabbbaaa";
    cout << "Is the string good? " << (isGoodString(s) ? "Yes" : "No") << endl;
    return 0;
}

Count Good Triples โœ…
C++
HackWithInfy
๐Ÿ‘1
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

const int MOD = 1e9 + 7;

vector<vector<int>> tree;
vector<int> a;
unordered_map<int, int> countMap;

bool checkPalindrome(unordered_map<int, int>& countMap) {
    int oddCount = 0;
    for (auto& it : countMap) {
        if (it.second % 2 != 0) oddCount++;
        if (oddCount > 1) return false;
    }
    return true;
}

int dfs(int node) {
    int ans = 0;
    countMap[a[node]]++;
   
    if (checkPalindrome(countMap)) {
        ans = 1;
    }
   
    for (auto& child : tree[node]) {
        ans += dfs(child);
        ans %= MOD;
    }
   
    countMap[a[node]]--; // Backtrack to remove the current node's count
    return ans;
}

int main() {
    int n;
    cin >> n;
   
    tree.resize(n + 1);
    a.resize(n + 1);
    vector<int> par(n + 1);
   
    for (int i = 2; i <= n; i++) {
        cin >> par[i];
        tree[par[i]].push_back(i);
    }
   
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
   
    int ans = dfs(1);
    cout << ans << endl;
   
    return 0;
}



C++ code Palindromic subtrees
#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int MOD = 1e9 + 7;

int countGoodStrings(int N, int M, string S) {
    long long totalGoodStrings = 1;
    for (int i = 0; i &lt; N; i++) {
        totalGoodStrings = (totalGoodStrings * M) % MOD;
    }
    return totalGoodStrings;
}

int main() {
    int N, M;
    cin &gt;&gt; N &gt;&gt; M;
   
    string S;
    cin &gt;&gt; S;
   
    int result = countGoodStrings(N, M, S);
    cout &lt;&lt; result &lt;&lt; endl;
   
    return 0;
}

C++

Good strings code

HackWithInfyโœ…
๐Ÿ‘2
To solve this problem, we can use dynamic programming and traverse the tree formed by the cities and roads. We'll maintain two arrays: one for the maximum potatoes Bob can buy if he visits a city (max_potatoes) and another for the maximum potatoes Bob can buy if the city is on his way (max_potatoes_on_way).

Here's the Python code to achieve this:

def max_potatoes(N, U, V, M, C, B, W):
    # Build adjacency list representation of the tree
    adj_list = [[] for _ in range(N+1)]
    for i in range(N-1):
        adj_list[U[i]].append(V[i])
        adj_list[V[i]].append(U[i])
   
    # Initialize arrays to store maximum potatoes
    max_potatoes = [0] * (N+1)
    max_potatoes_on_way = [0] * (N+1)
   
    # Perform DFS traversal
    def dfs(node, parent):
        potatoes_bought = 0
        potatoes_on_way = 0
       
        # Check if current city is in the list of cities to visit
        if node in W:
            potatoes_bought = M[node] if C[node] <= B else 0
            potatoes_on_way = M[node]
       
        # Traverse children
        for child in adj_list[node]:
            if child != parent:
                child_potatoes, child_potatoes_on_way = dfs(child, node)
                potatoes_bought += child_potatoes
                potatoes_on_way += max(child_potatoes, child_potatoes_on_way)
       
        # Update maximum potatoes arrays
        max_potatoes[node] = potatoes_bought
        max_potatoes_on_way[node] = potatoes_on_way
       
        return potatoes_bought, potatoes_on_way
   
    dfs(W[0], -1)  # Start DFS traversal from the first city in the list
   
    return max(max_potatoes_on_way)

# Example usage:
N = 7
U = [1, 1, 2, 2, 3, 4]
V = [2, 3, 4, 5, 6, 7]
M = [10, 20, 30, 40, 50, 60, 70]
C = [5, 10, 15, 20, 25, 30, 35]
B = 60
W = [1, 2, 3, 4, 5, 6, 7]

print(max_potatoes(N, U, V, M, C, B, W))  # Output: 170

This function max_potatoes takes inputs N, U, V, M, C, B, and W as described in the problem statement and returns the maximum potatoes Bob can buy. It performs a depth-first search (DFS) traversal of the tree formed by the cities and roads, calculating the maximum potatoes Bob can buy if he visits a city or if it's on his way. Finally, it returns the maximum of potatoes on the way.
โค2๐Ÿ‘2
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MOD = 1e9 + 7;

int main() {
    int N, Q;
   // cout<<"enter n\n";
    cin >> N;

    vector<int> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }

   // cout<<"enter q\n";
    cin >> Q;

    long long sum = 0;
    for (int q = 0; q < Q; ++q) {
        //cout<<"Enter query " << q <<endl;
        int type, L, R, X, i , zero1 , zero2;
        cin >> type;

        if (type == 1) {
            cin >> L >> R >> X;
            for (int j = L-1; j < R; ++j) {
                A[j] = min(A[j], X);
            }
        } else if (type == 2) {
           
            cin >> i >> zero1 >>zero2;
            //cout<<"value of i and A[i] is " << i<<" " << A[i]<<endl;
            sum = (sum + A[i - 1]) % MOD;
        }

    }

    cout << sum << endl;

    return 0;
}

Replace by minimum
๐Ÿ‘1
MOD = 10**9 + 7

def count_good_subsequences(arr, K):
    n = len(arr)
    dp = [[0] * (K + 2) for _ in range(n + 1)]
   
    for i in range(1, n + 1):
        dp[i][0] = 1
        for j in range(1, K + 2):
            dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % MOD
           
    result = 0
    for i in range(n):
        for j in range(K + 1):
            left = i - j
            if left >= 0:
                result = (result + dp[left][K - j]) % MOD
               
    return result

# Example usage:
N = int(input())
A = [int(input()) for _ in range(N)]
K = int(input())

print(count_good_subsequences(A, K))

Count Subsequence
MOD = 10**9 + 7

def solve(arrival_departure):
    arrival_departure.sort(key=lambda x: x[1])
    prev_departure = -1
    total_stations = 0

    for arrival, departure in arrival_departure:
        if arrival > prev_departure:
            total_stations += 1
            prev_departure = departure

    return total_stations % MOD

def main():
    N = int(input())
    arrival_departure = []
    for _ in range(N):
        arrival, departure = map(int, input().split())
        arrival_departure.append((arrival, departure))
   
    result = solve(arrival_departure)
    print(result)

if name == "main":
    main()

Trains Code
Python
HackWithInfy
๐Ÿ‘1
#include <iostream>
#include <vector>
#include <set>

using namespace std;

int getSmallestArea(vector<vector<int>>& grid) {
    int rows = grid.size();
    if (rows == 0) return 0;
    int cols = grid[0].size();
    if (cols == 0) return 0;

    set<int> rowsSet, colsSet;

    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            if (grid[i][j] == 1) {
                rowsSet.insert(i);
                colsSet.insert(j);
            }
        }
    }

    int width = colsSet.empty() ? 0 : *colsSet.rbegin() - *colsSet.begin() + 1;
    int height = rowsSet.empty() ? 0 : *rowsSet.rbegin() - *rowsSet.begin() + 1;

    return width * height;


shipping spaceโœ…
Salesforce