Skip to content

Commit e9b92c1

Browse files
Add files via upload
1 parent ebe85a5 commit e9b92c1

6 files changed

+551
-0
lines changed

BinarySubarrayWithSum.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Given a binary array arr of size N and an integer target, return the number of non-empty subarrays with a sum equal to target.
3+
Note : A subarray is the contiguous part of the array.
4+
5+
Example 1:
6+
Input:
7+
N = 5
8+
target = 2
9+
arr[ ] = {1, 0, 1, 0, 1}
10+
Output: 4
11+
Explanation: The 4 subarrays are:
12+
{1, 0, 1, _, _}
13+
{1, 0, 1, 0, _}
14+
{_, 0, 1, 0, 1}
15+
{_, _, 1, 0, 1}
16+
17+
Example 2:
18+
Input:
19+
N = 5
20+
target = 5
21+
arr[ ] = {1, 1, 0, 1, 1}
22+
Output: 0
23+
Explanation: There is no subarray with sum equal to target.
24+
25+
Your Task:
26+
You don't need to read input or print anything. Your task is to complete the function numberOfSubarrays() which takes the array arr, interger N and an integer target as input and returns the number of subarrays with a sum equal to target.
27+
28+
Expected Time Complexity: O(N).
29+
Expected Auxiliary Space: O(1).
30+
*/
31+
32+
/// Hashmap Solution
33+
34+
class Solution {
35+
public:
36+
int numSubarraysWithSum(vector<int>& nums, int goal) {
37+
long long n=nums.size(), counter=0, prefixSum=0;
38+
long long presum[n+1];
39+
for(int i=0;i<n+1;i++){
40+
presum[i]=0;
41+
}
42+
for(int i=0;i<n;i++){
43+
prefixSum+=nums[i];
44+
int leftover=prefixSum-goal;
45+
if(leftover>=0) counter+=presum[leftover];
46+
if(prefixSum==goal) counter++;
47+
presum[prefixSum]+=1;
48+
}
49+
return counter;
50+
}
51+
};
52+
53+
/// Sliding Window Approach (IDEA: get atmostsum (sum<=goal) and then remove sum<goal)
54+
55+
class Solution {
56+
public:
57+
int atmostSubarray(vector<int> &nums, int target){
58+
if(target<0) return 0;
59+
int i=0, j=0, result=0, sum=0, n=nums.size();
60+
while(j<n){
61+
sum+=nums[j];
62+
while(sum>target){
63+
sum-=nums[i];
64+
i++;
65+
}
66+
result+=(j-i+1);
67+
j++;
68+
}
69+
return result;
70+
}
71+
int numSubarraysWithSum(vector<int>& nums, int goal) {
72+
return atmostSubarray(nums,goal)-atmostSubarray(nums,goal-1);
73+
}
74+
};

BinarySubarraysWithSum.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal.
3+
A subarray is a contiguous part of the array.
4+
5+
Example 1:
6+
Input: nums = [1,0,1,0,1], goal = 2
7+
Output: 4
8+
Explanation: The 4 subarrays are bolded and underlined below:
9+
[1,0,1,0,1]
10+
[1,0,1,0,1]
11+
[1,0,1,0,1]
12+
[1,0,1,0,1]
13+
14+
Example 2:
15+
Input: nums = [0,0,0,0,0], goal = 0
16+
Output: 15
17+
Output: 15
18+
*/
19+
20+
21+
class Solution {
22+
public:
23+
int numSubarraysWithSum(vector<int>& nums, int goal) {
24+
long long n=nums.size(), counter=0, prefixSum=0;
25+
long long presum[n+1];
26+
for(int i=0;i<n+1;i++){
27+
presum[i]=0;
28+
}
29+
for(int i=0;i<n;i++){
30+
prefixSum+=nums[i];
31+
int leftover=prefixSum-goal;
32+
if(leftover>=0) counter+=presum[leftover];
33+
if(prefixSum==goal) counter++;
34+
presum[prefixSum]+=1;
35+
}
36+
return counter;
37+
}
38+
};
39+
40+
41+
/// Sliding Window
42+
43+
class Solution {
44+
public:
45+
int atmostSubarray(vector<int> &nums, int target){
46+
if(target<0) return 0;
47+
int i=0, j=0, result=0, sum=0, n=nums.size();
48+
while(j<n){
49+
sum+=nums[j];
50+
while(sum>goal){
51+
sum-=nums[i];
52+
i++;
53+
}
54+
result+=(j-i+1);
55+
j++;
56+
}
57+
return result;
58+
}
59+
int numSubarraysWithSum(vector<int>& nums, int goal) {
60+
return atmostSubarray(nums,goal)-atmostSubarray(nums,goals-1);
61+
}
62+
};
63+
64+

CountSubarraysWithMedianK.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
You are given an array nums of size n consisting of distinct integers from 1 to n and a positive integer k.
3+
Return the number of non-empty subarrays in nums that have a median equal to k.
4+
Note:
5+
The median of an array is the middle element after sorting the array in ascending order. If the array is of even length, the median is the left middle element.
6+
For example, the median of [2,3,1,4] is 2, and the median of [8,4,3,5,1] is 4.
7+
A subarray is a contiguous part of an array.
8+
9+
Example 1:
10+
Input: nums = [3,2,1,4,5], k = 4
11+
Output: 3
12+
Explanation: The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].
13+
14+
Example 2:
15+
Input: nums = [2,3,1], k = 3
16+
Output: 1
17+
Explanation: [3] is the only subarray that has a median equal to 3.
18+
*/
19+
20+
/// Prefix Sum + Hashmap
21+
22+
class Solution {
23+
public:
24+
int countSubarrays(vector<int>& nums, int k) {
25+
int counter=0, found=0, sum=0;
26+
unordered_map<int,int> mp;
27+
mp[0]=1;
28+
for(auto num : nums){
29+
if(num==k) found=1;
30+
else{
31+
if(num<k){
32+
sum+=-1;
33+
}else{
34+
sum+=1;
35+
}
36+
}
37+
if(found) counter+=mp[sum-1]+mp[sum];
38+
else mp[sum]++;
39+
}
40+
return counter;
41+
}
42+
};
43+

CountingBits.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
3+
4+
Example 1:
5+
Input: n = 2
6+
Output: [0,1,1]
7+
Explanation:
8+
0 --> 0
9+
1 --> 1
10+
2 --> 10
11+
12+
Example 2:
13+
Input: n = 5
14+
Output: [0,1,1,2,1,2]
15+
Explanation:
16+
0 --> 0
17+
1 --> 1
18+
2 --> 10
19+
3 --> 11
20+
4 --> 100
21+
5 --> 101
22+
23+
Constraints:
24+
0 <= n <= 105
25+
*/
26+
27+
class Solution {
28+
public:
29+
vector<int> countBits(int n) {
30+
vector<int> result;
31+
for(int i=0;i<=n;i++){
32+
int counter=__builtin_popcount(i);
33+
result.push_back(counter);
34+
}
35+
return result;
36+
}
37+
};
38+
39+
/// Optimal : DP + O(N)
40+
41+
class Solution {
42+
public:
43+
vector<int> countBits(int num) {
44+
vector<int> result(num+1, 0);
45+
for (int i = 1; i <= num; ++i)
46+
result[i] = result[i&(i-1)] + 1;
47+
return result;
48+
}
49+
};

ExcelSheetColumnTitle.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
3+
For example:
4+
5+
A -> 1
6+
B -> 2
7+
C -> 3
8+
...
9+
Z -> 26
10+
AA -> 27
11+
AB -> 28
12+
...
13+
14+
Example 1:
15+
Input: columnNumber = 1
16+
Output: "A"
17+
18+
Example 2:
19+
Input: columnNumber = 28
20+
Output: "AB"
21+
22+
Example 3:
23+
Input: columnNumber = 701
24+
Output: "ZY"
25+
*/
26+
27+
class Solution {
28+
public:
29+
string convertToTitle(int columnNumber) {
30+
string result;
31+
while(columnNumber>0){
32+
columnNumber--;
33+
int curr=columnNumber%26;
34+
columnNumber/=26;
35+
result.push_back(curr+'A');
36+
}
37+
reverse(result.begin(),result.end());
38+
return result;
39+
}
40+
};

0 commit comments

Comments
 (0)