You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<bits/stdc++.h>usingnamespacestd;intmain() {
int t;
cin >> t;
while (t--) {
// input
string s; cin >> s;
// reverse given stringreverse(s.begin(), s.end());
// reversing the wordsint start = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '.') {
reverse(s.begin() + start, s.begin() + i);
start = i + 1;
}
}
// reversing the last word;reverse(s.begin() + start, s.end());
// output
cout << s << endl;
}
return0;
}
Paran Matching (only one type of brace)
#include<bits/stdc++.h>usingnamespacestd;intmain() {
int t; cin >> t;
while (t--) {
string s; cin >> s;
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(') {
count++;
} else count--;
if (count < 0) {
break;
}
}
if (count == 0) cout << "True" << endl;
else cout << "False" << endl;
}
}
Paran Matching (Multiple)
#include<bits/stdc++.h>usingnamespacestd;intmain() {
int t; cin >> t;
while (t--) {
string s; cin >> s;
// CREATING A STACK!!!!!!! YAY YAY YAY --- WE DID NOT IMPLEMENT THISSS
stack<char> st;
bool is_true = true;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '('or s[i] == '['or s[i] == '{') {
st.push(s[i]);
} elseif (s[i] == ')'and !st.empty() and st.top() == '(') {
st.pop();
} elseif (s[i] == ']'and !st.empty() and st.top() == '[') {
st.pop();
} elseif (s[i] == '}'and !st.empty() and st.top() == '{') {
st.pop();
} else {
is_true = false;
break;
}
}
if (is_true and st.empty()) cout << "balanced" << endl;
else cout << "not balanced" << endl;
}
}
Pair sum
#include<bits/stdc++.h>usingnamespacestd;intmain() {
int t; cin >> t;
while (t--) {
int n, sum; cin >> n >> sum;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
bool found = false;
sort(arr, arr + n);
int i = 0, j = n - 1;
while (i < j) {
if (arr[i] + arr[j] == sum) {
found = true;
break;
}
if (arr[i] + arr[j] > sum) j--;
else i++;
}
if (found) cout << "Yes" << endl;
else cout << "No" << endl;
}
return0;
}
Sort array of 0, 1, 2
#include<bits/stdc++.h>usingnamespacestd;intmain() {
//codeint T;
cin>>T;
while(T--)
{
int N;
cin>>N;
int A[N];
for(int i = 0; i < N; i++)
cin>>A[i];
int low = 0, mid = 0, high = N-1;
while(mid <= high)
{
switch(A[mid])
{
case0:
swap(A[low++],A[mid++]);
break;
case1:
mid++;
break;
case2:
swap(A[mid],A[high--]);
break;
}
}
for(int i = 0; i < N; i++)
cout<<A[i]<<"";
cout<<endl;
}
return0;
}
Next Greater Element on Right
#include<bits/stdc++.h>usingnamespacestd;intmain()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
longlongint arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
stack<longlongint> st;
longlongint ans[n];
for (int i = n - 1; i >= 0; i--)
{
while (!st.empty() and st.top() < arr[i])
st.pop();
if (st.empty())
ans[i] = -1;
else
ans[i] = st.top();
st.push(arr[i]);
}
for (int i = 0; i < n; i++)
cout << ans[i] << "";
cout << endl;
}
return0;
}
Rain Water Harvesting
#include<bits/stdc++.h>usingnamespacestd;intmain() {
int T;
cin >> T;
while(T--)
{
int ans = 0;
int n;
cin >> n;
int A[n];
for(int i = 0; i < n; i++)
cin >> A[i];
int left[n],right[n];
left[0] = A[0];
for(int i = 1; i < n; i++)
left[i] = max(left[i-1], A[i]);
right[n-1] = A[n-1];
for(int i = n - 2; i >= 0; i--)
right[i] = max(right[i+1], A[i]);
for(int i = 0; i < n; i++)
ans += min(left[i], right[i]) - A[i];
cout << ans << endl;
}
return0;
}
Greedy + Sorting
Min cost Rope
#include<bits/stdc++.h>usingnamespacestd;intmain() {
int t;
cin >> t;
while(t--) {
int count, first, second;
longlongint result = 0;
cin >> count;
priority_queue< int, vector<int>, greater<int> > pq;
for(int i=0; i<n; i++){
int ropeLen;
scanf("%d", &ropeLen);
pq.push(ropeLen);
}
while(pq.size() > 1) {
first= pq.top(); pq.pop();
second = pq.top(); pq.pop();
result += (first+second);
pq.push(first+second);
}
cout << result << endl;
}
return0;
}
#include<bits/stdc++.h>usingnamespacestd;intbin_search(int A[],int left,int right,int k);
intmain()
{
int t; cin >> t;
while(t--)
{
int N;
cin>>N;
int a[N];
for(int i=0;i<N;i++)
cin>>a[i];
int key;
cin>>key;
int found = bin_search(a,0,N-1,key);
cout<<found<<endl;
}
}
intbin_search(int A[], int left, int right, int k)
{
if(left>right) return -1; // if left> right, value not foundint mid = (left+right)/2;
if(A[mid]==k) return mid;
if(left==right) return -1;
else{
if(k<A[mid]) returnbin_search(A,left,mid-1,k);
elsereturnbin_search(A, mid+1, right, k);
}
}
Fast Expo
#include<iostream>usingnamespacestd;longpower(int a, int b, int c){
if(b==1) return a;
else{
long x = power(a, b/2 , c);
if(b%2 == 0) return (x*x) % c;
else{
return (x*x *a) % c;
}
}
}
intmain() {
int t; cin>>t;
while(t--){
int a,b,c; cin>>a>>b>>c;
cout<<power(a,b,c)<<endl;
}
return0;
}
sqrt
#include<bits/stdc++.h>usingnamespacestd;longlongintfloorSqrt(longlongint x);
//Position this line where user code will be pasted.intmain()
{
int t;
cin>>t;
while(t--)
{
longlong n;
cin>>n;
cout << floorSqrt(n) << endl;
}
return0;
}
// x: element to find square rootlonglongintgetSquare(int val, int lo, int hi) {
if (lo == hi) return lo;
if (lo + 1 == hi) {
if (hi * hi <= val) return hi;
return lo;
}
longlongint mid = (lo + hi) / 2;
if ((mid * mid) > val) {
returngetSquare(val, lo, mid - 1);
} else {
returngetSquare(val, mid, hi);
}
}
longlongintfloorSqrt(longlongint x)
{
returngetSquare(x, 1, x);
}
Dynamic Programming
Longest Increasing Sub (Iterative Solution)
#include<bits/stdc++.h>usingnamespacestd;intmain() {
int t, n; cin >> t;
while (t--) {
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int dp[n];
dp[0] = 1;
for (int i = 1; i < n; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = max(dp[i], 1 + dp[j]);
}
}
}
// returns the max element of the array
cout << *max_element(dp, dp + n) << endl;
}
return0;
}
Word Break (Recursion + Memoization Solution)
#inlcude <bits/stdc++.h>
usingnamespacestd;// creating a global hash table
unordered_map<int, bool> H;
boolisPossible(string &s, vector<string> &dict, int index, int &len) {
// base caseif (index == len) returntrue;
// if value for current index is already calculated return it directly// Memoizationif (H.find(index) != H.end()) return H[index];
// trying for all words begining with current indexfor (int i = index; i < len; i++) {
string sub = s.substr(index, i - index + 1);
// if the word exist in dictif (find(dict.begin(), dict.end(), sub) != dict.end()) {
// then check for the remaining part recursivelyif(isPossible(s, dict, i + 1, len)) {
// store the curr val in hash table before returning
H[index] = true;
returntrue;
}
}
}
// store the value in hash table before returning
H[index] = false;
returnfalse;
}
intmain() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<string> dict(n);
for (int i = 0; i < n; i++) {
cin >> dict[i];
}
string s; cin >> s;
int len = s.length();
H.clear();
cout << isPossible(s, dict, 0, len) << endl;
}
return0;
}
Word Break (Iterative solution)
#include<bits/stdc++.h>usingnamespacestd;intmain() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<string> B(n);
for (int i = 0; i < n; i++) {
cin >> B[i];
}
string A; cin >> A;
int N = A.length();
// solutionbool dp[N + 1];
memset(dp, 0, sizeof dp);
dp[0] = true;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
if (dp[j - 1] andfind(B.begin(), B.end(), A.substr(j - 1, i - j + 1)) != B.end()) {
dp[i] = true;
break;
}
}
}
cout << dp[N] << endl;
}
return0;
}