Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
i
we find nums[i] + diff
and nums[i] + (2 * diff)
in the set, we can be sure that they exist at indexes that satisfy the constraint of i < j < k
since any value greater than current index number's value would be at a higher index than current index (nums[i] + diff
and nums[i] + (2 * diff)
will always be greater indexes than nums[i]
since according to problem constraints 1 <= diff <= 50
).HAPPY CASE
Example 1:
Input: nums = [0,1,4,6,7,10], diff = 3
Output: 2
Example 2:
Input: nums = [4,5,6,7,8,9], diff = 2
Output: 2
EDGE CASE
Example 3:
Input: nums = [0,100,101,102,103,200], diff = 3
Output: 0
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array problems, we want to consider the following approaches:
nums
array into a set (hashtable) data structure which has O(1) look up time complexity.Plan the solution with appropriate visualizations and pseudocode.
General Idea:
1. Store all the elements of array in a map or a set
2. Traverse the array and see for every element if there are two more elements present in map
3. If present, then increment the count by 1
4. Return the count variable
โ ๏ธ Common Mistakes
(i, j , k)
such that i < j < k
, nums[j] - nums[i] == diff
, and nums[k] - nums[j] == diff.
We can rearrange the last 2 constraints to define nums[j]
and nums[k]
in terms of nums[i]
as follows:
nums[j] - nums[i] == diff ---> nums[j] = nums[i] + diff
nums[k] - nums[j] == diff ---> nums[k] = nums[j] + diff ---> nums[k] = (nums[i] + diff) + diff) ---> nums[k] = nums[i] + (2 * diff)
Implement the code to solve the algorithm.
class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
# create variable to count the number of triplets
ans = 0
# loop through nums backwards
for i in range(len(nums)-1, -1, -1):
# the current number minus diff and diff*2
a = nums[i] - (diff*1)
b = nums[i] - (diff*2)
# if both of these are in nums, we have a triplet
if (a in nums) and (b in nums):
ans +=1
return ans
class Solution {
public int arithmeticTriplets(int[] nums, int diff) {
# create variable to count the number of triplets
final var arr = new boolean[201];
# loop through nums backwards
for (int i : nums)
arr[i] = true;
int cnt = 0, t = 0;
# if both of these are in nums, we have a triplet
for (int i : nums)
if ( (t = i - diff) >= 0 && arr[t] && (t -= diff) >= 0 && arr[t])
cnt++;
return cnt;
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n
is the length of nums
,
Time Complexity: O(n) - we use two separate loops that iterate over the same number of elements
Space Complexity: O(n) - we need to store the mapping from numbers to their positions in the array