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 โ
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โ
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
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()
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 โ
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 โ
#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 โ
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 โ
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
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++
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++
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
๐Harness is hiring for Software Engineer
Expected Salary: 15-25 LPA
Experience: 0-1 years
Apply here: https://instahyre.com/job-310707-software-engineer-at-harness-bangalore
Expected Salary: 15-25 LPA
Experience: 0-1 years
Apply here: https://instahyre.com/job-310707-software-engineer-at-harness-bangalore
Instahyre
Software Engineer job at Harness - Instahyre
Harness is looking for a Software Engineer in Bangalore with 0-1 years of experience in Backend Development, Golang, Java, etc. Apply today and get your dream job at Harness!
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Venture Labs hiring!
- Product Design Intern
- Frontend (Web) Intern
- Backend Intern
- Android Intern
- Growth Intern
Email at subhadeep.iit@gmail.com
- Product Design Intern
- Frontend (Web) Intern
- Backend Intern
- Android Intern
- Growth Intern
Email at subhadeep.iit@gmail.com
#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
#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 <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 < N; i++) {
totalGoodStrings = (totalGoodStrings * M) % MOD;
}
return totalGoodStrings;
}
int main() {
int N, M;
cin >> N >> M;
string S;
cin >> S;
int result = countGoodStrings(N, M, S);
cout << result << endl;
return 0;
}
C++
Good strings code
HackWithInfyโ
#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 < N; i++) {
totalGoodStrings = (totalGoodStrings * M) % MOD;
}
return totalGoodStrings;
}
int main() {
int N, M;
cin >> N >> M;
string S;
cin >> S;
int result = countGoodStrings(N, M, S);
cout << result << endl;
return 0;
}
C++
Good strings code
HackWithInfyโ
๐2