Problem:
We have an array of prices of clothes, and we want to find two items whose total price equals the target price (750 in this case). We are asked to use the two-pointer technique to efficiently find the pair of prices.
Two-pointer technique:
- Step 1: First, sort the array of prices. Sorting helps because we can use two pointers to efficiently find the two elements that sum up to the target.
- Step 2: Initialize two pointers, one at the beginning (
left) and the other at the end (right) of the sorted array. - Step 3: Calculate the sum of the elements at the
leftandrightpointers:- If the sum equals the target, print the indices and stop.
- If the sum is less than the target, move the
leftpointer to the right to increase the sum. - If the sum is greater than the target, move the
rightpointer to the left to decrease the sum.
- Step 4: Continue this process until the two pointers meet.
Corrected Code:
#include <stdio.h>
#include <stdlib.h>
// Comparator function to sort the array in ascending order
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
void best_items(int *nums, int numsSize, int target) {
// Sort the array to apply the two-pointer technique
qsort(nums, numsSize, sizeof(int), compare);
int left = 0;
int right = numsSize - 1;
while(left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
printf("2 products in indices: %d and %d with prices %d and %dn", left, right, nums[left], nums[right]);
return;
} else if (sum < target) {
left++;
} else {
right--;
}
}
printf("No products according to your money found.n");
}
int main() {
int nums[] = {200, 250, 350, 400, 450};
int target = 750;
int numsSize = sizeof(nums) / sizeof(nums[0]);
best_items(nums, numsSize, target);
return 0;
}
Explanation:
-
Sorting: We first sort the array using
qsort(), which is necessary for the two-pointer technique to work. After sorting, the array becomes:{200, 250, 350, 400, 450} -
Two-pointer technique:
leftstarts at index0(price200).rightstarts at index4(price450).- We check the sum of the values at
leftandrightpointers. If the sum is equal to750, we print the indices and prices. If the sum is less than750, we move theleftpointer rightwards to increase the sum. If the sum is greater than750, we move therightpointer leftwards to decrease the sum.
-
Output: If the pair of prices summing to
750is found, the function prints the indices and prices. If no such pair exists, it prints a message indicating that no products match the target price.
Example Walkthrough:
For the array {200, 250, 350, 400, 450}, and target 750:
- Initially,
left = 0(price200) andright = 4(price450), sum =200 + 450 = 650(less than750), so we moveleftpointer right to1. - Now,
left = 1(price250) andright = 4(price450), sum =250 + 450 = 700(still less than750), so we moveleftpointer right to2. - Now,
left = 2(price350) andright = 4(price450), sum =350 + 450 = 800(greater than750), so we moverightpointer left to3. - Now,
left = 2(price350) andright = 3(price400), sum =350 + 400 = 750, which matches the target!
Output:
2 products in indices: 2 and 3 with prices 350 and 400
Time Complexity:
- Sorting the array takes
O(n log n). - The two-pointer traversal of the array takes
O(n). - Overall, the time complexity is
O(n log n)due to the sorting step.
Space Complexity:
- We are using a constant amount of space for the pointers and variables, so the space complexity is
O(1)(ignoring the input array).
