From 834229300a3613f74cf55ffa609ba01b0861d918 Mon Sep 17 00:00:00 2001 From: Wander_Lust Date: Wed, 11 Mar 2026 18:38:08 +0530 Subject: [PATCH] added cses --- cses/1069.cpp | 44 ++++++++++++++++++++++ cses/1070.cpp | 51 +++++++++++++++++++++++++ cses/1071.cpp | 53 ++++++++++++++++++++++++++ cses/1072.cpp | 39 +++++++++++++++++++ cses/1083.cpp | 62 +++++++++++++++++++++++++++++++ cses/1094.cpp | 47 +++++++++++++++++++++++ leetcode/lc1009.cpp | 27 ++++++++++++++ leetcode/lc1784.cpp | 51 +++++++++++++++++++++++++ leetcode/lc1888/SlidingWindow.cpp | 56 ++++++++++++++++++++++++++++ templates/cses.cpp | 32 ++++++++++++++++ 10 files changed, 462 insertions(+) create mode 100644 cses/1069.cpp create mode 100644 cses/1070.cpp create mode 100644 cses/1071.cpp create mode 100644 cses/1072.cpp create mode 100644 cses/1083.cpp create mode 100644 cses/1094.cpp create mode 100644 leetcode/lc1009.cpp create mode 100644 leetcode/lc1784.cpp create mode 100644 leetcode/lc1888/SlidingWindow.cpp create mode 100644 templates/cses.cpp diff --git a/cses/1069.cpp b/cses/1069.cpp new file mode 100644 index 0000000..20f2929 --- /dev/null +++ b/cses/1069.cpp @@ -0,0 +1,44 @@ +//URL - https://cses.fi/problemset/task/1069 +//CSES +#include +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> tc; + while(tc--) solve(); +} + diff --git a/cses/1070.cpp b/cses/1070.cpp new file mode 100644 index 0000000..9edaa7b --- /dev/null +++ b/cses/1070.cpp @@ -0,0 +1,51 @@ +//URL - https://cses.fi/problemset/task/1070/ +//CSES +#include +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<> tc; + while(tc--) solve(); +} + diff --git a/cses/1071.cpp b/cses/1071.cpp new file mode 100644 index 0000000..0604a38 --- /dev/null +++ b/cses/1071.cpp @@ -0,0 +1,53 @@ +//URL -https://cses.fi/problemset/task/1071 +//CSES +#include +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<> tc; + while(tc--) solve(); +} + diff --git a/cses/1072.cpp b/cses/1072.cpp new file mode 100644 index 0000000..973f8a6 --- /dev/null +++ b/cses/1072.cpp @@ -0,0 +1,39 @@ +//URL - +//CSES +#include +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<> tc; + while(tc--) solve(); +} + diff --git a/cses/1083.cpp b/cses/1083.cpp new file mode 100644 index 0000000..1381303 --- /dev/null +++ b/cses/1083.cpp @@ -0,0 +1,62 @@ +//URL - https://cses.fi/problemset/task/1083 +//CSES +#include +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<> 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"; +} +*/ diff --git a/cses/1094.cpp b/cses/1094.cpp new file mode 100644 index 0000000..14eb5d5 --- /dev/null +++ b/cses/1094.cpp @@ -0,0 +1,47 @@ +//URL - https://cses.fi/problemset/task/1094 +//CSES +#include +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; + vectors(n); + ll count = 0; + for(ll i =0;i>s[i]; + } + for(ll i=1;i0) { + count+=diff; + s[i] = s[i-1]; + } + } + cout<> tc; + while(tc--) solve(); +} + diff --git a/leetcode/lc1009.cpp b/leetcode/lc1009.cpp new file mode 100644 index 0000000..155f101 --- /dev/null +++ b/leetcode/lc1009.cpp @@ -0,0 +1,27 @@ +class Solution{ + public : + int bitwiseComplement(int n ){ + if(n==0) return 1; + short c = bit_width(static_cast(n)); + int x = (1< mask = 111 + +3. Flip n using ~n, then remove extra leading 1s: + result = (~n) & mask + +Special case: n = 0 -> answer = 1 +*/ diff --git a/leetcode/lc1784.cpp b/leetcode/lc1784.cpp new file mode 100644 index 0000000..fc1a17b --- /dev/null +++ b/leetcode/lc1784.cpp @@ -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' + +-------------------------------- +*/ diff --git a/leetcode/lc1888/SlidingWindow.cpp b/leetcode/lc1888/SlidingWindow.cpp new file mode 100644 index 0000000..057875d --- /dev/null +++ b/leetcode/lc1888/SlidingWindow.cpp @@ -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=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. + +*/ diff --git a/templates/cses.cpp b/templates/cses.cpp new file mode 100644 index 0000000..aba1638 --- /dev/null +++ b/templates/cses.cpp @@ -0,0 +1,32 @@ +//URL - +//CSES +#include +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(); +}