allcoding1_official
83.5K subscribers
828 photos
2 videos
73 files
887 links
Download Telegram
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;
}
1
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
This media is not supported in your browser
VIEW IN TELEGRAM
import java.util.*;

class Main {
public static int solve(String word1, String word2) {
int n = word1.length();
int m = word2.length();

final int INF = 1_000_000;

int[][][] dp = new int[n + 1][m + 1][2];

for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
dp[i][j][0] = INF;
dp[i][j][1] = INF;
}
}

dp[n][m][0] = 0;
dp[n][m][1] = 0;

for (int i = n; i >= 0; i--) {
for (int j = m; j >= 0; j--) {
for (int p = 0; p < 2; p++) {

if (i == n && j == m) continue;

int ans = INF;


if (i < n) {
ans = Math.min(ans, 1 + dp[i + 1][j][p ^ 1]);
}


if (j < m) {
ans = Math.min(ans, 1 + dp[i][j + 1][p ^ 1]);
}


if (i < n && j < m) {
char c = word1.charAt(i);
if (p == 1) {
c = (char) ('a' + (c - 'a' + 13) % 26);
}

if (c == word2.charAt(j))
ans = Math.min(ans, dp[i + 1][j + 1][p]);
else
ans = Math.min(ans, 1 + dp[i + 1][j + 1][p]);
}

dp[i][j][p] = ans;
}
}
}

return dp[0][0][0];
}
import java.util.*;

class Main {

public static int solve(int N, String colors, int[] neededTime) {

long answer = 0;
int i = 0;

while (i < N) {

int j = i;
while (j + 1 < N && colors.charAt(j + 1) == colors.charAt(i))
j++;

int len = j - i + 1;

if (len >= 2) {

long keep = 0;
long diffuse = neededTime[i];

for (int k = i + 1; k <= j; k++) {
long newKeep = diffuse;
long newDiffuse = Math.min(keep, diffuse) + neededTime[k];
keep = newKeep;
diffuse = newDiffuse;
}

answer += Math.min(keep, diffuse);
}

i = j + 1;
}

return (int) answer;
}
2
Mastercard Hiring Software Engineer:

Graduation Year: 2022 / 2023 / 2024 / 2025

Salary: 16 LPA

Location: Pune

Apply Link: https://careers.mastercard.com/us/en/job/MASRUSR281108EXTERNALENUS/Software-engineer