Skip to content

Commit 92e2f10

Browse files
authored
added hacker rank folder
please accept as hacktoberfest chaalange
1 parent 93dbefc commit 92e2f10

31 files changed

+1742
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int lcsHelper(char *s1, char *s2, int m, int n, int **dp)
4+
{
5+
if (m == 0 || n == 0)
6+
{
7+
return 0;
8+
}
9+
if (dp[m][n] > -1)
10+
{
11+
return dp[m][n];
12+
}
13+
int ans;
14+
if (s1[0] == s2[0])
15+
{
16+
ans = 1 + lcsHelper(s1 + 1, s2 + 1, m - 1, n - 1, dp);
17+
}
18+
else
19+
{
20+
int option1 = lcsHelper(s1 + 1, s2, m - 1, n, dp);
21+
int option2 = lcsHelper(s1, s2 + 1, m, n - 1, dp);
22+
ans = max(option1, option2);
23+
}
24+
dp[m][n] = ans;
25+
return ans;
26+
}
27+
int lcs(char *s1, char *s2)
28+
{
29+
int m = strlen(s1);
30+
int n = strlen(s2);
31+
int **dp = new int *[m + 1];
32+
for (int i = 0; i <= m; i++)
33+
{
34+
dp[i] = new int[n + 1];
35+
for (int j = 0; j <= n; j++)
36+
{
37+
dp[i][j] = -1;
38+
}
39+
}
40+
int ans = lcsHelper(s1, s2, m, n, dp);
41+
for (int i = 0; i <= m; i++)
42+
{
43+
delete[] dp[i];
44+
}
45+
delete[] dp;
46+
return ans;
47+
}
48+
int main()
49+
{
50+
51+
char s1[5010], s2[5010];
52+
cin >> s1 >> s2;
53+
cout << lcs(s1, s2) << endl;
54+
return 0;
55+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <bits/stdc++.h>
2+
#define MOD 1000000007
3+
using namespace std;
4+
5+
vector<string> split_string(string);
6+
7+
// Complete the countArray function below.
8+
long countArray(int n, int k, int x) {
9+
// Return the number of ways to fill in the array.
10+
long oneCount = 1;
11+
long nonOneCount = 0;
12+
13+
for(int i=1;i<n;i++){
14+
long prevOneCount = oneCount;
15+
oneCount = (nonOneCount*(k-1))%MOD;
16+
nonOneCount = (prevOneCount + ((k-2)*nonOneCount)%MOD)%MOD;
17+
}
18+
if(x==1){
19+
return oneCount;
20+
}else{
21+
return nonOneCount;
22+
}
23+
24+
}
25+
26+
int main()
27+
{
28+
ofstream fout(getenv("OUTPUT_PATH"));
29+
30+
string nkx_temp;
31+
getline(cin, nkx_temp);
32+
33+
vector<string> nkx = split_string(nkx_temp);
34+
35+
int n = stoi(nkx[0]);
36+
37+
int k = stoi(nkx[1]);
38+
39+
int x = stoi(nkx[2]);
40+
41+
long answer = countArray(n, k, x);
42+
43+
fout << answer << "\n";
44+
45+
fout.close();
46+
47+
return 0;
48+
}
49+
50+
vector<string> split_string(string input_string) {
51+
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
52+
return x == y and x == ' ';
53+
});
54+
55+
input_string.erase(new_end, input_string.end());
56+
57+
while (input_string[input_string.length() - 1] == ' ') {
58+
input_string.pop_back();
59+
}
60+
61+
vector<string> splits;
62+
char delimiter = ' ';
63+
64+
size_t i = 0;
65+
size_t pos = input_string.find(delimiter);
66+
67+
while (pos != string::npos) {
68+
splits.push_back(input_string.substr(i, pos - i));
69+
70+
i = pos + 1;
71+
pos = input_string.find(delimiter, i);
72+
}
73+
74+
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
75+
76+
return splits;
77+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
#define MOD 1000000007
3+
using namespace std;
4+
5+
// Complete the substrings function below.
6+
int substrings(string s) {
7+
8+
long long lastValue = s[0]-'0';
9+
long long totalSum = s[0] - '0';
10+
for(int i=1;i<s.length();i++){
11+
lastValue = (lastValue*10) + ((s[i]-'0')*(i+1));
12+
lastValue = lastValue%MOD;
13+
totalSum = (totalSum + lastValue)%MOD;
14+
}
15+
return totalSum;
16+
17+
}
18+
19+
int main()
20+
{
21+
ofstream fout(getenv("OUTPUT_PATH"));
22+
23+
string n;
24+
getline(cin, n);
25+
26+
int result = substrings(n);
27+
28+
fout << result << "\n";
29+
30+
fout.close();
31+
32+
return 0;
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// Complete the sherlockAndAnagrams function below.
6+
int sherlockAndAnagrams(string s) {
7+
map<string,int>m;
8+
int n = s.length();
9+
for(int i=0;i<n;i++){
10+
for(int j=0;j<n;j++){
11+
string ans = "";
12+
for(int k=i;k<=j;k++){
13+
ans += s[k];
14+
}
15+
if(ans!=""){
16+
sort(ans.begin(),ans.end());
17+
m[ans]++;
18+
}
19+
}
20+
}
21+
int count = 0;
22+
for(auto p:m){
23+
if(p.second>=2){
24+
count+=((p.second)*(p.second-1))/2;
25+
}
26+
}
27+
28+
return count;
29+
}
30+
31+
int main()
32+
{
33+
ofstream fout(getenv("OUTPUT_PATH"));
34+
35+
int q;
36+
cin >> q;
37+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
38+
39+
for (int q_itr = 0; q_itr < q; q_itr++) {
40+
string s;
41+
getline(cin, s);
42+
43+
int result = sherlockAndAnagrams(s);
44+
45+
fout << result << "\n";
46+
}
47+
48+
fout.close();
49+
50+
return 0;
51+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int max_subsequence(int a[], int n)
4+
{
5+
int m = a[0], s = 0;
6+
for (int i = 0; i < n; i++)
7+
{
8+
if (m < a[i])
9+
{
10+
m = a[i];
11+
}
12+
if (a[i] >= 0)
13+
{
14+
s += a[i];
15+
}
16+
}
17+
if (m >= 0)
18+
{
19+
return s;
20+
}
21+
else
22+
{
23+
return m;
24+
}
25+
}
26+
int maxSubArraySum(int a[], int n)
27+
{
28+
int f = a[0];
29+
int ans = f;
30+
for(int i = 1;i < n ; i++){
31+
f = max(a[i], f + a[i]);
32+
ans = max(ans, f);
33+
}
34+
return ans;
35+
}
36+
vector<int> maximum_subarray(int a[], int n)
37+
{
38+
vector<int> ans;
39+
ans.push_back(maxSubArraySum(a, n));
40+
ans.push_back(max_subsequence(a, n));
41+
return ans;
42+
}
43+
int main()
44+
{
45+
int t;
46+
cin >> t;
47+
while (t--)
48+
{
49+
int n;
50+
cin >> n;
51+
int a[n];
52+
for (int i = 0; i < n; i++)
53+
{
54+
cin >> a[i];
55+
}
56+
57+
vector<int> ans = maximum_subarray(a, n);
58+
for(int i=0;i<ans.size();i++){
59+
cout<<ans[i]<<" ";
60+
}
61+
cout<<endl;
62+
}
63+
return 0;
64+
}

Hackerrank/Project_Euler/q1.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
typedef long long int ll;
5+
6+
int main()
7+
{
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
ll t;
11+
cin>>t;
12+
while(t--)
13+
{
14+
ll n;
15+
cin>>n;
16+
ll s = 0;
17+
n -= 1;
18+
ll three = n/3;
19+
ll five = n/5;
20+
ll fifteen = n/15;
21+
three = (three*(three+1))/2;
22+
five = (five*(five + 1))/2;
23+
fifteen = (fifteen*(fifteen+1))/2;
24+
s = s + (three*3) + (five*5) - (fifteen*15);
25+
26+
cout<<s<<"\n";
27+
}
28+
return 0;
29+
}

Hackerrank/Project_Euler/q10.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
typedef long long int ll;
5+
6+
int main()
7+
{
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
///////////////////////////////////////////seive
11+
ll size = 1e6+1;
12+
bool isprime[size];
13+
for(ll i=0;i<size;i++)
14+
{
15+
isprime[i] = true;
16+
}
17+
isprime[0] = isprime[1] = false;
18+
for(ll i=2;i*i<size;i++)
19+
{
20+
if(isprime[i])
21+
{
22+
for(ll j=i*i;j<size;j+=i)
23+
isprime[j] = false;
24+
}
25+
}
26+
set <ll> s;
27+
for(ll i=0;i<size;i++)
28+
{
29+
if(isprime[i])
30+
s.insert(i);
31+
}
32+
ll sizea = s.size();
33+
ll a[sizea],b[sizea],f = 0;
34+
for(auto it : s)
35+
{
36+
a[f++] = it;
37+
}
38+
b[0] = a[0];
39+
for(ll i=1;i<sizea;i++)
40+
{
41+
b[i] = a[i];
42+
b[i] += b[i-1];
43+
}
44+
///////////////////////////////////////////
45+
ll t;
46+
cin>>t;
47+
while(t--)
48+
{
49+
ll n;
50+
cin>>n;
51+
ll index = lower_bound(a,a+sizea,n) - a;
52+
if(a[index] != n)
53+
{
54+
index -= 1;
55+
}
56+
cout<<b[index]<<"\n";
57+
}
58+
return 0;
59+
}

0 commit comments

Comments
 (0)