#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
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 (
Here's the Python code to achieve this:
This function
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
#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
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
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
#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