Leetcode 2961: Double Modular Exponentiation

grid47
grid47
Exploring patterns and algorithms
Jan 15, 2024 6 min read

Given a 2D array variables where each element is a list of integers [a, b, c, m], and an integer target, find the indices where the formula (a * b % 10) ^ c % m equals the target. Return a list of these indices.
Problem
Approach
Steps
Complexity
Input: You are given a 2D array `variables` and an integer `target`. Each element of the array represents a group of four integers `[a, b, c, m]`.
Example: variables = [[4, 2, 3, 5], [7, 2, 2, 4], [6, 1, 1, 10]], target = 3
Constraints:
• 1 <= variables.length <= 100
• 1 <= a, b, c, m <= 103
• 0 <= target <= 103
Output: Return an array of indices where the result of the formula equals `target`.
Example: [0, 2]
Constraints:
Goal: To identify which indices satisfy the condition `(a * b % 10) ^ c % m == target`.
Steps:
• Iterate over the array `variables`.
• For each element `[a, b, c, m]`, compute `(a * b % 10) ^ c % m` and compare it with `target`.
• If they are equal, add the index to the result array.
• Return the result array.
Goal: Constraints for input sizes and value ranges.
Steps:
• 1 <= variables.length <= 100
• 1 <= a, b, c, m <= 103
• 0 <= target <= 103
Assumptions:
• All elements of the input are within the given constraints.
Input: Input: variables = [[4, 2, 3, 5], [7, 2, 2, 4], [6, 1, 1, 10]], target = 3
Explanation: For index 0: (4 * 2 % 10)^3 % 5 = 3, for index 1: (7 * 2 % 10)^2 % 4 = 0, for index 2: (6 * 1 % 10)^1 % 10 = 6. Thus, the good indices are [0, 2].

Input: Input: variables = [[5, 5, 3, 10]], target = 7
Explanation: For index 0: (5 * 5 % 10)^3 % 10 = 5. No indices match the target.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus