Given an integer array nums and an integer k, return all the indices i for which nums[i] - i is divisible by k.
k be zero?
k will be a non-zero integer, as division by zero is undefined.result to store the indices.nums.i, check if (nums[i] - i) % k == 0.
i to the list result.result.def find_indices(nums, k):
result = []
for i in range(len(nums)):
if (nums[i] - i) % k == 0:
result.append(i)
return result
# Example usage:
nums = [5, 3, 6, 2, 9]
k = 3
print(find_indices(nums, k)) # Output: [0, 2]
n elements exactly once. The modulo operation is constant time O(1). Thus, the total time complexity is O(n).result list, leading to a space complexity proportional to the input size. However, this is explicitly for the result list and not due to any auxiliary data structures.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?