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:
int kIncreasing(vector<int>& arr, int k) {
int lisLen=0;
int n=arr.size();
//LIS for K independent subarrays
for(int i=0;i<k;++i){
vector<int> lis;
for(int j=i;j<n;j+=k){
if(lis.empty() or lis.back()<=arr[j])
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n=nums.size();
vector<int> lis;
for(int i=0;i<n;++i){
int lb = lower_bound(lis.begin(),lis.end(),nums[i])-lis.begin();
if(lb==lis.size())
lis.push_back(nums[i]);
else
//Followup with K increasing subsequence
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int n=nums.size();
int k=3;
vector<long long> increasing(k,LONG_MAX);
//Find an increasing subsequence of length k
for(int i=0;i<n;++i){
class Solution {
public:
vector<int> closestPrimes(int left, int right) {
//Apply sieve of eratosthenes
vector<bool> isPrime(right+1,true);
for(int i=2;i*i<=right;++i){
if(isPrime[i]==true){
for(int j=2;i*j<=right;++j)
isPrime[i*j]=false;
}
class Solution {
bool isPalindrome(int no){
string s = to_string(no);
int left=0,right=s.size()-1;
while(left<right){
if(s[left]!=s[right])
return false;
left++;
right--;
}
class Solution {
long long permutation(int count,int MOD){
long long ans = 1;
for(int i=2;i<=count;++i)
ans = (ans*i)%MOD;
return ans;
}
public:
int numPrimeArrangements(int n) {
int MOD = 1e9+7;
class Solution {
void calculate_longestPrefixSuffix(string &needle,int m,vector<int>& lps){
int l=0,r=1;
while(r<m){
//If R-th char matches with L-th char
if(needle[l]==needle[r]){
lps[r] = 1+l;
l++;
r++;
} else {
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int power) {
int n = tokens.size();
sort(tokens.begin(),tokens.end());//sort to make choice-maing obvious
//2-pointer approach
int left=0,right=n-1;
int score = 0,ans = 0;
while(left<=right){
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
unordered_map<string,int> freq,curr;
for(string& word: words)
freq[word]++;
int len = s.size();
int n = words.size();
int wordSize = words[0].size();
class Solution {
public:
string maximumOddBinaryNumber(string s) {
int count = 0;
int n = s.size();
for(int i=0;i<n;++i){
if(s[i]=='1'){
count++;
s[i]='0'; //Reset value to 0
}