DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on • Edited on

DAY 51 Two Pointers and Sliding Window: Pairing and Averaging

Hello Everyone!

The week 11 emphasized on Two Pointers and Sliding Window, these two pointers for enhancing the problem-solving. Today’s problems were centered on combining items and minimizing the average time taken, as this is shown to eliminate unnecessary computation. I perceived it like working with data flows, and all the actions being calculated and performed with highest speed.


Course of the Day

  1. Max Number of K-Sum Pairs (Medium Difficulty)

    optimization – maximum pairs in an array that have sum equal to k.

    • Approach: Used a hash map in order to find the frequency of each element in the array. For each number checked if the complement to that number (k - number) exists in the map. If it did, it paired with the other and reduced the count of the ‘both’ numbers in the map.
    • What Stood Out: From the hash map, I was able to observe pair formation in action, as elements found their complementary counterpart.
  2. Maximum Average Subarray I (Easy)

    Simplify data structure – compute the maximum average of any subarray of size k.

    • Approach: Used the formula sum of first ‘k’ element to find out the value of the window. Sliding window technique was used where the sum can be increased by the addition of the next element and the decrease of the first element of the next window. – Was following the maximum sum and translated this into the maximum average.
    • What Stood Out: Calculating with a single visit was very promising – it was like nailing a moving target accurately.

Today Special

  1. Streamlining Operations:

    Both problems highlighted the use of the two-pointer technique and the sliding window technique to turn a problematic process into a straightforward procedure that can be executed in linear time.

  2. Dynamic Adjustments:

    : In the implementation of Max Number of K-Sum Pairs I realized the need for real time data management which was depicted from the way I managed the hash map and the in the implementation of Maximum Average Subcommittee I the use of rolling sum depicted the need for real time data management.

  3. Practical Applications:

    • These tasks were close to realistic a scenario where one needed to adjoin resources optimally, or to monitor average variable changes in streams.

Key Takeaways

  • Hash Maps for Efficient Pairing:

    Such patterns that involve tracking elements and their complements are very efficient when carried out using hash maps, as in Max Number of K-Sum Pairs.

  • Sliding Window Simplifies Subarray Calculations:

    Sliding window techniques are used to avoid repetitional calculations which are widely used in **Maximum Average Subarray I.

  • Flexibility in Problem-Solving:

    It therefore pays to focus on personalizing standard methodologies by handling the obstractions inherent in specific problem constraints.


Reflections

The Max Number of K-Sum Pairs problem was a good practice in how effective hash maps are in pairing and the Maximum Average Subarray I was a good example of how the use of sliding window makes optimization of contiguous subarray calculations possible. Each of these problems corroborated the fact that logic and optimal algorithms must be applied while dealing with arrays.


What’s Next?

The next post will cover Two Pointers and Sliding Window solving Maximum Number of Vowels in a Substring of Given Length and Max Consecutive Ones III. All of these tasks will also explore how well I can adapt to using constraints within dynamic time frames.

Thank you for tuning in to our follow along! In other words, let’s continue doing what we know how to do best

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay