Skip to content

Commit 3ad7e3e

Browse files
committed
Top 10
1 parent 5e2feb8 commit 3ad7e3e

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Given a string, print all possible palindromic partitions
3+
*/
4+
5+
#include<bits/stdc++.h>
6+
#define pb push_back
7+
#define pob pop_back
8+
using namespace std;
9+
10+
bool isPallin(string s, int l, int h){
11+
while(l<h){
12+
if(s[l]!=s[h])
13+
return false;
14+
l++;
15+
h--;
16+
}
17+
return true;
18+
}
19+
20+
void allPalPartion(vector< vector<string> >&allPal, vector<string>&currPal, int start, int end, string str){
21+
if(start>=end)
22+
{
23+
allPal.pb(currPal);
24+
return;
25+
}
26+
27+
for(int i=start;i<end;i++){
28+
if(isPallin(str,start,i))
29+
{
30+
currPal.pb(str.substr(start,i-start+1));
31+
allPalPartion(allPal,currPal,i+1,end,str);
32+
currPal.pob();
33+
}
34+
}
35+
}
36+
37+
int main()
38+
{
39+
string str;
40+
cin>>str;
41+
42+
vector< vector<string> >allPal;
43+
vector<string>currPal;
44+
45+
allPalPartion(allPal, currPal, 0, str.size(), str);
46+
47+
for(int i=0;i<allPal.size();i++)
48+
{
49+
for (int j=0; j<allPal[i].size(); j++)
50+
cout << allPal[i][j] << " ";
51+
cout << "\n";
52+
}
53+
return 0;
54+
}
37 KB
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Reverse an array without affecting special characters
3+
*/
4+
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
8+
bool isAlphabet(char ch){
9+
if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'))
10+
return true;
11+
return false;
12+
}
13+
14+
int main()
15+
{
16+
string s;
17+
cin>>s;
18+
19+
int l=0;
20+
int r=s.size()-1;
21+
22+
while(l<r){
23+
if(!isAlphabet(s[l]))
24+
l++;
25+
else if(!isAlphabet(s[r]))
26+
r--;
27+
else{
28+
swap(s[l],s[r]);
29+
l++;
30+
r--;
31+
}
32+
}
33+
cout<<s<<endl;
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Count triplets with sum smaller than a given value
3+
*/
4+
5+
#include<bits/stdc++.h>
6+
#define pb push_back
7+
using namespace std;
8+
9+
10+
int countTriplets(vector<int> arr,int sum){
11+
int ans=0;
12+
int n=arr.size();
13+
for(int i=0;i<n-2;i++){
14+
int l=i+1,h=n-1;
15+
while(l<h){
16+
if(arr[i]+arr[l]+arr[h]>=sum)
17+
h--;
18+
else{
19+
ans+=(h-l);//these many possible triplets can be formed with same i,l and varying h.
20+
l++;
21+
}
22+
}
23+
}
24+
return ans;
25+
}
26+
27+
int main(){
28+
int n,sum;
29+
cin>>n>>sum;
30+
vector<int>arr;
31+
for(int i=0;i<n;i++)
32+
{
33+
int a;
34+
cin>>a;
35+
arr.push_back(a);
36+
}
37+
sort(arr.begin(), arr.end());
38+
cout<<countTriplets(arr,sum)<<endl;
39+
}

0 commit comments

Comments
 (0)