๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.52K subscribers
5.55K photos
3 videos
95 files
9.64K 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
public static void main (String[] args) throws java.lang.Exception
  {
        int tes=1;
        StringBuilder sb=new StringBuilder();
      lable:for(int fuck=1;fuck<=tes;fuck++)
      {
           int n=sc.nextInt(),i;
           ArrayList<ArrayList<Integer>> arr=new ArrayList<>();
           for(i=0;i<=n;i++) arr.add(new ArrayList<>());
           for(i=2;i<=n;i++){
            int x=sc.nextInt();
            arr.get(x).add(i);
            arr.get(i).add(x);
           }
           int dp[]=new int[n+1];
           int okk=dfs(1,0,dp,arr);
           for(i=1;i<=n;i++) System.out.print(dp[i]+" ");
        }
    }
    public static int dfs(int u,int p,int dp[],ArrayList<ArrayList<Integer>> arr){
        for(int it:arr.get(u)){
            if(it==p) continue;
            dp[u]=dp[u]+dfs(it,u,dp,arr)+1;
        }
        return dp[u];
    }


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

vector<int> solve(int V, vector<int>& bosses) {
    vector<int> h(V, 0);
    for(int i = 0; i < V-1; i++) {
        int b = bosses[i];
        while(b != -1) {
            h[b-1]++;
            b = (b-1 > 0 ? bosses[b-2] : -1);
        }
    }
    return h;
}

int main() {
    int V;
    cin >> V;
    vector<int> b(V-1);
    for(int i = 0; i < V-1; i++) {
        cin >> b[i];
    }
    vector<int> h = solve(V, b);
    for(int i = 0; i < V; i++) {
        cout << h[i] << " ";
    }
    cout << endl;
    return 0;
}


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

void twiceIsEnough(string &s){
    int n=s.length();
    int i=0,j=0,ans=0;
    unordered_map<char,int>mp;
    while(i<n){
        while(j<i && mp[s[i]]==2){
            mp[s[j]]--;
            if(mp[s[j]]==0)mp.erase(s[j]);
            j++;
        }
        mp[s[i]]++;
        ans=max(ans,i-j+1);
        i++;
    }
    cout<<ans<<endl;
}

int main(){
    string s;
    cin>>s;
    twiceIsEnough(s);
}

first non repeating and twice is enoughโœ…
int a,b,c;
    cin>>a>>b>>c;
    int d=a&b;
    int ans=0;
    for(int i=0;i<32;i++)
    {
        if((c&(1<<i)))
        {
        if(!(d&(1<<i)))
           ans=ans|(1<<i);
        }
        else if((d&(1<<i)))
        {
            cout<<-1<<endl;
            return 0;
        }
        
    }
    cout<<ans<<endl;
    return 0;



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

int maxDiff(vector<int>& a) {
    int n = a.size();
    vector<int> ng(n), mr(n);
    stack<int> s;
    for(int i = n-1; i >= 0; i--) {
        while(!s.empty() && s.top() <= a[i]) {
            s.pop();
        }
        ng[i] = s.empty() ? 0 : s.top();
        s.push(a[i]);
    }
    mr[n-1] = a[n-1];
    for(int i = n-2; i >= 0; i--) {
        mr[i] = max(mr[i+1], a[i]);
    }
    int res = 0;
    for(int i = 0; i < n; i++) {
        if(ng[i] > 0) {
            res = max(res, abs(ng[i] - mr[i]));
        }
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    cout << maxDiff(a) << endl;
    return 0;
}


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

void twiceIsEnough(string &s){
    int n=s.length();
    int i=0,j=0,ans=0;
    unordered_map<char,int>mp;
    while(i<n){
        while(j<i && mp[s[i]]==2){
            mp[s[j]]--;
            if(mp[s[j]]==0)mp.erase(s[j]);
            j++;
        }
        mp[s[i]]++;
        ans=max(ans,i-j+1);
        i++;
    }
    cout<<ans<<endl;
}

int main(){
    string s;
    cin>>s;
    twiceIsEnough(s);
}

Twice is enough โœ…
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int dp[100001];
int solve(int n,vector<int>& ar,int ci){
    if(ci>=n) return INT_MAX;
    if(ci==n-1){
        return 0;
    }
    if(dp[ci]!=-1) return dp[ci];
    int a=INT_MAX,b=INT_MAX,c=INT_MAX;
    if(ci+1<n){
        int a1=0;
        a1+=abs(ar[ci]-ar[ci+1])+solve(n,ar,ci+1);
        a=min(a1,a);
    }
    if(ci+2<n){
        int b1=0;
        b1+=abs(ar[ci]-ar[ci+2])+solve(n,ar,ci+2);
        b=min(b1,b);
    }
    if(ci+3<n){
        int c1=0;
        c1+=abs(ar[ci]-ar[ci+3])+solve(n,ar,ci+3);
        c=min(c1,c);
    }
    return dp[ci]=min(a,min(b,c));
}
int main(){
    int n;
    cin>>n;
    vector<int>ar;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        ar.push_back(a);
    }
    if(n==1) return ar[0];
    memset(dp,-1,sizeof(dp));
    cout<<solve(n,ar,0);
}


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

vector<pair<int, int>> solve(vector<pair<int, int>>& p, int K) {
    priority_queue<pair<int, pair<int, int>>> mh;
    for(auto& i : p) {
        int d = i.first * i.first + i.second * i.second;
        mh.push({d, i});
        if(mh.size() > K) {
            mh.pop();
        }
    }
    vector<pair<int, int>> res;
    while(!mh.empty()) {
        res.push_back(mh.top().second);
        mh.pop();
    }
    return res;
}

int main() {
    int N, K;
    cin >> N;
    vector<pair<int, int>> p(N);
    for(int i = 0; i < N; i++) {
        cin >> p[i].first >> p[i].second;
    }
    cin >> K;
    vector<pair<int, int>> np = solve(p, K);
    for(auto& i : np) {
        cout << i.first << " " << i.second << endl;
    }
    return 0;
}

At the center โœ…
#include <iostream>
#include <vector>

using namespace std;

void solve(vector<int>& a, int n) {
    int ans = a[0];
    for (int i = 1; i < n; i++) {
        ans &= a[i];
    }
    cout << ans << " ";
}

int main() {

        int n;
        cin >> n;

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

        solve(v, n);

    return 0;
}

Minimum integerโœ…
def longest_beautiful_prefix(S, substrings):
    dp = {len(S): 0}

    for i in range(len(S) - 1, -1, -1):
        longest_prefix = 0

        for s in substrings:
            possible_end = i + len(s)

            if possible_end <= len(S) and S[i:possible_end] == s[::-1] and possible_end in dp:
                longest_prefix = max(longest_prefix, dp[possible_end] + len(s))

        dp[i] = longest_prefix

    return dp[0]

Longest Prefix โœ…
Infosys
vector<string> CheckStringPairs(int t, vector<string> a, vector<string> b) {
    vector<string> ans;
    int n = a.size();

    for (int j = 0; j < n; j++) {
        unordered_map<char, int> ump1, ump2;

        if (a[j].size() != b[j].size()) {
            ans.push_back("NO");
            continue;
        }

        for (int i = 0; i < a[j].size(); i++) {
            ump1[a[j][i]]++;
        }

        for (int i = 0; i < b[j].size(); i++) {
            ump2[b[j][i]]++;
        }

        bool isPossible = true;

        for (auto c : ump1) {
            char cc = c.first;

            if (ump2.find(cc) == ump2.end() || ump2[cc] != ump1[cc]) {
                isPossible = false;
                break;
            }
        }

        if (isPossible) {
            ans.push_back("YES");
        } else {
            ans.push_back("NO");
        }
    }

    return ans;
}

Strange String โœ…
Infosys
ll solve(ll n,ll m,vector<ll>a,vector<ll>b,vector<ll>t,vector<ll>&l,vector<ll>&r,vector<ll>&k)
{
    for(ll i=0;i<m;i++)
    {
        if(t[i]==1)
        {
            for(ll j=l[i]-1;j<r[i];j++)
              a[j]=a[j]+b[j]*k[i];
        }
        else{
            if(a[r[i]-l[i]]>k[i])
              a[r[i]-l[i]]=a[r[i]-l[i]]-((a[r[i]-l[i]]-k[i])+k[i]/2);
        }
    }
    ll sum=0;
    for(ll i=0;i<n;i++) sum=(sum%mod+a[i]%mod)%mod;
    return sum;
}

farmers fertiizers โœ…
infosys
int beauty(vector<int>& a , int l , int r , int x){
     int res = 0;
    int cnt = 0;
    int i = l-1;
    while(i < r){
        if(a[i] == x){
         cnt++;
         res = max(cnt,res);
        }
        else if(a[i] > x){
            cnt = 0;
        }
        i++;
    }
    return res;
}
int GetAns(int n , vector<int> a, int q , int three , vector<vector<int>> queries){
   
   long long sum = 0;
   int Mod = 1e9 + 7;
   for(int i = 0 ;i < q ;i++){
       sum += beauty(a,queries[i][0] , queries[i][1] ,queries[i][2]);
       sum %= Mod;
   }
   return sum;
}

Max queries
Infosys โœ…
int largerPath(int node,vector<int>adjLis[],vector<int>&a,vector<int>&more){
  if(more[node]!=-1) return more[node];
  int ans=0;
  for(auto& it:adjLis[node]){
    if(a[it-1]<=a[node-1]) continue;
    ans=max(ans,largerPath(it,adjLis,a,more));
  }
  return more[node]=1+ans;
}
int smallerPath(int node,vector<int>adjLis[],vector<int>&a,vector<int>&less){
  if(less[node]!=-1) return less[node];
  int ans=0;
  for(auto& it:adjLis[node]){
    if(a[it-1]>=a[node-1]) continue;
    ans=max(ans,smallerPath(it,adjLis,a,less));
  }
  return less[node]=1+ans;
}
vector<int>frog(int n,int m,int c,vector<int>a,vector<vector<int>>edges){
    vector<int>adjLis[n+1];
    for(auto& it:edges){
        adjLis[it[0]].push_back(it[1]);
        adjLis[it[1]].push_back(it[0]);
    }
    vector<int>less(n+1,-1),more(n+1,-1);
    vector<int>ans;
    for(int i=1;i<=n;i++){
        int tot=smallerPath(i,adjLis,a,less)+largerPath(i,adjLis,a,more)-1;
       ans.push_back(tot);
    }
    return ans;
}

Frog โœ…
Infosys
#include <iostream>
#include <vector>

using namespace std;

int solver(int idx, vector<int>& A) {
    if (idx >= A.size()) return 0;
    int pick = A[idx] + solver(idx + 2, A);
    int notpick = solver(idx + 1, A);
    return max(pick, notpick);
}

int solve(int n, vector<int>& A, int q, vector<vector<int>>& queries) {
    int ans = 0;
    for (auto it : queries) {
        int index = it[0];
        int value = it[1];
        A[index] -= value;
        ans += solver(0, A);
    }
    return ans;
}

int main() {
    int n;
    cin >> n;

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

    int q;
    cin >> q;

    vector<vector<int>> queries(q, vector<int>(2));
    for (int i = 0; i < q; ++i) {
        cin >> queries[i][0] >> queries[i][1];
    }

    int result = solve(n, A, q, queries);
    cout << result << endl;

    return 0;
}



yet another subsequence โœ…
Infosys