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
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 { |
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
/** | |
* 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 { |
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
#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(); |
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
#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]) |
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
#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){ |
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
#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]) |
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
#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); |
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
#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<< " "; |