Answers for "Given an unsorted array, print all triplets with sum less than given sum"

0

Given an unsorted array, print all triplets with sum less than given sum

// Given an unsorted array, print all triplets with sum less than or equal to given sum.

fn find_all_triplets( arr: [i32; 5], sum: i32) {
    let n = arr.len();
	for i in 0..n-2 {
	    for j in i+1..n-1 {
	        for k in j+1..n {
	            if arr[i] + arr[j] + arr[k] < sum {
	                println!("{}, {}, {}", arr[i], arr[j], arr[k]);
                }
            }
        }
	}
}

fn main() {
    let arr: [i32; 5] = [5, 1, 3, 4, 7];
    let sum = 12;
    find_all_triplets(arr, sum)
}
Posted by: Guest on August-18-2021

Code answers related to "Given an unsorted array, print all triplets with sum less than given sum"

Browse Popular Code Answers by Language