allcoding1_official
83.5K subscribers
828 photos
2 videos
73 files
887 links
Download Telegram
import java.util.*;

class Main {

public static int solve(int P, int M, int k, int[][] piles) {

List<int[]> best = new ArrayList<>();

for (int i = 0; i < P; i++) {

ArrayList<Integer> a = new ArrayList<>();
for (int j = 0; j < M; j++) {
if (piles[i][j] != -1) a.add(piles[i][j]);
}

int n = a.size();
int[] mx = new int[n + 1];
Arrays.fill(mx, Integer.MIN_VALUE);
mx[0] = 0;

if (n > 0) {
int[] pre = new int[n + 1];
for (int j = 0; j < n; j++)
pre[j + 1] = pre[j] + a.get(j);

for (int len = 1; len <= n; len++) {
int bestSum = Integer.MIN_VALUE;
for (int s = 0; s + len <= n; s++) {
bestSum = Math.max(bestSum,
pre[s + len] - pre[s]);
}
mx[len] = bestSum;
}
}

best.add(mx);
}

int NEG = -1000000000;
int[] dp = new int[k + 1];
Arrays.fill(dp, NEG);
dp[0] = 0;

for (int[] cur : best) {
int[] ndp = new int[k + 1];
Arrays.fill(ndp, NEG);

int lim = cur.length - 1;

for (int used = 0; used <= k; used++) {
if (dp[used] == NEG) continue;

for (int take = 0; take <= lim && used + take <= k; take++) {
ndp[used + take] = Math.max(
ndp[used + take],
dp[used] + cur[take]
);
}
}

dp = ndp;
}

return dp[k];
}
#include <bits/stdc++.h>
using namespace std;

long long sideSum(long long v, long long count, long long step) {
if (count <= 0) return 0;
long long full = min(count, (v - 1) / step);
long long sum = full * v - step * full * (full + 1) / 2;
sum += (count - full) * 1LL;
return sum;
}

bool feasible(long long v, long long n, long long index, long long maxSum) {
long long left = index;
long long right = n - 1 - index;
long long total = v + sideSum(v, left, 1) + sideSum(v, right, 2);
return total <= maxSum;
}

int solve(int n, int index, int maxSum) {
long long lo = 1, hi = maxSum, ans = 1;
while (lo <= hi) {
long long mid = lo + (hi - lo) / 2;
if (feasible(mid, n, index, maxSum)) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return (int)ans;
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n; cin >> n;
int index; cin >> index;
int maxSum; cin >>
import java.util.*;

class Main {

public static int solve(int N, int[] arr) {
int maxVal = 0;
for (int x : arr) {
if (x > maxVal) maxVal = x;
}

int[] freq = new int[maxVal + 1];
for (int x : arr) {
freq[x]++;
}

int need = N / 2;
int left = 1;
int count = 0;
int ans = Integer.MAX_VALUE;

for (int right = 1; right <= maxVal; right++) {
count += freq[right];

while (count >= need) {
ans = Math.min(ans, right - left + 1);
count -= freq[left];
left++;
}
}

return ans;
}
import java.util.*;

class Main {
static final int MOD = 1000000007;

public static int solve(int N, int F, int T) {
int m = N - 2;

// dp[s] = ways to obtain sum s using processed nodes
long[] dp = new long[T + 1];
dp[0] = 1;

for (int i = 0; i < m; i++) {
long[] ndp = new long[T + 1];
for (int sum = 0; sum <= T; sum++) {
if (dp[sum] == 0) continue;
for (int v = 1; v <= F && sum + v <= T; v++) {
ndp[sum + v] += dp[sum];
if (ndp[sum + v] >= MOD) ndp[sum + v] %= MOD;
}
}
dp = ndp;
}

long waysPerPosition = 0;

for (int a = 1; a <= F; a++) {
for (int b = 1; b <= F; b++) {
int need = T - a * b;
if (need >= 0 && need <= T) {
waysPerPosition += dp[need];
waysPerPosition %= MOD;
}
}
}

return (int) ((waysPerPosition * (N - 1)) % MOD);
}
int solve(int n, int index, int maxSum) {

auto getSum = [](long long peak, long long count, long long diff) {
long long k = min(count, (peak - 1) / diff);

long long sum = k * peak
- diff * k * (k + 1) / 2;

sum += (count - k);

return sum;
};

auto possible = [&](long long peak) {

long long left = getSum(peak, index, 1);
long long right = getSum(peak, n - index - 1, 2);

long long total = peak + left + right;

return total <= maxSum;
};

long long low = 1;
long long high = maxSum;
long long ans = 1;

while (low <= high) {

long long mid = low + (high - low) / 2;

if (possible(mid)) {
ans = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}

return (int)ans;
}
import java.util.*;

class Main {
public static long solve(int N, int M, int G, int[] A, int[] B) {
if (N == 0 || M == 0) return 0;

int maxL = 1;

for (int offset = -(M - 1); offset < N; offset++) {

int startA = Math.max(0, offset);
int endA = Math.min(N - 1, offset + M - 1);
int len = endA - startA + 1;

if (len <= maxL) continue;


int[] diffs = new int[len];
for (int k = 0; k < len; k++) {
int idxA = startA + k;
int idxB = idxA - offset;
diffs[k] = A[idxA] - B[idxB];
}


Map<Integer, List<Integer>> shiftMap = new HashMap<>();
for (int k = 0; k < len; k++) {
shiftMap.computeIfAbsent(diffs[k], x -> new ArrayList<>()).add(k);
}


for (int d : shiftMap.keySet()) {
List<Integer> exactIndices = shiftMap.get(d);


int nExact = exactIndices.size();

for (int e = 0; e < nExact; e++) {
int left = exactIndices.get(e);
int right = exactIndices.get(e);

int graceCount = 0;
int curLen = 0;

for (int k = left; k < len; k++) {
if (diffs[k] == d) {
curLen++;
} else if (Math.abs(diffs[k] - d) <= G && graceCount == 0) {
graceCount++;
curLen++;
} else {
break;
}
}


for (int k = left - 1; k >= 0; k--) {
if (diffs[k] == d) {
curLen++;
} else if (Math.abs(diffs[k] - d) <= G && graceCount == 0) {
graceCount++;
curLen++;
} else {
break;
}
}

maxL = Math.max(maxL, curLen);
}
}
}

return maxL;
}
2
Infosys Exam 2PM BE READY I WILL SHARE ANSWERS

Loading.....

Please share with your friends

@allcoding1_official
long long ans = 0;

for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int k = 2 * j - i;

if (k >= N) break;

long long a = nums[i];
long long b = nums[j];
long long c = nums[k];

long long x = min({a, b, c});
long long z = max({a, b, c});
long long y = a + b + c - x - z;

if (x + y > z)
ans = max(ans, a + b + c);
}
}
int solve(int N, int P, vector<vector<int>>& costs) {
int half = N / 2;
const int MAX_VAL = 1e9;
vector<vector<vector<int>>> table(N + 1, vector<vector<int>>(half + 1, vector<int>(2, MAX_VAL)));

table[1][1][0] = costs[0][0];
table[1][0][1] = costs[0][1];

for (int idx = 1; idx < N; idx++) {
for (int cityA_cnt = 0; cityA_cnt <= half; cityA_cnt++) {
for (int prev_city = 0; prev_city < 2; prev_city++) {
if (table[idx][cityA_cnt][prev_city] == MAX_VAL) continue;

if (cityA_cnt + 1 <= half) {
int extra = (prev_city == 0) ? P : 0;
table[idx + 1][cityA_cnt + 1][0] = min(table[idx + 1][cityA_cnt + 1][0], table[idx][cityA_cnt][prev_city] + costs[idx][0] + extra);
}

int cityB_cnt = idx - cityA_cnt;
if (cityB_cnt + 1 <= half) {
int extra = (prev_city == 1) ? P : 0;
table[idx + 1][cityA_cnt][1] = min(table[idx + 1][cityA_cnt][1], table[idx][cityA_cnt][prev_city] + costs[idx][1] + extra);
}
}
}
}

return min(table[N][half][0], table[N][half][1]);
}
int solve(int N, int days, vector<int>& weights) {
int start_cap = 1;
int end_cap = 0;
for (int cargo_w : weights) {
start_cap = max(start_cap, cargo_w);
end_cap += cargo_w;
}
end_cap *= 2;
int min_required_capacity = end_cap;

while (start_cap <= end_cap) {
int mid_cap = start_cap + (end_cap - start_cap) / 2;
int active_day = 1;
int loaded_weight = 0;
int pkg_idx = 0;
bool fits_in_time = true;

while (pkg_idx < N) {
if (active_day > days) {
fits_in_time = false;
break;
}
int day_capacity = (active_day % 2 == 1) ? mid_cap : (mid_cap / 2);
if (weights[pkg_idx] > day_capacity) {
active_day++;
loaded_weight = 0;
continue;
}
if (loaded_weight + weights[pkg_idx] <= day_capacity) {
loaded_weight += weights[pkg_idx];
pkg_idx++;
} else {
active_day++;
loaded_weight = 0;
}
}

if (fits_in_time && active_day <= days) {
min_required_capacity = mid_cap;
end_cap = mid_cap - 1;
} else {
start_cap = mid_cap + 1;
}
}

return min_required_capacity;
}
Car code

int solve(int T, int capacity, vector<vector<int>>& trips) {
map<int, long long> passenger_diffs;

for (int idx = 0; idx < T; idx++) {
int count = trips[idx][0];
int start_loc = trips[idx][1];
int end_loc = trips[idx][2];

passenger_diffs[start_loc] += count;
passenger_diffs[end_loc] -= count;
}

long long accumulated_standing_cost = 0;
long long active_riders = 0;
int last_point = -1;

for (auto& entry : passenger_diffs) {
int curr_point = entry.first;

if (last_point != -1 && curr_point > last_point) {
long long dist = curr_point - last_point;
if (active_riders > capacity) {
accumulated_standing_cost += (active_riders - capacity) * dist;
}
}

active_riders += entry.second;
last_point = curr_point;
}

return accumulated_standing_cost;
}
int solve(int N, int fee, vector<int>& prices) {
int max_cash = 0;
int bought_even_stock = -1e9;
int bought_odd_stock = -1e9;

for (int idx = 0; idx < N; idx++) {
int current_price = prices[idx];
int updated_cash = max_cash;
int updated_even_stock = bought_even_stock;
int updated_odd_stock = bought_odd_stock;

if (current_price % 2 == 0) {
updated_even_stock = max(bought_even_stock, max_cash - current_price);
if (bought_odd_stock != -1e9) {
updated_cash = max(updated_cash, bought_odd_stock + current_price - fee);
}
} else {
updated_odd_stock = max(bought_odd_stock, max_cash - current_price);
if (bought_even_stock != -1e9) {
updated_cash = max(updated_cash, bought_even_stock + current_price - fee);
}
}
max_cash = updated_cash;
bought_even_stock =
updated_odd_stock;
}

return max_cash;
}
def is_vowel(ch):
return ch in 'AEIOU'

def solve():
data = sys.stdin.read().strip()
if not data:
return 0
s = data.split()[0]
n = len(s)

dp_vowel = [0] * (n + 1)
dp_consonant = [0] * (n + 1)
dp_consonant[0] = 1

for i in range(1, n + 1):
if s[i-1] != '0':
digit = int(s[i-1])
letter = chr(ord('A') + digit - 1)
if is_vowel(letter):
dp_vowel[i] = (dp_vowel[i] + dp_consonant[i-1]) % MOD
else:
dp_consonant[i] = (dp_consonant[i] + dp_consonant[i-1] + dp_vowel[i-1]) % MOD

if i >= 2 and s[i-2] != '0':
two_digit = int(s[i-2:i])
if 10 <= two_digit <= 26:
letter = chr(ord('A') + two_digit - 1)
if is_vowel(letter):
dp_vowel[i] = (dp_vowel[i] + dp_consonant[i-2]) % MOD
else:
dp_consonant[i] = (dp_consonant[i] + dp_consonant[i-2] +dp_vowel[i-2]) % MOD
def solve(N: int, nums: list) -> int:

def max_product(arr):
mx = mn = ans = arr[0]

for x in arr[1:]:
if x < 0:
mx, mn = mn, mx

mx = max(x, mx * x)
mn = min(x, mn * x)
ans = max(ans, mx)

return ans

res = -10**18
i = 0

while i < N:
j = i
while j + 1 < N and abs(nums[j]) < abs(nums[j + 1]):
j += 1

res = max(res, max_product(nums[i:j + 1]))
i = j + 1

return res