📌IT learning courses
📌All programing courses
📌Abdul bari courses
📌Ashok IT
100 rupees
Contact:- @meterials_available
📌All programing courses
📌Abdul bari courses
📌Ashok IT
100 rupees
Contact:- @meterials_available
❤2
#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++
Palindromic subtrees
HackWithInfy
@allcoding1
#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++
Palindromic subtrees
HackWithInfy
@allcoding1
👍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
@allcoding1
#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
@allcoding1
👍2
Here's the C++ code to solve the problem:
You can compile and run this C++ code to find the maximum amount of money that Bob has to take in one month to settle his dealings. Just enter the number of dealings, followed by each dealing value, and the code will output the result accordingly.</int></int></algorithm></vector></iostream>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int settleAccounts(vector<int> A) {
int sum = 0;
for (int num : A) {
sum += num;
}
return -sum;
}
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
int result = settleAccounts(A);
cout << result << endl;
return 0;
}
You can compile and run this C++ code to find the maximum amount of money that Bob has to take in one month to settle his dealings. Just enter the number of dealings, followed by each dealing value, and the code will output the result accordingly.</int></int></algorithm></vector></iostream>