Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Competitive (LeetCode & SPOJ) #94

Merged
merged 4 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions C++/competitive/LeetCode/Best-Time-to-Buy-and-Sell-Stock-II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Avinash Trivedi
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int sum = 0;
//If the number of element in the array are zero or one just return zero.
if (prices.size() == 1 or prices.size() == 0)
{
return 0;
}

//Traverse the array and compare the consecutive two elements .
for (int i = 0; i < prices.size() - 1; i++)
{
// If first consecuitve element is less than second subtract both and add in the sum varibale.
if (prices[i] < prices[i + 1])
{
sum += prices[i + 1] - prices[i];

}
}

//Finally return the sum.
return sum;

}
};
40 changes: 40 additions & 0 deletions C++/competitive/LeetCode/maximum-subarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Avinash Trivedi
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
int cs = 0;
int ms = INT_MIN;
// if size of array is 1 return that elemnt .
if (nums.size() == 1)
{
return nums[0];
}

//Linearly traverse the array and add to the current sum variable and take maximum .
for (int i = 0; i < nums.size(); i++)
{
cs = cs + nums[i];

ms = max(cs, ms);
if (cs < 0)
{
cs = 0;
}
}

return ms;
}
};

65 changes: 65 additions & 0 deletions C++/competitive/SPOJ/CSUMQ-Cumulative-Sum-Query.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 Avinash Trivedi
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#include <iostream>


#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define intt long long
#define fori(n) for (ll i = 0; i < n; i++)
#define forj(n) for (ll j = 0; j < n; j++)
#define vl vector<ll>
#define mod 1000000007
#define mp make_pair
#define vp vector<pair<int, int>>

int main()
{


intt n;
cin >> n;
intt a[n];
//Taking the input array

for (intt i = 0; i < n; i++)
{
cin >> a[i];
}
//The next k lines each contain two numbers x and y which specify a query you must answer.

intt k;
cin >> k;
for (intt i = 0; i < k; i++)
{ //Range of query x,y.

intt x, y;
cin >> x >> y;
//Declared the sum variable

intt sum = 0;
//Traverse the array in the range x,y and keep adding the sum of elments which are in between the query range.

for (intt i = x; i <= y; i++)
{
sum += a[i];
}
//Finally return the sum of those elements.
cout << sum << endl;
}

return 0;
}