Skip to content

Instantly share code, notes, and snippets.

@shandanjay
Last active June 23, 2024 11:15
Show Gist options
  • Save shandanjay/f1654cf7296351ad64d82349ce84006c to your computer and use it in GitHub Desktop.
Save shandanjay/f1654cf7296351ad64d82349ce84006c to your computer and use it in GitHub Desktop.
Highest product of triplets of a given list of integers
;; Clojure
(defn highest-product-of-triplets [g]
"returns the highest product of 3 from given `list of integers` g"
(if (count g) > 2)
(let [sorted (sort g)
triplets (partition 3 1 sorted)
max-triplet (last triplets)
min-triplet (first triplets)
negatives (filter #(> 0 %) min-triplet)]
(if (and (> (count negatives) 1) ;; atleast 2 negatives to make a positive
(< (apply max max-triplet) (abs (apply * negatives))))
(let [[a b] min-triplet]
(* (last max-triplet) (apply max (map * [a b] [b a]))
))
;; Not enough negative integers found
;; (reduce * (take 3 (reverse (sort g))))
(reduce * (take 3 (reverse sorted)))
)))
(def a '(-1, 2, 3, -5, -6, 7, 11, 4))
(def b '(-1, 1, -20, 600, 15, 15))
(def c '(1, 10, 2, 6, 5, 3))
(highest-product-of-triplets a) ;; 330
(highest-product-of-triplets b) ;; => 135000
(highest-product-of-triplets c) ;; => 300
;; Clojurescript
(defn highest-product-of-triplets [g]
"returns the highest product of 3 from given `list of integers` g"
(if (count g) > 2)
(let [sorted (sort g)
triplets (partition 3 1 sorted)
max-triplet (last triplets)
min-triplet (first triplets)
negatives (filter #(> 0 %) min-triplet)]
(if (and (> (count negatives) 1) ;; atleast 2 negatives to make a positive
(< (apply max max-triplet) (js/Math.abs (apply * negatives))))
(let [[a b] min-triplet]
(* (last max-triplet) (apply max (map * [a b] [b a]))
))
;; Not enough negative integers found
;; (reduce * (take 3 (reverse (sort g))))
(reduce * (take 3 (reverse sorted)))
)))
(def a '(-1, 2, 3, -5, -6, 7, 11, 4))
(def b '(-1, 1, -20, 600, 15, 15))
(def c '(1, 10, 2, 6, 5, 3))
(highest-product-of-triplets a) ;; 330
(highest-product-of-triplets b) ;; => 135000
(highest-product-of-triplets c) ;; => 300
package org.example;
public class Main {
// Only using java.lang package Math.max(int, int) & Math.min(int, int) methods
public static int highestProductOfTriplets(int[] nums) {
if (nums.length < 3 ) {
throw new IllegalArgumentException("Array must contain at least 3 integers");
}
// Initialize variables to track highest and lowest values
int highest = Math.max(nums[0], nums[1]);
int lowest = Math.min(nums[0], nums[1]);
int highestProductOf2 = nums[0] * nums[1];
int lowestProductOf2 = nums[0] * nums[1];
int highestProductOf3 = nums[0] * nums[1] * nums[2];
// Iterate through the array, updating variables as needed
for (int i = 2; i < nums.length; i++) {
int current = nums[i];
highestProductOf3 = Math.max(highestProductOf3,
Math.max(current * highestProductOf2, current * lowestProductOf2));
highestProductOf2 = Math.max(highestProductOf2,
Math.max(current * highest, current * lowest));
lowestProductOf2 = Math.min(lowestProductOf2,
Math.min(current * highest, current * lowest));
highest = Math.max(highest, current);
lowest = Math.min(lowest, current);
}
return highestProductOf3;
}
public static void main(String[] args) {
int[] nums1 = {1, 10, 2, 6, 5, 3}; // 300
int [] nums2 = { 100, -10, -20, 600, 15, 15, 15 , 20, 600 }; // 36000000
int [] nums3 = {-1, -1, -20, 600, 15, 15}; // 135000
int [] nums4 = {-1, 2, 3, -5, -6, 7, 11, 4}; // 330
int [] nums5 = {10, 10, 20, 6, -15, -15, -15 , 2, 6,6,15}; // 4500
int [] nums6 = {200, 31, 422, 5, 1, 0, 45}; // 3798000
int result = highestProductOfTriplets (nums1);
System.out.println(result); // 300
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment