// Arup Guha
// 8/31/2023
// Code for COP 3502 Quiz 1 Version D Question 2

#include <stdio.h>
#include <stdlib.h>

int div_to_target(int list[], int n, int target);

int main() {

    int vals[10] = {1,2,6,24,120,3000,6000,12000,24000, 28000};

    // Run some tests!
    for (int i=2; i<=28000; i++)
        if (div_to_target(vals, 10, i))
            printf("%d ", i);
    printf("\n");
    return 0;
}

// Pre-condition: list is length n, sorted, with unique positive ints
//                less than 30000. 1 < target < 30000.
// Post-condition: Returns 1 if two different values in list are such
//                 that when the larger is divided by the smaller it
//                 equals target exactly, or 0 otherwise.

int div_to_target(int list[], int n, int target) {

    int i = 0, j = 0;

    // We can stop when larger number runs off the lsit.
    while ( j<n ) {

        // Our product is too big, which means that we need to divide into a bigger number.
        if (list[i]*target > list[j])
            j++;

        // Opposite is true here.
        else if (list[i]*target < list[j])
            i++;

        // We got it!
        else
            return 1;
     }

     // Never got it.
     return 0;
}
