This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
using ll = long long; | |
public: | |
long long countGood(vector<int>& nums, int k) { | |
ll n = nums.size(); | |
ll left = 0, right = 0; | |
ll good_subarrays = 0; | |
unordered_map<ll,ll> freq; | |
ll equal_pairs = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
using ll = long long; | |
vector<ll> seg_tree; | |
void updateSegTree(ll st_idx,ll start,ll end,ll& query_idx){ | |
if(end<query_idx or start>query_idx)//Case-1: No Overlap | |
return; | |
if(start==end){//Case-2: Total Overlap | |
seg_tree[st_idx]++; | |
return; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
#define ll long long | |
ll k_palindromes = 0; | |
unordered_set<ll> done; | |
vector<ll> fact; | |
void precomputeFactorial(int& n){ | |
fact[0] = 1; | |
fact[1] = 1; | |
for(ll i=2;i<=10;++i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
#define ll long long | |
#define MAX_DIGITS 17 | |
ll dp[MAX_DIGITS][2]; | |
// Helper: Check if number should be subtracted based on suffix and digit constraints | |
static bool checkSubtract(const string& num_str, ll num_digits, const string& suffix, int limit) { | |
if (num_digits < suffix.size()) return false; | |
string suffix_of_num = num_str.substr(num_digits - suffix.size()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> largestDivisibleSubset(vector<int>& nums) { | |
int n = nums.size(); | |
vector<int> ans; | |
//Step-1: Sort the array and Find LIS length | |
sort(nums.begin(),nums.end()); | |
int lis = 1; | |
vector<int> dp(n+1,1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int subsetXORSum(vector<int>& nums) { | |
int n = nums.size(); | |
int orr = 0; | |
for(int ele: nums) | |
orr |= ele; | |
return orr * (1<<(n-1)); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
#define ll long long | |
public: | |
long long maximumTripletValue(vector<int>& nums) { | |
int n = nums.size(); | |
//Step-1: Find right_max for all indices | |
vector<int> right_max(n); | |
int max_val = nums[n-1];//last element | |
for(int i=n-2;i>0;--i){ | |
right_max[i] = max_val; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
#define ll long long | |
ll findMaxPoints(vector<vector<int>>& questions,int pos,vector<ll>& mem){ | |
if(pos >= questions.size()) | |
return 0; | |
if(mem[pos]!=-1) | |
return mem[pos]; | |
ll exclude = findMaxPoints(questions,pos+1,mem); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | |
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | |
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | |
* }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
#define ll long long | |
int MOD = 1000000007; | |
ll binaryExponentiation(ll a,ll b){ | |
ll res = 1; | |
while(b){ | |
if(b&1) | |
res = (res * a) % MOD; | |
a = (a * a) % MOD; |