Skip to content

Adding files to Dynamic programming and Mathematical concept under C++ #167

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
49 changes: 49 additions & 0 deletions C++/Dynamic Programming/interval_queries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
// this program will give an intuition behind the solution for CP questions based on interval quries. Refer to the question
//http://codeforces.com/gym/294377/problem/E

//this following two lines are used for fast input-output
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin>>t;
while(t--) //running the program for 't' test cases
{
int n;
cin>>n; //number of students in th college

//declare an array of max size (based on the input constraint) to indicate the timeline of all the students
int N=2*100000;
int a[N]={0};

for(int i=0;i<n;i++)
{
int x,y;
cin>>x>>y;
// while taking the entering and leaving time of each student, increase the (x-1)th index by one and decrease the (y-1)th index by 1.
a[x-1]+=1;
a[y-1]-=1;// Here the student is outside the college during the yth sec, hence we subtract 1 from (y-1)th index. Else we would subtract the same from yth index
}
for(int i=1;i<2*100000;i++)
{
// Summing the value in the current index by its previous index's value will give the total number of students at a perticular instance of time on the timeline a[N]
a[i]+=a[i-1];
}
int q;
cin>>q;
while(q--)// running a loop to ans all the queries
{
int pp;
cin>>pp;// The required time to output number of students present at that particular instance

// We can find out the number of students present at that perticular time by accessing the value stored in the array 'a' at (pp-1) index
cout<<a[pp-1]<<endl;
}
}
return 0;
}
32 changes: 32 additions & 0 deletions C++/Math/particular_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//This is a detailed solution to provide intuition on solving CP problems based on finding the reduced max size of a given array whose elements are divisible by given number. (Elements of the array can be replaced by sum of elements present in the array).
//Have a look into at the question: https://codeforces.com/problemset/problem/1176/B

#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t; // Input to run the program for 't' test cases
while(t--){
int n,f=0,r1=0,r2=0;
cin>>n; // Input to know the total number of elements in the array
vector<int>a; // array initialization (a vector can also be used as an array)
for(int i=0;i<n;i++){
int x;
cin>>x;
if(x%3==0)
f++; // counting the number of elements which are divisible by 3
if(x%3==1)
r1++; // counting the number of elements which leave reminder 1 when divided by 3
if(x%3==2)
r2++; // counting the number of elements which leave reminder 2 when divided by 3
}
int ans=f;
int m=min(r1,r2); // We can add-up the elements whose reminder is 1 with those whose reminder is 2 such that the newly formed number is divisible by 3
ans+=m;
m=(r1+r2-2*m)/3; // Consider r1 as a set and r2 as a set. The remaining elements of a particular set, the one which has max elements and are not pared with the other remainder set but can still yield a sum which is divisible by 3.
ans+=m;
cout<<ans<<endl;
}
return 0;
}
4 changes: 4 additions & 0 deletions C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

- [Longest Common Subsequence](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/lcs.cpp)

- [Interval_queries](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/interval_queries)

<a name="graph"></a>

## Graph Algorithms
Expand Down Expand Up @@ -66,6 +68,8 @@

- [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp)

-[particular_sum](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/Particular_sum)

<a name="searching"></a>

## Searching
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ You can also create an issue or [contact us](https://github.com/aniketsharma0041

- [Coin change](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/dp_coin_change.cpp)
- [Longest Common Subsequence](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/lcs.cpp)
- [Interval_queries](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/interval_queries)

<a name="graph"></a>

Expand Down Expand Up @@ -83,6 +84,7 @@ You can also create an issue or [contact us](https://github.com/aniketsharma0041
- [Matrix exponentiation](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/matrix_exponentiation.cpp)
- [Pascal triangle](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pascal_triangle.cpp)
- [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp)
-[particular_sum](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/Particular_sum)

<a name="searching"></a>

Expand Down