Complement of two numbers
What does it mean 'complement of two numbers' for example in a dictionary? Why don't they explain everything ? I had problem with this exercise :
Implement a function that finds a pair of numbers in a list (nums) that, when multiplied, equal a given target product (target_product). The function should return the first pair of indices [i, j] as a list such that nums[i] * nums[j] == target_product. If no such pair exists, return -1.
You can assume that all numbers in the nums list, as well as target_product, are non-zero.
Implement a function that finds a pair of numbers in a list (nums) that, when multiplied, equal a given target product (target_product). The function should return the first pair of indices [i, j] as a list such that nums[i] * nums[j] == target_product. If no such pair exists, return -1.
You can assume that all numbers in the nums list, as well as target_product, are non-zero.
2 answers ( 0 marked as helpful)
Hi Lenka!
Thanks for reaching out!
The "complement" of a number refers to the second number needed to reach a specific target product when multiplied by a given number in the list.
For each number in the list, calculate the complement needed to reach the target_product.
For example, if target_product is 20 and you’re examining the number 4 in the list, the complement would be 5, because 4×5=20.
So, as you go through each number in the list, you can calculate the complement for each one based on the target product. Then, store each number you've seen so far in a dictionary for quick lookup. If the complement is already in the dictionary, you've found a pair that meets the condition.
Let me know if this clarifies the concept or if you have more questions!
Best,
Ivan
Thanks for reaching out!
The "complement" of a number refers to the second number needed to reach a specific target product when multiplied by a given number in the list.
For each number in the list, calculate the complement needed to reach the target_product.
For example, if target_product is 20 and you’re examining the number 4 in the list, the complement would be 5, because 4×5=20.
So, as you go through each number in the list, you can calculate the complement for each one based on the target product. Then, store each number you've seen so far in a dictionary for quick lookup. If the complement is already in the dictionary, you've found a pair that meets the condition.
Let me know if this clarifies the concept or if you have more questions!
Best,
Ivan
Thanks for answer.