Skip to content

Commit 1bb3760

Browse files
committed
Updated
1 parent cdf7821 commit 1bb3760

File tree

89 files changed

+2802
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+2802
-0
lines changed

07-Algorithm STL/01-Iterator/Pair.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include<iostream>
2+
#include<utility>
3+
using namespace std;
4+
int main()
5+
{
6+
pair<int,int>p(10,20), p1(30,40);
7+
cout<<(p==p1)<<endl;
8+
cout<<(p!=p1)<<endl;
9+
cout<<(p>p1)<<endl;
10+
cout<<(p<p1)<<endl;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
5+
int main(){
6+
vector<int>v= {10,20,30,40,50,60};
7+
8+
vector<int>::iterator i= v.begin(); // Use this signature if you want to traverse only the single element.
9+
cout<<(*i)<<endl; // Iterator are treated like Pointer.
10+
i= next(i,2);
11+
cout<<(*i)<<endl;
12+
i= prev(i);
13+
cout<<(*i)<<endl;
14+
15+
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include<iostream>
2+
using namespace std;
3+
template <typename T>
4+
struct pair{
5+
T x,y;
6+
pair(T i, T j){
7+
x=i;
8+
y=j;
9+
}
10+
T getFirst(){
11+
return x;
12+
}
13+
T getSecond(){
14+
return y;
15+
}
16+
};
17+
int main()
18+
{
19+
pair<int>p1(10,20);
20+
cout<<p1.getFirst();
21+
cout<p1.getSecond();
22+
}

07-Algorithm STL/binary_search.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
int main()
5+
{
6+
int n;cin>>n;
7+
int a[1000];
8+
for(int i=0;i<n;i++){
9+
cin>>a[i];
10+
}
11+
int key;cin>>key;
12+
13+
bool present= binary_search(a, a+n, key);
14+
if(present){
15+
cout<<"Present"<<endl;
16+
}
17+
else{
18+
cout<<"Not Present"<<endl;
19+
}
20+
}

07-Algorithm STL/find.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
int main()
5+
{
6+
int arr[]= {10,20,30,14,155};
7+
int n= sizeof(arr)/sizeof(int);
8+
int key;
9+
cin>>key;
10+
auto it= find(arr,arr+n,key);
11+
int index= it-arr;
12+
if(key==n){
13+
cout<<"Key not found"<<endl;
14+
}
15+
else{
16+
cout<<index<<endl;
17+
}
18+
}

07-Algorithm STL/indian_money.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
bool compare(int a, int b){
5+
return a<=b;
6+
}
7+
int main()
8+
{
9+
int coins[]={1,2,5,10,20,50,100,200,500,2000};
10+
int money;cin>>money;
11+
int n= sizeof(coins)/sizeof(int);
12+
while(money>0){
13+
int lb= lower_bound(coins, coins+n, money,compare)-coins-1;
14+
int m= coins[lb];
15+
cout<<m<<",";
16+
money= money-m;
17+
}
18+
}

07-Algorithm STL/lower_bound.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
int main(){
5+
int n;cin>>n;
6+
int a[1000];
7+
for(int i=0;i<n;i++){
8+
cin>>a[i];
9+
}
10+
int key;cin>>key;
11+
12+
auto it= lower_bound(a,a+n, key);
13+
int index= it-a;
14+
cout<<"Lower Bound of "<<key<<"is: "<<index<<endl;
15+
}

07-Algorithm STL/pair.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
int main()
5+
{
6+
pair<int,string> p= make_pair(100,"Audi");
7+
cout<<p.first<<endl;
8+
cout<<p.second<<endl;
9+
10+
//second way;
11+
12+
pair<int,string>p2;
13+
p2.first= 200;
14+
p2.second="Buggatti";
15+
cout<<p2.first<<" "<<p2.second<<endl;
16+
17+
//Taking input;
18+
19+
int n;cin>>n;
20+
string s;
21+
getline(cin,s);
22+
pair<int, string> p3= make_pair(n,s);
23+
cout<<p3.first<< "," <<p3.second<<endl;
24+
}

07-Algorithm STL/rotate.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
#include<vector>
4+
using namespace std;
5+
int main(){
6+
int a[]= {10,20,30,40,50,60,70};
7+
int n= sizeof(a)/sizeof(int);
8+
rotate(a,a+3,a+n);
9+
for(int i=0;i<n;i++){
10+
cout<<a[i]<<" ";
11+
}
12+
cout<<endl;
13+
vector <int> v{10,20,30,40,50,60};
14+
rotate(v.begin(), v.begin()+3, v.end());
15+
for(int i=0;i<v.size();i++){
16+
cout<<v[i]<<" ";
17+
}
18+
}

07-Algorithm STL/upper_bound.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
int main()
5+
{
6+
int n;cin>>n;
7+
int a[1000];
8+
for(int i=0;i<n;i++){
9+
cin>>a[i];
10+
11+
}
12+
int key;cin>>key;
13+
auto it= lower_bound(a,a+n,key);
14+
int lb= it-a;
15+
auto it2= upper_bound(a,a+n,key);
16+
int ub= it-a;
17+
18+
cout<<"Occurrence of "<<key<<"is :"<<(ub-lb)<<endl;
19+
20+
}

08-Vector/insert.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
int main()
5+
{
6+
vector<int>v{1,2,3,4,5,14};
7+
v.insert(v.begin()+3, 3, 100);
8+
for(int x:v){
9+
cout<<x<<",";
10+
}
11+
}

08-Vector/pop_back.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
int main()
5+
{
6+
vector<int>v{1,2,3,4,5,6,19};
7+
v.pop_back();
8+
for(int x:v){
9+
cout<<x<<",";
10+
}
11+
}

08-Vector/push_back.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
int main()
5+
{
6+
vector<int>v{1,2,3,4,5,6};
7+
v.push_back(10);
8+
for(int x:v){
9+
cout<<x<<",";
10+
}
11+
}

08-Vector/vector_pair.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<algorithm>
4+
bool compare()
5+
using namespace std;
6+
int main()
7+
{
8+
int n;cin>>n;
9+
vector<pair<int,int>>v;
10+
for(int i=0;i<n;i++){
11+
int x,y;
12+
cin>>x>>y;
13+
v.push_back(make_pair(x,y));
14+
}
15+
sort(v.begin(),v.end());
16+
for(auto p:v){
17+
cout<<p.first<<","<<p.second<<endl;
18+
}
19+
}

09- Number Theory/PrimeSeive.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define ll long long
4+
void primeSieve(int *p){
5+
for(int i=3;i<=1000000;i+=2){
6+
p[i]=1;
7+
}
8+
//Sieve
9+
10+
for(ll i=3;i<=1000000;i+=2){
11+
if(p[i]==1){
12+
for(ll j=i*i;j<=1000000;j++){
13+
p[j]=0;
14+
}
15+
}
16+
}
17+
//special Case
18+
p[2]=1;
19+
p[1]=p[0]=0;
20+
}
21+
int main()
22+
{
23+
int n;cin>>n;
24+
int p[100005]= {0};
25+
primeSieve(p);
26+
for(int i=0;i<=n;i++){
27+
if(p[i]==1){
28+
cout<<i<<" ";
29+
}
30+
}
31+
return 0;
32+
}

09- Number Theory/SieveProblem.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define ll long long
4+
void primesieve(int *p){
5+
for(int i=3;i<=1000000;i+=2){
6+
p[i]=1;
7+
}
8+
//Sieve
9+
10+
for(ll i=3;i<=1000000;i+=2){
11+
if(p[i]==1){
12+
for(ll j=i*i;j<=1000000;j++){
13+
p[j]=0;
14+
}
15+
}
16+
}
17+
//special Case
18+
p[2]=1;
19+
p[1]=p[0]=0;
20+
}
21+
22+
int main()
23+
{
24+
int p[1000005]={0};
25+
int csum[100000]={0};
26+
primesieve(p);
27+
int q;cin>>q;
28+
for(int i=1;i<=1000000;i++){
29+
csum[i]= csum[i-1]+p[i];
30+
}
31+
while(q--){
32+
int a,b;cin>>a>>b;
33+
cout<<csum[b]-csum[a-1]<<endl;
34+
}
35+
}

09- Number Theory/gcd.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include<iostream>
2+
using namespace std;
3+
int gcdofnumber(int a, int b){
4+
return b==0?a:gcdofnumber(b,a%b);
5+
}
6+
int main()
7+
{
8+
int a,b;cin>>a>>b;
9+
cout<<gcdofnumber(a,b);
10+
}

09- Number Theory/lcm.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
using namespace std;
3+
int gcdofnumbers(int a, int b){
4+
return b==0?a:gcdofnumbers(b,a%b);
5+
}
6+
int lcmofnumbers(int a,int b){
7+
int lcm=0;
8+
int product= 0;
9+
if(a>0 and b>0){
10+
product+= a*b;
11+
lcm= product/gcdofnumbers(a,b);
12+
}
13+
return lcm;
14+
15+
}
16+
int main()
17+
{
18+
int a,b;cin>>a>>b;
19+
cout<<lcmofnumbers(a,b);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<iostream>
2+
using namespace std;
3+
int linearSearch(int *a, int i, int key, int n){
4+
if(i==n){
5+
return -1;
6+
}
7+
if(i==key){
8+
return i;
9+
}
10+
return linearSearch(a, i+1, key,n);
11+
}
12+
int main()
13+
{
14+
int a[]= {1,2,3,5,4,8};
15+
int n= sizeof(a)/sizeof(int);
16+
int key;cin>>key;
17+
cout<<linearSearch(a,0,key,n);
18+
}

0 commit comments

Comments
 (0)