Skip to content

Instantly share code, notes, and snippets.

View SuryaPratapK's full-sized avatar

Surya Pratap SuryaPratapK

  • Durgapur
View GitHub Profile
class Solution {
public:
string countAndSay(int n) {
if(n==1) return "1";
string number = "1";
for(int i=2;i<=n;++i){
//Build new string
string res;
int count = 1;
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;
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;
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)
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());
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);
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));
}
};
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;
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);
/**
* 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) {}
* };