This multi-part problem evaluates algorithmic problem-solving skills across bit manipulation and number theory, sliding-window and frequency counting, monotonic-stack patterns, permutation and prefix reasoning, numerical simulation/optimization, and constrained-jump dynamic programming, testing complexity analysis and data-structure selection.
You are given the following independent coding tasks. For each task, design an algorithm and implement a function that returns the requested output.
n to 0You are given a positive integer n.
Operation: choose an integer i >= 0 and replace n with n - 2^i or n + 2^i.
Assume you must keep n >= 0 after each operation.
Goal: return the minimum number of operations needed to make n = 0.
Input: n (fits in 64-bit signed integer)
Output: minimum operations (integer)
k distinct integersGiven an integer array arr and integer k, call a subarray good if it contains at least k distinct values.
Goal: return the length of the shortest good subarray; if no such subarray exists, return -1.
Example: arr = [1, 2, 2, 3, 1, 4], k = 3 → answer is 3 (e.g., subarray [2,3,1]).
Given an array prices.
For each index i, find the first index j > i such that prices[j] <= prices[i].
j
exists, the final price is
prices[i] - prices[j]
.
prices[i]
.
Return two things:
You are given a permutation p of 1..n.
For a given k (1 <= k <= n), say k is balanced if there exists some subarray p[l..r] that is a permutation of 1..k (each of 1..k appears exactly once in the subarray).
Goal: for every k = 1..n, determine if k is balanced and return a binary string s of length n where s[k-1] = '1' if balanced else '0'.
There are n floors to reach from floor 0 to floor n.
You may take the elevator for exactly x floors first (where 0 <= x <= n), then walk the remaining n - x floors.
e1
energy and takes
t1
time.
curr_energy = x * e1
.
ceil(c / curr_energy)
(given constant
c > 0
).
e2
after climbing that floor.
Goal: choose x to minimize |T_elevator(x) - T_stairs(x)| and return that minimum absolute difference.
If for some x the stairs part is infeasible due to energy dropping below 0, that x cannot be chosen.
Given an integer array arr of length n.
You start at index 0 with score arr[0], and must finish at index n-1.
From index i, you may jump to:
i + 1
, or
i + d
where
d
is in the set
{3, 13, 23, 33, ...}
and
d
is prime (i.e., prime numbers whose last digit is 3).
(Only jumps that stay within bounds are allowed.)
Goal: return the maximum achievable score at n-1, or -1 if n-1 is unreachable.
You are given a directed graph on n nodes labeled 0..n-1 with n-1 edges such that the underlying undirected graph is a tree.
You may pick any node as the root. You want to reverse as few directed edges as possible so that after reversals, every edge points away from the root.
Goal: return a root node that achieves the minimum number of reversals (if multiple, returning any one is acceptable).
Given an array prices (length n) and queries of the form (pos, amount):
pos
is
1-based
.
pos-1
, you can buy items consecutively to the right as long as the total cost does not exceed
amount
.
Goal: for each query, return the maximum number of items you can buy.
Return an array of answers in query order.
Given an array nums and an array queries.
For each query value q, you may pick a subsequence (not necessarily contiguous) of nums to maximize the subsequence length such that the sum of chosen elements is <= q.
Goal: return the maximum length for each query.
There are m services in a pipeline. Service i+1 consumes the output of service i.
throughput[i]
.
i
any number of times; each unit increase of throughput costs
scalecost[i]
.
<= budget
.
Assume the end-to-end pipeline throughput equals min(throughput[i]) after scaling.
Goal: return the maximum achievable pipeline throughput.
You are given arrays layers and energy of length n, and an initial energy K.
If you start at level i with current energy K:
j
(starting from
j=i
and increasing):
layers[j]
energy.
>= energy[j]
, you pass the level and gain 1 point; otherwise you fail and the run stops immediately.
Goal: return an array score of length n where score[i] is the number of consecutive levels you can pass starting from level i.
k tasks for person 1There are n tasks. If task i is done by person 1, you gain reward1[i]. If done by person 2, you gain reward2[i].
Constraint: person 1 must do exactly k tasks (and person 2 does the rest).
Goal: maximize total reward and return that maximum.
You are given a tree of treeNodes nodes labeled 0..treeNodes-1, rooted at node 0.
Each node u has a lowercase letter label nodes[u].
A downward path that ends at node u and starts at some ancestor a on the root-to-u path (including possibly a = u) is considered palindromic-rearrangeable if the multiset of letters along the path can be permuted to form a palindrome (equivalently: at most one character appears an odd number of times).
You are given queries, a list of node indices.
Goal: for each queried node q, return the number of ancestors a on the path from root to q such that the path a -> ... -> q is palindromic-rearrangeable.
Input format: edges are provided by parallel arrays nodeFrom, nodeTo (undirected edges).
Output: array of answers aligned with queries.