Sliding Window : 2

Sliding Window : 2

Day 2/75:

Question: Subarray Product Less Than K - (Medium)

Problem Statement : Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.

Input: nums = [10,5,2,6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are:
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.        

Intuition: Our goal is find Count of Subarray whose Product is Less Than target K. By utilizing the sliding window technique, we can dynamically adjust the size of our window to explore different subarrays and find product of each subarray window.

Approach:

Initialization:

  • Initialize two pointers, start and end, to mark the boundaries of our window.
  • Set the product variable to 1, representing the product of elements within the current window.
  • Initialize a count variable to track the number of valid subarrays.

Sliding Window Technique:

  • Iterate through the array, advancing the right pointer.
  • Update the product by multiplying it with the current element.
  • While the product exceeds the target, adjust the window by moving the left pointer and dividing the product by the element at the left pointer.
  • Increment the count by the size of the subarray, which is (end- start + 1)..

Result: Return the total count of valid subarrays.

Time Complexity: O(N), where N is the length of the input array. We traverse the array only once with our sliding window, making it a linear time solution.

Space Complexity: O(1) since we use a constant amount of extra space for variables, irrespective of the input size. Hence, our solution is memory-efficient.

??Burning through those coding challenges, Suyash Awathe! Your passion for code is contagious. Stay inspired, keep innovating, and never stop building!??

回复

要查看或添加评论,请登录

Suyash Awathe的更多文章

  • Tree: Add One Row to Tree

    Tree: Add One Row to Tree

    Day 17: Problem Statement: Given the root of a binary tree and two integers val and depth, add a row of nodes with…

  • Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Day 16/75 : Problem : Trapping Rain Water -(Hard) Given n non-negative integers representing an elevation map where the…

  • Stack: April POTD

    Stack: April POTD

    Question: Remove K Digits -(Medium) Given string num representing a non-negative integer num, and an integer k, return…

  • Queue-Array: April POTD

    Queue-Array: April POTD

    Day 14/75: Question: Reveal Cards In Increasing Order -(Medium) You are given an integer array . There is a deck of…

    1 条评论
  • Array: 9 April POTD

    Array: 9 April POTD

    Day 13/75: Question: Time Needed to Buy Tickets -(Easy) There are people in a line queuing to buy tickets, where the…

  • Queue : April POTD

    Queue : April POTD

    Day 12/75: Question: Number of Students Unable to Eat Lunch -(Easy) The school cafeteria offers circular and square…

    1 条评论
  • String : Parentheses : April-POTD

    String : Parentheses : April-POTD

    Day 11/75: Question: Minimum Remove to Make Valid Parentheses -(Medium) Given a string s of , and lowercase English…

    3 条评论
  • String : 5April POTD

    String : 5April POTD

    Day 10/75: Question: Make The String Great -(Easy) Given a string of lower and upper case English letters. A good…

  • String Parentheses : 4April POTD

    String Parentheses : 4April POTD

    Day 9/75: Question: Maximum Nesting Depth of the Parentheses -(Easy) A string is a valid parentheses string (denoted…

  • BackTracking : WordSearch :4April POTD

    BackTracking : WordSearch :4April POTD

    Day 9/75: Question: Word Search -(Medium) Given an m x n grid of characters board and a string word, return true if…

    1 条评论

社区洞察

其他会员也浏览了