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:
vector<int> smallestSubarrays(vector<int>& nums) {
vector<int> ans;
int n=nums.size();
vector<int> nextSetBitPos(32,-1);
//Solve from right to left
int maxOR=0;
for(int i=n-1;i>=0;--i){
class Solution {
int diameter(unordered_map<int,vector<int>>& adj,int n){
//Pick any node and find the farthest node from that node
vector<bool> visited(n,false);
queue<int> q;
q.push(0);
visited[0]=true;
int last;
while(!q.empty()){
int size = q.size();
class Solution {
public:
int maxArea(vector<int>& height) {
int left=0, right=height.size()-1;
int max_area = 0;
int curr_area;
while(left < right)
{
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 {