Minimum Inversions π
Updated
Problem statement
Given an array of n integers, arr[n], find the value of x that can be applied to the array to minimize the number of inversions. The array can be modified by applying the bitwise XOR to each element of the array with x. The symbol β means XOR in the example.
An inversion in an array arr is a pair of indices (i, j) where i > j and arr[i] < arr[j].
Complete the function findMinInversions in the editor below.
findMinInversions has the following parameter:
int arr[n]: the array
long: the minimum possible number of inversions after modifying the array with integer x.
Function
findMinInversions(arr: int[]) β longExamples
Example 1
arr = [8, 5, 2]return = 0| Value of x to test | New Array | Number of Inversions |
|---|---|---|
| 4 | [8β4, 5β4, 2β4] = [12, 1, 6] | 2 |
| 3 | [11, 6, 7] | 2 |
| 12 | [4, 9, 14] | 0 |
In the first row, after the elements are XORed with 4, there are two inversions.
For [i, j] = [1, 0], arr[i] = 1 and arr[j] = 12. For [i, j] = [2, 0], arr[i] = 6 and arr[j] = 12.
For x = 12, the number of inversions is 0. This is the minimum possible, so the answer is 0.
Example 2
arr = [0, 8, 2, 4]return = 1| Value of x to test | New Array | Number of Inversions |
|---|---|---|
| 4 | [4, 12, 6, 0] | 4 |
| 8 | [8, 0, 10, 12] | 1 |
| 12 | [12, 4, 14, 8] | 3 |
Constraints
- 1 <= n <= 105
- 0 <= arr[i] <= 109
More TikTok interview questions
- Center of the Largest Perfect X-ShapeOA Β· Seen Nov 2026
- Count Subarrays Matching a Comparison PatternOA Β· Seen Nov 2026
- Count Suffix Word PairsOA Β· Seen Nov 2026
- Minimum Cost of Every K-Capable Model SetOA Β· Seen Nov 2026
- Longest Substring Without Repeating CharactersPHONE SCREEN Β· Seen Oct 2026
- Next Greater Indices and Maximum GapPHONE SCREEN Β· Seen Oct 2026
- Count Subarrays with at Least K Equal-Fruit PairsOA Β· Seen Oct 2026
- Shell Command Counts with History ReferencesOA Β· Seen Oct 2026
- Product Minus Sum of DigitsOA Β· Seen Aug 2026
- Count 2x2 Submatrices by Black CellsOA Β· Seen Aug 2026
Similar questions at other companies
- Maximize K-Element AND with an Increment BudgetAmazon Β· OA
- Sliding Window MedianAuxoAI Β· OA
- Delete and Freeze GameRubrik Β· OARubrik interview questions
- Most Segments That Sort the ArrayRippling Β· OARippling interview questions
- Optimize Discount Selection Under a Point BudgetPalantir Β· OAPalantir interview questions
- Count Prefix Subsets with a Given XORGoDaddy Β· OA
- Parcel Attribute Store with TTLAirbnb Β· OAAirbnb interview questions
- Maximum Index-Weighted Sum with Disjoint SwapsVisa Β· OAVisa interview questions
- Minimum Cost to Reach a Target WeightChubb Β· OAChubb interview questions
- Maximum Points in a Perimeter-Bounded RectangleAmerican Express Β· OAAmerican Express interview questions
TikTok interview experiences
- TikTok AI Infra Intern LLM Round 2: 1 Experience
- TikTok AI Infra Intern DeepSpeed Round: 1 Experience
- TikTok AI Infra Intern Kernel Round: 1 Experience
- TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
- TikTok AI Infra Intern Interview Timeline: 1 Experience
- TikTok AI Infra Intern Onsite Round 2: 1 Experience