// Arup Guha
// 8/31/2023
// Code for COP 3502 Quiz 1 Version B Question 2

#include <stdio.h>
#include <stdlib.h>

int sub_to_target(int list[], int n, int target);

int main() {

    int vals[10] = {1,4,5,19,22,25,30,37,45,60};

    // Set up some tests.
    for (int i=1; i<=105; i++)
        if (sub_to_target(vals, 10, i))
            printf("%d ", i);
    printf("\n");
    return 0;
}

// Pre-condition: list is length n, sorted, with unique values.
// Post-condition: Returns 1 if two different values in list have a
//                 difference of target, and 0 otherwise.
int sub_to_target(int list[], int n, int target) {

    int i = 0, j = 0;

    // Can stop when first pointer falls off list.
    while ( j<n ) {

        // Must increase larger value.
        if (list[j]-list[i]<target)
            j++;

        // Must increase smaller value.
        else if (list[j]-list[i]>target)
            i++;

        // We got it!
        else
            return 1;
     }

     // No answer.
     return 0;
}
