Infosys SP and DSE 2
Infosys SP and DSE 2
Problem Statement:
Dev loves XOR operations. He has an array A of N elements, and he wants to select at most K
elements from it (not necessarily consecutive) to maximize the XOR value of the selected elements.
Your task is to help Dev find the maximum possible XOR value he can get by selecting at most K
elements.
Input Format:
Constraints:
1 ≤ N ≤ 100
1≤K≤N
1 ≤ A[i] ≤ 10⁶
Sample Input 1:
42
2468
Sample Output 1:
14
Explanation:
Choosing 2 and 8 gives 2 ^ 8 = 10.
Choosing 6 and 8 gives 6 ^ 8 = 14 — this is the maximum possible.
C++ Solution:
#include <bits/stdc++.h>
int N = arr.size();
int maxXOR = 0;
for (int mask = 0; mask < (1 << N); ++mask) {
int currXOR = 0;
currXOR ^= arr[i];
return maxXOR;
int main() {
int N, K;
vector<int> A(N);
return 0;
Problem Statement:
Sania gave you an array A of length N and a number K. She defined a function:
Input Format:
Constraints:
1 ≤ N ≤ 10⁵
0 ≤ K ≤ 10⁹
0 ≤ A[i] ≤ 10⁹
Sample Input 1:
37
163
Sample Output 1:
14
Explanation:
Try all values of x from 0 to 7.
At x = 4, (4^1 + 4^6 + 4^3) = 14 (maximum).
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, K;
vector<int> A(N);
int x = A[i], b = 0;
while (x) {
if (x & 1) bitCount[b]++;
x >>= 1;
b++;
int ans = 0, x = 0;
x |= (1 << i);
int maxXorSum = 0;
maxXorSum += (x ^ A[i]);
return 0;
Problem Statement:
Aarav is playing with an array of integers. He wants to select a contiguous subarray from the array
such that the XOR of all the elements in that subarray is maximum.
You need to help Aarav find this maximum XOR value among all possible subarrays.
Input Format:
The first line contains an integer N.
Constraints:
1 ≤ N ≤ 10⁵
1 ≤ A[i] ≤ 10⁹
Sample Input 1:
1234
Sample Output 1:
Explanation:
The subarray [3,4] gives 3^4 = 7, which is the maximum possible.
C++ Solution:
#include <bits/stdc++.h>
struct TrieNode {
};
if (!node->child[bit])
node = node->child[bit];
}
int query(TrieNode* root, int num) {
int maxXOR = 0;
if (node->child[!bit]) {
node = node->child[!bit];
} else {
node = node->child[bit];
return maxXOR;
int main() {
int n;
cin >> n;
vector<int> A(n);
insert(root, 0);
prefixXOR ^= A[i];
insert(root, prefixXOR);
}
cout << result << endl;
return 0;
Problem Statement:
Meher has a list of integers. She wants to choose any two distinct numbers from the list such that
their XOR is maximized.
Can you help Meher find the maximum XOR value possible between any pair of numbers from the
list?
Input Format:
Constraints:
2 ≤ N ≤ 10⁵
0 ≤ A[i] ≤ 10⁹
Sample Input 1:
8 1 2 12
Sample Output 1:
14
Explanation:
8 ^ 6 = 14 is the maximum XOR among all pairs.
C++ Solution:
#include <bits/stdc++.h>
struct TrieNode {
TrieNode* child[2] = {nullptr, nullptr};
};
if (!node->child[bit])
node = node->child[bit];
int maxXOR = 0;
if (node->child[!bit]) {
node = node->child[!bit];
} else {
node = node->child[bit];
return maxXOR;
int main() {
int n;
cin >> n;
vector<int> A(n);
insert(root, A[0]);
int maxResult = 0;
insert(root, A[i]);
return 0;
Problem Statement:
Isha has an array of integers. She wants to divide the array into two non-overlapping subsets such
that the XOR of both subsets is the same.
Input Format:
Constraints:
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 10⁶
Sample Input 1:
4
1230
Sample Output 1:
YES
Explanation:
One possible partition: {1, 2, 3} and {0}
1^2^3 = 0 and 0^0 = 0.
Sample Input 2:
124
Sample Output 2:
NO
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N;
cin >> N;
vector<int> A(N);
int totalXOR = 0;
totalXOR ^= A[i];
if (totalXOR == 0)
else
Problem Statement:
Kabir has an integer array and a limit value L. He wants to choose a number X such that 0 ≤ X ≤ L and
XOR it with each array element. His goal is to minimize the total XOR sum.
Your task is to find the value of X (in the range [0, L]) that gives the minimum XOR-sum.
Input Format:
Constraints:
1 ≤ N ≤ 10⁵
0 ≤ L ≤ 10⁹
0 ≤ A[i] ≤ 10⁹
Sample Input 1:
37
231
Sample Output 1:
Explanation:
Try all values x from 0 to 7. Minimum total XOR-sum occurs at x = 3.
C++ Solution:
#include <bits/stdc++.h>
int total = 0;
for (int a : A)
total += (a ^ x);
return total;
int main() {
int N, L;
vector<int> A(N);
minSum = curr;
result = x;
return 0;
Problem Statement:
In the land of Bitsaria, a game is played using an array of integers. The player is allowed to pick any
subset of elements such that:
Can you help the player determine the maximum XOR achievable?
Input Format:
Constraints:
2 ≤ N ≤ 1000 (even)
1 ≤ A[i] ≤ 10⁶
Sample Input 1:
382519
Sample Output 1:
15
Explanation:
Pick elements [8, 5, 9] (positions 1, 3, 5 – not adjacent). 8^5^9 = 15
C++ Solution:
#include <bits/stdc++.h>
int maxXOR(vector<int>& A, int i, int count, int limit, int xorVal, vector<vector<int>>& dp) {
return xorVal;
if (dp[i][count] != -1)
return dp[i][count];
int main() {
int N;
cin >> N;
vector<int> A(N);
return 0;
Problem Statement:
You are given an array A of size N. A triplet (i, j, k) is valid if i < j < k and A[i] ^ A[j] ^ A[k] == 0.
Input Format:
Constraints:
1 ≤ N ≤ 500
0 ≤ A[i] ≤ 10⁶
Sample Input 1:
12301
Sample Output 1:
Explanation:
Triplets that satisfy: (0,1,2) → 1^2^3 = 0
and (1,2,3) → 2^3^0 = 1
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N;
cin >> N;
vector<int> A(N);
int count = 0;
count++;
}
cout << count << endl;
return 0;
Problem Statement:
Priya is stuck in a game level represented by an array A, where each value A[i] indicates the
maximum number of steps she can jump forward from position i. She starts from index 0.
Find the minimum number of jumps required to reach the end of the array. If it is not possible,
return -1.
Input Format:
Constraints:
1 ≤ N ≤ 10⁵
0 ≤ A[i] ≤ 10⁵
Sample Input 1:
231142
Sample Output 1:
Explanation:
Jump from 0 → 1 → 4 → end (3 jumps)
C++ Solution:
#include <bits/stdc++.h>
int minJumps(vector<int>& A) {
int n = A.size();
if (n <= 1) return 0;
if (i == n - 1) return jumps;
if (i == currEnd) {
jumps++;
currEnd = farthest;
return -1;
int main() {
int N;
cin >> N;
vector<int> A(N);
return 0;
Problem Statement:
Ravi wants to invest in K consecutive stocks for K days to get the maximum profit. The array A
contains the profit values for N days.
Constraints:
1 ≤ N ≤ 10⁶
1≤K≤N
Sample Input 1:
73
2151326
Sample Output 1:
10
Explanation:
Subarray [5,1,3] has max sum 9. Subarray [3,2,6] has sum 11 (maximum).
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, K;
vector<int> A(N);
currSum += A[i];
maxSum = currSum;
for (int i = K; i < N; ++i) {
return 0;
Problem Statement:
A school has N books, with pages given in an array A[]. There are M students, and books must be
allocated in contiguous blocks.
Each student gets at least one book, and each book must be assigned to exactly one student.
Your task is to allocate the books so that the maximum number of pages assigned to any student is
minimized.
Input Format:
Constraints:
1 ≤ N ≤ 10⁵
1≤M≤N
1 ≤ A[i] ≤ 10⁹
Sample Input 1:
42
12 34 67 90
Sample Output 1:
113
Explanation:
One optimal way: [12, 34, 67] and [90] → Max pages = 113
C++ Solution:
#include <bits/stdc++.h>
students++;
sum = A[i];
} else {
sum += A[i];
int main() {
int N, M;
vector<int> A(N);
if (isPossible(A, N, M, mid)) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
return 0;
Problem Statement:
Find the shortest path from top-left (0, 0) to bottom-right (N-1, N-1). Return the length of the
shortest path, or -1 if no path exists.
Input Format:
Constraints:
1 ≤ N ≤ 100
grid[i][j] ∈ {0,1}
Sample Input 1:
010
000
100
Sample Output 1:
Explanation:
One path is (0,0) → (1,0) → (1,1) → (1,2) → (2,2)
C++ Solution:
#include <bits/stdc++.h>
int N = grid.size();
return -1;
q.push({0, 0, 1});
visited[0][0] = true;
while (!q.empty()) {
visited[nx][ny] = true;
return -1;
int main() {
int N;
cin >> N;
return 0;
Problem Statement:
Arjun is at a shop and wants to pay a total amount X using unlimited coins of certain denominations
available in an array coins[].
He wants to find the total number of ways he can make the payment using any number of coins.
Input Format:
First line: Two integers N (number of coin types) and X (target amount)
Constraints:
1 ≤ N ≤ 100
1 ≤ X ≤ 10⁴
1 ≤ coins[i] ≤ 100
Sample Input 1:
34
123
Sample Output 1:
Explanation:
The 4 ways to make 4 are:
1+1+1+1, 1+1+2, 2+2, 1+3
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, X;
vector<int> coins(N);
dp[0] = 1;
return 0;
Problem Statement:
Sanya is the in-charge of a conference room. She receives N meeting requests with their start and
end times. Only one meeting can be held at a time.
Input Format:
Next N lines: Two space-separated integers start and end time of each meeting
Constraints:
1 ≤ N ≤ 10⁵
Sample Input 1:
12
34
06
57
89
59
Sample Output 1:
4
Explanation:
Max meetings = 4
Possible meetings: (1-2), (3-4), (5-7), (8-9)
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N;
cin >> N;
int count = 1;
count++;
lastEnd = meetings[i].second;
}
cout << count << endl;
return 0;
Problem Statement:
No two queens attack each other (i.e., no two queens share the same row, column, or
diagonal).
Input Format:
Constraints:
1 ≤ N ≤ 12
Sample Input 1:
Sample Output 1:
Explanation:
There are two valid ways to place 4 queens on a 4x4 board.
C++ Solution:
#include <bits/stdc++.h>
int countSolutions = 0;
void solve(int row, int n, vector<int>& col, vector<int>& diag1, vector<int>& diag2) {
if (row == n) {
countSolutions++;
return;
continue;
int main() {
int N;
cin >> N;
return 0;
Problem Statement:
Aryan is constructing binary strings of length N such that no two consecutive 1s appear.
Input Format:
One integer N
Constraints:
1 ≤ N ≤ 10⁵
Sample Input 1:
3
Sample Output 1:
Explanation:
The valid strings of length 3:
000, 001, 010, 100, 101
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N;
cin >> N;
long long a = 1, b = 1, c;
c = (a + b) % MOD;
a = b;
b = c;
return 0;
Problem Statement:
Yusuf is analyzing a dependency system where each task is represented as a node in a directed
graph. A cycle in the graph means a deadlock.
Constraints:
1 ≤ N ≤ 10⁵
1 ≤ M ≤ 10⁵
1 ≤ u, v ≤ N
Sample Input 1:
44
12
23
34
42
Sample Output 1:
YES
C++ Solution:
#include <bits/stdc++.h>
return true;
else if (recStack[neighbor])
return true;
}
recStack[node] = false;
return false;
int main() {
int N, M;
int u, v;
graph[u].push_back(v);
return 0;
return 0;
Problem Statement:
Anjali has a string S and a pattern P. She wants to know how many times the pattern P occurs in S as
a substring (even overlapping allowed).
Constraints:
Sample Input 1:
ababcabcabababc
ababc
Sample Output 1:
C++ Solution:
#include <bits/stdc++.h>
vector<int> computeLPS(string& P) {
int m = P.length();
int len = 0;
if (P[i] == P[len])
lps[i++] = ++len;
else if (len != 0)
else
lps[i++] = 0;
return lps;
}
int i = 0, j = 0, count = 0;
while (i < n) {
if (S[i] == P[j]) {
i++; j++;
if (j == m) {
count++;
j = lps[j - 1];
if (j != 0)
j = lps[j - 1];
else
i++;
return count;
int main() {
string S, P;
return 0;
}
Question 21: Cargo Route Optimization (Graph + Dijkstra + Priority Queue)
Problem Statement:
An international cargo company wants to find the cheapest route to ship goods from city A to city B.
The cities are connected by directed roads, each with a cost.
You are given a map with N cities and M roads. You need to find the minimum cost to travel from city
S to city D.
Input Format:
Constraints:
1 ≤ N ≤ 10⁵
1 ≤ M ≤ 2×10⁵
1 ≤ U, V ≤ N
1 ≤ W ≤ 10⁴
1 ≤ S, D ≤ N
Sample Input:
5615
122
234
1 3 10
343
451
2 5 20
Sample Output:
10
Explanation:
Path: 1 → 2 → 3 → 4 → 5, total cost = 2 + 4 + 3 + 1 = 10
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, M, S, D;
int U, V, W;
graph[U].push_back({V, W});
dist[S] = 0;
pq.push({0, S});
while (!pq.empty()) {
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
return 0;
Problem Statement:
Akira has a string S and an integer K. She can perform the following operation any number of times (≤
K times):
Choose any two characters at indices i and j (0 ≤ i < j < length) and swap them.
Your task is to return the lexicographically smallest string that can be formed by at most K swaps.
Input Format:
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ K ≤ 10⁴
Sample Input:
dcab
Sample Output:
bacd
Explanation:
Swap 'd' and 'b', then swap 'c' and 'a' → result = "bacd"
C++ Solution:
#include <bits/stdc++.h>
int n = s.length();
int pos = i;
minChar = s[j];
pos = j;
K -= (pos - i);
int main() {
string S;
int K;
kSwaps(S, K);
return 0;
Naina wants to divide a string S into the minimum number of palindromic substrings. Each substring
must itself be a palindrome.
Your task is to return the minimum number of cuts needed to partition S into such substrings.
Input Format:
Constraints:
1 ≤ |S| ≤ 1000
Sample Input 1:
aab
Sample Output 1:
Explanation:
"a | ab" → Not valid (ab is not palindrome)
"a | a | b" → 2 cuts (valid)
"a | ab" or "aa | b" → 1 cut (valid, minimum)
C++ Solution:
#include <bits/stdc++.h>
while (i < j) {
return true;
int minCuts(string& s) {
int n = s.length();
if (isPalindrome(s, j, i)) {
int main() {
string S;
cin >> S;
return 0;
Problem Statement:
Sara is playing a word search game on a 2D board. Each cell contains a lowercase letter. You are given
a word, and you must check whether the word exists in the board by traversing horizontally or
vertically (not diagonally).
You can only use each cell once per word search.
Input Format:
Constraints:
1 ≤ N, M ≤ 20
Sample Input:
34
abce
sfcs
adee
abcced
Sample Output:
YES
Explanation:
Word “abcced” is found by path: (0,0)→(0,1)→(0,2)→(1,2)→(2,2)→(2,1)
C++ Solution:
#include <bits/stdc++.h>
int N, M;
vector<vector<char>> board;
string word;
return false;
visited[i][j] = true;
visited[i][j] = false;
return found;
int main() {
if (dfs(i, j, 0, visited)) {
return 0;
return 0;
Problem Statement:
You are given a list of N strings. You must find the longest common prefix among them.
To solve this efficiently, implement a Trie (prefix tree).
Input Format:
Constraints:
1 ≤ N ≤ 10⁴
Sample Input:
flower
flow
flight
flame
Sample Output:
fl
Explanation:
All strings start with fl, which is the longest common prefix.
#include <bits/stdc++.h>
struct TrieNode {
TrieNode* children[26];
int count;
TrieNode() {
count = 0;
};
if (!node->children[idx])
node = node->children[idx];
node->count++;
while (true) {
if (node->children[i]) {
if (node->children[i]->count == totalWords) {
childCount++;
index = i;
if (childCount != 1) break;
node = node->children[index];
}
return prefix;
int main() {
int N;
cin >> N;
vector<string> words(N);
insert(root, words[i]);
return 0;
Question 26: Count Number of Subsets with Given XOR (Bitmask + DP)
Problem Statement:
Given an array A of N integers and a target value K, find the number of subsets whose XOR is equal
to K.
Input Format:
Constraints:
1 ≤ N ≤ 100
0 ≤ A[i], K ≤ 1000
Sample Input:
46
1234
Sample Output:
Explanation:
Subsets with XOR = 6:
{2, 4} → 2^4 = 6
{1, 3, 4} → 1^3^4 = 6
So total = 2
C++ Solution:
#include <bits/stdc++.h>
int n = A.size();
dp[0][0] = 1;
return dp[n][K];
int main() {
int N, K;
return 0;
Problem Statement:
A logistics company wants to maximize profit by choosing which items to load into a truck. Each item
has a weight and a profit. The truck can carry at most W weight.
Input Format:
First line: Two integers N and W (number of items and max weight)
Next N lines: Each line contains two integers: weight[i] and profit[i]
Constraints:
1 ≤ N ≤ 1000
1 ≤ W ≤ 10⁴
Sample Input:
4 10
5 10
4 40
6 30
3 50
Sample Output:
90
Explanation:
Choose items 2 and 4 → Total weight = 4+3 = 7, Total profit = 40+50 = 90
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, W;
return 0;
Problem Statement:
There are N workers and N jobs. Each worker can do one job, and each job can be assigned to only
one worker.
You are given a cost matrix where cost[i][j] is the cost for worker i to do job j. Your task is to assign
one job to each worker so that the total cost is minimized.
Input Format:
Constraints:
1 ≤ N ≤ 20
1 ≤ cost[i][j] ≤ 100
Sample Input:
927
643
581
Sample Output:
13
Explanation:
Worker 0 → job 1 (cost 2)
Worker 1 → job 2 (cost 3)
Worker 2 → job 0 (cost 5)
Total = 2 + 3 + 5 = 10
C++ Solution:
#include <bits/stdc++.h>
int N;
int cost[21][21];
int main() {
cin >> N;
return 0;
Problem Statement:
A robot is placed in a grid with m x n cells. It starts at the top-left and wants to reach the bottom-
right. Some cells have obstacles (1), and others are free (0).
Only right or down moves are allowed. Find the number of unique paths avoiding obstacles.
Input Format:
Constraints:
1 ≤ m, n ≤ 100
Sample Input:
33
000
010
000
Sample Output:
C++ Solution:
#include <bits/stdc++.h>
int main() {
int m, n;
if (grid[0][0] == 0) dp[0][0] = 1;
if (grid[i][j] == 0) {
if (i > 0) dp[i][j] += dp[i - 1][j];
Problem Statement:
Given N piles of stones, with the number of stones in each pile as A[i], determine who will win the
Nim Game if both play optimally.
Input Format:
Output:
Print "First" if first player will win, else "Second"
Sample Input:
145
Sample Output:
First
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, x = 0, a;
cin >> N;
cin >> a;
x ^= a;
Problem Statement:
Given a directed graph with N nodes, find if there's a Hamiltonian Path (a path that visits every node
exactly once).
Input Format:
Constraints:
1 ≤ N ≤ 20
0 ≤ M ≤ N*(N-1)
Sample Input:
44
12
23
34
13
Sample Output:
YES
C++ Solution:
#include <bits/stdc++.h>
int N, M;
vector<vector<int>> adj;
vector<vector<int>> dp;
bool isHamiltonian() {
return false;
int main() {
adj.assign(N, {});
for (int i = 0; i < M; ++i) {
Problem Statement:
You are given jobs with start time, end time, and profit. Schedule non-overlapping jobs to get
maximum total profit.
Input Format:
Constraints:
1 ≤ N ≤ 10⁴
1 ≤ profit ≤ 10⁴
Sample Input:
1 3 50
3 5 20
6 19 100
2 100 200
Sample Output:
250
#include <bits/stdc++.h>
using namespace std;
struct Job {
};
int main() {
int N;
cin >> N;
vector<Job> jobs(N);
vector<int> dp(N);
dp[0] = jobs[0].profit;
int l = -1, r = i - 1;
while (l <= r) {
int m = (l + r) / 2;
else r = m - 1;
Problem Statement:
Given a string S of digits and operators (+, -, *), return all possible results from computing the
expression with different valid parentheses placement.
Input Format:
Output:
All possible results (order doesn’t matter)
Sample Input:
2*3-4*5
Sample Output:
C++ Solution:
#include <bits/stdc++.h>
vector<int> res;
if (res.empty()) res.push_back(stoi(input));
int main() {
string expr;
Problem Statement:
You are given a tree with N nodes, each having a weight. You need to find the maximum sum of
weights of a subset of nodes such that no two adjacent nodes are both chosen.
Input Format:
First line: Integer N
Constraints:
1 ≤ N ≤ 10⁵
1 ≤ weight[i] ≤ 10⁴
Sample Input:
12345
12
13
34
35
Sample Output:
11
Explanation:
Choose nodes 2, 4, and 5 → sum = 2 + 4 + 5 = 11
C++ Solution:
#include <bits/stdc++.h>
vector<vector<int>> tree;
vector<int> weight;
vector<vector<int>> dp;
dp[node][0] = 0;
dp[node][1] = weight[node];
for (int child : tree[node]) {
dfs(child, node);
dp[node][1] += dp[child][0];
int main() {
int N;
cin >> N;
weight.resize(N + 1);
tree.resize(N + 1);
dp.assign(N + 1, vector<int>(2));
tree[u].push_back(v);
tree[v].push_back(u);
dfs(1, 0);
Problem Statement:
You're given an array of size N, and you need to answer Q queries. Each query gives two integers L
and R and asks for the maximum element in the range [L, R].
Input Format:
First line: N Q
Constraints:
1 ≤ N ≤ 10⁵
1 ≤ Q ≤ 10⁵
1 ≤ A[i] ≤ 10⁹
0≤L≤R<N
Sample Input:
53
13524
13
02
24
Sample Output:
#include <bits/stdc++.h>
vector<int> seg;
for (int i = n - 1; i > 0; --i) seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}
return res;
int main() {
int N, Q;
vector<int> A(N);
seg.resize(2 * N);
build(A, N);
while (Q--) {
int L, R;
Problem Statement:
Given a string S, return the length of the longest subsequence that is also a palindrome.
Input Format:
Constraints:
1 ≤ |S| ≤ 1000
Sample Input:
bbbab
Sample Output:
Explanation:
Longest palindromic subsequence is “bbbb”
C++ Solution:
#include <bits/stdc++.h>
int longestPalinSubseq(string s) {
int n = s.length();
dp[i][i] = 1;
if (s[i] == s[j])
else
int main() {
string s;
cin >> s;
Problem Statement:
Given an array of N integers, find the maximum XOR of any two elements.
Input Format:
Constraints:
1 ≤ N ≤ 10⁵
0 ≤ A[i] ≤ 10⁹
Sample Input:
3 10 5 25
Sample Output:
28
#include <bits/stdc++.h>
struct TrieNode {
node = node->child[b];
int maxXor = 0;
if (node->child[1 - b]) {
} else {
node = node->child[b];
return maxXor;
int main() {
int N;
cin >> N;
vector<int> nums(N);
cin >> x;
insert(root, x);
int ans = 0;
Problem Statement:
You are given an array where each element represents the max number of steps you can take
forward from that position. Write a function to return the minimum number of jumps to reach the
end of the array (from index 0).
Input Format:
Constraints:
1 ≤ N ≤ 10⁴
0 ≤ A[i] ≤ N
Sample Input:
231142
Sample Output:
3
Explanation:
Jump: 0 → 1 → 4 → 5
C++ Solution:
#include <bits/stdc++.h>
if (i == end) {
jumps++;
end = far;
int main() {
int n;
cin >> n;
vector<int> A(n);
Problem Statement:
You are given a graph with N nodes and M edges, each edge has a weight. Find the shortest distance
from node 1 to every other node using Dijkstra's algorithm.
Input Format:
First line: N M
Constraints:
1 ≤ N ≤ 10⁵
1 ≤ M ≤ 2*10⁵
1 ≤ w ≤ 10⁴
Sample Input:
56
122
134
231
247
353
451
Sample Output:
02396
C++ Solution:
#include <bits/stdc++.h>
int main() {
int n, m;
vector<vector<pair<int,int>>> adj(n+1);
adj[u].push_back({v, w});
adj[v].push_back({u, w});
dist[1] = 0;
pq.push({0, 1});
while (!pq.empty()) {
dist[v] = dist[u] + w;
pq.push({dist[v], v});
for (int i = 1; i <= n; i++) cout << (dist[i] == INF ? -1 : dist[i]) << " ";
Problem Statement:
Given an array A of N integers and a target sum K, find the maximum possible sum ≤ K by selecting a
subset of elements.
Input Format:
First line: N K
Constraints:
1 ≤ N ≤ 100
1 ≤ K ≤ 10⁵
1 ≤ A[i] ≤ 10⁴
Sample Input:
59
3 34 4 12 5
Sample Output:
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, K;
vector<int> A(N);
dp[0] = true;
for (int a : A)
break;
Problem Statement:
Given an array and integer K, find the maximum sum of any contiguous subarray of size K.
Input Format:
First line: N K
Constraints:
1 ≤ N ≤ 10⁵
1≤K≤N
Sample Input:
52
1 4 2 10 23
Sample Output:
33
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, K;
maxSum = sum;
Problem Statement:
Given a sorted array and a target sum, check if there exists a pair (i, j) such that arr[i] + arr[j] = target.
Input Format:
Constraints:
1 ≤ N ≤ 10⁵
1 ≤ arr[i] ≤ 10⁹
Sample Input:
69
1 2 4 4 7 10
Sample Output:
YES
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, target;
vector<int> A(N);
int i = 0, j = N - 1;
while (i < j) {
if (sum == target) {
return 0;
else j--;
Problem Statement:
You are given a directed graph with N nodes and M edges. Some edge weights may be negative.
Determine if the graph contains a negative weight cycle.
Input Format:
First line: N M
Next M lines: u v w
Constraints:
1 ≤ N ≤ 1000
1 ≤ M ≤ 10⁴
-10⁴ ≤ w ≤ 10⁴
Sample Input:
44
121
2 3 -1
3 4 -1
4 2 -1
Sample Output:
YES
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N, M;
vector<tuple<int,int,int>> edges(M);
dist[1] = 0;
dist[v] = dist[u] + w;
return 0;
Problem Statement:
Given a 2D board and a word, check if the word exists in the grid.
Input Format:
Constraints:
1 ≤ M, N ≤ 10
1 ≤ len(word) ≤ 100
Sample Input:
34
ABCE
SFCS
ADEE
ABCCED
Sample Output:
YES
C++ Solution:
#include <bits/stdc++.h>
int m, n;
vector<vector<char>> board;
vector<vector<bool>> visited;
string word;
visited[i][j] = true;
visited[i][j] = false;
return found;
int main() {
board.resize(m, vector<char>(n));
if (dfs(i, j, 0)) {
return 0;
Problem Statement:
Given N activities with start and end times, find the maximum number of non-overlapping activities
you can perform.
Input Format:
Constraints:
1 ≤ N ≤ 10⁵
Sample Input:
13
25
46
67
Sample Output:
C++ Solution:
#include <bits/stdc++.h>
int main() {
int N;
cin >> N;
sort(activities.begin(), activities.end());
count++;
lastEnd = activities[i].first;
Problem Statement:
Given an integer n, return the n-th Fibonacci number using Matrix Exponentiation in O(log n).
Input Format:
Single integer: n
Constraints:
0 ≤ n ≤ 10⁹
Sample Input:
10
Sample Output:
55
C++ Solution:
#include <bits/stdc++.h>
return C;
while (n) {
A = mul(A, A);
n >>= 1;
return res;
int main() {
int n;
cin >> n;
if (n == 0) {
cout << 0;
return 0;
Problem Statement:
Given n, find how many numbers from 1 to n are coprime with n using Euler’s Totient Function.
Input Format:
Single integer: n
Constraints:
1 ≤ n ≤ 10⁶
Sample Input:
Sample Output:
6
C++ Solution:
#include <bits/stdc++.h>
int phi(int n) {
int res = n;
if (n % p == 0) {
while (n % p == 0) n /= p;
res -= res / p;
return res;
int main() {
int n;
cin >> n;
Problem Statement:
Given a matrix dist[N][N] where dist[i][j] is the cost of going from city i to j, find the minimum cost to
visit all cities once and return to the starting city.
Input Format:
Constraints:
2 ≤ N ≤ 15
1 ≤ dist[i][j] ≤ 1000
Sample Input:
0 10 15 20
10 0 35 25
15 35 0 30
20 25 30 0
Sample Output:
80
C++ Solution:
#include <bits/stdc++.h>
int dist[15][15];
int N;
}
}
int main() {
cin >> N;
Done😊