Interview Question Series | Season 2

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 left and right pointers:
    • If the sum equals the target, print the indices and stop.
    • If the sum is less than the target, move the left pointer to the right to increase the sum.
    • If the sum is greater than the target, move the right pointer 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:

  1. 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}
    
  2. Two-pointer technique:

    • left starts at index 0 (price 200).
    • right starts at index 4 (price 450).
    • We check the sum of the values at left and right pointers. If the sum is equal to 750, we print the indices and prices. If the sum is less than 750, we move the left pointer rightwards to increase the sum. If the sum is greater than 750, we move the right pointer leftwards to decrease the sum.
  3. Output: If the pair of prices summing to 750 is 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 (price 200) and right = 4 (price 450), sum = 200 + 450 = 650 (less than 750), so we move left pointer right to 1.
  • Now, left = 1 (price 250) and right = 4 (price 450), sum = 250 + 450 = 700 (still less than 750), so we move left pointer right to 2.
  • Now, left = 2 (price 350) and right = 4 (price 450), sum = 350 + 450 = 800 (greater than 750), so we move right pointer left to 3.
  • Now, left = 2 (price 350) and right = 3 (price 400), 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).
Scroll to Top