Skip to content

Instantly share code, notes, and snippets.

View Sreejit7's full-sized avatar
🎨
tinkering with code, just for the fun of it. what else you here for?

Sreejit De Sreejit7

🎨
tinkering with code, just for the fun of it. what else you here for?
View GitHub Profile
#include<bits/stdc++.h>
using namespace std;
void intersection( vector<int> arr1, vector<int> arr2,int m,int n){
set <int> is;
for(int i=0;i<m;i++)
is.insert(arr1[i]);
for(int j=0;j<n;j++){
auto it= is.find(arr2[j]);
if(it!=is.end())
cout<< *it<< " ";
#include<bits/stdc++.h>
using namespace std;
int length(vector <int> arr){
int max=0;
auto i=arr.begin();
while(i!=arr.end()){
auto j=upper_bound(arr.begin(),arr.end(),*i);
auto copy=j;
if(*j-*i == 1){
j=upper_bound(arr.begin(),arr.end(),*j);
@Sreejit7
Sreejit7 / twoNumberSum.cpp
Created September 28, 2020 16:30
A program to find the pair of array elements for a particular sum
#include<bits/stdc++.h>
using namespace std;
//Approach 1 O(n^2) time
vector<int> twoNumberSum1(vector<int> array, int targetSum) {
vector <int> result;
for(int i=0;i<array.size();i++){
for(int j=0;j<array.size();j++){
if(array[i]==array[j])
@Sreejit7
Sreejit7 / knapSack.cpp
Created September 29, 2020 14:33
0/1 Knapsack Problem using DP
#include<bits/stdc++.h>
using namespace std;
int maxValue(int W, vector <int> val, vector <int> wt, int n){
int i,w;
int knap[n+1][W+1];
for(i=0;i<=n;i++){
for(w=0;w<=W;w++){
if(i ==0 || w==0)
knap[i][w]=0;
else if(wt[i-1] <= w){
@Sreejit7
Sreejit7 / maxSumnoAdj.cpp
Created October 1, 2020 18:15
Finding the Max Subset Sum from an array without adding adjacent values
#include <bits/stdc++.h>
using namespace std;
//Approach1 (O(n) time)
int maxSubsetSumNoAdjacent1(vector<int> array) {
vector <int> maxSum;
maxSum.push_back(array[0]);
maxSum.push_back(array[1]);
if(array.size()>2){
for(int i=2;i<array.size();i++){
if(maxSum[i-2] + array[i] > maxSum[i-1])
@Sreejit7
Sreejit7 / validateSeq.cpp
Created October 22, 2020 15:00
Given two non-empty arrays of integers, find if the second array is a subsequence of the first one. Elements in the subsequence should appear in the same order in the array
#include<bits/stdc++.h>
using namespace std;
//Approach 1 O(nlog(n)) time
bool isValidSubsequence(vector<int> array, vector<int> sequence) {
if(sequence.size()>array.size())
return false;
int pos = 0;
auto it = array.begin();
for(int i = 0; i<sequence.size();i++){
int index = find(it,array.end(),sequence[i]) - array.begin();
@Sreejit7
Sreejit7 / threeSum.cpp
Created October 26, 2020 12:18
Find all unique triplets in the array which gives the sum of zero
/**
* Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0?
* Find all unique triplets in the array which gives the sum of zero.
* Notice that the solution set must not contain duplicate triplets.
* */
#include<bits/stdc++.h>
using namespace std;
//O(n^2) T, O(n) S
class Solution {
@Sreejit7
Sreejit7 / useOutsideClick.ts
Created January 7, 2022 18:48
A custom react hook for returning the state whether a click was made in or outside of a certain element or not. [Ex. use case: for closing a sidebar on outside click]
import { MutableRefObject, useEffect, useState } from "react";
const useOutsideClick = (ref: MutableRefObject<any>) => {
const [isClickedOutside, setIsClickedOutside] = useState(false);
useEffect(() => {
const handleOutsideClick = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target)) {
setIsClickedOutside(true);
} else {