0% found this document useful (0 votes)
32 views3 pages

Sliding Window

Uploaded by

Utkarsh Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Sliding Window

Uploaded by

Utkarsh Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignments

Sliding Window
Q1. Given an array of integers arr and two integers k and threshold , return the number of sub-arrays of size k

and average greater than or equal to threshold . [Leetcode 1343]

Example 1:

Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4

Output: 3

Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All

other sub-arrays of size 3 have averages less than 4 (the threshold).

Example 2:

Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5

Output: 6

Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that

averages are not integers.

Q2. The score of an array is defined as the product of its sum and its length.

For example, the score of [1, 2, 3, 4, 5] is (1 + 2 + 3 + 4 + 5) * 5 = 75 .

Given a positive integer array nums and an integer k , return the number of non-empty subarrays of nums

whose score is strictly less than k . [Leetcode 2302]

A subarray is a contiguous sequence of elements within an array.

Example 1:

Input: nums = [2,1,4,3,5], k = 10

Output: 6

Explanation:

The 6 subarrays having scores less than 10 are:

[2] with score 2 * 1 = 2.

[1] with score 1 * 1 = 1.

[4] with score 4 * 1 = 4.

[3] with score 3 * 1 = 3.

[5] with score 5 * 1 = 5.

[2,1] with score (2 + 1) * 2 = 6.

C
Java &
++ + DSA
DSA
Note that subarrays such as [1,4] and [4,3,5] are not considered because their scores are 10

and 36 respectively, while we need scores strictly less than 10.

Example 2:

Input: nums = [1,1,1], k = 5

Output: 5

Explanation:

Every subarray except [1,1,1] has a score less than 5.

[1,1,1] has a score (1 + 1 + 1) * 3 = 9, which is greater than 5.

Thus, there are 5 subarrays having scores less than 5.

Q3. Given an array of integers nums and an integer k . A continuous subarray is called nice if there are k odd

numbers on it.

Return the number of nice sub-arrays. [Leetcode 1248]

Example 1:

Input: nums = [1,1,2,1,1], k = 3

Output: 2

Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].

Example 2:

Input: nums = [2,4,6], k = 1

Output: 0

Explanation: There is no odd numbers in the array.

Example 3:

Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2

Output: 16

Note:- Please try to invest time doing the assignments which are necessary to build a strong foundation. Do not

directly Copy Paste using Google or ChatGPT. Please use your brain.

Java
C++ &+ DSA
DSA

You might also like