🧠
Brute Force (Nested Loops)
💡 This approach exists to build intuition by directly computing sums of all possible subarrays of size K, which helps understand the problem before optimizing.
Intuition
Check every possible subarray of size K by summing its elements and track the maximum sum found.
Algorithm
- Initialize max_sum to smallest possible integer.
- For each index i from 0 to n-K:
- Calculate the sum of subarray from i to i+K-1.
- Update max_sum if current sum is greater.
💡 The nested loops make it clear how we consider every subarray, but summing repeatedly is inefficient.
def max_sum_subarray_brute(arr, k):
n = len(arr)
max_sum = float('-inf')
for i in range(n - k + 1):
current_sum = 0
for j in range(i, i + k):
current_sum += arr[j]
if current_sum > max_sum:
max_sum = current_sum
return max_sum
# Driver code
if __name__ == '__main__':
arr = [2, 1, 5, 1, 3, 2]
k = 3
print(max_sum_subarray_brute(arr, k)) # Output: 9
Line Notes
n = len(arr)Determine the length of the array to set loop boundaries
max_sum = float('-inf')Initialize max_sum to smallest number to ensure any sum will be larger
for i in range(n - k + 1)Loop over all valid starting indices for subarrays of size k
for j in range(i, i + k)Sum elements in the current subarray from i to i+k-1
public class MaxSumSubarrayBrute {
public static int maxSumSubarrayBrute(int[] arr, int k) {
int n = arr.length;
int maxSum = Integer.MIN_VALUE;
for (int i = 0; i <= n - k; i++) {
int currentSum = 0;
for (int j = i; j < i + k; j++) {
currentSum += arr[j];
}
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
return maxSum;
}
public static void main(String[] args) {
int[] arr = {2, 1, 5, 1, 3, 2};
int k = 3;
System.out.println(maxSumSubarrayBrute(arr, k)); // Output: 9
}
}
Line Notes
int n = arr.length;Get array length for loop limits
int maxSum = Integer.MIN_VALUE;Initialize maxSum to smallest integer to track maximum
for (int i = 0; i <= n - k; i++)Iterate over all starting indices for subarrays of size k
for (int j = i; j < i + k; j++)Sum elements in the current subarray
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int maxSumSubarrayBrute(const vector<int>& arr, int k) {
int n = arr.size();
int maxSum = INT_MIN;
for (int i = 0; i <= n - k; i++) {
int currentSum = 0;
for (int j = i; j < i + k; j++) {
currentSum += arr[j];
}
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
return maxSum;
}
int main() {
vector<int> arr = {2, 1, 5, 1, 3, 2};
int k = 3;
cout << maxSumSubarrayBrute(arr, k) << endl; // Output: 9
return 0;
}
Line Notes
int n = arr.size();Get size of vector for loop boundaries
int maxSum = INT_MIN;Initialize maxSum to smallest int to track maximum sum
for (int i = 0; i <= n - k; i++)Loop over all valid start indices for subarrays
for (int j = i; j < i + k; j++)Sum elements in the current subarray
function maxSumSubarrayBrute(arr, k) {
const n = arr.length;
let maxSum = -Infinity;
for (let i = 0; i <= n - k; i++) {
let currentSum = 0;
for (let j = i; j < i + k; j++) {
currentSum += arr[j];
}
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
return maxSum;
}
// Test
const arr = [2, 1, 5, 1, 3, 2];
const k = 3;
console.log(maxSumSubarrayBrute(arr, k)); // Output: 9
Line Notes
const n = arr.length;Store array length for loop limits
let maxSum = -Infinity;Initialize maxSum to smallest number to track maximum
for (let i = 0; i <= n - k; i++)Iterate over all possible subarray start indices
for (let j = i; j < i + k; j++)Sum elements in the current subarray
For each of the n-k+1 subarrays, we sum k elements, resulting in O(n*k) time.
💡 If n=1000 and k=500, this means about 500,000 operations, which is inefficient for large inputs.
Interview Verdict: TLE for large inputs
This approach is too slow for large arrays, so we need to optimize it using a better technique.