🧠
Brute Force (Nested Loops Checking All Subsets)
💡 This approach exists to build intuition by trying all possible subsets of size K and checking their max-min difference. It’s slow but conceptually simple.
Intuition
Try every possible combination of K elements, find the difference between max and min in that subset, and track the minimum difference found.
Algorithm
- Generate all subsets of size K from the array.
- For each subset, find the maximum and minimum elements.
- Calculate the difference (max - min) for that subset.
- Keep track of the minimum difference found and return it.
💡 This algorithm is straightforward but inefficient because generating all subsets grows exponentially with input size.
from itertools import combinations
def min_diff_brute_force(arr, k):
min_diff = float('inf')
for subset in combinations(arr, k):
current_diff = max(subset) - min(subset)
if current_diff < min_diff:
min_diff = current_diff
return min_diff
# Driver code
if __name__ == '__main__':
arr = [10, 12, 10, 7, 5, 22]
k = 3
print(min_diff_brute_force(arr, k)) # Output: 2
Line Notes
from itertools import combinationsImport combinations to generate all subsets of size k
for subset in combinations(arr, k):Iterate over every possible subset of size k
current_diff = max(subset) - min(subset)Calculate difference between max and min in current subset
if current_diff < min_diff:Update minimum difference if current is smaller
import java.util.*;
public class MinDiffBruteForce {
public static int minDiffBruteForce(int[] arr, int k) {
int n = arr.length;
int minDiff = Integer.MAX_VALUE;
List<List<Integer>> subsets = new ArrayList<>();
generateCombinations(arr, k, 0, new ArrayList<>(), subsets);
for (List<Integer> subset : subsets) {
int maxVal = Collections.max(subset);
int minVal = Collections.min(subset);
int diff = maxVal - minVal;
if (diff < minDiff) minDiff = diff;
}
return minDiff;
}
private static void generateCombinations(int[] arr, int k, int start, List<Integer> current, List<List<Integer>> subsets) {
if (current.size() == k) {
subsets.add(new ArrayList<>(current));
return;
}
for (int i = start; i < arr.length; i++) {
current.add(arr[i]);
generateCombinations(arr, k, i + 1, current, subsets);
current.remove(current.size() - 1);
}
}
public static void main(String[] args) {
int[] arr = {10, 12, 10, 7, 5, 22};
int k = 3;
System.out.println(minDiffBruteForce(arr, k)); // Output: 2
}
}
Line Notes
generateCombinationsRecursively generate all subsets of size k
if (current.size() == k)Base case: subset of size k formed
int maxVal = Collections.max(subset);Find max in current subset
int minVal = Collections.min(subset);Find min in current subset
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void generateCombinations(vector<int>& arr, int k, int start, vector<int>& current, vector<vector<int>>& subsets) {
if (current.size() == k) {
subsets.push_back(current);
return;
}
for (int i = start; i < arr.size(); i++) {
current.push_back(arr[i]);
generateCombinations(arr, k, i + 1, current, subsets);
current.pop_back();
}
}
int minDiffBruteForce(vector<int>& arr, int k) {
vector<vector<int>> subsets;
vector<int> current;
generateCombinations(arr, k, 0, current, subsets);
int minDiff = INT_MAX;
for (auto& subset : subsets) {
int maxVal = *max_element(subset.begin(), subset.end());
int minVal = *min_element(subset.begin(), subset.end());
int diff = maxVal - minVal;
if (diff < minDiff) minDiff = diff;
}
return minDiff;
}
int main() {
vector<int> arr = {10, 12, 10, 7, 5, 22};
int k = 3;
cout << minDiffBruteForce(arr, k) << endl; // Output: 2
return 0;
}
Line Notes
generateCombinationsRecursive function to generate all subsets of size k
if (current.size() == k)Stop recursion when subset size k is reached
int maxVal = *max_element(subset.begin(), subset.end())Find max element in subset
int minVal = *min_element(subset.begin(), subset.end())Find min element in subset
function generateCombinations(arr, k, start, current, subsets) {
if (current.length === k) {
subsets.push([...current]);
return;
}
for (let i = start; i < arr.length; i++) {
current.push(arr[i]);
generateCombinations(arr, k, i + 1, current, subsets);
current.pop();
}
}
function minDiffBruteForce(arr, k) {
const subsets = [];
generateCombinations(arr, k, 0, [], subsets);
let minDiff = Infinity;
for (const subset of subsets) {
const maxVal = Math.max(...subset);
const minVal = Math.min(...subset);
const diff = maxVal - minVal;
if (diff < minDiff) minDiff = diff;
}
return minDiff;
}
// Driver code
const arr = [10, 12, 10, 7, 5, 22];
const k = 3;
console.log(minDiffBruteForce(arr, k)); // Output: 2
Line Notes
generateCombinationsRecursive helper to generate all subsets of size k
if (current.length === k)Base case: subset of size k formed
const maxVal = Math.max(...subset)Find max element in current subset
const minVal = Math.min(...subset)Find min element in current subset
TimeO(n choose k * k) which is exponential and impractical for large n
SpaceO(n choose k * k) due to storing all subsets
Generating all subsets of size k is combinatorial, and checking max-min for each subset adds extra work
💡 For n=20 and k=3, this means over 1140 subsets to check, which is borderline but grows quickly beyond that.
Interview Verdict: TLE / Use only to introduce problem
This approach is too slow for large inputs but helps understand the problem fully before optimizing.