added cses

This commit is contained in:
2026-03-11 18:38:08 +05:30
parent e698c35654
commit 834229300a
10 changed files with 462 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
//URL - https://cses.fi/problemset/task/1069
//CSES
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
#define ll long long
#define ld long double
#define ar array
#define all(x) (x).begin(), (x).end()
#define sza(x) (int)(x).size()
const ll INF = 1e18;
const int MOD = 1e9 + 7;
void solve() {
string s;
cin>>s;
int cur = 1, best = 1;
for(int i =1;i<s.size();i++){
if(s[i]==s[i-1]) cur++;
else cur =1;
best = max(best,cur);
}
cout<<best;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
while(tc--) solve();
}
+51
View File
@@ -0,0 +1,51 @@
//URL - https://cses.fi/problemset/task/1070/
//CSES
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
#define ll long long
#define ld long double
#define ar array
#define all(x) (x).begin(), (x).end()
#define sza(x) (int)(x).size()
const ll INF = 1e18;
const int MOD = 1e9 + 7;
void solve() {
int n;
cin>>n;
if(n==1) {
cout<<n;
return;
}
if(n<4){ cout<<"NO SOLUTION";
return;
}
for(int i =2;i<=n;i+=2)
cout<<i<<" ";
for(int i = 1;i<=n;i+=2){
cout<<i<<" ";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
while(tc--) solve();
}
+53
View File
@@ -0,0 +1,53 @@
//URL -https://cses.fi/problemset/task/1071
//CSES
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
#define ll long long
#define ld long double
#define ar array
#define all(x) (x).begin(), (x).end()
#define sza(x) (int)(x).size()
const ll INF = 1e18;
const int MOD = 1e9 + 7;
void solve() {
long long y,x;
cin>>y>>x;
long long ans = 0;
long long n = max(y,x);
if(n%2==0){
if(y==n){
ans = n*n-(x-1);
}else{
ans = (n-1)*(n-1)+y;
}
}
else{
if(x==n){
ans = n*n-(y-1);
}else{
ans = (n-1)*(n-1)+x;
}
}
cout<<ans<<"\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
cin >> tc;
while(tc--) solve();
}
+39
View File
@@ -0,0 +1,39 @@
//URL -
//CSES
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
#define ll long long
#define ld long double
#define ar array
#define all(x) (x).begin(), (x).end()
#define sza(x) (int)(x).size()
const ll INF = 1e18;
const int MOD = 1e9 + 7;
void solve() {
long long n;
cin>>n;
for(long long k = 1;k<=n;k++){
long long total = (k*k*(k*k-1))/2;
long long attack = 4 *((k-1)*(k-2));
cout<<total - attack<<"\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
while(tc--) solve();
}
+62
View File
@@ -0,0 +1,62 @@
//URL - https://cses.fi/problemset/task/1083
//CSES
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
#define ll long long
#define ld long double
#define ar array
#define all(x) (x).begin(), (x).end()
#define sza(x) (int)(x).size()
const ll INF = 1e18;
const int MOD = 1e9 + 7;
void solve() {
ll n;
cin>>n;
ll sum = (n*(n+1))/2;
int num;
while(cin>>num){
sum-=num;
}
cout<<sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
while(tc--) solve();
}
//XOR approach
/*
void solve() {
int n;
cin >> n;
ll x = 0;
for(int i = 1; i <= n; i++)
x ^= i;
for(int i = 0; i < n-1; i++){
int a;
cin >> a;
x ^= a;
}
cout << x << "\n";
}
*/
+47
View File
@@ -0,0 +1,47 @@
//URL - https://cses.fi/problemset/task/1094
//CSES
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
#define ll long long
#define ld long double
#define ar array
#define all(x) (x).begin(), (x).end()
#define sza(x) (int)(x).size()
const ll INF = 1e18;
const int MOD = 1e9 + 7;
void solve() {
ll n;
cin>>n;
vector<ll>s(n);
ll count = 0;
for(ll i =0;i<n;i++){
cin>>s[i];
}
for(ll i=1;i<n;i++){
ll diff = s[i-1] - s[i];
if(diff>0) {
count+=diff;
s[i] = s[i-1];
}
}
cout<<count;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
while(tc--) solve();
}
+27
View File
@@ -0,0 +1,27 @@
class Solution{
public :
int bitwiseComplement(int n ){
if(n==0) return 1;
short c = bit_width(static_cast<unsigned int>(n));
int x = (1<<c) -1;
return ~n&x;
}
};
/*
Idea:
Flip only the bits used in n's binary representation.
1. Find number of bits in n using:
std::bit_width(n)
It returns how many bits are needed to represent n.
2. Create a mask with all those bits set to 1:
mask = (1 << bit_width) - 1
Example: bit_width = 3 -> mask = 111
3. Flip n using ~n, then remove extra leading 1s:
result = (~n) & mask
Special case: n = 0 -> answer = 1
*/
+51
View File
@@ -0,0 +1,51 @@
class Solution{
public:
bool checkOnesSegment(string s){
return s.find("01") == string::npos;
}
};
//TC: o(n), SC:o(1)
//npos is a special cpp constant stands for "no position" which is essentially not found value, (size_t(-1)) returned by find() and such functions
/*
find()
- finds FIRST occurrence of substring
Example:
string s = "banana";
s.find("na") -> 2
--------------------------------
rfind()
- finds LAST occurrence of substring
Example:
string s = "banana";
s.rfind("na") -> 4
--------------------------------
find_first_of()
- finds first character that matches
ANY char from a given set
Example:
string s = "hello";
s.find_first_of("aeiou") -> 1 // 'e'
--------------------------------
find_last_of()
- finds last character that matches
ANY char from a given set
Example:
string s = "hello";
s.find_last_of("aeiou") -> 4 // 'o'
--------------------------------
*/
+56
View File
@@ -0,0 +1,56 @@
class Solution {
public:
int minFlips(string s) {
int n = s.size();
string t = s + s;
int ans = INT_MAX;
int diff1=0, diff2 = 0;
for(int i =0;i<t.size();i++){
char expected1 = (i%2 ? '1':'0');
char expected2 = (i%2 ? '0':'1');
if(t[i]!=expected1) diff1++;
if(t[i]!=expected2) diff2++;
if(i>=n){
char old = t[i-n];
char oldExpected1 = ((i-n)%2 ? '1' : '0');
char oldExpected2 = ((i-n)%2 ? '0' : '1');
if(old!=oldExpected1) diff1--;
if(old!=oldExpected2) diff2--;
}
if(i>=n-1)
ans = min(ans, min(diff1,diff2));
}
return ans;
}
};
//SC : o(n) TC : o(n)
/*
Approach:
Since the string can be rotated, we simulate all possible rotations by
doubling the string (t = s + s). Any rotation of s will appear as a
substring of length n in this doubled string. We then slide a window of
size n across t and compute how many flips are required to make the
current window alternating.
There are only two possible alternating patterns: "010101..." and
"101010...". We maintain two mismatch counters (diff1 and diff2) that
track how many characters in the current window differ from each pattern.
As the window expands, we add the mismatch contribution of the new
character. When the window exceeds size n, we remove the contribution of
the character that leaves the window.
For every window of size n, the minimum of diff1 and diff2 gives the
number of flips required for that rotation. We keep track of the global
minimum across all windows.
*/
+32
View File
@@ -0,0 +1,32 @@
//URL -
//CSES
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
#define ll long long
#define ld long double
#define ar array
#define all(x) (x).begin(), (x).end()
#define sza(x) (int)(x).size()
const ll INF = 1e18;
const int MOD = 1e9 + 7;
void solve() {
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
while(tc--) solve();
}