👨‍💻
Coding Interview Patterns
  • Coding Interview Patterns
  • 1. Pattern: Sliding Window
    • 1.0 Introduction
    • 1.1 Maximum Sum Subarray of Size K (easy)
    • 1.2 Smallest Subarray with a given sum (easy)
    • 1.3 Longest Substring with K Distinct Characters (medium)
    • 1.4 Fruits into Baskets (medium)
    • 1.5 No-repeat Substring (hard)
    • 1.6 Longest Substring with Same Letters after Replacement (hard)
    • 1.7 Longest Subarray with Ones after Replacement (hard)
    • 1.8 - Permutation in a String (hard)
    • 1.9 String Anagrams (hard)
    • 1.10 Smallest Window containing Substring (hard)
    • 1.11 Words Concatenation (hard)
  • 2. Pattern: Two Pointers
    • 2.0 Introduction
    • 2.1 Pair with Target Sum (easy)
    • 2.2 Remove Duplicates (easy)
    • 2.3 Squaring a Sorted Array (easy)
    • 2.4 Triplet Sum to Zero (medium)
    • 2.5 Triplet Sum Close to Target (medium)
    • 2.6 Triplets with Smaller Sum (medium)
    • 2.7 Subarrays with Product Less than a Target (medium)
    • 2.8 Dutch National Flag Problem (medium)
    • 2.9 Comparing Strings containing Backspaces (medium)
    • 2.10 Minimum Window Sort (medium)
  • 7. Pattern: Tree Breadth First Search
    • 7.0 Introduction
    • 7.1 Binary Tree Level Order Traversal (easy)
    • 7.2 Reverse Level Order Traversal (easy)
    • 7.3 Zigzag Traversal (medium)
    • 7.4 Level Averages in a Binary Tree (easy)
    • 7.5 Minimum Depth of a Binary Tree (easy)
    • 7.6 Maximum Depth of Binary Tree (easy)
    • 7.7 Level Order Successor (easy)
    • 7.8 Connect Level Order Siblings (medium)
    • 7.9 Problem Challenge 1 - Connect All Level Order Siblings (medium)
    • 7.10 Problem Challenge 2 - Right View of a Binary Tree (easy)
  • 11. Pattern: Modified Binary Search
    • 11.1 Introduction
    • 11.2 Order-agnostic Binary Search (easy)
    • 11.3
  • 16. Pattern: Topological Sort (Graph)
    • 16.1 Introduction
    • 16.2 Topological Sort (medium)
    • 16.3 Tasks Scheduling (medium)
    • 16.4 Tasks Scheduling Order (medium)
  • Contributor Covenant Code of Conduct
  • Page 1
Powered by GitBook
On this page

Was this helpful?

  1. 1. Pattern: Sliding Window

1.1 Maximum Sum Subarray of Size K (easy)

Problem Statement

Given an array of positive numbers and a positive number ‘k,’ find the maximum sum of any contiguous subarray of size ‘k’.

Example 1:

Input: [2, 1, 5, 1, 3, 2], k=3 
Output: 9
Explanation: Subarray with maximum sum is [5, 1, 3].

Example 2:

Input: [2, 3, 4, 1, 5], k=2 
Output: 7
Explanation: Subarray with maximum sum is [3, 4].

How can we solve the question?

We can think of the 'k' as window size of sliding window in an array.

  1. With each slide we will remove an element from the left and add an element from the right.

  2. Calculate the sum for that window and compare the sum of that window to prevous max sum of sliding windows.

  3. Beware: Until the window size becomes equal to 'k', we have to just add the next element to the right but we shouldn't remove the element to the left.

So, code for that condition:

public int maxSumOfContiguousSubArray(int arr[], int k) {
    int maxSum = 0; // It will keep the maximum Sum of the subarrays 
        // with the size 'k' and it will have the result by the end of the for-loop

    int windowSum = 0; // It will keep the sum of elements of that 
        // window till the current element

    int windowStart = 0; // Starting index of the window
    for(int windowEnd = 0; windowEnd < arr.length; windowEnd++) {
        // For each iteration ending of the window would be increase by 1.

        windowSum += arr[windowEnd]; //same as windowSum = windowSum + arr[windowEnd],
            // element to the right is added to the window so value of that element added 
            // with the previous elements within the window

        if(windowEnd >= k - 1) {
            // when the window size becomes equal to k(before that the size would less that k)

            maxSum = Math.max(maxSum, windowSum);
            // Taking the maximum value out of maxSum and the current window(i.e windowSum)

            windowSum -= arr[windowStart];
            // Removing the element from the left, windowStart is the starting index of the window;

            windowStart++;
            // windowStart is updated by 1 as the window slides to the right 
        }
    }
    return maxSum;
}

Time Complexity

The time complexity of the above algorithm will be O(N).

Space Complexity

The algorithm runs in constant space O(1).

Previous1.0 IntroductionNext1.2 Smallest Subarray with a given sum (easy)

Last updated 2 years ago

Was this helpful?