Question: Amazon Hackon, Online Assesment Asked Question | Tech Curator | Given Integer N to Zero | 29th September 2023
1
Entering edit mode

ADD COMMENTlink 13 months ago Delta 2.9k
Entering edit mode
0

Question 2:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> adj(n + 1);
  for (int i = 2; i <= n; i++) {
    int x;
    cin >> x;
    adj[i].push_back(x);
    adj[x].push_back(i);
  }
  vector<int> subtree(n + 1, 0);

  function<int(int, int)> dfs = [&](int u, int p) -> int {
    int ans = 1;
    for (int v : adj[u]) {
      if (v != p) {
        ans += dfs(v, u);
      }
    }
    return subtree[u] = ans;
  };
  dfs(1, 0);

  for (int i = 1; i <= n; i++) {
    cout << subtree[i] - 1 << " ";
  }
  cout << "\n";
}

 

ADD REPLYlink 13 months ago
Ankit Josh
• 0
0
Entering edit mode

Question 1

#include<bits/stdc++.h>

using namespace std;

int func(int N){

 

   int i = 2;

   while((i * i) < N && (N % i)){

      i++;

   }

   if((i * i) > N){

      i = N;

   }

   return 1 + (N-i)/2;

 

}

int main(){

  int n;

  cin>>n;

  cout<<func(n);

}

ADD COMMENTlink 13 months ago Komal Kumari • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts