Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
7oSkaaa authored Jun 20, 2023
2 parents 9420670 + 362a5a7 commit 1f50cec
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Author: Ahmed Hossam

class Solution {
public:
// This function takes a vector of integers and an integer 'k' as input and returns a vector of averages.
vector<int> getAverages(vector<int>& nums, int k) {
int n = nums.size(); // 'n' stores the size of the input vector
int len = 2 * k + 1; // 'len' calculates the length of the subarray
vector < int > avg(n); // 'avg' is a vector to store the averages of subarrays
vector < long long > prefix(n); // 'prefix' is a vector to store the prefix sum of the input vector

for(int i = 0; i < n; i++)
prefix[i] = (i ? prefix[i - 1] : 0) + nums[i]; // Calculate the prefix sum

// Create a lambda function 'sum' to calculate the sum of elements in a given range
auto sum = [&](int l, int r) {
return prefix[r] - (l ? prefix[l - 1] : 0);
};

for(int i = 0; i < n; i++) {
int L = i - k; // Calculate the left index of the subarray
int R = i + k; // Calculate the right index of the subarray
bool isIn = (L >= 0 && R < n); // Check if the subarray is within the bounds of the input vector
avg[i] = (isIn ? sum(L, R) / len : -1); // Calculate the average of the subarray or assign -1 if it is out of bounds
}
return avg; // Return the vector of averages
}
};
46 changes: 46 additions & 0 deletions 06- June/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
1. **[Make Array Strictly Increasing](#17--make-array-strictly-increasing)**
1. **[Number of Increasing Paths in a Grid](#18--number-of-increasing-paths-in-a-grid)**
1. **[Find the Highest Altitude](#19--find-the-highest-altitude)**
1. **[K Radius Subarray Averages](#20--k-radius-subarray-averages)**

<hr>
<br><br>
Expand Down Expand Up @@ -1139,4 +1140,49 @@ public:
}
};
```

<hr>
<br><br>

## 20) [K Radius Subarray Averages](https://leetcode.com/problems/k-radius-subarray-averages/)

### Difficulty

![](https://img.shields.io/badge/Medium-orange?style=for-the-badge)

### Related Topic

`Array` `Sliding Window`

### Code


```cpp
class Solution {
public:
// This function takes a vector of integers and an integer 'k' as input and returns a vector of averages.
vector<int> getAverages(vector<int>& nums, int k) {
int n = nums.size(); // 'n' stores the size of the input vector
int len = 2 * k + 1; // 'len' calculates the length of the subarray
vector < int > avg(n); // 'avg' is a vector to store the averages of subarrays
vector < long long > prefix(n); // 'prefix' is a vector to store the prefix sum of the input vector

for(int i = 0; i < n; i++)
prefix[i] = (i ? prefix[i - 1] : 0) + nums[i]; // Calculate the prefix sum

// Create a lambda function 'sum' to calculate the sum of elements in a given range
auto sum = [&](int l, int r) {
return prefix[r] - (l ? prefix[l - 1] : 0);
};

for(int i = 0; i < n; i++) {
int L = i - k; // Calculate the left index of the subarray
int R = i + k; // Calculate the right index of the subarray
bool isIn = (L >= 0 && R < n); // Check if the subarray is within the bounds of the input vector
avg[i] = (isIn ? sum(L, R) / len : -1); // Calculate the average of the subarray or assign -1 if it is out of bounds
}
return avg; // Return the vector of averages
}
};
```

0 comments on commit 1f50cec

Please sign in to comment.