aboutsummaryrefslogtreecommitdiff
path: root/find_pos_int_soln_given_eq.c
blob: b17ecf6d90dca4ac03d8a3ed6963bcc8312e20f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 1237. Find Positive Integer Solution for a Given Equation
#include "leetcode.h"

/*
 * given a callable function 'f(x, y)' with a hidden formula and a value 'z',
 * reverse engineer the formula and return all positive integer pairs 'x' and
 * 'y' where 'f(x, y) == z'. you may return the pairs in any order. while the
 * exact formula is hidden, the function is monotonically increasing.
 */

int function_id = 0;

int customFunction(int x, int y) {
  switch (function_id) {
  case 1:
    return x + y;
  case 2:
    return x * y;
  }
}

int **findSolution(int (*customFunction)(int, int), int z, int *returnSize,
                   int **returnColumnSizes) {
  int **ans = (int **)malloc(z * sizeof(int *)), k = 0;
  int *cols = *returnColumnSizes = (int *)malloc(z * sizeof(int));
  for (int i = 1; i <= z; i++)
    for (int j = z; j >= 1; j--)
      if (customFunction(i, j) == z) {
        (ans[k] = (int *)malloc(sizeof(int[cols[k] = 2])))[0] = i;
        ans[k++][1] = j;
      }
  *returnSize = k;
  return ans;
}

int main() {
  function_id = 1;
  int rs1, *rcs1, **fs1 = findSolution(customFunction, 5, &rs1, &rcs1);
  for (int i = 0; i < rs1; i++) {
    for (int j = 0; j < rcs1[i]; j++)
      printf("%d ", fs1[i][j]); // expect: [[1,4],[2,3],[3,2],[4,1]]
    printf("\n");
  }
  function_id = 2;
  int rs2, *rcs2, **fs2 = findSolution(customFunction, 5, &rs2, &rcs2);
  for (int i = 0; i < rs2; i++) {
    for (int j = 0; j < rcs2[i]; j++)
      printf("%d ", fs2[i][j]); // expect: [[1,5],[5,1]]
    printf("\n");
  }
  for (int i = 0; i < rs1; i++)
    free(fs1[i]);
  free(rcs1);
  free(fs1);
  for (int i = 0; i < rs2; i++)
    free(fs2[i]);
  free(rcs2);
  free(fs2);
}