Skip to content

Commit 78ad7b9

Browse files
committed
added more codes
1 parent 9e4a050 commit 78ad7b9

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

apartments.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
#define ll long long
4+
#define mod 1000000007
5+
6+
void solve()
7+
{
8+
ll n,m,k;
9+
cin >> n >> m >> k;
10+
vector<ll> a(n), b(m);
11+
for(ll i=0; i<n; i++) cin >> a[i];
12+
for(ll i=0; i<m; i++) cin >> b[i];
13+
sort(a.begin(), a.end());
14+
sort(b.begin(), b.end());
15+
16+
ll cnt = 0, i = 0, j = 0;
17+
18+
while(i<n and j<m){
19+
if(abs(a[i]-b[j]) <= k){
20+
cnt++;
21+
i++; j++;
22+
}
23+
else{
24+
if(a[i]-b[j] > k) j++;
25+
else i++;
26+
}
27+
}
28+
cout << cnt << "\n";
29+
}
30+
31+
int main()
32+
{
33+
ios_base::sync_with_stdio(false);
34+
cin.tie(NULL);
35+
cout.tie(NULL);
36+
solve();
37+
}

distinctNumbers.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
#define ll long long
4+
#define mod 1000000007
5+
6+
void solve()
7+
{
8+
ll n; cin >> n;
9+
set<ll> s;
10+
for (ll i = 0; i < n; ++i)
11+
{
12+
ll x; cin >> x;
13+
s.insert(x);
14+
}
15+
cout << s.size() << "\n";
16+
}
17+
18+
int main()
19+
{
20+
ios_base::sync_with_stdio(false);
21+
cin.tie(NULL);
22+
cout.tie(NULL);
23+
solve();
24+
}

editDistance.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
#define ll long long
4+
#define mod 1000000007
5+
6+
ll editDistance(string s1, string s2, ll m, ll n)
7+
{
8+
ll dp[m+1][n+1];
9+
10+
for(ll i=0;i<=m;i++)
11+
dp[i][0]=i;
12+
13+
for(ll j=0;j<=n;j++)
14+
dp[0][j]=j;
15+
16+
for(ll i=1;i<=m;i++)
17+
{
18+
for(ll j=1;j<=n;j++)
19+
{
20+
if(s1[i-1] == s2[j-1])
21+
dp[i][j] = dp[i-1][j-1];
22+
else
23+
dp[i][j] = 1 + min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1]));
24+
}
25+
}
26+
return dp[m][n];
27+
}
28+
29+
void solve()
30+
{
31+
string s1; cin>>s1;
32+
string s2; cin>>s2;
33+
cout<<editDistance(s1,s2,s1.length(),s2.length())<<endl;
34+
}
35+
36+
int main()
37+
{
38+
ios_base::sync_with_stdio(false);
39+
cin.tie(NULL);
40+
cout.tie(NULL);
41+
solve();
42+
}

exponentiation.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
#define ll long long
4+
#define mod 1000000007
5+
6+
ll power(ll a, ll b)
7+
{
8+
if(b == 0) return 1;
9+
10+
a = a % mod;
11+
ll temp = power(a, b/2);
12+
if(b&1)
13+
return (((temp * temp) % mod) * a) % mod;
14+
else
15+
return (temp * temp) % mod;
16+
}
17+
18+
void solve()
19+
{
20+
ll n; cin>>n;
21+
while(n--)
22+
{
23+
ll a,b;
24+
cin >> a >> b;
25+
cout << power(a,b) << "\n";
26+
}
27+
}
28+
29+
int main()
30+
{
31+
ios_base::sync_with_stdio(false);
32+
cin.tie(NULL);
33+
cout.tie(NULL);
34+
solve();
35+
}

maximumSubarraySum.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
#define ll long long
4+
#define mod 1000000007
5+
6+
void solve()
7+
{
8+
ll n; cin>>n;
9+
vector<ll> v(n);
10+
for(ll i=0; i<n; ++i) cin>>v[i];
11+
12+
ll curSum = v[0], ans = v[0];
13+
for(ll i=1; i<n; ++i){
14+
curSum = max(curSum+v[i], v[i]);
15+
ans = max(ans, curSum);
16+
}
17+
cout<<ans<<endl;
18+
}
19+
20+
int main()
21+
{
22+
ios_base::sync_with_stdio(false);
23+
cin.tie(NULL);
24+
cout.tie(NULL);
25+
solve();
26+
}

staticRangeSum.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
#define ll long long
4+
#define mod 1000000007
5+
6+
void solve()
7+
{
8+
ll n,q;
9+
cin >> n >> q;
10+
vector<ll> x(n+1);
11+
for(ll i=1; i<=n; i++) cin >> x[i];
12+
13+
vector<ll> prefix(n+1);
14+
prefix[1] = x[1];
15+
for(ll i=2; i<=n; i++){
16+
prefix[i] = prefix[i-1] + x[i];
17+
}
18+
19+
while(q--){
20+
ll a,b;
21+
cin >> a >> b;
22+
cout << (prefix[b]-prefix[a-1]) <<"\n";
23+
}
24+
}
25+
26+
int main()
27+
{
28+
ios_base::sync_with_stdio(false);
29+
cin.tie(NULL);
30+
cout.tie(NULL);
31+
solve();
32+
}

0 commit comments

Comments
 (0)