Given a list of celestial body masses and a target gravitational pull, find all pairs of celestial bodies with masses that add up to the target pull and return their indices in the list.
Explanation: Step-by-step: Given the input [9, 12, 3, 7, 6, 2, 1, 5, 4, 8], we need to find all pairs of celestial bodies with masses that add up to the target pull. We first sort the masses array in ascending order. Then, we initialize two pointers, one at the start and one at the end of the array. We iterate through the array, and for each pair of masses, we check if their sum equals the target pull. If it does, we add the pair's indices to the output list. We continue this process until the two pointers meet.
Explanation: Step-by-step: Given the input [30, 30, 10, 20, 15, 25], we need to find all pairs of celestial bodies with masses that add up to the target pull. We first sort the masses array in ascending order. Then, we initialize two pointers, one at the start and one at the end of the array. We iterate through the array, and for each pair of masses, we check if their sum equals the target pull. If it does, we add the pair's indices to the output list. We continue this process until the two pointers meet.
Master coding challenges related to Arrays and solve the [Backup] Astronomy Duo problem optimally.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak