Skip to content

Commit 72fae01

Browse files
RunningSumOf1dArray
1 parent 35892cb commit 72fae01

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
// Approach 1
4+
// class Solution {
5+
// public:
6+
// vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
7+
// // Firstly let's calculate the max element in the array
8+
// int max=*max_element(candies.begin(),candies.end());
9+
// vector<bool> a;
10+
// for (int i = 0; i < candies.size(); i++)
11+
// {
12+
// // If the no. of candies and extra candies is greater than equal to max then we will push_back true else false
13+
// if(candies[i]+extraCandies>=max){
14+
// a.push_back(true);
15+
// }
16+
// else{
17+
// a.push_back(false);
18+
// }
19+
// }
20+
// return a;
21+
22+
// }
23+
// };
24+
// Approach 2
25+
class Solution {
26+
public:
27+
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
28+
int maxc=*max_element(candies.begin(),candies.end());
29+
vector<bool> result;
30+
// auto will automatically define the data type
31+
// x is the element of candies array that will be changed after every iteration of for loop
32+
for(auto x:candies){
33+
if(x+extraCandies>=maxc)
34+
result.push_back(true);
35+
else
36+
result.push_back(false);
37+
}
38+
return result;
39+
}
40+
};
41+
int main(){
42+
int element,excandy;
43+
Solution v;
44+
vector<int> vc;
45+
46+
for (int i = 0; i < 5; i++)
47+
{
48+
cin>>element;
49+
vc.push_back(element);
50+
}
51+
cout<<"Size="<<vc.size()/2;
52+
cout<<"\nEnter the extra candies:";
53+
cin>>excandy;
54+
vector<bool> v1=v.kidsWithCandies(vc,excandy);
55+
cout<<"\nAfter running sum";
56+
for (int i = 0; i < v1.size(); i++)
57+
{
58+
cout<<v1[i]<<",";
59+
}
60+
61+
return 0;
62+
}

Arrays/RunningSumOf1dArray.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*Question:-
2+
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
3+
4+
Return the running sum of nums.
5+
6+
7+
8+
Example 1:
9+
10+
Input: nums = [1,2,3,4]
11+
Output: [1,3,6,10]
12+
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
13+
Example 2:
14+
15+
Input: nums = [1,1,1,1,1]
16+
Output: [1,2,3,4,5]
17+
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
18+
Constraints:
19+
20+
1 <= nums.length <= 1000
21+
-10^6 <= nums[i] <= 10^6
22+
*/
23+
#include<bits/stdc++.h>
24+
using namespace std;
25+
vector<int> runningSum(vector<int>& nums) {
26+
for(int i=1;i<nums.size();i++){
27+
nums[i]+=nums[i-1];
28+
}
29+
return nums;//returning the array
30+
}
31+
int main(){
32+
vector<int> nums;//to store elements
33+
int n,element;
34+
cin>>n;
35+
for (int i = 0; i < n; i++)
36+
{
37+
cin>>element;
38+
nums.push_back(element);//storeing the elements
39+
}
40+
vector<int> result=runningSum(nums);//calling the fuction to return the running sum of array
41+
for (int i = 0; i < result.size(); i++)
42+
{
43+
cout<<result[i]<<",";//print the elements of running sum array
44+
}
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)