aboutsummaryrefslogtreecommitdiff
path: root/find_pos_int_soln_given_eq.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-30 10:03:07 -0600
committerJack Sangdahl <[email protected]>2025-08-30 10:03:07 -0600
commit06f004e1d27ec3a4a75220f7b61ccfe47b4db75e (patch)
treebdb210a9f31adc9596e769bceef43dcdbe6e82a9 /find_pos_int_soln_given_eq.c
parent9bdedfa9d768668c571d721d0b7a30f0eae1b58c (diff)
downloadleetcode-06f004e1d27ec3a4a75220f7b61ccfe47b4db75e.tar.gz
leetcode-06f004e1d27ec3a4a75220f7b61ccfe47b4db75e.zip
1237. find positive integer solution for a given equation
Diffstat (limited to 'find_pos_int_soln_given_eq.c')
-rw-r--r--find_pos_int_soln_given_eq.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/find_pos_int_soln_given_eq.c b/find_pos_int_soln_given_eq.c
new file mode 100644
index 0000000..b17ecf6
--- /dev/null
+++ b/find_pos_int_soln_given_eq.c
@@ -0,0 +1,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);
+}