#include <iostream>
#include <string>
#include <algorithm>
int findLargestX(std::string& str) {
int max_x = 0;
int sum = 0;
for (char c : str) {
if (isdigit(c)) {
sum += c - '0';
} else if (c == '-') {
max_x = std::max(max_x, sum);
sum = 0;
}
}
return max_x;
}
int main() {
std::string input = "63-1+2-1+3+4-9-1+2-3-3+4+9";
int largest_x = findLargestX(input);
std::cout << "Largest value of x: " << largest_x << std::endl;
return 0;
}
Maximum_Subarray
C++
HackWithInfy
@allcoding1
#include <string>
#include <algorithm>
int findLargestX(std::string& str) {
int max_x = 0;
int sum = 0;
for (char c : str) {
if (isdigit(c)) {
sum += c - '0';
} else if (c == '-') {
max_x = std::max(max_x, sum);
sum = 0;
}
}
return max_x;
}
int main() {
std::string input = "63-1+2-1+3+4-9-1+2-3-3+4+9";
int largest_x = findLargestX(input);
std::cout << "Largest value of x: " << largest_x << std::endl;
return 0;
}
Maximum_Subarray
C++
HackWithInfy
@allcoding1
π2
#include <iostream>
#include <vector>
#include <unordered_map>
const int MOD = 1000000007;
int countUniqueArrangements(std::vector<std::pair<int, int="">>& dominoes) {
std::unordered_map<int, int=""> dp;
dp[0] = 1; // Base case
int n = dominoes.size();
for(int i = 1; i <= n; ++i) {
std::unordered_map<int, int=""> next_dp;
for(auto& domino : dominoes) {
int key = dp.count(domino.first) ? domino.first : domino.second;
next_dp[key] = (next_dp[key] + dp[key ^ domino.first ^ domino.second]) % MOD;
}
dp = std::move(next_dp);
}
int total_arrangements = 0;
for(auto& p : dp) {
total_arrangements = (total_arrangements + p.second) % MOD;
}
return total_arrangements;
}
int main() {
int N;
std::cin >> N;
std::vector<std::pair<int, int="">> dominoes(N);
for(int i = 0; i < N; ++i) {
std::cin >> dominoes[i].first >> dominoes[i].second;
}
int total_arrangements = countUniqueArrangements(dominoes);
std::cout << total_arrangements << std::endl;
return 0;
}
C++
You are given a set of dominoes each with two sides. Each side is marked with dots ranging from 1 to 6.
HackWithInfy
@allcoding1
#include <vector>
#include <unordered_map>
const int MOD = 1000000007;
int countUniqueArrangements(std::vector<std::pair<int, int="">>& dominoes) {
std::unordered_map<int, int=""> dp;
dp[0] = 1; // Base case
int n = dominoes.size();
for(int i = 1; i <= n; ++i) {
std::unordered_map<int, int=""> next_dp;
for(auto& domino : dominoes) {
int key = dp.count(domino.first) ? domino.first : domino.second;
next_dp[key] = (next_dp[key] + dp[key ^ domino.first ^ domino.second]) % MOD;
}
dp = std::move(next_dp);
}
int total_arrangements = 0;
for(auto& p : dp) {
total_arrangements = (total_arrangements + p.second) % MOD;
}
return total_arrangements;
}
int main() {
int N;
std::cin >> N;
std::vector<std::pair<int, int="">> dominoes(N);
for(int i = 0; i < N; ++i) {
std::cin >> dominoes[i].first >> dominoes[i].second;
}
int total_arrangements = countUniqueArrangements(dominoes);
std::cout << total_arrangements << std::endl;
return 0;
}
C++
You are given a set of dominoes each with two sides. Each side is marked with dots ranging from 1 to 6.
HackWithInfy
@allcoding1
π1
#include <iostream>
#include <vector>
#include <unordered_map>
const int MOD = 1000000007;
int n, m, startRow, startColumn, moves, q;
std::vector<std::vector<int>> obstacles;
bool isObstacle(int x, int y) {
for (auto& obstacle : obstacles) {
if (obstacle[0] == x && obstacle[1] == y) {
return true;
}
}
return false;
}
int countPaths(int x, int y, int movesLeft, std::unordered_map<std::string, int="">& dp) {
if (x < 0 y < 0 x >= n || y >= m) {
return 1;
}
if (movesLeft == 0 || isObstacle(x, y)) {
return 0;
}
std::string key = std::to_string(x) + ":" + std::to_string(y) + ":" + std::to_string(movesLeft);
if (dp.find(key) != dp.end()) {
return dp[key];
}
int paths = countPaths(x + 1, y, movesLeft - 1, dp) % MOD;
paths = (paths + countPaths(x - 1, y, movesLeft - 1, dp)) % MOD;
paths = (paths + countPaths(x, y + 1, movesLeft - 1, dp)) % MOD;
paths = (paths + countPaths(x, y - 1, movesLeft - 1, dp)) % MOD;
dp[key] = paths;
return paths;
}
int main() {
std::cin >> n >> m >> startRow >> startColumn >> moves >> q;
obstacles.resize(q, std::vector<int>(2));
for (int i = 0; i < q; ++i) {
std::cin >> obstacles[i][0] >> obstacles[i][1];
}
std::unordered_map<std::string, int=""> dp;
int totalPaths = countPaths(startRow, startColumn, moves, dp);
std::cout << totalPaths << std::endl;
return 0;
}
C++
In a town called Gridland, there lived a man named Alex who had a special skill. He could move around a grid like a big square map. The grid had obstacles, things he couldn't go through.
The grid is described by its size n x m, and Alex starts at a specΓfic position [startRow, startColumn). There are also obstacles in fixed positions.
HackWithInfy
@allcoding1
#include <vector>
#include <unordered_map>
const int MOD = 1000000007;
int n, m, startRow, startColumn, moves, q;
std::vector<std::vector<int>> obstacles;
bool isObstacle(int x, int y) {
for (auto& obstacle : obstacles) {
if (obstacle[0] == x && obstacle[1] == y) {
return true;
}
}
return false;
}
int countPaths(int x, int y, int movesLeft, std::unordered_map<std::string, int="">& dp) {
if (x < 0
return 1;
}
if (movesLeft == 0 || isObstacle(x, y)) {
return 0;
}
std::string key = std::to_string(x) + ":" + std::to_string(y) + ":" + std::to_string(movesLeft);
if (dp.find(key) != dp.end()) {
return dp[key];
}
int paths = countPaths(x + 1, y, movesLeft - 1, dp) % MOD;
paths = (paths + countPaths(x - 1, y, movesLeft - 1, dp)) % MOD;
paths = (paths + countPaths(x, y + 1, movesLeft - 1, dp)) % MOD;
paths = (paths + countPaths(x, y - 1, movesLeft - 1, dp)) % MOD;
dp[key] = paths;
return paths;
}
int main() {
std::cin >> n >> m >> startRow >> startColumn >> moves >> q;
obstacles.resize(q, std::vector<int>(2));
for (int i = 0; i < q; ++i) {
std::cin >> obstacles[i][0] >> obstacles[i][1];
}
std::unordered_map<std::string, int=""> dp;
int totalPaths = countPaths(startRow, startColumn, moves, dp);
std::cout << totalPaths << std::endl;
return 0;
}
C++
In a town called Gridland, there lived a man named Alex who had a special skill. He could move around a grid like a big square map. The grid had obstacles, things he couldn't go through.
The grid is described by its size n x m, and Alex starts at a specΓfic position [startRow, startColumn). There are also obstacles in fixed positions.
HackWithInfy
@allcoding1
π3β€1
https://www.allcoding1.com/2023/12/allcoding1-answers.html?m=1
π Copy ur question and past you get Answer
π Copy ur question and past you get Answer
π1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
int N, Q;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
cin >> Q;
long long sum = 0;
for (int q = 0; q < Q; ++q) {
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;
sum = (sum + A[i - 1]) % MOD;
}
}
cout << sum << endl;
return 0;
}
Replace by Minimum Code
C++
HackWithInfy
Telegram:- @allcoding1
#include <vector>
#include <algorithm>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
int N, Q;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
cin >> Q;
long long sum = 0;
for (int q = 0; q < Q; ++q) {
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;
sum = (sum + A[i - 1]) % MOD;
}
}
cout << sum << endl;
return 0;
}
Replace by Minimum Code
C++
HackWithInfy
Telegram:- @allcoding1
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
Telegram:- @allcoding1
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
Telegram:- @allcoding1
π2
Forwarded from allcoding1
π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
#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
Telegram:- @allcoding1
#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
Telegram:- @allcoding1
π1
import heapq
def reduce_sum(lst):
heapq.heapify(lst)
s = 0
while len(lst) > 1:
first = heapq.heappop(lst)
second = heapq.heappop(lst)
s += first + second
heapq.heappush(lst, first + second)
return s
Reduce the Array
Salesforce
Telegram:- @allcoding1
def reduce_sum(lst):
heapq.heapify(lst)
s = 0
while len(lst) > 1:
first = heapq.heappop(lst)
second = heapq.heappop(lst)
s += first + second
heapq.heappush(lst, first + second)
return s
Reduce the Array
Salesforce
Telegram:- @allcoding1
#include <iostream>
#include <vector>
#include <algorithm>
int getPotentialOfWinner(std::vector<int>& potential, long long k) {
int n = potential.size();
int x = potential[0];
int m = 0;
for (int i = 1; i < n; i++) {
if (m != k) {
if (x > potential[i]) {
m++;
} else {
x = potential[i];
m = 1;
}
}
}
return x;
}
int main() {
std::vector<int> potentials = {3, 2, 1, 4};
long long k = 2;
std::cout << getPotentialOfWinner(potentials, k) << std::endl;
return 0;
}
Potential winner code
Telegram:- @allcoding1
#include <vector>
#include <algorithm>
int getPotentialOfWinner(std::vector<int>& potential, long long k) {
int n = potential.size();
int x = potential[0];
int m = 0;
for (int i = 1; i < n; i++) {
if (m != k) {
if (x > potential[i]) {
m++;
} else {
x = potential[i];
m = 1;
}
}
}
return x;
}
int main() {
std::vector<int> potentials = {3, 2, 1, 4};
long long k = 2;
std::cout << getPotentialOfWinner(potentials, k) << std::endl;
return 0;
}
Potential winner code
Telegram:- @allcoding1
π2