Count Operations
Updated
Problem statement
A permutation of n numbers is a sequence where each number from 1 to n appears exactly once. For a given permutation p and any arbitrary array arr, a permutation operation is defined as:
- class = 'mt-3'>For each index
i(1 ≤ i ≤ n) temp_arr[i] = arr[p[i]]
Given a permutation p of n numbers, start with any arbitrary array arr of n distinct elements and find out the minimum number of permutation operations (at least 1) needed in order to reach the original array. Since the answer can be quite large, return the answer modulo (10^9+7).
Function
countOperations(p: int[]) → int
Complete the function countOperations in the editor.
countOperations has the following parameter:
int p[n]: a permutation of the integers from1ton
Returns
int: the number of operations required modulo (10^9+7)
Examples
Example 1
p = [1, 3, 2]return = 2
In the above example, n = 3. Taking any arbitrary array arr = [7, 8, 9]:
- In each operation;
- the element at index 1 stays at index 1
- the element at index 2 gets mapped to index 3
- the element at index 3 gets mapped to index 2
- After applying operation for the first time on
arr, the resulting array is[7, 9, 8]. - After applying operation for the second time the resulting array is
[7, 8, 9].
After 2 operations we get back to the original array, hence we return 2 as the answer.
Constraints
1 ≤ n ≤ 10^51 ≤ p[i] ≤ npcontains all distinct elements, the integers1throughn
More Wells Fargo interview questions
- Server Upgrade PlanningOA · Seen Nov 2026
- Student and Course EnrollmentOA · Seen Nov 2026
- Bond Interest Summary Above 3%OA · Seen Oct 2026
- Count Phrases Formed by Anagram ReplacementOA · Seen Oct 2026
- Event Statistics AggregatorOA · Seen Sep 2026
- Ordered Deployment Configuration ValidatorOA · Seen Sep 2026
- Longest Subsequence That Is a SubstringOA · Seen Jul 2026
- Overloaded Game Account InventoriesOA · Seen Jul 2026
- Count Server ReplacementsOA · PHONE SCREEN · Seen Feb 2026
- Minimum Remaining LengthOA · PHONE SCREEN · Seen Feb 2026
Similar questions at other companies
- Drawing EdgeWeRide · OAWeRide interview questions
- Drawing EdgeIBM · OAIBM interview questions
- Drawing EdgeSnowflake · OASnowflake interview questions
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Two Ranked Teams Meeting in a Random BracketOptiver · OAOptiver interview questions
- Count Distinct Arrays After Decrement OperationsBloomberg LP · OABloomberg LP interview questions
- Count Routes Through Four Shop TypesOpenAI · OAOpenAI interview questions
- Count Fancy NumbersHudson River Trading · OAHudson River Trading interview questions
- Count Numbers with the Same Set-Bit CountMicrosoft · OAMicrosoft interview questions
- Count Reverse-Digit PairsVisa · OAVisa interview questions