This multi-problem set evaluates broad algorithmic problem-solving skills, including bitwise reasoning, sliding-window and frequency analysis, stack/monotonic patterns, graph/DP reasoning with constrained jumps, and tree-path parity techniques.
Below are multiple independent coding problems.
You are given a positive integer . In one operation you may replace with or for any integer .
Task: Return the minimum number of operations needed to make .
Constraints (typical): .
You are given an integer array arr of length and an integer .
A subarray is good if it contains at least distinct values.
Task: Return the length of the shortest good subarray. If no such subarray exists, return -1.
Constraints (typical): .
You are given an array prices.
For each index i, define the discount as the first index j > i such that prices[j] <= prices[i].
j
exists:
final[i] = prices[i] - prices[j]
final[i] = prices[i]
(sold at full price)
Task: Output:
Constraints (typical): .
You are given an integer array arr of length . You start at index 0 and must end at index n-1.
From index i, you may jump to:
i + 1
, or
i + p
where
p
is a
prime number
and
p % 10 == 3
(e.g., 3, 13, 23, 43, ...), and
i + p < n
.
Your score is the sum of arr values on all visited indices (including start and end).
Task: Return the maximum achievable score when reaching n-1. If n-1 is unreachable, return -1.
Constraints (typical): , arr[i] may be negative.
You are given a rooted tree with nodes 0..n-1 (root is node 0). Each node has a lowercase letter.
Input is provided as:
treeNodes = n
nodes
: a char array of length
n
, where
nodes[i]
is the character on node
i
nodeFrom
,
nodeTo
: arrays of length
n-1
describing directed parent→child edges (
nodeFrom[i] -> nodeTo[i]
), forming a tree
queries
: array of start nodes
For each query start node u:
v
on the path from
u
up to the root
0
(i.e.,
v
can be
u
, its parent, ...,
0
).
u -> ... -> v
(inclusive) be collected.
Task: For each query u, return how many such endpoints v exist.
Constraints (typical): .
You have services connected in a pipeline. The pipeline throughput is:
You may scale service i any number of times. Each scaling:
throughput[i]
by
1
scale_cost[i]
Given integer budget, total scaling cost must be .
Task: Return the maximum achievable pipeline throughput T.
Constraints (typical): , values up to .
You must go from floor 0 to floor N.
You may take the elevator first from floor 0 to floor x (choose x once, 0 <= x <= N).
e1
energy and spend
t1
time.
E = x * e1
.
Then you must climb the remaining N - x floors using stairs. For each stair floor:
E
, the time spent is
ceil(c / E)
.
e2
energy:
E := E - e2
.
E < 0
at any step, that choice of
x
is invalid.
Task: Choose x to minimize total time. If no x is feasible, return -1.
Constraints (typical): , parameters are positive integers.
You have n tasks. If task i is done by:
reward1[i]
reward2[i]
Each task must be assigned to exactly one person. Person A must do exactly k tasks.
Task: Return the maximum possible total reward.
Constraints (typical): , .
You are given:
layers[0..n-1]
: energy cost to attempt/pass level
j
energyReq[0..n-1]
: minimum remaining energy needed
after paying the cost
to pass level
j
K
Starting from index i:
E = K
.
j = i..n-1
:
E := E - layers[j]
. If
E < 0
, stop.
E >= energyReq[j]
, you pass and gain 1 point; otherwise stop (cannot proceed further).
Task: Return an array score of length n where score[i] is the number of points you can earn starting at level i.
Note: An solution will time out; design a faster approach.
You are given prices[0..n-1] (positive integers). Each query provides:
pos
(1-based starting index)
amount
(budget)
From index start = pos - 1, you can buy items consecutively to the right while the running sum does not exceed amount.
Task: For each query, return the maximum number of items you can buy.
Constraints (typical): .
You are given n nodes and n-1 directed edges edges[i] = [a, b] meaning a -> b. Ignoring direction, the graph is a tree.
For each node r considered as the root, you may reverse any subset of edges.
Task: Compute res[r] = the minimum number of edge reversals required so that every node has a directed path to r (equivalently, all edges point toward r along the tree). Return res for all r.
Constraints (typical): .
You are given a permutation perm[0..n-1] containing each integer from 1 to n exactly once.
For each window size , define ans[w] = 1 if the positions of the numbers {1,2,...,w} in perm form a contiguous subarray (in any order). Otherwise, ans[w] = 0.
Equivalently, let pos[x] be the index where value x appears. Then ans[w] = 1 iff:
Task: Return ans[1..n].
Constraints (typical): .