Double Modular Exponentiation - Problem

You are given a 0-indexed 2D array variables where variables[i] = [ai, bi, ci, mi], and an integer target.

An index i is good if the following formula holds:

((aibi % 10)ci) % mi == target

Return an array consisting of good indices in any order.

Input & Output

Example 1 — Basic Case
$ Input: variables = [[2,3,3,10],[3,1,4,12],[7,3,1,17],[4,2,5,5]], target = 3
Output: [2]
💡 Note: For index 2: ((7³ % 10)¹) % 17 = (343 % 10)¹ % 17 = 3¹ % 17 = 3. Only index 2 matches target 3.
Example 2 — Multiple Matches
$ Input: variables = [[39,3,1000,1000]], target = 49
Output: [0]
💡 Note: For index 0: ((39³ % 10)¹⁰⁰⁰) % 1000 = (59319 % 10)¹⁰⁰⁰ % 1000 = 9¹⁰⁰⁰ % 1000 = 49.
Example 3 — No Matches
$ Input: variables = [[2,2,2,2]], target = 5
Output: []
💡 Note: For index 0: ((2² % 10)²) % 2 = (4²) % 2 = 16 % 2 = 0 ≠ 5. No indices match.

Constraints

  • 1 ≤ variables.length ≤ 1000
  • variables[i] = [ai, bi, ci, mi]
  • 1 ≤ ai, bi, ci, mi ≤ 106
  • 0 ≤ target ≤ 106

Visualization

Tap to expand
Double Modular Exponentiation ProblemVariables Array[2,3,3,10][3,1,4,12][7,3,1,17]Formula Application((a^b % 10)^c) % mStep 1: a^b % 10Step 2: result^c % mTarget CheckTarget = 3Index 2: 3 = 3 ✓Good indices: [2]Example: For [7,3,1,17] → (7³%10)¹%17 = 3¹%17 = 3
Understanding the Visualization
1
Input
2D array of variables and target value
2
Process
Apply double modular exponentiation formula to each row
3
Output
Array of indices where formula equals target
Key Takeaway
🎯 Key Insight: Use modular exponentiation to efficiently handle large powers while preventing integer overflow
Asked in
Google 15 Amazon 12 Microsoft 8
23.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen