about summary refs log tree commit diff stats
path: root/janet/tmplt/src
diff options
context:
space:
mode:
Diffstat (limited to 'janet/tmplt/src')
-rw-r--r--janet/tmplt/src/app.janet18
1 files changed, 18 insertions, 0 deletions
diff --git a/janet/tmplt/src/app.janet b/janet/tmplt/src/app.janet
new file mode 100644
index 0000000..f67e8e3
--- /dev/null
+++ b/janet/tmplt/src/app.janet
@@ -0,0 +1,18 @@
+(defn sum3
+  "Solve the 3SUM problem in O(n^2) time."
+  [s]
+  (def tab @{})
+  (def solutions @{})
+  (def len (length s))
+  (for k 0 len
+    (put tab (s k) k))
+  (for i 0 len
+    (for j 0 len
+      (def k (get tab (- 0 (s i) (s j))))
+      (when (and k (not= k i) (not= k j) (not= i j))
+        (put solutions {i true j true k true} true))))
+  (map keys (keys solutions)))
+
+(let [arr @[2 4 1 3 8 7 -3 -1 12 -5 -8]]
+  (printf "3sum of %j: " arr)
+  (printf "%j" (sum3 arr)))
\ No newline at end of file