├── STL_DEMO ├── tempCodeRunnerFile.cpp └── Front-to-back.cpp ├── cluster1.yaml ├── README.md ├── GFG_DSA_Topicwise ├── Mathematics │ ├── Celsius-Fahrenheit-Conversion.cpp │ ├── Absolute-Value.cpp │ └── Quadratic-Equation-Roots.cpp ├── Bit-Magic │ ├── Count-set-bits.cpp │ ├── Kth-bit-is-set-or-not.cpp │ ├── First-set-bit.cpp │ ├── Rightmost-Different-Bit.cpp │ ├── Odd-occurence-in-array.cpp │ ├── Power-of-2.cpp │ ├── Bit-Difference.cpp │ ├── Total-Set-Bits-From-1-to-n.cpp │ ├── Reverse-Bits.cpp │ ├── Position-of-set-bit.cpp │ ├── Sparse-Number.cpp │ ├── Swap-all-odd-even-bits.cpp │ ├── Longest-consecutive-setbit.cpp │ ├── Generate-lexicographic-powerset.cpp │ ├── Maximum-AND-Value.cpp │ ├── SWAP-Nibbles-(ACCOLITE,CISCO,SAMS,QUAL).cpp │ ├── Check-all-bits-are-set.cpp │ ├── Swap-every-two-bits-in-bytes(CISCO).cpp │ ├── Missing-Ele-1-to-n-(ADO,AMA,MICRO,DES,CIS,INTU..).cpp │ ├── Decimal-to-Binary.cpp │ ├── XOR-of-subarrays(AMAZON).cpp │ ├── Replace-Bit-(MICRO).cpp │ ├── Sum-bit-diff-in-pairs-of-array(GOOGLE).cpp │ ├── Set-Kth-bit.cpp │ ├── Trail-of-ones-(AMAZON).cpp │ ├── Copy-Set-Bits-in-Range-(DE-SHAW).cpp │ ├── Binary-Multiple-of-3-(AMA,ADO,MICRO).cpp │ ├── Flip-bits-max1s-(KADANE)-(AMAZON).cpp │ ├── 2-odd-occurrence-in-array(AMA,GOO,MICRO,QUAL,MMT).cpp │ └── Bleak-Numbers(SAP).cpp ├── Tree │ ├── Height-Of-Tree.cpp │ ├── PostOrder-Traversal.cpp │ ├── Preorder-Traversal.cpp │ ├── Inorder-Traversal.cpp │ ├── Check-Identical-Trees.cpp │ ├── Level-Order-Traversal.cpp │ ├── Reverse-LOT.cpp │ ├── Check-Balanced-Tree(AMAZON,WALMART).cpp │ ├── Count-Nodes-In-Complete-BT.cpp │ ├── Maximum-Path-Sum.cpp │ ├── Diameter-Tree.cpp │ ├── K-Dist-From-Root(AMAZON,MICROSOFT,SAMSUNG).cpp │ ├── Foldable-BT.cpp │ ├── LCA.cpp │ ├── MaxSum-Non-Adjacent-Nodes.cpp │ ├── Convert-BT-to-DLL(AMAZON,GS,MICROSOFT,SLAESFORCE,MORG-STAN).cpp │ ├── Create-Tree-From-Inorder-Preorder(AMAZON,MICROSOFT).cpp │ ├── Subtrees-with-sum=X(MICROSOFT).cpp │ ├── Serialize-Deserialize-BT(IMP).cpp │ ├── Top-View(AMAZON,PAYTM,SAMSUNG,WALMART,OLA).cpp │ ├── Line-By-Line-LOT(AMAZON,MICROSOFT,MORG-STAN,SNAPDEAL).cpp │ ├── Burn-Tree.cpp │ ├── BT-From-Postorder-Inorder.cpp │ ├── Max-Path-Sum(IMP)(AMAZON,FACEBOOK,MICROSOFT,DIRECTI).cpp │ ├── Children-Sum-Parent.cpp │ ├── Burning-Tree(IMP)(MICROSOFT,AMAZON,FLIPKART).cpp │ └── Spiral-LOT.cpp ├── Maths │ ├── Fast-Modulo-Exponentiation(bitmasking).cpp │ ├── Catalan-DP-approach.cpp │ ├── Sieve-of-Eratosthenes.cpp │ └── Long-factorial.cpp ├── Recursion │ └── Tower-Of-Hanoi.cpp ├── Dynamic-Programming │ ├── LCS(MEMOIZATION).cpp │ ├── LCS(TABULATION-FASTER).cpp │ ├── Catalan-Number.cpp │ ├── House-Robber(LEETCODE).cpp │ └── Climbing-Stairs(LEETCODE).cpp ├── Heap │ ├── Heap-Sort(VVIP).cpp │ ├── Nearly-Sorted(AMAZON).cpp │ ├── Heap-Operations(VVIP).cpp │ ├── Kth-Largest.cpp │ ├── Kth-Smallest.cpp │ ├── Kth-Largest-In-Stream(AMAZON,CISCO,MICROSOFT,WALMART).cpp │ └── Rearrange-Characters(AMAZON).cpp ├── Graphs │ ├── Topological-Sort(DFS).cpp │ ├── Check-Cycle-Directed(TopoSort-Kahns-BFS).cpp │ ├── Check-Cycle-Undirected(DFS).cpp │ ├── Check-Cycle-DIrected(DFS).cpp │ ├── Prims-Algo-o(n^2).cpp │ ├── Check-Cycle-Undirected(BFS).cpp │ └── Adjacency-List-Implementation.cpp ├── Binary-Search-Tree │ ├── LCA.cpp │ ├── Floor-In-BST.cpp │ ├── Ceil-In-BST(SAMSUNG).cpp │ ├── Closest-Element(AMAZON,MAQ).cpp │ ├── Node-In-Range(AMAZON,MICROSOFT,FLIPKART).cpp │ ├── Check-BST(IMP).cpp │ ├── Vertical-Traversal(IMP).cpp │ └── Pair-Target-Sum(IMP)(AMAZON,MICROSOFT,VMWARE).cpp ├── Sorting │ ├── Three-Way-Partitioning(YAHOO).cpp │ ├── Quick-Sort_(Lomuto-Partition).cpp │ └── Quick-Sort_(Hoare-Partition).cpp ├── Hashing │ ├── Subarray-with-sum-0(PAYTM).cpp │ ├── Count-Subarray-with-equal-0-and-1.cpp │ ├── First-Repeating-Element(AMAZON,PAYPAL,ACCOLITE).cpp │ ├── Count-zero-sum-subarray(AMAZON,MICROSOFT).cpp │ ├── Longest-Consecutive-Subsequence(AMAZON,WALMART).cpp │ ├── Count-pair-with-given-sum(ADOBE,AMAZON,GS,FLIPKART,ACCOLITE).cpp │ ├── Intersection-of-two-arrays(AMAZON,PAYPAL).cpp │ └── Equal-Arrays-Using-Hash(GOLDMAN-SACHS).cpp ├── Arrays │ └── Longest-Eve-Odd-Subarray.cpp ├── Searching │ ├── count-more-than-(n\k)occr(AMAZON).cpp │ ├── Count-1s.cpp │ ├── Count-repeated(AMAZON).cpp │ ├── Left-Index.cpp │ ├── Maximum-Water(AMAZON).cpp │ └── Peak-Element(AMAZON,ADOBE,GOOGLE,VISA).cpp ├── Greedy │ ├── Largest-Number-With-Given-Sum(MICROSOFT).cpp │ ├── Activity-Selection(FACEBOOK,MORGAN-STANLEY).cpp │ ├── N-Meetings(AMAZON,MICROSOFT,FLIPKART,MMT).cpp │ └── Fractional-Knapsack(MICROSOFT).cpp ├── Stacks │ └── Maximum-Rectangular-Area-Histogram(MICROSOFT).cpp └── Matrix │ ├── Rotate-90degree(AMAZON,DESHAW,MICROSOFT,MORGSTAN,PAYTM,,SAMS,SNAPDEAL,ZOHO).cpp │ └── Search-In-MAT(AMAZON,ADOBE,DIRECTI,GS,VISA,ORACLE,PAYTM,SAP,SANPD,ACCOLITE,OLA,MMT).cpp ├── GFG-MUST-DO ├── DP │ └── Medium │ │ ├── Get-Min-Square(or-Coin-change).cpp │ │ └── Nth-stairs.cpp └── Graph │ └── Medium │ ├── Min-Swaps-to-Sort.cpp │ ├── Prerequisite-Task.cpp │ ├── Strongly-Connected-Component(Kosaraju-Algo).cpp │ └── Number-Of-Islands.cpp └── Leetcode ├── Company-Wise └── Intuit │ ├── Easy │ ├── Valid-Paranthesis.cpp │ ├── Longest_Palindrome.cpp │ └── Min-Stack(Time-O(1)).cpp │ └── Medium │ ├── Group-Anagrams.cpp │ ├── Find-Duplicate.cpp │ ├── Word-Search.cpp │ ├── Max-Area-Island.cpp │ └── Course-Schedule-II.cpp ├── Easy ├── Arrays │ ├── Shuffle-the-arrays.cpp │ └── Running-Sum.cpp ├── Bit-Manipulation │ ├── Hamming-Distance.cpp │ ├── Flip-Bits.cpp │ ├── Sorting-by-count-1s.cpp │ └── Reduce-number-to-0.cpp └── Hash-Tables │ └── Two-Sum.cpp ├── Top-Interview-Questions ├── Easy │ ├── Majority-Element.cpp │ └── Reverse-Bits.cpp └── Medium │ └── Majority-Element2.cpp ├── Medium ├── Hashing │ └── Contigous-Array.cpp ├── Bit-Manipulation │ └── Count-bits-from-1-to-n.cpp └── Arrays │ └── Maximum-Sum-Circular-Subarray.cpp └── Hard └── Binary-Search └── Number-of -elements-having-Kzeroes-at-end-of-factorials.cpp /STL_DEMO/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | ( -------------------------------------------------------------------------------- /cluster1.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: cluster.example.dev/v1alpha1 2 | kind: Cluster 3 | metadata: 4 | name: local1 5 | spec: 6 | kubeconfig: | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeForces-Spoj-CodeChef-GFG-Solutions 2 | Collection of solutions of different mathematical and data structures-algorithmic problems from CodeForces, Codechef, GFG, SPOJ in C++ and problems from HackerEarth in Java. 3 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Mathematics/Celsius-Fahrenheit-Conversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>>n; 13 | cout< 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,count=0; 12 | cin>>n; 13 | while(n!=0) 14 | { 15 | if(n&1) 16 | { 17 | count++; 18 | } 19 | n=n>>1; 20 | } 21 | cout< 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,k,count=0; 11 | cin>>n; 12 | cin>>k; 13 | n = n>>k; 14 | if(n&1) 15 | { 16 | cout<<"Yes"< 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | //code 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,count=0,reduced; 12 | cin>>n; 13 | reduced = n & (~(n-1)); 14 | while(reduced!=0) 15 | { 16 | count++; 17 | reduced = reduced>>1; 18 | 19 | } 20 | cout< 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | //code 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int a,b,XOR,count=0; 12 | cin>>a>>b; 13 | XOR = a^b; 14 | int sn = XOR & (~(XOR-1)); 15 | while(sn!=0) 16 | { 17 | count++; 18 | sn =sn>>1; 19 | } 20 | cout<left), height(node->right))+1); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Odd-occurence-in-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define fo(i,n) for(i=0;i>t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | int res=0; 15 | fo(i,n) 16 | { 17 | cin>>a[i]; 18 | res = res^a[i]; 19 | } 20 | cout< 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | ll n; 11 | cin>>n; 12 | if(n==0) 13 | { 14 | cout<<"NO"< 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | //code 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int a,b,XOR,count=0; 12 | cin>>a>>b; 13 | XOR = a^b; 14 | while(XOR!=0) 15 | { 16 | if(XOR & 1) 17 | { 18 | count++; 19 | } 20 | XOR = XOR>>1; 21 | } 22 | cout< &res) 10 | { 11 | if(root==NULL)return; 12 | recPostorder(root->left,res); 13 | recPostorder(root->right, res); 14 | res.push_back(root->data); 15 | } 16 | 17 | 18 | vector postOrder(Node* root) 19 | { 20 | vector res; 21 | recPostorder(root, res); 22 | return res; 23 | } 24 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Total-Set-Bits-From-1-to-n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | //code 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,i,count = 0; 12 | cin>>n; 13 | for(i=1;i<=n;i++) 14 | { 15 | int num = i; 16 | while(num!=0) 17 | { 18 | num = num & (num-1); 19 | count++; 20 | } 21 | } 22 | cout< 2 | #include 3 | #define ll long long 4 | using namespace std; 5 | int main() 6 | { 7 | //code 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | long long n, count=0,res=0; 13 | cin>>n; 14 | while(n!=0) 15 | { 16 | count++; 17 | if(n&1!=0) 18 | { 19 | res = res + pow(2,(32-count)); 20 | } 21 | n = n>>1; 22 | } 23 | cout< 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | //code 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,count=0; 12 | cin>>n; 13 | if((n&(n-1))==0) 14 | { 15 | while(n!=0) 16 | { 17 | count++; 18 | n = n>>1; 19 | } 20 | cout< 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | ll a; 11 | ll b; 12 | ll res; 13 | res=1; 14 | cin>>a>>b; 15 | while(b>0) 16 | { 17 | if(b & 1) 18 | { 19 | res = (res*a)%(1000000007); 20 | } 21 | a = (a*a)%(1000000007); 22 | b = b>>1; 23 | } 24 | cout< &res) 14 | { 15 | if(root==NULL)return; 16 | res.push_back(root->data); 17 | recPreorder(root->left, res); 18 | recPreorder(root->right, res); 19 | } 20 | 21 | vector preorder(Node* root) 22 | { 23 | vector res; 24 | recPreorder(root, res); 25 | return res; 26 | } 27 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Recursion/Tower-Of-Hanoi.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // You need to complete this function 4 | 5 | // avoid space at the starting of the string in "move disk....." 6 | long long toh(int N, int from, int to, int aux) { 7 | // Your code here 8 | long long moves = 0ll; 9 | if(N>=1) 10 | { 11 | moves += toh(N-1,from,aux,to); 12 | cout<<"move disk "< &res) 16 | { 17 | if(root==NULL) 18 | { 19 | return; 20 | } 21 | recInorder(root->left,res); 22 | res.push_back(root->data); 23 | recInorder(root->right,res); 24 | 25 | } 26 | 27 | vector inOrder(struct Node* root) 28 | { 29 | 30 | vector res; 31 | recInorder(root,res); 32 | return res; 33 | } 34 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Mathematics/Absolute-Value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int absolute(int I); 6 | 7 | int main() { 8 | int T; // number of testcases 9 | cin >> T; // input number of testcases 10 | while (T--) { 11 | int I; 12 | cin >> I; 13 | cout << absolute(I) << endl; // print the output 14 | } 15 | return 0; 16 | }// } Driver Code Ends 17 | 18 | 19 | // User function Template for C++ 20 | 21 | // Should return absolute value of I 22 | // You need to complete this function 23 | int absolute(int I) { 24 | // Your code here 25 | return abs(I); 26 | } 27 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Mathematics/Quadratic-Equation-Roots.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define ll long long 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int a,b,c; 12 | cin>>a>>b>>c; 13 | int D = pow(b,2)-(a*4*c); 14 | if(a==0) 15 | { 16 | cout<<"Invalid"< sq(sqlen,0); 10 | for(int i=0;i dp(n+1, 100001); 13 | dp[0] = 0; 14 | for(int i = 1;i<=n;i++) 15 | { 16 | for(int j=0;j 2 | #include 3 | using boost::multiprecision::cpp_int; 4 | using namespace std; 5 | 6 | cpp_int catalan(int n){ 7 | cpp_int cat[n+1]; 8 | cat[0] = 1; 9 | cat[1] = 1; 10 | 11 | for (int i=2; i<=n; i++) 12 | { 13 | cat[i] = 0; 14 | for (int j=0; j> t; 26 | while(t--){ 27 | cin >> n; 28 | cout << catalan(n) << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Sparse-Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,LS1,count=0; 11 | cin>>n; 12 | while(n!=0) 13 | { 14 | LS1 = n>>1; 15 | if((LS1%2!=0) && (n%2!=0)) 16 | { 17 | count++; 18 | break; 19 | } 20 | else 21 | { 22 | n = LS1; 23 | LS1 = LS1>>1; 24 | } 25 | } 26 | if(count!=0) 27 | { 28 | cout<<"0"< 2 | #define ll long long 3 | using namespace std; 4 | 5 | 6 | unsigned int swapBits(unsigned int x) 7 | { 8 | // Get all even bits of x 9 | unsigned int even_bits = x & 0xAAAAAAAA; 10 | 11 | // Get all odd bits of x 12 | unsigned int odd_bits = x & 0x55555555; 13 | 14 | even_bits >>= 1; // Right shift even bits 15 | odd_bits <<= 1; // Left shift odd bits 16 | 17 | return (even_bits | odd_bits); // Combine even and odd bits 18 | } 19 | 20 | 21 | 22 | int main() 23 | { 24 | //code 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | int x; 30 | cin>>x; 31 | cout<data==r2->data && 28 | isIdentical(r1->left,r2->left) && 29 | isIdentical(r1->right,r2->right) 30 | }; 31 | } 32 | return false; 33 | } 34 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Dynamic-Programming/LCS(MEMOIZATION).cpp: -------------------------------------------------------------------------------- 1 | //O(x*y) 2 | 3 | class Solution 4 | { 5 | public: 6 | //Function to find the length of longest common subsequence in two strings. 7 | int dp[1001][1001] 8 | memset(dp,-1,sizeof(dp)); 9 | int lcs(int x, int y, string s1, string s2) 10 | { 11 | if(dp[x][y]!=-1) return dp[x][y]; 12 | 13 | if(x==0||y==0) dp[x][y]=0; 14 | else 15 | { 16 | if(s1[x-1]==s2[y-1]) 17 | { 18 | dp[x][y] = 1+lcs(x-1,y-1,s1,s2); 19 | } 20 | else 21 | { 22 | dp[x][y] = max(lcs(x-1,y,s1,s2),lcs(x,y-1,s1,s2)); 23 | } 24 | } 25 | return dp[x][y]; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Longest-consecutive-setbit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define ll long long 4 | using namespace std; 5 | int main() 6 | { 7 | //code 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | int n,LS,count=0; 13 | vector arr; 14 | cin>>n; 15 | LS = n>>1; 16 | while(n!=0) 17 | { 18 | 19 | 20 | if((n%2!=0) && (LS%2!=0)) 21 | { 22 | count++; 23 | n = LS; 24 | LS =LS>>1; 25 | } 26 | else 27 | { 28 | n = LS; 29 | LS =LS>>1; 30 | arr.push_back(count); 31 | count=0; 32 | } 33 | } 34 | sort(arr.begin(),arr.end(),greater()); 35 | cout< levelOrder(Node* node) 15 | { 16 | //Your code here 17 | vector res; 18 | queue q; 19 | q.push(node); 20 | 21 | while(!q.empty()) 22 | { 23 | Node* curr = q.front(); 24 | q.pop(); 25 | res.push_back(curr->data); 26 | if(curr->left!=NULL) 27 | { 28 | q.push(curr->left); 29 | } 30 | if(curr->right!=NULL) 31 | { 32 | q.push(curr->right); 33 | } 34 | } 35 | 36 | return res; 37 | } 38 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Reverse-LOT.cpp: -------------------------------------------------------------------------------- 1 | struct Node 2 | { 3 | int data; 4 | Node* left; 5 | Node* right; 6 | }; 7 | 8 | 9 | 10 | vector reverseLevelOrder(Node *root) 11 | { 12 | // code here 13 | vector res; 14 | queue q; 15 | 16 | q.push(root); 17 | stack st; 18 | 19 | while(!q.empty()) 20 | { 21 | Node* curr = q.front(); 22 | q.pop(); 23 | st.push(curr->data); 24 | if(curr->right!=NULL) 25 | q.push(curr->right); 26 | 27 | if(curr->left!=NULL) 28 | q.push(curr->left); 29 | } 30 | 31 | while(!st.empty()) 32 | { 33 | int x = st.top(); 34 | st.pop(); 35 | res.push_back(x); 36 | } 37 | return res; 38 | } 39 | -------------------------------------------------------------------------------- /Leetcode/Company-Wise/Intuit/Easy/Valid-Paranthesis.cpp: -------------------------------------------------------------------------------- 1 | // Problem: https://leetcode.com/problems/valid-parentheses/ 2 | 3 | class Solution { 4 | public: 5 | bool isValid(string str) { 6 | int n = str.size(); 7 | stack st; 8 | st.push(str[0]); 9 | for(int i = 1;i> groupAnagrams(vector& strs) { 10 | unordered_map > mp; 11 | for(int i=0;i> ans; 18 | for(auto it : mp) 19 | ans.push_back(it.second); 20 | 21 | return ans; 22 | 23 | } 24 | }; -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Dynamic-Programming/LCS(TABULATION-FASTER).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | 5 | int longestCommonSubsequence(string text1, string text2) { 6 | int x = text1.length(); 7 | int y = text2.length(); 8 | int dp[x+1][y+1]; 9 | memset(dp,-1,sizeof(dp)); 10 | for(int i=0;i<=x;i++) 11 | { 12 | dp[i][0] = 0; 13 | } 14 | for(int j=0;j<=y;j++) 15 | { 16 | dp[0][j]=0; 17 | } 18 | for(int i=1;i<=x;i++) 19 | { 20 | for(int j=1;j<=y;j++) 21 | { 22 | if(text1[i-1]==text2[j-1]) dp[i][j]=dp[i-1][j-1]+1; 23 | else dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 24 | 25 | } 26 | } 27 | return dp[x][y]; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Heap/Heap-Sort(VVIP).cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void heapify(int arr[], int n, int i) 5 | { 6 | int largest = i,r = 2*i+2,l=2*i+1; 7 | if(larr[largest]) largest = l; 8 | if(rarr[largest]) largest = r; 9 | 10 | if(largest!=i) 11 | { 12 | swap(arr[i],arr[largest]); 13 | heapify(arr,n,largest); 14 | } 15 | } 16 | 17 | public: 18 | void buildHeap(int arr[], int n) 19 | { 20 | for(int i=n/2-1;i>=0;i--) 21 | heapify(arr,n,i); 22 | } 23 | 24 | 25 | public: 26 | void heapSort(int arr[], int n) 27 | { 28 | buildHeap(arr,n); 29 | for(int i=n-1;i>0;i--) 30 | { 31 | swap(arr[0],arr[i]); 32 | heapify(arr,i,0); 33 | } 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Maths/Sieve-of-Eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | 5 | 6 | void seive(int n) 7 | { 8 | int prime[n+1]={0}; 9 | 10 | for (int i = 3; i <= n; i += 2) { 11 | prime[i] = 1; 12 | } 13 | for(ll p=3;p<=n;p+=2) 14 | { 15 | if(prime[p]==1) 16 | { 17 | for(ll j=p*p;j<=n;j+=p) 18 | { 19 | prime[j]=0; 20 | } 21 | } 22 | } 23 | prime[2]=1; 24 | prime[0]=prime[1]=0; 25 | for(int p=0;p<=n;p++) 26 | { 27 | if(prime[p]==1) 28 | { 29 | cout<>t; 41 | while(t--) 42 | { 43 | int n; 44 | cin>>n; 45 | seive(n); 46 | cout< shuffle(vector& nums, int n) { 26 | vector res; 27 | for(int i = 0;i runningSum(vector& nums) { 23 | vector res; 24 | int sum; 25 | int n = nums.size(); 26 | sum = nums[0]; 27 | res.push_back(sum); 28 | for(int i=1;i>1; 36 | } 37 | return count; 38 | 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Leetcode/Easy/Hash-Tables/Two-Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of integers, return indices of the two numbers such that they add up to a specific target. 3 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 4 | Example: 5 | Given nums = [2, 7, 11, 15], target = 9, 6 | Because nums[0] + nums[1] = 2 + 7 = 9, 7 | return [0, 1]. 8 | */ 9 | 10 | 11 | 12 | class Solution { 13 | public: 14 | vector twoSum(vector& nums, int target) { 15 | int n = nums.size(); 16 | vector res; 17 | int a=0; 18 | int b=0; 19 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #define ll long long 6 | #define fo(i,n) for(i=0;i&str,int n,int pow_set) 11 | { 12 | int i,j; 13 | fo(i,pow_set) 14 | { 15 | string x; 16 | fo(j,n) 17 | { 18 | if(i & 1<>t; 34 | while(t--) 35 | { 36 | int n,i; 37 | cin>>n; 38 | string s; 39 | vector str; 40 | cin>>s; 41 | int pow_set=pow(2,n); 42 | func(s,str, n, pow_set); 43 | sort(str.begin(),str.end()); 44 | fo(i,str.size()) 45 | cout< adj[], vector& visited, int start, vector& st) 12 | { 13 | visited[start] = true; 14 | for(auto x:adj[start]) 15 | { 16 | if(!visited[x]) 17 | { 18 | dfs(adj,visited,x,st); 19 | } 20 | } 21 | 22 | st.push_back(start); 23 | } 24 | 25 | vector topoSort(int V, vector adj[]) { 26 | // code here 27 | vector visited(V,false); 28 | 29 | vector st; 30 | for(int i=0;i& nums) { 23 | int major = nums[0], count=1; 24 | int n = nums.size(); 25 | for(int i=1;i&nums) 9 | { 10 | // Code here 11 | vector> arrPos; 12 | for(int i=0;i visited(n,false); 20 | int res = 0; 21 | for(int i = 0;i0) res+=(cycle_size-1); 34 | } 35 | } 36 | return res; 37 | } 38 | }; -------------------------------------------------------------------------------- /Leetcode/Company-Wise/Intuit/Medium/Find-Duplicate.cpp: -------------------------------------------------------------------------------- 1 | //PROBLEM: https://leetcode.com/problems/find-the-duplicate-number/ 2 | 3 | 4 | 5 | // This sol is used when there is a constraint that array cannot be modified 6 | // Uses Tortoise Hare Algorithm 7 | // If we can modify array then we can visit every element and consider it as index and make value negative 8 | // at that index and if ever wr find already neg value that would be our ans. Complexity will be same for both sols. 9 | 10 | class Solution { 11 | public: 12 | int findDuplicate(vector& nums) { 13 | if(nums.size()>1) 14 | { 15 | int slow = nums[0]; 16 | int fast = nums[nums[0]]; 17 | while(slow!=fast) 18 | { 19 | slow = nums[slow]; 20 | fast = nums[nums[fast]]; 21 | } 22 | fast = 0; 23 | while(slow!=fast) 24 | { 25 | slow = nums[slow]; 26 | fast = nums[fast]; 27 | } 28 | return slow; 29 | } 30 | return -1; 31 | 32 | } 33 | }; -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Maths/Long-factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 100000 4 | 5 | int multiply(int x, int res[], int res_size); 6 | 7 | void factorial(int n) 8 | { 9 | int res[MAX]; 10 | 11 | res[0] = 1; 12 | int res_size = 1; 13 | 14 | for (int x=2; x<=n; x++) 15 | res_size = multiply(x, res, res_size); 16 | 17 | for (int i=res_size-1; i>=0; i--) 18 | cout << res[i]; 19 | cout<<"\n"; 20 | } 21 | 22 | 23 | int multiply(int x, int res[], int res_size) 24 | { 25 | int carry = 0; 26 | 27 | 28 | for (int i=0; i>t; 48 | while(t--) 49 | { 50 | int n; 51 | cin>>n; 52 | factorial(n); 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Check-Balanced-Tree(AMAZON,WALMART).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree, determine if it is height-balanced. 3 | 4 | For this problem, a height-balanced binary tree is defined as: 5 | 6 | a binary tree in which the left and right subtrees of every node differ in height by no more than 1. 7 | Example 1: 8 | 9 | 10 | Input: root = [3,9,20,null,null,15,7] 11 | Output: true 12 | Example 2: 13 | 14 | 15 | Input: root = [1,2,2,3,3,null,null,4,4] 16 | Output: false 17 | Example 3: 18 | 19 | Input: root = [] 20 | Output: true 21 | */ 22 | 23 | 24 | class Solution { 25 | public: 26 | 27 | int helperFunc(TreeNode* root) 28 | { 29 | if(root==NULL) return 0; 30 | 31 | int l = helperFunc(root->left); 32 | int r = helperFunc(root->right); 33 | 34 | if(l==-1||r==-1||abs(l-r)>1) return -1; 35 | 36 | return (max(l,r)+1); 37 | } 38 | 39 | bool isBalanced(TreeNode* root) { 40 | if(root==NULL) return true; 41 | 42 | if(helperFunc(root)>-1) return true; 43 | return false; 44 | 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /Leetcode/Company-Wise/Intuit/Medium/Word-Search.cpp: -------------------------------------------------------------------------------- 1 | //PROBLEM : https://leetcode.com/problems/word-search/ 2 | 3 | class Solution { 4 | public: 5 | 6 | bool dfs(vector>& board,int count, int i, int j, int m, int n, string& word) 7 | { 8 | if(word.size()==count) return true; 9 | if(i<0 || i>=m || j<0 || j>=n || board[i][j]!=word[count]) return false; 10 | 11 | char c = board[i][j]; 12 | board[i][j] = ' '; 13 | bool res = dfs(board, count+1, i+1,j,m,n,word) || dfs(board,count+1,i-1,j,m,n,word) || dfs(board, count+1,i,j+1,m,n,word) || dfs(board, count+1,i,j-1,m,n,word); 14 | board[i][j] = c; 15 | return res; 16 | } 17 | 18 | 19 | bool exist(vector>& board, string word) { 20 | if(board.empty()) return false; 21 | int m = board.size(); 22 | int n = board[0].size(); 23 | for(int i=0;i>1; 28 | } 29 | return count; 30 | } 31 | 32 | 33 | int findComplement(int num) { 34 | int len = length(num)-1; 35 | int test = 0; 36 | for(int i=0;i<=len;i++) 37 | { 38 | test = test+pow(2,i); 39 | } 40 | int res = num^test; 41 | return res; 42 | 43 | } 44 | }; 45 | 46 | -------------------------------------------------------------------------------- /Leetcode/Company-Wise/Intuit/Medium/Max-Area-Island.cpp: -------------------------------------------------------------------------------- 1 | //PROBLEM : https://leetcode.com/problems/max-area-of-island/ 2 | 3 | 4 | 5 | class Solution { 6 | public: 7 | 8 | 9 | void DFSrec(int i, int j, int m,int n, vector>& grid, int &currarea) 10 | { 11 | if(i<0 || j<0 || i>=m || j>=n || grid[i][j]!=1) 12 | return; 13 | currarea++; 14 | grid[i][j] = 2; 15 | DFSrec(i+1,j,m,n,grid,currarea); 16 | DFSrec(i-1,j,m,n,grid,currarea); 17 | DFSrec(i,j-1,m,n,grid,currarea); 18 | DFSrec(i,j+1,m,n,grid,currarea); 19 | } 20 | 21 | 22 | int maxAreaOfIsland(vector>& grid) { 23 | int m = grid.size(); 24 | int n = grid[0].size(); 25 | int area = 0; 26 | for(int i=0;i findOrder(int numCourses, vector>& prerequisites) { 6 | vector res; 7 | int n = numCourses; 8 | vector> graph(n,vector()); 9 | vector indegree(n,0); 10 | 11 | for(auto p: prerequisites) { 12 | graph[p[1]].push_back(p[0]); 13 | ++indegree[p[0]]; 14 | } 15 | 16 | queue q; 17 | for(int i=0;i(); 38 | 39 | 40 | 41 | } 42 | }; -------------------------------------------------------------------------------- /Leetcode/Company-Wise/Intuit/Easy/Longest_Palindrome.cpp: -------------------------------------------------------------------------------- 1 | // Problem: https://leetcode.com/problems/longest-palindrome/ 2 | 3 | 4 | class Solution { 5 | public: 6 | int longestPalindrome(string str) { 7 | unordered_map mp; 8 | int n=str.length(); 9 | for(int i=0;i=0) 16 | { 17 | mp[str[i]-97+26]++; 18 | } 19 | } 20 | int oddcount = 0; 21 | int evecount = 0; 22 | bool check = false; 23 | for(auto it : mp) 24 | { 25 | 26 | if(it.second%2!=0) 27 | { 28 | if(it.second==1 && (!check)) 29 | { 30 | check = true; 31 | oddcount++; 32 | } 33 | oddcount+=(it.second)-1; 34 | } 35 | else if(it.second%2==0) evecount+=it.second; 36 | } 37 | if(oddcount>0 && (!check)) oddcount++; 38 | int ans =oddcount+evecount; 39 | return ans; 40 | } 41 | }; -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Count-Nodes-In-Complete-BT.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given the root of a complete binary tree, return the number of the nodes in the tree. 3 | 4 | According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far 5 | left as possible. It can have between 1 and 2h nodes inclusive at the last level h. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: root = [1,2,3,4,5,6] 13 | Output: 6 14 | Example 2: 15 | 16 | Input: root = [] 17 | Output: 0 18 | Example 3: 19 | 20 | Input: root = [1] 21 | Output: 1 22 | 23 | Complexity: O(logN*logN) 24 | */ 25 | 26 | class Solution { 27 | public: 28 | int countNodes(TreeNode* root) { 29 | int lh = 0,rh=0; 30 | if (root==NULL) return 0; 31 | TreeNode* curr = root; 32 | 33 | while(curr!=NULL) 34 | { 35 | lh++; 36 | curr=curr->left; 37 | } 38 | curr = root; 39 | while(curr!=NULL) 40 | { 41 | rh++; 42 | curr=curr->right; 43 | } 44 | 45 | if(lh==rh) return (pow(2,lh)-1); 46 | 47 | return 1+countNodes(root->left)+countNodes(root->right); 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /Leetcode/Company-Wise/Intuit/Easy/Min-Stack(Time-O(1)).cpp: -------------------------------------------------------------------------------- 1 | //Problem: https://leetcode.com/problems/min-stack/ 2 | 3 | 4 | class MinStack { 5 | public: 6 | /** initialize your data structure here. */ 7 | 8 | stack st; 9 | stack minstack; 10 | 11 | void push(int val) { 12 | if(st.empty() && minstack.empty()) 13 | { 14 | st.push(val); 15 | minstack.push(val); 16 | } 17 | else 18 | { 19 | int currmin = minstack.top(); 20 | if(currmin>=val) minstack.push(val); 21 | st.push(val); 22 | } 23 | } 24 | 25 | void pop() { 26 | if(st.top()==minstack.top()) 27 | { 28 | minstack.pop(); 29 | } 30 | st.pop(); 31 | } 32 | 33 | int top() { 34 | int n = st.top(); 35 | return n; 36 | } 37 | 38 | int getMin() { 39 | int mini = minstack.top(); 40 | return mini; 41 | 42 | } 43 | }; 44 | 45 | /** 46 | * Your MinStack object will be instantiated and called as such: 47 | * MinStack* obj = new MinStack(); 48 | * obj->push(val); 49 | * obj->pop(); 50 | * int param_3 = obj->top(); 51 | * int param_4 = obj->getMin(); 52 | */ -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Binary-Search-Tree/LCA.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Binary Search Tree (with all values unique) and two node values. Find the Lowest Common Ancestors of the two nodes in the BST. 3 | 4 | Example 1: 5 | 6 | Input: 7 | 5 8 | / \ 9 | 4 6 10 | / \ 11 | 3 7 12 | \ 13 | 8 14 | n1 = 7, n2 = 8 15 | Output: 7 16 | Example 2: 17 | 18 | Input: 19 | 2 20 | / \ 21 | 1 3 22 | n1 = 1, n2 = 3 23 | Output: 2 24 | Your Task: 25 | You don't need to read input or print anything. Your task is to complete the function LCA() which takes the root Node of the BST and two integer values 26 | n1 and n2 as inputs and returns the Lowest Common Ancestor of the Nodes with values n1 and n2 in the given BST. 27 | 28 | Expected Time Complexity: O(Height of the BST). 29 | Expected Auxiliary Space: O(Height of the BST). 30 | */ 31 | Node* LCA(Node *root, int n1, int n2) 32 | { 33 | //Your code here 34 | if(root==NULL) return NULL; 35 | if(root->data >n1 && root->data>n2) 36 | { 37 | return LCA(root->left,n1,n2); 38 | } 39 | if(root->datadataright,n1,n2); 42 | } 43 | return root; 44 | } 45 | -------------------------------------------------------------------------------- /Leetcode/Easy/Bit-Manipulation/Sorting-by-count-1s.cpp: -------------------------------------------------------------------------------- 1 | /*Given an integer array arr. You have to sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order. 2 | 3 | Return the sorted array. 4 | 5 | Example 1: 6 | 7 | Input: arr = [0,1,2,3,4,5,6,7,8] 8 | Output: [0,1,2,4,8,3,5,6,7] 9 | Explantion: [0] is the only integer with 0 bits. 10 | [1,2,4,8] all have 1 bit. 11 | [3,5,6] have 2 bits. 12 | [7] has 3 bits. 13 | The sorted array by bits is [0,1,2,4,8,3,5,6,7] 14 | 15 | Example 2: 16 | 17 | Input: arr = [1024,512,256,128,64,32,16,8,4,2,1] 18 | Output: [1,2,4,8,16,32,64,128,256,512,1024] 19 | Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order. 20 | */ 21 | 22 | class Solution { 23 | public: 24 | 25 | static bool compare(const int& a, const int& b){ 26 | int c1 = __builtin_popcount(a); 27 | int c2 = __builtin_popcount(b); 28 | if(c1 == c2) 29 | return a < b; 30 | return c1 < c2; 31 | } 32 | vector sortByBits(vector& arr) { 33 | std::sort(arr.begin(),arr.end(), compare); 34 | return arr; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Maximum-AND-Value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | 5 | 6 | // Utility function to check number of elements 7 | // having set msb as of pattern 8 | int checkBit(int pattern, int arr[], int n) 9 | { 10 | int count = 0; 11 | for (int i = 0; i < n; i++) 12 | if ((pattern & arr[i]) == pattern) 13 | count++; 14 | return count; 15 | } 16 | 17 | // Function for finding maximum and value pair 18 | int maxAND (int arr[], int n) 19 | { 20 | int res = 0, count; 21 | 22 | // iterate over total of 30bits from msb to lsb 23 | for (int bit = 31; bit >= 0; bit--) 24 | { 25 | // find the count of element having set msb 26 | count = checkBit(res | (1 << bit),arr,n); 27 | 28 | // if count >= 2 set particular bit in result 29 | if ( count >= 2 ) 30 | res |= (1 << bit); 31 | } 32 | 33 | return res; 34 | } 35 | 36 | 37 | 38 | 39 | int main() 40 | { 41 | //code 42 | int t; 43 | cin>>t; 44 | while(t--) 45 | { 46 | int n; 47 | cin>>n; 48 | int arr[n]; 49 | for(int i=0;i>arr[i]; 52 | } 53 | cout<> buildGraph(int N, vector>& prerequisites) 7 | { 8 | vector> graph(N); 9 | for (auto pre : prerequisites) 10 | graph[pre.second].insert(pre.first); 11 | return graph; 12 | } 13 | 14 | bool dfsCycle(vector>& graph, int curr, vector& onpath, vector& visited) 15 | { 16 | if(visited[curr]) return false; 17 | onpath[curr] = true; 18 | visited[curr] = true; 19 | for(int i : graph[curr]) 20 | { 21 | if(onpath[i] || dfsCycle(graph, i, onpath, visited)) return true; 22 | } 23 | 24 | return onpath[curr] = false;; 25 | } 26 | 27 | 28 | bool isPossible(int N, vector >& prerequisites) { 29 | // Code here 30 | vector> graph = buildGraph(N, prerequisites); 31 | vector onpath(N, false), visited(N, false); 32 | for(int i=0;i& nums) { 18 | int n = nums.size(); 19 | for(int i=0;i mp; 27 | int maxlen=0,currsum=0; 28 | for(int i=0;idata; 45 | int res=-1; 46 | while(root!=NULL) 47 | { 48 | if(root->data == key) return key; 49 | else if(root->data>key) root=root->left; 50 | else 51 | { 52 | res = root->data; 53 | root=root->right; 54 | } 55 | } 56 | return res; 57 | 58 | } 59 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Binary-Search-Tree/Ceil-In-BST(SAMSUNG).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a BST and a number X, find Ceil of X. 3 | Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. 4 | 5 | Example 1: 6 | 7 | Input: 8 | 5 9 | / \ 10 | 1 7 11 | \ 12 | 2 13 | \ 14 | 3 15 | X = 3 16 | Output: 3 17 | Explanation: We find 3 in BST, so ceil 18 | of 3 is 3. 19 | Example 2: 20 | 21 | Input: 22 | 10 23 | / \ 24 | 5 11 25 | / \ 26 | 4 7 27 | \ 28 | 8 29 | X = 6 30 | Output: 7 31 | Explanation: We find 7 in BST, so ceil 32 | of 6 is 7. 33 | Your task: 34 | You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the 35 | given BST. 36 | 37 | Expected Time Complexity: O(Height of the BST) 38 | Expected Auxiliary Space: O(Height of the BST). 39 | */ 40 | 41 | int findCeil(Node* root, int input) 42 | { 43 | // Base case 44 | if (root == NULL) 45 | return -1; 46 | int res = -1; 47 | while(root!=NULL) 48 | { 49 | if(root->data==input) return input; 50 | else if(root->dataright; 51 | else 52 | { 53 | if(res==-1) res = root->data; 54 | else res = min(res,root->data); 55 | root=root->left; 56 | } 57 | } 58 | return res; 59 | } 60 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Heap/Nearly-Sorted(AMAZON).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of n elements, where each element is at most k away from its target position, you need to sort the array optimally. 3 | 4 | Example 1: 5 | 6 | Input: 7 | n = 7, k = 3 8 | arr[] = {6,5,3,2,8,10,9} 9 | Output: 2 3 5 6 8 9 10 10 | Explanation: The sorted array will be 11 | 2 3 5 6 8 9 10 12 | Example 2: 13 | 14 | Input: 15 | n = 5, k = 2 16 | arr[] = {4,3,1,2,5} 17 | Output: 1 2 3 4 5 18 | Note: DO NOT use STL sort() function for this question. 19 | 20 | Your Task: 21 | You are required to complete the method nearlySorted() which takes 3 arguments and returns the sorted array. 22 | 23 | Expected Time Complexity : O(nlogk) 24 | Expected Auxilliary Space : O(n) 25 | */ 26 | 27 | class Solution{ 28 | public: 29 | vector nearlySorted(int arr[], int n, int k){ 30 | // Your code here 31 | priority_queue, greater> pq; 32 | vector res(n,0); 33 | for(int i=0;i 7 | using namespace std; 8 | 9 | // Returns number of ways 10 | // to reach s'th stair 11 | long long countWays(int n, int m) 12 | { 13 | long long res[n + 1]; 14 | long long temp = 0; 15 | res[0] = 1; 16 | for (int i = 1; i <= n; i++) 17 | { 18 | long long s = i - m - 1; 19 | long long e = i - 1; 20 | if (s >= 0) 21 | { 22 | temp -= res[s]; 23 | } 24 | temp += res[e]; 25 | res[i] = temp; 26 | } 27 | return res[n]; 28 | } 29 | 30 | // Driver Code 31 | int main() 32 | { 33 | int n = 84, m = 2; 34 | cout << "Number of ways = " 35 | << countWays(n, m); 36 | return 0; 37 | } 38 | 39 | //Specific sol 40 | 41 | class Solution 42 | { 43 | public: 44 | //Function to count number of ways to reach the nth stair. 45 | 46 | int countWays(int n) 47 | { 48 | // your code here 49 | 50 | if(n<=1) return 1; 51 | int mod = 1000000007; 52 | int a = 1, b= 1, temp; 53 | for(int i=2;i<=n;i++) 54 | { 55 | temp=(a+b)%mod; 56 | a = b; 57 | b = temp; 58 | } 59 | return b; 60 | } 61 | }; 62 | -------------------------------------------------------------------------------- /Leetcode/Easy/Bit-Manipulation/Reduce-number-to-0.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. 3 | 4 | Example 1: 5 | 6 | Input: num = 14 7 | Output: 6 8 | Explanation: 9 | Step 1) 14 is even; divide by 2 and obtain 7. 10 | Step 2) 7 is odd; subtract 1 and obtain 6. 11 | Step 3) 6 is even; divide by 2 and obtain 3. 12 | Step 4) 3 is odd; subtract 1 and obtain 2. 13 | Step 5) 2 is even; divide by 2 and obtain 1. 14 | Step 6) 1 is odd; subtract 1 and obtain 0. 15 | 16 | Example 2: 17 | 18 | Input: num = 8 19 | Output: 4 20 | Explanation: 21 | Step 1) 8 is even; divide by 2 and obtain 4. 22 | Step 2) 4 is even; divide by 2 and obtain 2. 23 | Step 3) 2 is even; divide by 2 and obtain 1. 24 | Step 4) 1 is odd; subtract 1 and obtain 0. 25 | 26 | OPTIMIZED TIme: O(logN) 27 | 28 | */ 29 | 30 | class Solution { 31 | public: 32 | int numberOfSteps (int n) { 33 | int count=0; 34 | while(n!=0) 35 | { 36 | if((n&1)==0 || (n==1)) 37 | { 38 | count++; 39 | n = n>>1; 40 | } 41 | else if((n&1)!=0 && (n!=1)) 42 | { 43 | count += 2; 44 | n = n>>1; 45 | } 46 | } 47 | return count; 48 | 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Maximum-Path-Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree, the task is to find the maximum path sum. The path may start and end at any node in the tree. 3 | 4 | Example 1: 5 | 6 | Input: 7 | 10 8 | / \ 9 | 2 -25 10 | / \ / \ 11 | 20 1 3 4 12 | Output: 32 13 | Explanation: Path in the given tree goes 14 | like 10 , 2 , 20 which gives the max 15 | sum as 32. 16 | Example 2: 17 | 18 | Input: 19 | 10 20 | / \ 21 | 2 5 22 | \ 23 | -2 24 | Output: 17 25 | Explanation: Path in the given tree goes 26 | like 2 , 10 , 5 which gives the max sum 27 | as 17. 28 | Your Task: 29 | You don't need to take input or print anything. Your task is to complete the function findMaxSum() that takes root as input and returns max sum between any 30 | two nodes in the given Binary Tree. 31 | 32 | Expected Time Complexity: O(N). 33 | Expected Auxiliary Space: O(Height of the Tree). 34 | */ 35 | 36 | int maxUtil(Node* root,int& res) 37 | { 38 | if(root==NULL) return 0; 39 | 40 | int l = maxUtil(root->left,res); 41 | int r = maxUtil(root->right,res); 42 | 43 | int s1 = max(max(l,r)+root->data,root->data); 44 | int s2 = max(s1,l+r+root->data); 45 | 46 | res = max(res,s2); 47 | 48 | return s1; 49 | } 50 | 51 | 52 | int findMaxSum(Node* root) 53 | { 54 | // Your code goes here 55 | int res = INT_MIN; 56 | maxUtil(root,res); 57 | return res; 58 | } 59 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Binary-Search-Tree/Closest-Element(AMAZON,MAQ).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. 3 | 4 | Example 1: 5 | 6 | Input: 7 | 10 8 | / \ 9 | 2 11 10 | / \ 11 | 1 5 12 | / \ 13 | 3 6 14 | \ 15 | 4 16 | K = 13 17 | Output: 2 18 | Explanation: K=13. The node that has 19 | value nearest to K is 11. so the answer 20 | is 2 21 | Example 2: 22 | 23 | Input: 24 | 8 25 | / \ 26 | 1 9 27 | \ \ 28 | 4 10 29 | / 30 | 3 31 | K = 9 32 | Output: 0 33 | Explanation: K=9. The node that has 34 | value nearest to K is 9. so the answer 35 | is 0. 36 | Your Task: 37 | You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input 38 | and returns the minimum absolute difference between any node value of the BST and the integer K. 39 | 40 | Expected Time Complexity: O(Height of the BST). 41 | Expected Auxiliary Space: O(Height of the BST). 42 | */ 43 | 44 | int minDiff(Node *root, int K) 45 | { 46 | //Your code here 47 | if(root==NULL) return 0; 48 | int ans = INT_MAX; 49 | while(root!=NULL) 50 | { 51 | ans = min(abs(root->data-K),ans); 52 | if(root->data==K) return 0; 53 | else if(root->dataright; 54 | else if(root->data>K) root=root->left; 55 | } 56 | return ans; 57 | } 58 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Binary-Search-Tree/Node-In-Range(AMAZON,MICROSOFT,FLIPKART).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Binary Search Tree and a range. Find all the numbers in the BST that lie in the given range. 3 | Note: Element greater than or equal to root go to the right side. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: 10 | 17 11 | / \ 12 | 4 18 13 | / \ 14 | 2 9 15 | l = 4, h = 24 16 | Output: 4 9 17 18 17 | Example 2: 18 | 19 | Input: 20 | 16 21 | / \ 22 | 7 20 23 | / \ 24 | 1 10 25 | l = 13, h = 23 26 | Output: 16 20 27 | Your Task: 28 | You don't need to read input or print anything. Your task is to complete the function printNearNodes() which takes the root Node of the BST and the 29 | range elements low and high as inputs and returns an array that contains the BST elements in the given range low to high (inclusive) in non-decreasing order. 30 | 31 | Expected Time Complexity: O(N). 32 | Expected Auxiliary Space: O(Height of the BST). 33 | */ 34 | 35 | void printutil(Node *root, int low, int high, vector &res) 36 | { 37 | if(root==NULL) return; 38 | 39 | if(root->data>low) printutil(root->left,low,high,res); 40 | 41 | if(root->data>=low && root->data<=high) res.push_back(root->data); 42 | 43 | if(root->dataright,low,high,res); 44 | } 45 | vector printNearNodes(Node *root, int low, int high) 46 | { 47 | // your code goes here 48 | vector res; 49 | printutil(root,low,high,res); 50 | return res; 51 | } 52 | -------------------------------------------------------------------------------- /Leetcode/Hard/Binary-Search/Number-of -elements-having-Kzeroes-at-end-of-factorials.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) 3 | 4 | For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. 5 | 6 | Given K, find how many non-negative integers x have the property that f(x) = K. 7 | 8 | Example 1: 9 | Input: K = 0 10 | Output: 5 11 | Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. 12 | 13 | Example 2: 14 | Input: K = 5 15 | Output: 0 16 | Explanation: There is no x such that x! ends in K = 5 zeroes. 17 | 18 | */ 19 | 20 | class Solution { 21 | 22 | long trailingZeroes(long n) 23 | { 24 | long res=0; 25 | while(n>0) 26 | { 27 | res+=n/5; 28 | n/=5; 29 | } 30 | return res; 31 | } 32 | 33 | 34 | public: 35 | int preimageSizeFZF(int K) { 36 | long l = 0; 37 | long long r = 5L*(K+1); 38 | long mid = 0; 39 | while(lK) 48 | { 49 | r = mid-1; 50 | } 51 | else if(k==K) 52 | { 53 | return 5; 54 | } 55 | } 56 | return 0; 57 | } 58 | }; 59 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Diameter-Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Binary Tree, find diameter of it. 3 | The diameter of a tree is the number of nodes on the longest path between two end nodes in the tree. The diagram below shows two trees each with diameter 4 | nine, the leaves that form the ends of a longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than 5 | nine nodes). 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: 12 | 1 13 | / \ 14 | 2 3 15 | Output: 3 16 | Example 2: 17 | 18 | Input: 19 | 10 20 | / \ 21 | 20 30 22 | / \ 23 | 40 60 24 | Output: 4 25 | Your Task: 26 | You need to complete the function diameter() that takes root as parameter and returns the diameter. 27 | 28 | Expected Time Complexity: O(N). 29 | Expected Auxiliary Space: O(Height of the Tree). 30 | */ 31 | 32 | struct Node 33 | { 34 | int data; 35 | struct Node* left; 36 | struct Node* right; 37 | 38 | Node(int x){ 39 | data = x; 40 | left = right = NULL; 41 | } 42 | }; 43 | 44 | /* Computes the diameter of binary tree with given root. */ 45 | 46 | int height(Node* root,int& res) { 47 | 48 | if(root==NULL) return 0; 49 | 50 | int hl = height(root->left,res); 51 | int hr = height(root->right,res); 52 | 53 | res = max(res,hl+hr+1); 54 | 55 | return 1+max(hl,hr); 56 | } 57 | 58 | int diameter(Node* root) 59 | { 60 | int res = 0; 61 | int x = height(root,res); 62 | return res; 63 | } 64 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/SWAP-Nibbles-(ACCOLITE,CISCO,SAMS,QUAL).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,b,rem,count=0,sum=0; 33 | cin>>n; 34 | bitset<8>a(n); 35 | cout<<((a<<4) | (a>>4)).to_ulong()<0) 47 | { 48 | if(exp & 1) 49 | { 50 | result = ((ll)result*base); 51 | } 52 | 53 | base = ((ll)base*base) % MOD; 54 | exp >>= 1; 55 | } 56 | 57 | return result; 58 | } 59 | 60 | 61 | 62 | 63 | int main() 64 | { 65 | int t; 66 | cin>>t; 67 | while(t--) 68 | { 69 | solve(); 70 | } 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Dynamic-Programming/House-Robber(LEETCODE).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from 3 | robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken 4 | into on the same night. 5 | 6 | Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: nums = [1,2,3,1] 13 | Output: 4 14 | Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). 15 | Total amount you can rob = 1 + 3 = 4. 16 | Example 2: 17 | 18 | Input: nums = [2,7,9,3,1] 19 | Output: 12 20 | Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). 21 | Total amount you can rob = 2 + 9 + 1 = 12. 22 | */ 23 | 24 | class Solution { 25 | public: 26 | 27 | int rec(vector& nums, unordered_map& mp, int i) 28 | { 29 | if(i>=nums.size()) return 0; 30 | if(mp.find(i)!=mp.end()) return mp[i]; 31 | int sum1 = nums[i] + rec(nums, mp, i+2); 32 | int sum2 = nums[i] + rec(nums,mp,i+3); 33 | mp[i] = max(sum1,sum2); 34 | return mp[i]; 35 | } 36 | 37 | int rob(vector& nums) { 38 | unordered_map mp; 39 | int sum1=rec(nums,mp,0); 40 | int sum2=rec(nums,mp,1); 41 | return max(sum1,sum2); 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Check-all-bits-are-set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,a,b,rem,count=0,sum=0; 33 | cin>>n; 34 | if((n&(n+1))==0) 35 | { 36 | cout<<"YES"<0) 53 | { 54 | if(exp & 1) 55 | { 56 | result = ((ll)result*base); 57 | } 58 | 59 | base = ((ll)base*base) % MOD; 60 | exp >>= 1; 61 | } 62 | 63 | return result; 64 | } 65 | 66 | 67 | 68 | 69 | int main() 70 | { 71 | int t; 72 | cin>>t; 73 | while(t--) 74 | { 75 | solve(); 76 | } 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Sorting/Three-Way-Partitioning(YAHOO).cpp: -------------------------------------------------------------------------------- 1 | /*Given an array A[] and a range [a, b]. The task is to partition the array around the range such that array is divided into three parts. 2 | 1) All elements smaller than a come first. 3 | 2) All elements in range a to b come next. 4 | 3) All elements greater than b appear in the end. 5 | The individual elements of three sets can appear in any order. You are required to return the modified arranged array. 6 | 7 | Example 1: 8 | 9 | Input: N = 5, A[] = {1, 2, 3, 3, 4} 10 | [a, b] = [1, 2] 11 | Output: {1, 2, 3, 3, 4} 12 | Explanation: One possible arrangement is: 13 | {1, 2, 3, 3, 4}. 14 | Example 2: 15 | 16 | Input: N = 3, A[] = {1, 2, 3} 17 | [a, b] = [1, 3] 18 | Output: 1 2 3 19 | 20 | Your Task: The task is to complete the function threeWayPartition() which should segregate the elements as required. The function returns an array. 21 | 22 | Expected Time Complexity: O(N). 23 | Expected Auxiliary Space: O(1). 24 | */ 25 | 26 | //Only function implemented 27 | 28 | vector threeWayPartition(vector arr, int a, int b) 29 | { 30 | //Your code here 31 | int n = arr.size(); 32 | int l = 0,mid=0,h = n-1; 33 | while(mid<=h) 34 | { 35 | if(arr[mid]=a) && (arr[mid]<=b)) 42 | { 43 | mid++; 44 | } 45 | else if(arr[mid]>b) 46 | { 47 | swap(arr[mid],arr[h]); 48 | h--; 49 | } 50 | } 51 | return arr; 52 | 53 | } 54 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Hashing/Subarray-with-sum-0(PAYTM).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of positive and negative numbers. Find if there is a subarray (of size at-least one) with 0 sum. 3 | 4 | Example 1: 5 | 6 | Input: 7 | 5 8 | 4 2 -3 1 6 9 | 10 | Output: 11 | Yes 12 | 13 | Explanation: 14 | 2, -3, 1 is the subarray 15 | with sum 0. 16 | Example 2: 17 | 18 | Input: 19 | 5 20 | 4 2 0 1 6 21 | 22 | Output: 23 | Yes 24 | 25 | Explanation: 26 | 0 is one of the element 27 | in the array so there exist a 28 | subarray with sum 0. 29 | Your Task: 30 | You only need to complete the function subArrayExists() that takes array and n as parameters and returns true or false depending upon whether there is a 31 | subarray present with 0-sum or not. Printing will be taken care by the drivers code. 32 | 33 | Expected Time Complexity: O(n). 34 | Expected Auxiliary Space: O(n). 35 | */ 36 | 37 | 38 | #include 39 | using namespace std; 40 | 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while(t--) 46 | { 47 | int n; 48 | cin>>n; 49 | int arr[n]; 50 | for(int i=0;i>arr[i]; 52 | if (subArrayExists(arr, n)) 53 | cout << "Yes\n"; 54 | else 55 | cout << "No\n"; 56 | } 57 | return 0; 58 | } 59 | 60 | bool subArrayExists(int arr[], int n) 61 | { 62 | //Your code here 63 | int sum = 0; 64 | unordered_map mp; 65 | mp[0]++; 66 | for(int i=0;i=2) 74 | { 75 | return true; 76 | } 77 | } 78 | return false; 79 | } 80 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/K-Dist-From-Root(AMAZON,MICROSOFT,SAMSUNG).cpp: -------------------------------------------------------------------------------- 1 | /*Given a Binary Tree of size N and an integer K. Print all nodes that are at distance k from root (root is considered at distance 0 from itself). 2 | Nodes should be printed from left to right. If k is more that height of tree, nothing should be printed. 3 | 4 | For example, if below is given tree and k is 2. Output should be 4 5 6. 5 | 6 | 1 7 | / \ 8 | 2 3 9 | / / \ 10 | 4 5 6 11 | \ 12 | 8 13 | 14 | Example 1: 15 | 16 | Input: 17 | K = 0 18 | 1 19 | / \ 20 | 3 2 21 | Output: 1 22 | 23 | Example 2: 24 | 25 | Input: 26 | K = 3 27 | 3 28 | / 29 | 2 30 | \ 31 | 1 32 | / \ 33 | 5 3 34 | Output: 5 3 35 | Your Task: 36 | You don't have to take input. Complete the function Kdistance() that accepts root node and k as parameter and return the value of the nodes 37 | that are at a distance k from the root. 38 | Expected Time Complexity: O(N). 39 | Expected Auxiliary Space: O(Height of the Tree). 40 | */ 41 | 42 | 43 | struct Node 44 | { 45 | int data; 46 | Node* left; 47 | Node* right; 48 | }; 49 | 50 | // function should print the nodes at k distance from root 51 | 52 | void recDist(Node *root, int k, vector &res) 53 | { 54 | if(root==NULL) return; 55 | if(k==0) res.push_back(root->data); 56 | recDist(root->left,k-1,res); 57 | recDist(root->right,k-1,res); 58 | } 59 | 60 | 61 | vector Kdistance(struct Node *root, int k) 62 | { 63 | // Your code here 64 | vector res; 65 | recDist(root,k,res); 66 | return res; 67 | } 68 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Foldable-BT.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree, check if the tree can be folded or not. A tree can be folded if left and right subtrees of the tree are structure wise same. 3 | An empty tree is considered as foldable. 4 | Consider the below trees: 5 | (a) and (b) can be folded. 6 | (c) and (d) cannot be folded. 7 | 8 | 9 | (a) 10 | 10 11 | / \ 12 | 7 15 13 | \ / 14 | 9 11 15 | (b) 16 | 10 17 | / \ 18 | 7 15 19 | / \ 20 | 9 11 21 | (c) 22 | 10 23 | / \ 24 | 7 15 25 | / / 26 | 5 11 27 | (d) 28 | 10 29 | / \ 30 | 7 15 31 | / \ / 32 | 9 10 12 33 | 34 | Example 1: 35 | 36 | Input: 37 | 10 38 | / \ 39 | 7 15 40 | / \ / \ 41 | N 9 11 N 42 | Output:Yes 43 | Example 2: 44 | 45 | Input: 46 | 10 47 | / \ 48 | 7 15 49 | / \ / \ 50 | 5 N 11 N 51 | Output: No 52 | 53 | Your Task: 54 | The task is to complete the function isFoldable() that takes root of the tree as input and returns true or false depending upon whether the tree is 55 | foldable or not. 56 | 57 | Expected Time Complexity: O(N). 58 | Expected Auxiliary Space: O(Height of the Tree). 59 | */ 60 | 61 | bool foldableUtil(Node* n1, Node* n2) 62 | { 63 | if(n1==NULL && n2==NULL) return true; 64 | if(n1==NULL || n2==NULL) return false; 65 | return(foldableUtil(n1->left,n2->right) && foldableUtil(n1->right,n2->left)); 66 | } 67 | 68 | bool IsFoldable(Node* root) 69 | { 70 | // Your code goes here 71 | if(root==NULL) return true; 72 | return foldableUtil(root->left,root->right); 73 | } 74 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Swap-every-two-bits-in-bytes(CISCO).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,b,rem,count=0,sum=0,temp; 33 | cin>>n; 34 | bitset<8>a(n); 35 | fo(i,7) 36 | { 37 | temp = a[i]; 38 | a[i] = a[i+1]; 39 | a[i+1] = temp; 40 | i++; 41 | } 42 | cout<0) 54 | { 55 | if(exp & 1) 56 | { 57 | result = ((ll)result*base); 58 | } 59 | 60 | base = ((ll)base*base) % MOD; 61 | exp >>= 1; 62 | } 63 | 64 | return result; 65 | } 66 | 67 | 68 | 69 | 70 | int main() 71 | { 72 | int t; 73 | cin>>t; 74 | while(t--) 75 | { 76 | solve(); 77 | } 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Leetcode/Top-Interview-Questions/Easy/Reverse-Bits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse bits of a given 32 bits unsigned integer. 3 | 4 | 5 | 6 | Example 1: 7 | 8 | Input: 00000010100101000001111010011100 9 | Output: 00111001011110000010100101000000 10 | Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000. 11 | 12 | Example 2: 13 | 14 | Input: 11111111111111111111111111111101 15 | Output: 10111111111111111111111111111111 16 | Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111. 17 | 18 | 19 | APPROACH: 20 | 21 | The key idea is that for a bit that is situated at the index i, after the reversion, its position should be 31-i (note: the index starts from zero). 22 | 23 | 1. We iterate through the bit string of the input integer, from right to left (i.e. n = n >> 1). To retrieve the right-most bit of an integer, we apply the bit AND operation (n & 1). 24 | 2. For each bit, we reverse it to the correct position (i.e. (n & 1) << power). Then we accumulate this reversed bit to the final result. 25 | 3. When there is no more bits of one left (i.e. n == 0), we terminate the iteration. 26 | 27 | Complexity: O(1) 28 | 29 | */ 30 | 31 | class Solution { 32 | public: 33 | uint32_t reverseBits(uint32_t n) { 34 | int ret = 0,power=31; 35 | while(n!=0) 36 | { 37 | ret +=(n&1)<>1; 39 | power = power-1; 40 | } 41 | return ret; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Binary-Search-Tree/Check-BST(IMP).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree. Check whether it is a BST or not. 3 | Note: We are considering that BSTs can not contain duplicate Nodes. 4 | 5 | Example 1: 6 | 7 | Input: 8 | 2 9 | / \ 10 | 1 3 11 | Output: 1 12 | Explanation: 13 | The left subtree of root node contains node 14 | with key lesser than the root node’s key and 15 | the right subtree of root node contains node 16 | with key greater than the root node’s key. 17 | Hence, the tree is a BST. 18 | Example 2: 19 | 20 | Input: 21 | 2 22 | \ 23 | 7 24 | \ 25 | 6 26 | \ 27 | 5 28 | \ 29 | 9 30 | \ 31 | 2 32 | \ 33 | 6 34 | Output: 0 35 | Explanation: 36 | Since the node with value 7 has right subtree 37 | nodes with keys less than 7, this is not a BST. 38 | Your Task: 39 | You don't need to read input or print anything. Your task is to complete the function isBST() which takes the root of the tree as a parameter and returns 40 | true if the given binary tree is BST, else returns false. 41 | 42 | Expected Time Complexity: O(N). 43 | Expected Auxiliary Space: O(Height of the BST). 44 | */ 45 | 46 | class Solution 47 | { 48 | public: 49 | 50 | bool util(Node *root,int min,int max) 51 | { 52 | if(root==NULL) return true; 53 | if(root->datadata>max) return false; 54 | 55 | return(util(root->left,min,root->data-1)&&util(root->right,root->data+1,max)); 56 | } 57 | 58 | 59 | bool isBST(Node* root) 60 | { 61 | if(util(root,INT_MIN,INT_MAX)) return true; 62 | return false; 63 | } 64 | }; 65 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Heap/Heap-Operations(VVIP).cpp: -------------------------------------------------------------------------------- 1 | void MinHeap::MinHeapify(int i) { 2 | int l = left(i); 3 | int r = right(i); 4 | int smallest = i; 5 | if (l < heap_size && harr[l] < harr[i]) smallest = l; 6 | if (r < heap_size && harr[r] < harr[smallest]) smallest = r; 7 | if (smallest != i) { 8 | swap(harr[i], harr[smallest]); 9 | MinHeapify(smallest); 10 | } 11 | } 12 | 13 | int MinHeap::extractMin() { 14 | // Your code here 15 | if(heap_size<=0) 16 | return -1; 17 | 18 | if(heap_size==1) 19 | { 20 | heap_size--; 21 | return harr[0]; 22 | } 23 | swap(harr[0],harr[heap_size-1]); 24 | heap_size--; 25 | MinHeapify(0); 26 | return harr[heap_size]; 27 | } 28 | 29 | /* Removes element from position x in the min heap */ 30 | void MinHeap::deleteKey(int i) { 31 | // Your code here 32 | //cout<harr[i]) 48 | { 49 | swap(harr[i], harr[parent(i)]); 50 | i = parent(i); 51 | } 52 | } 53 | 54 | // Decrease Key operation, helps in deleting key from heap 55 | void MinHeap::decreaseKey(int i, int new_val) { 56 | harr[i] = new_val; 57 | while (i != 0 && harr[parent(i)] > harr[i]) { 58 | swap(harr[i], harr[parent(i)]); 59 | i = parent(i); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Graphs/Check-Cycle-Directed(TopoSort-Kahns-BFS).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Directed Graph with V vertices and E edges, check whether it contains any cycle or not. 3 | Expected Time Complexity: O(V + E) 4 | Expected Auxiliary Space: O(V) 5 | */ 6 | 7 | 8 | #include 9 | using namespace std; 10 | 11 | class Solution { 12 | public: 13 | bool isCyclic(int V, vector adj[]) { 14 | int count=0; 15 | vector indegree(V,0); 16 | for(int i=0;i q; 25 | for(int i=0;i> t; 63 | while(t--) 64 | { 65 | int V, E; 66 | cin >> V >> E; 67 | 68 | vector adj[V]; 69 | 70 | for(int i = 0; i < E; i++) 71 | { 72 | int u, v; 73 | cin >> u >> v; 74 | adj[u].push_back(v); 75 | } 76 | 77 | Solution obj; 78 | cout << obj.isCyclic(V, adj) << "\n"; 79 | } 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Graphs/Check-Cycle-Undirected(DFS).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an undirected graph with V vertices and E edges, check whether it contains any cycle or not. 3 | Expected Time Complexity: O(V + E) 4 | Expected Space Complexity: O(V) 5 | */ 6 | 7 | 8 | #include 9 | using namespace std; 10 | 11 | class Solution { 12 | public: 13 | 14 | bool DFSrec(vector adj[],int s, bool visited[], int parent) 15 | { 16 | visited[s] = true; 17 | 18 | for(int u:adj[s]) 19 | { 20 | if(!visited[u]) 21 | { 22 | if(DFSrec(adj,u,visited,s)) 23 | { 24 | return true; 25 | } 26 | } 27 | else if(u!=parent) 28 | { 29 | return true; 30 | } 31 | } 32 | return false; 33 | } 34 | 35 | 36 | bool isCycle(int V, vectoradj[]){ 37 | // Code here 38 | bool visited[V]; 39 | memset(visited, false, V); 40 | for(int i=0;i> tc; 57 | while(tc--){ 58 | int V, E; 59 | cin >> V >> E; 60 | vectoradj[V]; 61 | for(int i = 0; i < E; i++){ 62 | int u, v; 63 | cin >> u >> v; 64 | adj[u].push_back(v); 65 | adj[v].push_back(u); 66 | } 67 | Solution obj; 68 | bool ans = obj.isCycle(V, adj); 69 | if(ans) 70 | cout << "1\n"; 71 | else cout << "0\n"; 72 | } 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Hashing/Count-Subarray-with-equal-0-and-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array containing 0s and 1s. Find the number of subarrays having equal number of 0s and 1s. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 7 8 | A[] = {1,0,0,1,0,1,1} 9 | Output: 8 10 | Explanation: The index range for the 8 11 | sub-arrays are: (0, 1), (2, 3), (0, 3), (3, 4), 12 | (4, 5) ,(2, 5), (0, 5), (1, 6) 13 | Example 2: 14 | 15 | Input: 16 | N = 5 17 | A[] = {1,1,1,1,0} 18 | Output: 1 19 | Explanation: The index range for the 20 | subarray is (3,4). 21 | Your Task: 22 | You don't need to read input or print anything. Your task is to complete the function countSubarrWithEqualZeroAndOne() which takes the array arr[] and the 23 | size of the array as inputs and returns the number of subarrays with equal number of 0s and 1s. 24 | 25 | Expected Time Complexity: O(N). 26 | Expected Auxiliary Space: O(N). 27 | 28 | */ 29 | 30 | 31 | #include 32 | using namespace std; 33 | 34 | int main() 35 | { 36 | int t; 37 | cin>>t; 38 | while(t--) 39 | { 40 | int n,i; 41 | cin>>n; 42 | int arr[n]; 43 | for(i=0;i>arr[i]; 45 | cout<< countSubarrWithEqualZeroAndOne(arr, n)<<"\n"; 46 | } 47 | return 0; 48 | } 49 | 50 | long long int countSubarrWithEqualZeroAndOne(int arr[], int n) 51 | { 52 | //Your code here 53 | unordered_map mp; 54 | int sum=0,count=0; 55 | for(int i=0;idata==n1 || root->data == n2) return root; 60 | 61 | Node* lca1 = lca(root->left,n1,n2); 62 | Node* lca2 = lca(root->right,n1,n2); 63 | 64 | if(lca1!=NULL && lca2!=NULL) return root; 65 | 66 | if(lca1!=NULL) return lca1; 67 | 68 | else return lca2; 69 | 70 | } 71 | -------------------------------------------------------------------------------- /STL_DEMO/Front-to-back.cpp: -------------------------------------------------------------------------------- 1 | /*Iterators are used to iterate over vectors, maps, sets etc. Here we will learn to iterate over a vector from begin to end. 2 | You are given a vector V of size n. You need print its elements. 3 | 4 | Input Format: 5 | The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line contains n denoting the size of the array. The second line contains the elements of the vector. 6 | 7 | Output Format: 8 | For each testcase, in a new line, print the required output. 9 | 10 | Your Task: 11 | Since this is a function problem, you don't need to take any input. Just complete the function iter() that takes two iterators as parameters. 12 | 13 | Constraints: 14 | 1 <= T <= 100 15 | 1 <= n <= 107 16 | 0 <= Vi <= 107 17 | 18 | Examples: 19 | Input: 20 | 1 21 | 5 22 | 1 2 3 4 5 23 | Output: 24 | 1 2 3 4 5 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | void iter(vector::iterator it1,vector::iterator it2); 33 | 34 | void iter(vector::iterator it1,vector::iterator it2) 35 | { 36 | //Your code here. Use iterator to print all the elements 37 | for(auto x=it1;x!=it2;x++) 38 | { 39 | cout<<*x<<" "; //print the element at xth address 40 | } 41 | cout<>t; 47 | while(t--) 48 | { 49 | int n; 50 | cin>>n; 51 | vectorv; 52 | for(int i=0;i>x; 56 | v.push_back(x); 57 | } 58 | 59 | iter(v.begin(),v.end()); //passing begin and end addresses of vector to the function 60 | 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/MaxSum-Non-Adjacent-Nodes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree with a value associated with each node, we need to choose a subset of these nodes such that sum of chosen nodes is maximum under a constraint 3 | that no two chosen node in subset should be directly connected that is, if we have taken a node in our sum then we can’t take its any children in consideration 4 | and vice versa. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | Input: 11 | 11 12 | / \ 13 | 1 2 14 | Output: 11 15 | Explanation: The maximum sum is sum of 16 | node 11. 17 | Example 2: 18 | 19 | Input: 20 | 1 21 | / \ 22 | 2 3 23 | / / \ 24 | 4 5 6 25 | Output: 16 26 | Explanation: The maximum sum is sum of 27 | nodes 1 4 5 6 , i.e 16. These nodes are 28 | non adjacent. 29 | Your Task: 30 | You don't need to read input or print anything. You just have to complete function getMaxSum() which accepts root node of the tree as parameter and returns the 31 | maximum sum as described. 32 | 33 | Expected Time Complexity: O(N). 34 | Expected Auxiliary Space: O(Height of the Tree). 35 | */ 36 | 37 | pair sumUtil(Node* root) 38 | { 39 | if(root==NULL) 40 | { 41 | pair sum(0,0); 42 | return sum; 43 | } 44 | pair sum1 = sumUtil(root->left); 45 | pair sum2 = sumUtil(root->right); 46 | pair sum; 47 | sum.first = sum1.second+sum2.second+root->data; 48 | sum.second = max(sum1.first,sum1.second)+max(sum2.first,sum2.second); 49 | return sum; 50 | } 51 | 52 | 53 | int getMaxSum(Node *root) 54 | { 55 | // Add your code here 56 | pair res = sumUtil(root); 57 | return max(res.first,res.second); 58 | } 59 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Heap/Kth-Largest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array arr[] of N positive integers and a number K. The task is to find the kth largest element in the array. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 5, K = 3 8 | arr[] = {3, 5, 4, 2, 9} 9 | Output: 10 | 4 11 | Explanation: 12 | Third largest element 13 | in the array is 4. 14 | Example 2: 15 | 16 | Input: 17 | N = 5, K = 5 18 | arr[] = {4, 3, 7, 6, 5} 19 | Output: 20 | 3 21 | Explanation: 22 | Fifth largest element 23 | in the array is 3. 24 | Your Task: 25 | You are required to complete the method KthLargest() which takes 3 arguments and returns the Kth Largest element. 26 | 27 | Constraints: 28 | 1 <= N <= 106 29 | 1 <= arr[i] <= 106 30 | 1 <= K <= N 31 | 32 | Expected Time Complexity : O(NlogK) 33 | Expected Auxilliary Space : O(K) 34 | */ 35 | 36 | class Solution{ 37 | 38 | 39 | public: 40 | void maxHeapify(int arr[],int n,int i) 41 | { 42 | int smallest=i,l=2*i+1,r=2*i+2; 43 | if(larr[l]) smallest = l; 44 | if(rarr[r]) smallest = r; 45 | if(smallest!=i) 46 | { 47 | swap(arr[i],arr[smallest]); 48 | maxHeapify(arr,n,smallest); 49 | } 50 | 51 | } 52 | 53 | public: 54 | void buildHeap(int arr[],int n) 55 | { 56 | for(int i=n/2-1;i>=0;i--) 57 | { 58 | maxHeapify(arr,n,i); 59 | } 60 | } 61 | 62 | 63 | public: 64 | int KthLargest(int arr[], int n, int k) { 65 | // Your code here 66 | buildHeap(arr,n); 67 | for(int i=n-1;i>0;i--) 68 | { 69 | swap(arr[0],arr[i]); 70 | maxHeapify(arr,i,0); 71 | } 72 | return arr[k-1]; 73 | } 74 | }; 75 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Convert-BT-to-DLL(AMAZON,GS,MICROSOFT,SLAESFORCE,MORG-STAN).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes are to be used as previous and next 3 | pointers respectively in converted DLL. The order of nodes in DLL must be same as Inorder of the given Binary Tree. The first node of Inorder traversal 4 | (leftmost node in BT) must be the head node of the DLL. 5 | 6 | TreeToList 7 | 8 | Example 1: 9 | 10 | Input: 11 | 1 12 | / \ 13 | 3 2 14 | Output: 15 | 3 1 2 16 | 2 1 3 17 | Explanation: DLL would be 3<=>1<=>2 18 | Example 2: 19 | 20 | Input: 21 | 10 22 | / \ 23 | 20 30 24 | / \ 25 | 40 60 26 | Output: 27 | 40 20 60 10 30 28 | 30 10 60 20 40 29 | Explanation: DLL would be 30 | 40<=>20<=>60<=>10<=>30. 31 | Your Task: 32 | You don't have to take input. Complete the function bToDLL() that takes root node of the tree as a parameter and returns the head of DLL . 33 | The driver code prints the DLL both ways. 34 | 35 | Expected Time Complexity: O(N). 36 | Expected Auxiliary Space: O(H). 37 | */ 38 | 39 | struct Node 40 | { 41 | int data; 42 | struct Node* left; 43 | struct Node* right; 44 | 45 | Node(int x){ 46 | data = x; 47 | left = right = NULL; 48 | } 49 | }; 50 | 51 | Node * prevv = NULL; 52 | Node * bToDLL(Node *root) 53 | { 54 | // your code here 55 | if(root==NULL) return root; 56 | 57 | Node *head = bToDLL(root->left); 58 | 59 | if(prevv==NULL) 60 | { 61 | head = root; 62 | } 63 | else 64 | { 65 | root->left = prevv; 66 | prevv->right = root; 67 | } 68 | prevv = root; 69 | bToDLL(root->right); 70 | return head; 71 | } 72 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Missing-Ele-1-to-n-(ADO,AMA,MICRO,DES,CIS,INTU..).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,a,b,rem,count=0,sum=0,XOR_ele=0,XOR_n=0,result; 33 | cin>>n; 34 | int arr[n-1]; 35 | fo(i,n-1) 36 | { 37 | cin>>arr[i]; 38 | XOR_ele = XOR_ele ^ arr[i]; 39 | } 40 | fo(i,n+1) 41 | { 42 | XOR_n = XOR_n ^ i; 43 | } 44 | result = XOR_ele ^ XOR_n; 45 | cout<0) 57 | { 58 | if(exp & 1) 59 | { 60 | result = ((ll)result*base); 61 | } 62 | 63 | base = ((ll)base*base) % MOD; 64 | exp >>= 1; 65 | } 66 | 67 | return result; 68 | } 69 | 70 | 71 | 72 | 73 | int main() 74 | { 75 | int t; 76 | cin>>t; 77 | while(t--) 78 | { 79 | solve(); 80 | } 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Decimal-to-Binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j=13,n,m,a,b,rem,count=0,sum=0; 33 | cin>>n; 34 | int arr[14]={0}; 35 | while(n!=0) 36 | { 37 | if(n&1) 38 | { 39 | arr[j]=1; 40 | } 41 | else 42 | { 43 | arr[j]=0; 44 | } 45 | n = n>>1; 46 | j--; 47 | } 48 | fo(i,14) 49 | { 50 | cout<0) 64 | { 65 | if(exp & 1) 66 | { 67 | result = ((ll)result*base); 68 | } 69 | 70 | base = ((ll)base*base) % MOD; 71 | exp >>= 1; 72 | } 73 | 74 | return result; 75 | } 76 | 77 | 78 | 79 | 80 | int main() 81 | { 82 | int t; 83 | cin>>t; 84 | while(t--) 85 | { 86 | solve(); 87 | } 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/XOR-of-subarrays(AMAZON).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n,p) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,a,b,rem,count=0,sum=0,result=0; 33 | cin>>n; 34 | int arr[n]; 35 | fo(i,n,1) 36 | { 37 | cin>>arr[i]; 38 | } 39 | if(n%2==0) 40 | { 41 | cout<<"0"<0) 62 | { 63 | if(exp & 1) 64 | { 65 | result = ((ll)result*base); 66 | } 67 | 68 | base = ((ll)base*base) % MOD; 69 | exp >>= 1; 70 | } 71 | 72 | return result; 73 | } 74 | 75 | 76 | 77 | 78 | int main() 79 | { 80 | int t=1; 81 | cin>>t; 82 | while(t--) 83 | { 84 | solve(); 85 | } 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Graphs/Check-Cycle-DIrected(DFS).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Directed Graph with V vertices and E edges, check whether it contains any cycle or not. 3 | Expected Time Complexity: O(V + E) 4 | Expected Auxiliary Space: O(V) 5 | */ 6 | 7 | 8 | #include 9 | using namespace std; 10 | 11 | class Solution { 12 | 13 | public: 14 | 15 | bool DFSrec(int s,vector &visited,vector &recStack,vector adj[]) 16 | { 17 | visited[s] = true; 18 | recStack[s] = true; 19 | 20 | for(auto i:adj[s]) 21 | { 22 | if(!visited[i] && DFSrec(i,visited,recStack,adj)==true) 23 | { 24 | return true; 25 | } 26 | else if(recStack[i]==true) 27 | { 28 | return true; 29 | } 30 | } 31 | recStack[s] = false; 32 | return false; 33 | } 34 | 35 | 36 | bool isCyclic(int V, vector adj[]) { 37 | vector visited(V, false); 38 | vector recStack(V, false); 39 | 40 | for(int i = 0;i> t; 59 | while(t--) 60 | { 61 | int V, E; 62 | cin >> V >> E; 63 | 64 | vector adj[V]; 65 | 66 | for(int i = 0; i < E; i++) 67 | { 68 | int u, v; 69 | cin >> u >> v; 70 | adj[u].push_back(v); 71 | } 72 | 73 | Solution obj; 74 | cout << obj.isCyclic(V, adj) << "\n"; 75 | } 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Arrays/Longest-Eve-Odd-Subarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Longest Subarray Of Evens And Odds 3 | 4 | You are given an array of size n. Find the maximum possible length of a subarray such that its elements are arranged alternately either as even and odd or 5 | odd and even . 6 | 7 | Example 1: 8 | 9 | Input: 10 | n = 5 11 | a[] = {10,12,14,7,8} 12 | Output: 3 13 | Explanation: The max length of subarray 14 | is 3 and the subarray is {14 7 8}. Here 15 | the array starts as an even element and 16 | has odd and even elements alternately. 17 | 18 | Example 2: 19 | 20 | Input: 21 | n = 2 22 | a[] = {4,6} 23 | Output: 1 24 | Explanation: The array contains {4 6}. 25 | So, we can only choose 1 element as 26 | that will be the max length subarray. 27 | 28 | OPTIMIZED 29 | 30 | TIME: O(N) 31 | SPACE: O(1) 32 | */ 33 | 34 | 35 | #include 36 | using namespace std; 37 | 38 | int maxEvenOdd(int a[], int n) 39 | { 40 | //Your code here 41 | int count = 1,curr_max = 1,i; 42 | for(i=0;icurr_max) 48 | { 49 | curr_max = count; 50 | } 51 | } 52 | else 53 | { 54 | count = 1; 55 | } 56 | } 57 | return curr_max; 58 | } 59 | 60 | 61 | /* Driver program to test maxSubArraySum */ 62 | int main() 63 | { 64 | int testcases; 65 | cin>>testcases; 66 | while(testcases--) 67 | { 68 | int sizeOfArray; 69 | cin>>sizeOfArray; 70 | int arr[sizeOfArray]; 71 | 72 | for(int i=0;i>arr[i]; 75 | } 76 | cout << maxEvenOdd(arr, sizeOfArray)< 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,a,b,rem,count=0,sum=0; 33 | int k,result=0,XOR; 34 | cin>>n>>k; 35 | int copyn = n; 36 | while(n!=0) 37 | { 38 | count++; 39 | n = n>>1; 40 | } 41 | k = count-k; 42 | k = pow(2,k); 43 | XOR = copyn^k; 44 | if(copynXOR) 49 | { 50 | cout<0) 63 | { 64 | if(exp & 1) 65 | { 66 | result = ((ll)result*base); 67 | } 68 | 69 | base = ((ll)base*base) % MOD; 70 | exp >>= 1; 71 | } 72 | 73 | return result; 74 | } 75 | 76 | 77 | 78 | 79 | int main() 80 | { 81 | int t; 82 | cin>>t; 83 | while(t--) 84 | { 85 | solve(); 86 | } 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /GFG-MUST-DO/Graph/Medium/Strongly-Connected-Component(Kosaraju-Algo).cpp: -------------------------------------------------------------------------------- 1 | //Problem: https://practice.geeksforgeeks.org/problems/strongly-connected-components-kosarajus-algo/1 2 | 3 | class Solution 4 | { 5 | public: 6 | //Function to find number of strongly connected components in the graph. 7 | stack order; 8 | 9 | void findOrder(vector adj[], vector& visited, int u) 10 | { 11 | visited[u] = true; 12 | for(auto it : adj[u]) 13 | { 14 | if(!visited[it]) findOrder(adj, visited, it); 15 | } 16 | order.push(u); 17 | } 18 | 19 | 20 | void transpose(vector adj[], vector adjTranspose[], int V) 21 | { 22 | for(int i = 0;i adjTranspose[], vector& visited, int u) 32 | { 33 | visited[u] = true; 34 | for(auto it : adjTranspose[u]) 35 | { 36 | if(!visited[it]) dfs(adjTranspose, visited, it); 37 | } 38 | } 39 | 40 | int kosaraju(int V, vector adj[]) 41 | { 42 | //code here 43 | vector visited(V,false); 44 | for(int i=0;i adjTranspose[V]; 50 | transpose(adj, adjTranspose, V); 51 | int result=0; 52 | for(int i=0;i 41 | using namespace std; 42 | 43 | int main(){ 44 | 45 | int t; 46 | cin >> t; 47 | 48 | while(t--){ 49 | int n; 50 | cin >> n; 51 | 52 | int arr[n]; 53 | 54 | for(int i = 0 ; i < n ; ++ i ) 55 | cin >> arr[i] ; 56 | 57 | cout << firstRepeated(arr,n) << "\n"; 58 | } 59 | 60 | return 0; 61 | } 62 | 63 | 64 | int firstRepeated(int arr[], int n) { 65 | map mp; 66 | for(int i=0;i1) 73 | { 74 | return (i+1); 75 | } 76 | } 77 | return -1; 78 | } 79 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Sum-bit-diff-in-pairs-of-array(GOOGLE).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | ll i,j,n,m,a,b,rem,count=0,sum=0; 33 | cin>>n; 34 | int arr[n]; 35 | fo(i,n) 36 | { 37 | cin>>arr[i]; 38 | } 39 | fo(i,32) 40 | { 41 | count = 0; 42 | fo(j,n) 43 | { 44 | if((arr[j] & (1<0) 63 | { 64 | if(exp & 1) 65 | { 66 | result = ((ll)result*base); 67 | } 68 | 69 | base = ((ll)base*base) % MOD; 70 | exp >>= 1; 71 | } 72 | 73 | return result; 74 | } 75 | 76 | 77 | 78 | 79 | int main() 80 | { 81 | int t; 82 | cin>>t; 83 | while(t--) 84 | { 85 | solve(); 86 | } 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Create-Tree-From-Inorder-Preorder(AMAZON,MICROSOFT).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given 2 Arrays of Inorder and preorder traversal. Construct a tree and print the Postorder traversal. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 4 8 | inorder[] = {1 6 8 7} 9 | preorder[] = {1 6 7 8} 10 | Output: 8 7 6 1 11 | Example 2: 12 | 13 | Input: 14 | N = 6 15 | inorder[] = {3 1 4 0 5 2} 16 | preorder[] = {0 1 3 4 2 5} 17 | Output: 3 4 1 5 2 0 18 | Explanation: The tree will look like 19 | 0 20 | / \ 21 | 1 2 22 | / \ / 23 | 3 4 5 24 | Your Task: 25 | Your task is to complete the function buildTree() which takes 3 arguments(inorder traversal array, preorder traversal array, and size of tree n) and returns the root node to the tree constructed. You are not required to print anything and a new line is added automatically (The post order of the returned tree is printed by the driver's code.) 26 | 27 | Expected Time Complexity: O(N*N). 28 | Expected Auxiliary Space: O(N). 29 | */ 30 | 31 | struct Node 32 | { 33 | int data; 34 | Node* left; 35 | Node* right; 36 | }; 37 | 38 | static int preInd; 39 | Node* buildmain(int in[],int pre[],int start, int end) 40 | { 41 | //static int preInd = 0; 42 | if(start>end) return NULL; 43 | 44 | Node* root = new Node(pre[preInd++]); 45 | if(start==end) return root; 46 | int inInd; 47 | for(int i=start; i<=end;i++) 48 | { 49 | if(in[i]==root->data) 50 | { 51 | inInd = i; 52 | break; 53 | } 54 | } 55 | 56 | root->left = buildmain(in,pre,start, inInd-1); 57 | root->right = buildmain(in,pre,inInd+1,end); 58 | 59 | return root; 60 | } 61 | 62 | 63 | Node* buildTree(int in[],int pre[], int n) 64 | { 65 | preInd = 0; 66 | Node* root = buildmain(in,pre,0,n-1); 67 | return root; 68 | } 69 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Hashing/Count-zero-sum-subarray(AMAZON,MICROSOFT).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given an array A[] of size N. Find the total count of sub-arrays having their sum equal to 0. 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 6 9 | A[] = {0,0,5,5,0,0} 10 | Output: 6 11 | Explanation: The 6 subarrays are 12 | [0], [0], [0], [0], [0,0], and [0,0]. 13 | 14 | Example 2: 15 | 16 | Input: 17 | N = 10 18 | A[] = {6,-1,-3,4,-2,2,4,6,-12,-7} 19 | Output: 4 20 | Explanation: The 4 subarrays are [-1 -3 4] 21 | [-2 2], [2 4 6 -12], and [-1 -3 4 -2 2] 22 | 23 | Your Task: 24 | You don't need to read input or print anything. Complete the function findSubarray() that takes the array A and its size N as input parameters and returns 25 | the total number of sub-arrays with 0 sum. 26 | 27 | 28 | Expected Time Complexity : O(N) 29 | Expected Auxilliary Space : O(N) 30 | */ 31 | 32 | 33 | 34 | #include 35 | using namespace std; 36 | 37 | #define ll long long 38 | ll findSubarray(vector , int ); 39 | 40 | int main() 41 | { 42 | int t; 43 | cin>>t; 44 | while(t--) 45 | { 46 | int n; 47 | cin>>n; //input size of array 48 | 49 | vector arr(n,0); 50 | 51 | for(int i=0;i>arr[i]; //input array elements 53 | 54 | cout << findSubarray(arr,n) << "\n"; 55 | } 56 | return 0; 57 | } 58 | 59 | 60 | 61 | ll findSubarray(vector arr, int n ) { 62 | //code here 63 | unordered_map mp; 64 | int count=0,sum=0; 65 | for(int i=0;i 34 | using namespace std; 35 | 36 | int findLongestConseqSubseq(int arr[], int N) 37 | { 38 | map mp; 39 | for(int i=0;i>t; 67 | while(t--) 68 | { 69 | cin>>n; 70 | for(i=0;i>a[i]; 72 | cout< majorityElement(vector& nums) { 22 | vector res; 23 | int n = nums.size(); 24 | int num1=-1,num2=-1,count1=0,count2=0; 25 | for(int i=0;i(n/3)) 65 | { 66 | res.push_back(num1); 67 | } 68 | if(count2>(n/3)) 69 | { 70 | res.push_back(num2); 71 | } 72 | return res; 73 | 74 | } 75 | }; 76 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Graphs/Prims-Algo-o(n^2).cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | class Solution{ 8 | public: 9 | 10 | int spanningTree(int V, vector> adj[]) { 11 | // code here 12 | int key[V]; 13 | memset(key,INT_MAX,V); 14 | key[0] = 0; 15 | int mst[V]; 16 | memset(mst,V,false); 17 | int result = 0,u; 18 | 19 | for(int i=0;i> t; 55 | while (t--) { 56 | int V, E; 57 | cin >> V >> E; 58 | vector> adj[V]; 59 | int i=0; 60 | while (i++> u >> v >> w; 63 | vector t1,t2; 64 | t1.push_back(v); 65 | t1.push_back(w); 66 | adj[u].push_back(t1); 67 | t2.push_back(u); 68 | t2.push_back(w); 69 | adj[v].push_back(t2); 70 | } 71 | 72 | Solution obj; 73 | cout << obj.spanningTree(V, adj) << "\n"; 74 | } 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Hashing/Count-pair-with-given-sum(ADOBE,AMAZON,GS,FLIPKART,ACCOLITE).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K. 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 4, K = 6 9 | arr[] = {1, 5, 7, 1} 10 | Output: 2 11 | Explanation: 12 | arr[0] + arr[1] = 1 + 5 = 6 13 | and arr[1] + arr[3] = 5 + 1 = 6. 14 | 15 | Example 2: 16 | 17 | Input: 18 | N = 4, X = 2 19 | arr[] = {1, 1, 1, 1} 20 | Output: 6 21 | Explanation: 22 | Each 1 will produce sum 2 with any 1. 23 | 24 | Your Task: 25 | You don't need to read input or print anything. Your task is to complete the function getPairsCount() which takes arr[], n and k as input parameters 26 | and returns the number of pairs that have sum K. 27 | 28 | 29 | Expected Time Complexity: O(N) 30 | Expected Auxiliary Space: O(N) 31 | */ 32 | 33 | 34 | #include 35 | using namespace std; 36 | 37 | class Solution{ 38 | public: 39 | int getPairsCount(int arr[], int n, int k) { 40 | // code here 41 | unordered_map mp; 42 | for(int i=0;i> t; 63 | while (t--) { 64 | int n, k; 65 | cin >> n >> k; 66 | int arr[n]; 67 | for (int i = 0; i < n; i++) { 68 | cin >> arr[i]; 69 | } 70 | Solution ob; 71 | auto ans = ob.getPairsCount(arr, n, k); 72 | cout << ans << "\n"; 73 | } 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Searching/count-more-than-(n\k)occr(AMAZON).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array arr[] of size N and an element k. The task is to find all elements in array that appear more than n/k times. 3 | Example 1: 4 | Input: 5 | N = 8 6 | arr[] = {3,1,2,2,1,2,3,3} 7 | k = 4 8 | Output: 2 9 | Explanation: In the given array, 3 and 10 | 2 are the only elements that appears 11 | more than n/k times. 12 | Example 2: 13 | Input: 14 | N = 4 15 | arr[] = {2,3,3,2} 16 | k = 3 17 | Output: 2 18 | Explanation: In the given array, 3 and 2 19 | are the only elements that appears more 20 | than n/k times. So the count of elements 21 | are 2. 22 | Your Task: 23 | The task is to complete the function countOccurence() which returns count of elements with more than n/k times appearance. 24 | Expected Time Complexity: O(N). 25 | Expected Auxiliary Space: O(N). 26 | */ 27 | 28 | 29 | #include 30 | #include 31 | using namespace std; 32 | int countOccurence(int arr[], int n, int k); 33 | 34 | int main() { 35 | int t, k; 36 | cin >> t; 37 | while (t--) { 38 | int n, i; 39 | cin >> n; 40 | 41 | int arr[n]; 42 | 43 | for (i = 0; i < n; i++) cin >> arr[i]; 44 | cin >> k; 45 | cout << countOccurence(arr, n, k) << endl; 46 | } 47 | return 0; 48 | } 49 | 50 | int countOccurence(int arr[], int n, int k) { 51 | // Your code here 52 | int reqCount = n/k; 53 | int count = 0; 54 | int maxi = *max_element(arr, arr+n); 55 | for(int i=0;ireqCount) 68 | { 69 | count++; 70 | } 71 | } 72 | return count; 73 | 74 | } 75 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Greedy/Largest-Number-With-Given-Sum(MICROSOFT).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Geek lost the password of his super locker. He remembers the number of digits N as well as the sum S of all the digits of his password. He know that his 3 | password is the largest number of N digits that can be made with given sum S. As he is busy doing his homework, help him retrieving his password. 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 5, S = 12 9 | Output: 10 | 93000 11 | Explanation: 12 | Sum of elements is 12. Largest possible 13 | 5 digit number is 93000 with sum 12. 14 | Example 2: 15 | 16 | Input: 17 | N = 3, S = 29 18 | Output: 19 | -1 20 | Explanation: 21 | There is no such three digit number 22 | whose sum is 29. 23 | Your Task : 24 | You don't need to read input or print anything. Your task is to complete the function largestNumber() which takes 2 integers N and S as input parameters and 25 | returns the password in the form of string, else return "-1" in the form of string. 26 | 27 | Constraints: 28 | 1 ≤ N ≤ 104 29 | 0 ≤ S ≤ 106 30 | 31 | Expected Time Complexity : O(N) 32 | Expected Auxilliary Space : O(1) 33 | */ 34 | 35 | class Solution{ 36 | public: 37 | string largestNumber(int n, int sum){ 38 | // Your code here 39 | string res = ""; 40 | while(sum>0 && n>0) 41 | { 42 | if(sum<=9) 43 | { 44 | string str = to_string(sum); 45 | res+=str; 46 | n--; 47 | sum=0; 48 | } 49 | else 50 | { 51 | res+='9'; 52 | sum-=9; 53 | n--; 54 | } 55 | } 56 | while(n>0) 57 | { 58 | res+='0'; 59 | n--; 60 | } 61 | if(sum!=0) 62 | { 63 | res="-1"; 64 | } 65 | return res; 66 | } 67 | }; 68 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Set-Kth-bit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,a,b,rem,count=0,sum=0,k,result; 33 | cin>>n>>k; 34 | 35 | 36 | //O(1) 37 | k = pow(2,k); 38 | n = n|k; 39 | cout<>1; 48 | count++; 49 | } 50 | if((n&1)==0) 51 | { 52 | result = copyn+pow(2,k); 53 | } 54 | else 55 | { 56 | result = copyn; 57 | } 58 | cout<0) 70 | { 71 | if(exp & 1) 72 | { 73 | result = ((ll)result*base); 74 | } 75 | 76 | base = ((ll)base*base) % MOD; 77 | exp >>= 1; 78 | } 79 | 80 | return result; 81 | } 82 | 83 | 84 | 85 | 86 | int main() 87 | { 88 | int t; 89 | cin>>t; 90 | while(t--) 91 | { 92 | solve(); 93 | } 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Graphs/Check-Cycle-Undirected(BFS).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an undirected graph with V vertices and E edges, check whether it contains any cycle or not. 3 | Expected Time Complexity: O(V + E) 4 | Expected Space Complexity: O(V) 5 | */ 6 | 7 | 8 | #include 9 | using namespace std; 10 | 11 | 12 | class Solution { 13 | public: 14 | 15 | 16 | bool BFS(vector adj[], int V, int s,vector& visited) 17 | { 18 | vector parent(V,-1); 19 | queue q; 20 | visited[s] = true; 21 | q.push(s); 22 | 23 | 24 | while(!q.empty()) 25 | { 26 | int u = q.front(); 27 | q.pop(); 28 | for(auto v:adj[u]) 29 | { 30 | if(!visited[v]) 31 | { 32 | 33 | visited[v] = true; 34 | q.push(v); 35 | parent[v] = u; 36 | } 37 | else if(parent[u]!=v) 38 | { 39 | return true; 40 | } 41 | } 42 | } 43 | 44 | return false; 45 | } 46 | 47 | bool isCycle(int V, vectoradj[]){ 48 | // Code here 49 | vector visited(V, false); 50 | for(int i=0;i> tc; 65 | while(tc--){ 66 | int V, E; 67 | cin >> V >> E; 68 | vectoradj[V]; 69 | for(int i = 0; i < E; i++){ 70 | int u, v; 71 | cin >> u >> v; 72 | adj[u].push_back(v); 73 | adj[v].push_back(u); 74 | } 75 | Solution obj; 76 | bool ans = obj.isCycle(V, adj); 77 | if(ans) 78 | cout << "1\n"; 79 | else cout << "0\n"; 80 | } 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Subtrees-with-sum=X(MICROSOFT).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree and an integer X. Your task is to complete the function countSubtreesWithSumX() that returns the count of the number of subtress having 3 | total node’s data sum equal to the value X. 4 | Example: For the tree given below: 5 | 6 | 5 7 | / \ 8 | -10 3 9 | / \ / \ 10 | 9 8 -4 7 11 | 12 | Subtree with sum 7: 13 | -10 14 | / \ 15 | 9 8 16 | 17 | and one node 7. 18 | 19 | Example 1: 20 | 21 | Input: 22 | 5 23 | / \ 24 | -10 3 25 | / \ / \ 26 | 9 8 -4 7 27 | X = 7 28 | Output: 2 29 | Explanation: Subtrees with sum 7 are 30 | [9, 8, -10] and [7] (refer the example 31 | in the problem description). 32 | Example 2: 33 | 34 | Input: 35 | 1 36 | / \ 37 | 2 3 38 | X = 5 39 | Output: 0 40 | Explanation: No subtree has sum equal 41 | to 5. 42 | Your Task: 43 | You don't need to read input or print anything. Your task is to complete the function countSubtreesWithSumX() which takes the root node and an integer X 44 | as inputs and returns the number of subtrees of the given Binary Tree having sum exactly equal to X. 45 | 46 | Expected Time Complexity: O(N). 47 | Expected Auxiliary Space: O(Height of the Tree). 48 | */ 49 | 50 | int subtreeSum(Node* root,int& count, int X) 51 | { 52 | if(root==NULL) return 0; 53 | int ls = subtreeSum(root->left,count,X); 54 | int rs = subtreeSum(root->right,count,X); 55 | if(ls+rs+root->data == X) count++; 56 | return (ls+rs+root->data); 57 | } 58 | 59 | int countSubtreesWithSumX(Node* root, int X) 60 | { 61 | // Code here 62 | int count = 0; 63 | if(root==NULL) return 0; 64 | int ls = subtreeSum(root->left,count,X); 65 | int rs = subtreeSum(root->right,count,X); 66 | 67 | if(ls+rs+root->data==X) count++; 68 | 69 | return count; 70 | } 71 | -------------------------------------------------------------------------------- /Leetcode/Medium/Bit-Manipulation/Count-bits-from-1-to-n.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. 3 | 4 | Example 1: 5 | 6 | Input: 2 7 | Output: [0,1,1] 8 | 9 | Example 2: 10 | 11 | Input: 5 12 | Output: [0,1,1,2,1,2] 13 | 14 | Follow up: 15 | 16 | It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass? 17 | Space complexity should be O(n). 18 | Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language. 19 | 20 | 21 | 22 | OPTIMIZED DP Approach 23 | TIME: O(N) 24 | 25 | */ 26 | 27 | 28 | class Solution { 29 | public: 30 | 31 | // dp implementation function 32 | int find(int n, vector &res) 33 | { 34 | if(n==0) 35 | { 36 | return 0; 37 | } 38 | if(n==1) 39 | { 40 | return 1; 41 | } 42 | if(res[n]!=-1) 43 | { 44 | return res[n]; 45 | } 46 | else 47 | { 48 | if(n%2==0) 49 | { 50 | res[n]=find(n/2, res); //store intermmediate value 51 | return find(n/2, res); //call the function again 52 | } 53 | else 54 | { 55 | res[n]=1+find(n/2, res); //store intermmediate value 56 | return 1+find(n/2,res); //call the function again 57 | } 58 | } 59 | } 60 | 61 | 62 | 63 | 64 | vector countBits(int n) { 65 | vector res(n+1, -1); 66 | for(int i=0;i<=n;i++) 67 | { 68 | res[i]=find(i,res); 69 | } 70 | return res; 71 | 72 | 73 | } 74 | }; 75 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Searching/Count-1s.cpp: -------------------------------------------------------------------------------- 1 | /*Given a binary sorted non-increasing array of 1s and 0s. You need to print the count of 1s in the binary array. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 8 7 | arr[] = {1,1,1,1,1,0,0,0} 8 | Output: 5 9 | Explanation: Number of 1's in given 10 | binary array : 1 1 1 1 1 0 0 0 is 5. 11 | 12 | Example 2: 13 | 14 | Input: 15 | N = 8 16 | arr[] = {1,1,0,0,0,0,0,0} 17 | Output: 2 18 | Explanation: Number of 1's in given 19 | binary array : 1 1 0 0 0 0 0 0 is 2. 20 | 21 | Your Task: 22 | The task is to complete the function countOne() which takes the array arr[] and its size N as inputs and returns the count of 1s in the input array. 23 | 24 | Expected Time Complexity: O(LogN). 25 | Expected Auxiliary Space: O(1). 26 | 27 | */ 28 | 29 | 30 | 31 | 32 | 33 | #include 34 | using namespace std; 35 | 36 | int countOnes(int arr[], int n) 37 | { 38 | 39 | // Your code here 40 | int low = 0; 41 | int high = n-1; 42 | while(low<=high) 43 | { 44 | int mid = (low+high)/2; 45 | if(arr[mid]==1) 46 | { 47 | if(arr[mid+1]==0) 48 | { 49 | return mid+1; 50 | } 51 | else 52 | { 53 | low = mid+1; 54 | } 55 | } 56 | else 57 | { 58 | if(arr[mid-1]==1) 59 | { 60 | return mid; 61 | } 62 | else 63 | { 64 | high = mid-1; 65 | } 66 | } 67 | } 68 | 69 | } 70 | 71 | 72 | int main() 73 | { 74 | int t; 75 | cin>>t; 76 | 77 | while(t--) 78 | { 79 | int n; 80 | cin>>n; 81 | int *arr = new int[n]; 82 | for(int i = 0; i < n; i++) 83 | cin>>arr[i]; 84 | 85 | cout < n) { 33 | return 0; 34 | } 35 | if (i == n) { 36 | return 1; 37 | } 38 | return climb_Stairs(i + 1, n) + climb_Stairs(i + 2, n); 39 | } 40 | } 41 | 42 | 43 | //DP - APPROACH 1 - SPACE and TIME-O(N) 44 | 45 | class Solution { 46 | public: 47 | 48 | 49 | int fun(int i,int n,int *dp) 50 | { 51 | if(i>n) return 0; 52 | if(i==n) return 1; 53 | if(dp[i]>-1) return dp[i]; 54 | dp[i] = fun(i+1,n,dp)+fun(i+2,n,dp); 55 | return dp[i]; 56 | } 57 | 58 | 59 | int climbStairs(int n) { 60 | int dp[n+1]; 61 | memset(dp,-1,sizeof(dp)); 62 | int i=0; 63 | return fun(i,n,dp); 64 | } 65 | }; 66 | 67 | //DP(FIBONACCI) - APPROACH 2 - SPACE and TIME-O(N) 68 | class Solution { 69 | public: 70 | int climbStairs(int n) { 71 | if(n==1) return 1; 72 | int dp[n+1]; 73 | 74 | dp[1]=1; 75 | dp[2]=2; 76 | 77 | for(int i=3;i<=n;i++) 78 | { 79 | dp[i]=dp[i-1]+dp[i-2]; 80 | } 81 | return dp[n]; 82 | 83 | } 84 | }; 85 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Heap/Kth-Smallest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array arr[] of N positive integers and a number K. The task is to find the kth smallest element in the array. 3 | 4 | 5 | 6 | Example 1: 7 | 8 | Input: 9 | N = 5, K = 3 10 | arr[] = {3,5,4,2,9} 11 | 12 | Output: 13 | 4 14 | 15 | Explanation: 16 | Third smallest element in the array is 4. 17 | Example 2: 18 | 19 | Input: 20 | N = 5, k = 5 21 | arr[] = {4,3,7,6,5} 22 | 23 | Output: 24 | 7 25 | 26 | Explanation: 27 | Fifth smallest element in the array is 7. 28 | 29 | Your Task: 30 | You don't need to read inputs or print anything. Complete the method kthSmallest() which takes array arr[], size of the array n and value k as input 31 | parameters and returns kth smallest element. 32 | 33 | Constraints: 34 | 1 <= N <= 106 35 | 1 <= arr[i] <= 106 36 | 1 <= K <= N 37 | Array do not contains duplicates. 38 | 39 | Expected Time Complexity: O(NlogK) 40 | Expected Auxilliary Space: O(K) 41 | */ 42 | 43 | class Solution{ 44 | 45 | public: 46 | void maxHeapify(int arr[],int n,int i) 47 | { 48 | int largest = i, l = 2*i+1, r = 2*i+2; 49 | if(l=0;i--) 63 | maxHeapify(arr,n,i); 64 | } 65 | public: 66 | //Function to find the kth smallest element in the array. 67 | int kthSmallest(int arr[], int n, int k){ 68 | // Your code here 69 | buildHeap(arr,n); 70 | for(int i=n-1;i>0;i--) 71 | { 72 | swap(arr[0],arr[i]); 73 | maxHeapify(arr,i,0); 74 | } 75 | return arr[k-1]; 76 | 77 | } 78 | }; 79 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Stacks/Maximum-Rectangular-Area-Histogram(MICROSOFT).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. For simplicity, 3 | assume that all bars have the same width and the width is 1 unit. 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 7 9 | arr[] = {6,2,5,4,5,1,6} 10 | Output: 12 11 | Explanation: 12 | 13 | 14 | Example 2: 15 | 16 | Input: 17 | N = 8 18 | arr[] = {7 2 8 9 1 3 6 5} 19 | Output: 16 20 | Explanation: Maximum size of the histogram 21 | will be 8 and there will be 2 consecutive 22 | histogram. And hence the area of the 23 | histogram will be 8x2 = 16. 24 | Your Task: 25 | The task is to complete the function getMaxArea() which takes the array arr[] and its size N as inputs and finds the largest rectangular area possible and 26 | returns the answer. 27 | 28 | Expected Time Complxity : O(N) 29 | Expected Auxilliary Space : O(N) 30 | */ 31 | 32 | class Solution 33 | { 34 | public: 35 | long long getMaxArea(long long arr[], int n){ 36 | 37 | 38 | long long i=0,maxarea = 0,currarea; 39 | long long popind; 40 | stack st; 41 | while(i= 0.) 5 | 6 | Also, a subarray may only include each element of the fixed buffer A at most once. (Formally, for a subarray C[i], C[i+1], ..., C[j], there does not exist i <= k1, k2 <= j with k1 % A.length = k2 % A.length.) 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: [1,-2,3,-2] 13 | Output: 3 14 | Explanation: Subarray [3] has maximum sum 3 15 | 16 | Example 2: 17 | 18 | Input: [5,-3,5] 19 | Output: 10 20 | Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10 21 | 22 | Example 3: 23 | 24 | Input: [3,-1,2,-1] 25 | Output: 4 26 | Explanation: Subarray [2,-1,3] has maximum sum 2 + (-1) + 3 = 4 27 | 28 | */ 29 | 30 | class Solution { 31 | public: 32 | 33 | int normalSum(vector& A) 34 | { 35 | int n = A.size(); 36 | int curr_sum = A[0]; 37 | int max_sum = A[0]; 38 | for(int i=1;i& A) { 47 | 48 | int n = A.size(); 49 | int normal_max_sum = normalSum(A); 50 | if(normal_max_sum<0) 51 | { 52 | return normal_max_sum; 53 | } 54 | int tot_sum = 0, circular_max_sum,res; 55 | for(int i= 0;i &A) 37 | { 38 | if(root==NULL) 39 | { 40 | A.push_back(-1); 41 | return; 42 | } 43 | A.push_back(root->data); 44 | serialUtil(root->left,A); 45 | serialUtil(root->right,A); 46 | } 47 | vector serialize(Node *root) { 48 | //Your code here 49 | vector A; 50 | serialUtil(root, A); 51 | return A; 52 | } 53 | /*this function deserializes 54 | the serialized vector A*/ 55 | Node * deSerialUtil(vector &A,int& index) 56 | { 57 | if(index == A.size()) return NULL; 58 | int val = A[index]; 59 | index++; 60 | Node * root = new Node(val); 61 | root->left = deSerialUtil(A,index); 62 | root->right = deSerialUtil(A,index); 63 | return root; 64 | } 65 | Node * deSerialize(vector &A) 66 | { 67 | //Your code here 68 | int index = 0 69 | Node * ans = deSerialUtil(A,index); 70 | return ans; 71 | } 72 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Trail-of-ones-(AMAZON).cpp: -------------------------------------------------------------------------------- 1 | /* Time Complexity: O(N) 2 | Space Complexity: O(1) 3 | */ 4 | 5 | 6 | 7 | #include 8 | using namespace std; 9 | #define ll long long 10 | #define fo(i,n) for(i=0;i0;i--) 12 | #define si(x) scanf("%d", &x) 13 | #define sl(x) scanf("%lld", &x) 14 | #define ss(s) sacnf("%s", s) 15 | #define pi(x) printf("%d\n", x) 16 | #define pl(x) printf("%lld\n", x) 17 | #define ps(s) printf("%s\n", s) 18 | #define deb(x) cout<< #x << "=" << x << endl; 19 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 26 | typedef pair pl; 27 | typedef vector vi; 28 | typedef vector vl; 29 | typedef vector vvi; 30 | typedef vector vvl; 31 | 32 | const int MOD = 1000000007; //(10^9 + 7) 33 | 34 | int fib(int n) 35 | { 36 | int a=0,b=1,c,i; 37 | if(n==0) 38 | { 39 | return a; 40 | } 41 | else 42 | { 43 | fo(i,n+1) 44 | { 45 | c=a+b; 46 | a=b; 47 | b=c; 48 | 49 | } 50 | return b; 51 | } 52 | } 53 | 54 | 55 | void solve() 56 | { 57 | int i,j,n,m,a,b,rem,count=0,sum=0,result; 58 | cin>>n; 59 | result = pow(2,n) - fib(n); 60 | cout<0) 72 | { 73 | if(exp & 1) 74 | { 75 | result = ((ll)result*base); 76 | } 77 | 78 | base = ((ll)base*base) % MOD; 79 | exp >>= 1; 80 | } 81 | 82 | return result; 83 | } 84 | 85 | 86 | 87 | 88 | int main() 89 | { 90 | int t; 91 | cin>>t; 92 | while(t--) 93 | { 94 | solve(); 95 | } 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Top-View(AMAZON,PAYTM,SAMSUNG,WALMART,OLA).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is 3 | viewed from the top. For the given below tree 4 | 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | 11 | Top view will be: 4 2 1 3 7 12 | Note: Print from leftmost node to rightmost node. 13 | 14 | Example 1: 15 | 16 | Input: 17 | 1 18 | / \ 19 | 2 3 20 | Output: 2 1 3 21 | 22 | Example 2: 23 | 24 | Input: 25 | 10 26 | / \ 27 | 20 30 28 | / \ / \ 29 | 40 60 90 100 30 | Output: 40 20 10 30 100 31 | Your Task: 32 | Since this is a function problem. You don't have to take input. Just complete the function printTopView() that takes root node as parameter and prints the 33 | top view. Print endline after end of printing the top view. 34 | 35 | Expected Time Complexity: O(N) 36 | Expected Auxiliary Space: O(N). 37 | */ 38 | 39 | struct Node 40 | { 41 | int data; 42 | struct Node* left; 43 | struct Node* right; 44 | 45 | Node(int x){ 46 | data = x; 47 | left = right = NULL; 48 | } 49 | }; 50 | 51 | void topView(struct Node *root) 52 | { 53 | //Your code here 54 | queue> q; 55 | if(root==NULL) return; 56 | q.push({root,0}); 57 | map mp; 58 | while(!q.empty()) 59 | { 60 | 61 | Node* curr = q.front().first; 62 | int idx = q.front().second; 63 | if(mp.find(idx)==mp.end()) 64 | { 65 | mp[idx]=curr->data; 66 | } 67 | q.pop(); 68 | 69 | if(curr->left!=NULL) q.push({curr->left, idx-1}); 70 | if(curr->right!=NULL) q.push({curr->right, idx+1}); 71 | 72 | 73 | } 74 | for(auto it:mp) 75 | { 76 | cout< 8 | using namespace std; 9 | #define ll long long 10 | #define fo(i,n) for(i=0;i0;i--) 12 | #define si(x) scanf("%d", &x) 13 | #define sl(x) scanf("%lld", &x) 14 | #define ss(s) sacnf("%s", s) 15 | #define pi(x) printf("%d\n", x) 16 | #define pl(x) printf("%lld\n", x) 17 | #define ps(s) printf("%s\n", s) 18 | #define deb(x) cout<< #x << "=" << x << endl; 19 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 26 | typedef pair pl; 27 | typedef vector vi; 28 | typedef vector vl; 29 | typedef vector vvi; 30 | typedef vector vvl; 31 | 32 | const int MOD = 1000000007; //(10^9 + 7) 33 | 34 | 35 | 36 | void solve() 37 | { 38 | int i,j,n,m,a,b,rem,count=0,sum=0,l,r,x,y,AND=0,result,copyy; 39 | cin>>x>>l>>r>>y; 40 | copyy = y; 41 | while(copyy!=0) 42 | { 43 | count++; 44 | copyy = copyy>>1; 45 | } 46 | if(r>count) 47 | { 48 | r = count; 49 | } 50 | for(i = l;i<=r;i++) 51 | { 52 | AND = AND+pow(2,(i-1)); 53 | } 54 | result = (y & AND) | x; 55 | cout<0) 68 | { 69 | if(exp & 1) 70 | { 71 | result = ((ll)result*base); 72 | } 73 | 74 | base = ((ll)base*base) % MOD; 75 | exp >>= 1; 76 | } 77 | 78 | return result; 79 | } 80 | 81 | 82 | 83 | 84 | int main() 85 | { 86 | int t; 87 | cin>>t; 88 | while(t--) 89 | { 90 | solve(); 91 | } 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Binary-Multiple-of-3-(AMA,ADO,MICRO).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,a,b,rem,count=0,sum=0; 33 | ll num=0,countodd=0,counteven=0; 34 | string str; 35 | cin>>str; 36 | for(int i=0;i0) 68 | { 69 | if(exp & 1) 70 | { 71 | result = ((ll)result*base); 72 | } 73 | 74 | base = ((ll)base*base) % MOD; 75 | exp >>= 1; 76 | } 77 | 78 | return result; 79 | } 80 | 81 | 82 | 83 | 84 | int main() 85 | { 86 | int t; 87 | cin>>t; 88 | while(t--) 89 | { 90 | solve(); 91 | } 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Hashing/Intersection-of-two-arrays(AMAZON,PAYPAL).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given two arrays A and B respectively of size N and M, the task is to print the count of elements in the intersection (or common elements) of the two arrays. 3 | 4 | For this question, the intersection of two arrays can be defined as the set containing distinct common elements between the two arrays. 5 | 6 | Example 1: 7 | 8 | Input: 9 | 5 3 10 | 89 24 75 11 23 11 | 89 2 4 12 | 13 | Output: 14 | 1 15 | 16 | Explanation: 17 | 89 is the only element 18 | in the intersection of two arrays. 19 | Example 2: 20 | 21 | Input: 22 | 6 5 23 | 1 2 3 4 5 6 24 | 3 4 5 6 7 25 | 26 | Output: 27 | 4 28 | 29 | Explanation: 30 | 3 4 5 and 6 are the elements 31 | in the intersection of two arrays. 32 | Your Task: 33 | The task is to complete the function NumberofElementsInIntersection() which takes 4 inputs ie- array a, array b, n which is the size of array a, m 34 | which is the size of array b. The function should return the count of the number of elements in the intersection. 35 | 36 | Expected Time Complexity: O(N + M). 37 | Expected Auxiliary Space: O(min(N,M)). 38 | */ 39 | 40 | 41 | 42 | #include 43 | using namespace std; 44 | 45 | int NumberofElementsInIntersection (int a[], int b[], int n, int m ) 46 | { 47 | // Your code goes here 48 | set st; 49 | for(int i=0;i>t; 70 | while(t--) 71 | { 72 | int n,m; 73 | cin>>n>>m; 74 | int a[n], b[m]; 75 | for(int i=0; i>a[i]; 77 | 78 | for(int i=0; i>b[i]; 80 | 81 | cout << NumberofElementsInIntersection(a,b,n,m) << endl; 82 | } 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Line-By-Line-LOT(AMAZON,MICROSOFT,MORG-STAN,SNAPDEAL).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Binary Tree, your task is to find its level order traversal. 3 | For the below tree the output will be 1 $ 2 3 $ 4 5 6 7 $ 8 $. 4 | 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | \ 11 | 8 12 | 13 | Example 1: 14 | 15 | Input: 16 | 1 17 | / 18 | 4 19 | / \ 20 | 4 2 21 | Output:1 $ 4 $ 4 2 $ 22 | 23 | Example 2: 24 | 25 | Input: 26 | 10 27 | / \ 28 | 20 30 29 | / \ 30 | 40 60 31 | Output: 10 $ 20 30 $ 40 60 $ 32 | Your Task: 33 | This is a function problem. You don't need to read input. Just complete the function levelOrder() that takes nodes as parameter and returns level order traversal as a 2D list. 34 | 35 | Note: The driver code prints the levels '$' separated. 36 | Expected Time Complexity: O(N). 37 | Expected Auxiliary Space: O(N). 38 | */ 39 | 40 | struct Node 41 | { 42 | int data; 43 | struct Node* left; 44 | struct Node* right; 45 | 46 | Node(int x){ 47 | data = x; 48 | left = right = NULL; 49 | } 50 | }; 51 | 52 | //You are required to complete this method 53 | vector> levelOrder(Node* node) 54 | { 55 | //Your code here 56 | vector> res; 57 | queue q; 58 | q.push(node); 59 | while(1) 60 | { 61 | 62 | 63 | int size = q.size(); 64 | if(size==0) 65 | break; 66 | vector line; 67 | 68 | while(size>0) 69 | { 70 | Node* curr = q.front(); 71 | line.push_back(curr->data); 72 | q.pop(); 73 | 74 | if(curr->left!=NULL) 75 | q.push(curr->left); 76 | 77 | if(curr->right!=NULL) 78 | q.push(curr->right); 79 | 80 | size--; 81 | } 82 | res.push_back(line); 83 | } 84 | return res; 85 | } 86 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/Flip-bits-max1s-(KADANE)-(AMAZON).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,a,b,rem,count=0,max_sum=0,sum_now=0; 33 | cin>>n; 34 | int arr[n]; 35 | fo(i,n) 36 | { 37 | cin>>arr[i]; 38 | if(arr[i]==1) 39 | { 40 | count++; 41 | arr[i] = -1; 42 | } 43 | else 44 | { 45 | arr[i] = 1; 46 | } 47 | } 48 | fo(i,n) 49 | { 50 | sum_now += arr[i]; 51 | if(sum_now<0) 52 | { 53 | sum_now=0; 54 | } 55 | if(sum_now>max_sum) 56 | { 57 | max_sum=sum_now; 58 | } 59 | } 60 | cout<<(count+max_sum)<0) 72 | { 73 | if(exp & 1) 74 | { 75 | result = ((ll)result*base); 76 | } 77 | 78 | base = ((ll)base*base) % MOD; 79 | exp >>= 1; 80 | } 81 | 82 | return result; 83 | } 84 | 85 | 86 | 87 | 88 | int main() 89 | { 90 | int t; 91 | cin>>t; 92 | while(t--) 93 | { 94 | solve(); 95 | } 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Heap/Kth-Largest-In-Stream(AMAZON,CISCO,MICROSOFT,WALMART).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an input stream arr[] of n integers, find the kth largest element for each element in the stream. 3 | 4 | Example 1: 5 | 6 | Input: 7 | k = 4, n = 6 8 | arr[] = {1, 2, 3, 4, 5, 6} 9 | Output: 10 | -1 -1 -1 1 2 3 11 | Explanation: 12 | k = 4 13 | For 1, the 4th largest element doesn't 14 | exist so we print -1. 15 | For 2, the 4th largest element doesn't 16 | exist so we print -1. 17 | For 3, the 4th largest element doesn't 18 | exist so we print -1. 19 | For 4, the 4th largest element is 1. 20 | For 5, the 4th largest element is 2. 21 | for 6, the 4th largest element is 3. 22 | Example 2: 23 | 24 | Input: 25 | k = 1, n = 2 26 | arr[] = {3, 4} 27 | Output: 28 | 3 4 29 | Explanation: 30 | For the 1st and 2nd element the 1st largest 31 | element is itself. 32 | 33 | 34 | Your Task: 35 | You don't need to read input or print anything. Your task is to complete the function kthLargest() which takes 2 Integers k, and n and also an array 36 | arr[] of size n as input and returns the kth largest element in the stream. 37 | 38 | 39 | 40 | Expected Time Complexity: O(nlogk) 41 | Expected Auxiliary Space: O(n) 42 | 43 | */ 44 | class Solution { 45 | public: 46 | vector kthLargest(int k, int arr[], int n) { 47 | // code here 48 | vector res; 49 | priority_queue,greater> pq; 50 | 51 | for(int i=0;i 28 | using namespace std; 29 | void rotate (vector >& matrix); 30 | 31 | void rotate(vector >& matrix) 32 | { 33 | // Your code goes here 34 | int row = matrix.size(); 35 | int col = matrix[0].size(); 36 | for(int i=0;i>t; 63 | while(t--) 64 | { 65 | int n; 66 | cin>>n; 67 | vector > matrix(n); 68 | for(int i=0; i>matrix[i][j]; 73 | } 74 | rotate(matrix); 75 | for (int i = 0; i < n; ++i) 76 | { 77 | for(int j=0; jval==leaf) 56 | { 57 | dist = 0; 58 | return 1; 59 | } 60 | int ldist = -1,rdist = -1; 61 | int lh = burnTree(A->left,leaf,dist,res); 62 | int rh = burnTree(A->right,leaf,dist,res); 63 | 64 | if(ldist!=-1) 65 | { 66 | dist=ldist+1; 67 | res = max(res,dist+rh); 68 | } 69 | else if(rdist!=-1) 70 | { 71 | dist = rdist+1; 72 | res = max(res,dist+lh); 73 | } 74 | 75 | return max(lh,rh)+1; 76 | 77 | } 78 | 79 | int Solution::solve(TreeNode* A, int B) { 80 | int dist = 0; 81 | int res= 0; 82 | dist = burnTree(A,B,dist,res); 83 | return dist; 84 | } 85 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Bit-Magic/2-odd-occurrence-in-array(AMA,GOO,MICRO,QUAL,MMT).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | 29 | 30 | void solve() 31 | { 32 | int i,j,n,m,a,b,rem,count=0,sum=0,res1=0,res2=0,XOR=0,l; 33 | cin>>l; 34 | n = l*2+2; 35 | int arr[n]; 36 | fo(i,n) 37 | { 38 | cin>>arr[i]; 39 | XOR = XOR ^ arr[i]; 40 | } 41 | int rsb = XOR &(~(XOR-1)); 42 | fo(i,n) 43 | { 44 | if((arr[i] & rsb)!=0) 45 | { 46 | res1 = res1 ^ arr[i]; 47 | } 48 | else 49 | { 50 | res2 = res2 ^ arr[i]; 51 | } 52 | } 53 | if(res1>res2) 54 | { 55 | cout<0) 72 | { 73 | if(exp & 1) 74 | { 75 | result = ((ll)result*base); 76 | } 77 | 78 | base = ((ll)base*base) % MOD; 79 | exp >>= 1; 80 | } 81 | 82 | return result; 83 | } 84 | 85 | 86 | 87 | 88 | int main() 89 | { 90 | int t; 91 | cin>>t; 92 | while(t--) 93 | { 94 | solve(); 95 | } 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Graphs/Adjacency-List-Implementation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given the adjacency list of a bidirectional graph. Your task is to print the adjacency list for each vertex. 3 | 4 | Input: 5 | The first line of input is T denoting the number of testcases.Then first line of each of the T contains two positive integer V and E where 'V' is 6 | the number of vertex and 'E' is number of edges in graph. Next, 'E' line contains two positive numbers showing that there is an edge between these two vertex. 7 | 8 | Output: 9 | For each vertex, print their connected nodes in order you are pushing them inside the list. See the example input-output for the reference of format. 10 | 11 | Your Task: 12 | You don't need to read input or print anything. Your task is to complete the function printGraph () which takes the adjacency list of the graph and 13 | the number of vertices V as inputs and prints the adjacency list of the Graph. 14 | 15 | Expected Time Complexity: O(V + E). 16 | Expected Auxiliary Space: O(1). 17 | 18 | Constraints: 19 | 1 <= T <= 200 20 | 1 <= V <= 103 21 | 1 <= E = V*(V-1) 22 | 23 | Example: 24 | Input: 25 | 1 26 | 5 7 27 | 0 1 28 | 0 4 29 | 1 2 30 | 1 3 31 | 1 4 32 | 2 3 33 | 3 4 34 | 35 | Output: 36 | 0-> 1-> 4 37 | 1-> 0-> 2-> 3-> 4 38 | 2-> 1-> 3 39 | 3-> 1-> 2-> 4 40 | 4-> 0-> 1-> 3 41 | */ 42 | 43 | #include 44 | using namespace std; 45 | 46 | void printGraph(vector adj[], int V) 47 | { 48 | // Your code here 49 | for(int i = 0;i "<>t; 65 | while(t--) 66 | { int v, e; 67 | cin>>v>>e; 68 | int a, b; 69 | vector adj[v]; 70 | for(int i=0;i>a>>b; 73 | adj[a].push_back(b); 74 | adj[b].push_back(a); 75 | } 76 | printGraph(adj, v); 77 | 78 | } 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Binary-Search-Tree/Vertical-Traversal(IMP).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Binary Tree, find the vertical traversal of it starting from the leftmost level to the rightmost level. 3 | If there are multiple nodes passing through a vertical line, then they should be printed as they appear in level order traversal of the tree. 4 | 5 | Example 1: 6 | 7 | Input: 8 | 2 9 | \ 10 | 3 11 | / 12 | 4 13 | Output: 2 4 3 14 | Example 2: 15 | 16 | Input: 17 | 1 18 | / \ 19 | 2 3 20 | / \ \ 21 | 4 5 6 22 | Output: 4 2 1 5 3 6 23 | Your Task: 24 | You don't need to read input or print anything. Your task is to complete the function verticalOrder() which takes the root node as input and returns an 25 | array containing the vertical order traversal of the tree from the leftmost to the rightmost level. If 2 nodes lie in the same vertical level, they should 26 | be printed in the order they appear in the level order traversal of the tree. 27 | 28 | Expected Time Complexity: O(N). 29 | Expected Auxiliary Space: O(N). 30 | */ 31 | 32 | void util(Node *root, vector &res) 33 | { 34 | if(root==NULL) return; 35 | queue> q; 36 | map> mp; 37 | q.push({root,0}); 38 | 39 | while(!q.empty()) 40 | { 41 | pair curr = q.front(); 42 | q.pop(); 43 | int dist = curr.second; 44 | mp[dist].push_back(curr.first->data); 45 | if(curr.first->left!=NULL) 46 | { 47 | q.push({curr.first->left,dist-1}); 48 | } 49 | if(curr.first->right!=NULL) 50 | { 51 | q.push({curr.first->right,dist+1}); 52 | } 53 | } 54 | for(auto it:mp) 55 | { 56 | for(int i=0;i verticalOrder(Node *root) 66 | { 67 | //Your code here 68 | vector res; 69 | util(root,res); 70 | return res; 71 | } 72 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Heap/Rearrange-Characters(AMAZON).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string S such that it may contain repeated lowercase alphabets. Rearrange the characters in the string such that no two adjacent characters are same. 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | S = geeksforgeeks 9 | Output: 1 10 | Explanation: egeskerskegof can be one way of 11 | rearranging the letters. 12 | 13 | Example 2: 14 | 15 | Input: 16 | S = bbbabaaacd 17 | Output: 1 18 | Explanation: abababdcab can be one way of 19 | rearranging the letters. 20 | 21 | Your Task: 22 | You do not need to read input or print anything. Complete the function rearrangeString() which takes string S as input parameter and returns the string after 23 | rearrangement. 24 | If the characters are successfully rearranged then the generated output will be 1 else 0. 25 | 26 | 27 | Expected Time Complexity : O(n log n) 28 | Expected Auxilliary Space : O(n) 29 | */ 30 | 31 | class Solution{ 32 | struct Key 33 | { 34 | int freq; 35 | char ch; 36 | 37 | bool operator<(const Key &k) const 38 | { 39 | return freq pq; 47 | int count[26] = {0}; 48 | for(int i=0;i0) 67 | { 68 | pq.push(prev); 69 | } 70 | (k.freq)--; 71 | prev = k; 72 | } 73 | if(str.length()!=n) 74 | { 75 | int ne = str.length(); 76 | str+=str[ne-1]; 77 | } 78 | return str; 79 | } 80 | }; 81 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Greedy/Activity-Selection(FACEBOOK,MORGAN-STANLEY).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given N activities with their start and finish day given in array start[ ] and end[ ]. Select the maximum number of activities that can be performed by 3 | a single person, assuming that a person can only work on a single activity at a given day. 4 | Note : Duration of the activity includes both starting and ending day. 5 | 6 | Example 1: 7 | 8 | Input: 9 | N = 2 10 | start[] = {2, 1} 11 | end[] = {2, 2} 12 | Output: 13 | 1 14 | Explanation: 15 | A person can perform only one of the 16 | given activities. 17 | Example 2: 18 | 19 | Input: 20 | N = 4 21 | start[] = {1, 3, 2, 5} 22 | end[] = {2, 4, 3, 6} 23 | Output: 24 | 3 25 | Explanation: 26 | A person can perform activities 1, 3 27 | and 4. 28 | Your Task : 29 | You don't need to read input or print anything. Your task is to complete the function activityselection() which takes array start[ ], array end[ ] and 30 | integer N as input parameters and returns the maximum number of activities that can be done. 31 | 32 | Expected Time Complexity : O(N * Log(N)) 33 | Expected Auxilliary Space : O(1) 34 | */ 35 | 36 | class Solution{ 37 | public: 38 | 39 | 40 | static bool comp(pair a,pair b) 41 | { 42 | if(a.second==b.second) return a.first start, vector end, int n){ 48 | // Your code here 49 | vector> arr; 50 | for(int i=0;iprevend) 61 | { 62 | count++; 63 | prevend = arr[i].second; 64 | } 65 | } 66 | return count; 67 | } 68 | }; 69 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Binary-Search-Tree/Pair-Target-Sum(IMP)(AMAZON,MICROSOFT,VMWARE).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. 3 | 4 | Example 1: 5 | 6 | Input: 7 | 2 8 | / \ 9 | 1 3 10 | sum = 5 11 | Output: 1 12 | Explanation: 13 | Nodes with value 2 and 3 sum up to 5. 14 | Example 2: 15 | 16 | Input: 17 | 6 18 | / 19 | 5 20 | / 21 | 3 22 | / \ 23 | 1 4 24 | sum = 2 25 | Output: 0 26 | Explanation: 27 | There's no pair that sums up to 2. 28 | 29 | 30 | Your Task: 31 | You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as 32 | a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. 33 | 34 | Expected Time Complexity: O(N). 35 | Expected Auxiliary Space: O(Height of the BST). 36 | */ 37 | int isPairPresent(struct Node *root, int target) 38 | { 39 | stack l,r; 40 | Node *c = root; 41 | while(c!=NULL) 42 | { 43 | l.push(c); 44 | c=c->left; 45 | } 46 | c = root; 47 | while(c!=NULL) 48 | { 49 | r.push(c); 50 | c=c->right; 51 | } 52 | int ldata,rdata; 53 | while(l.top()!=r.top()) 54 | { 55 | ldata = l.top()->data; 56 | rdata = r.top()->data; 57 | if(ldata+rdata == target) return true; 58 | 59 | else if(ldata+rdataright; 62 | l.pop(); 63 | while(c!=NULL) 64 | { 65 | l.push(c); 66 | c=c->left; 67 | } 68 | } 69 | else if(ldata+rdata>target) 70 | { 71 | c = r.top()->left; 72 | r.pop(); 73 | while(c!=NULL) 74 | { 75 | r.push(c); 76 | c = c->right; 77 | } 78 | } 79 | } 80 | return false; 81 | } 82 | -------------------------------------------------------------------------------- /GFG-MUST-DO/Graph/Medium/Number-Of-Islands.cpp: -------------------------------------------------------------------------------- 1 | //Question: https://practice.geeksforgeeks.org/problems/find-the-number-of-islands/1 2 | 3 | 4 | #include 5 | using namespace std; 6 | 7 | int findIslands(vector A[], int N, int M); 8 | 9 | int main() { 10 | 11 | int T; 12 | cin >> T; 13 | while (T--) { 14 | int N, M; 15 | cin >> N >> M; 16 | vector A[N]; 17 | for (int i = 0; i < N; i++) { 18 | vector temp(M); 19 | A[i] = temp; 20 | for (int j = 0; j < M; j++) { 21 | cin >> A[i][j]; 22 | } 23 | } 24 | cout << findIslands(A, N, M) << endl; 25 | } 26 | return 0; 27 | }// } Driver Code Ends 28 | 29 | int isSafe(vector MM[], int row, int col, vector> &visited, int N, int M) 30 | { 31 | return (row >= 0) && (row < N) && (col >= 0) && (col < M) && 32 | (MM[row][col] && !visited[row][col]); 33 | } 34 | 35 | 36 | void DFSrec(vector A[], int i, int j, vector> &visited, int N, int M) 37 | { 38 | 39 | int row[] = {-1, -1, -1, 0, 0, 1, 1, 1}; // Check for all 8 neighbours of i and j 40 | int col[] = {-1, 0, 1, -1, 1, -1, 0, 1}; 41 | visited[i][j] = true; 42 | for(int k=0;k<8;k++) 43 | { 44 | if((i+row[k] A[], int N, int M) { 58 | 59 | // Your code here 60 | int count = 0; 61 | vector> visited(N,vector(M, false)); 62 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define fo(i,n) for(i=0;i0;i--) 6 | #define si(x) scanf("%d", &x) 7 | #define sl(x) scanf("%lld", &x) 8 | #define ss(s) sacnf("%s", s) 9 | #define pi(x) printf("%d\n", x) 10 | #define pl(x) printf("%lld\n", x) 11 | #define ps(s) printf("%s\n", s) 12 | #define deb(x) cout<< #x << "=" << x << endl; 13 | #define deb2(x, y) cout<< #x << "=" << x <<","<< #y << "=" << y < pii; 20 | typedef pair pl; 21 | typedef vector vi; 22 | typedef vector vl; 23 | typedef vector vvi; 24 | typedef vector vvl; 25 | 26 | const int MOD = 1000000007; //(10^9 + 7) 27 | 28 | int countBits(int i) 29 | { 30 | int count = 0; 31 | while(i!=0) 32 | { 33 | if((i&1) == 1) 34 | { 35 | count++; 36 | } 37 | i = i>>1; 38 | } 39 | return count; 40 | } 41 | 42 | void solve() 43 | { 44 | int i,j,n,m,a,b,rem,count=0,sum=0; 45 | cin>>n; 46 | fo(i,n) 47 | { 48 | sum = i + countBits(i); 49 | if(sum == n) 50 | { 51 | cout<<"0"<0) 75 | { 76 | if(exp & 1) 77 | { 78 | result = ((ll)result*base); 79 | } 80 | 81 | base = ((ll)base*base) % MOD; 82 | exp >>= 1; 83 | } 84 | 85 | return result; 86 | } 87 | 88 | 89 | 90 | 91 | int main() 92 | { 93 | int t; 94 | cin>>t; 95 | while(t--) 96 | { 97 | solve(); 98 | } 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Matrix/Search-In-MAT(AMAZON,ADOBE,DIRECTI,GS,VISA,ORACLE,PAYTM,SAP,SANPD,ACCOLITE,OLA,MMT).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a matrix mat[][] of size N x M, where every row and column is sorted in increasing order, and a number X is given. The task is to 3 | find whether element X is present in the matrix or not. 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 3 9 | M = 3 10 | mat[][]: 3 30 38 11 | 44 52 54 12 | 57 60 69 13 | X = 62 14 | Output: 15 | 0 16 | Explanation: 62 is not present in the 17 | matrix, so output is 0 18 | 19 | Example 2: 20 | 21 | Input: 22 | N = 1 23 | M = 6 24 | mat[][] : 18 21 27 38 55 67 25 | X = 55 26 | Output: 27 | 1 28 | Explanation: 55 is present in the 29 | matrix at 5th cell. 30 | 31 | Your Task: 32 | You don't need to read input or print anything. You just have to complete the function matSearch() which takes a 2D matrix mat[][], its dimensions 33 | N and M and integer X as inputs and returns 1 if the element X is present in the matrix and 0 otherwise. 34 | 35 | Expected Time Complexity: O(N+M). 36 | Expected Auxiliary Space: O(1). 37 | 38 | */ 39 | 40 | 41 | #include 42 | using namespace std; 43 | 44 | class Solution{ 45 | public: 46 | int matSearch (vector > &mat, int n, int m, int x) 47 | { 48 | // your code here 49 | int right = m-1,top = 0; 50 | while(right>=0 && topx) 61 | { 62 | right--; 63 | } 64 | } 65 | return 0; 66 | } 67 | }; 68 | 69 | 70 | int main () 71 | { 72 | int t; cin >> t; 73 | while (t--){ 74 | int n, m; cin >> n >> m; 75 | vector > arr (n, vector (m)); 76 | for (int i=0;i> arr[i][j]; 79 | 80 | int x; cin >> x; 81 | Solution ob; 82 | cout << ob.matSearch (arr, n, m, x) << endl; 83 | } 84 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/BT-From-Postorder-Inorder.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from 3 | these traversals. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: 10 | N = 8 11 | in[] = 4 8 2 5 1 6 3 7 12 | post[] =8 4 5 2 6 7 3 1 13 | Output: 1 2 4 8 5 3 6 7 14 | Explanation: For the given postorder and 15 | inorder traversal of tree the resultant 16 | binary tree will be 17 | 1 18 | / \ 19 | 2 3 20 | / \ / \ 21 | 4 5 6 7 22 | \ 23 | 8 24 | 25 | 26 | Example 2: 27 | 28 | Input: 29 | N = 5 30 | in[] = 9 5 2 3 4 31 | post[] = 5 9 3 4 2 32 | Output: 2 9 5 4 3 33 | Explanation: 34 | the resultant binary tree will be 35 | 2 36 | / \ 37 | 9 4 38 | \ / 39 | 5 3 40 | 41 | 42 | Your Task: 43 | You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of 44 | odes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. 45 | The generated output contains the preorder traversal of the constructed tree. 46 | 47 | 48 | 49 | Expected Time Complexity: O(N2) 50 | Expected Auxiliary Space: O(N) 51 | 52 | */ 53 | 54 | static int postInd; 55 | Node* fun(int in[], int post[], int start, int end) 56 | { 57 | if(start>end) return NULL; 58 | 59 | Node* root = new Node(post[postInd--]); 60 | 61 | if(start==end) return root; 62 | 63 | int inInd; 64 | for(int i = start;i<=end;i++) 65 | { 66 | if(in[i]==root->data) 67 | { 68 | inInd = i; 69 | break; 70 | } 71 | } 72 | root->right = fun(in,post,inInd+1,end); 73 | root->left = fun(in,post,start,inInd-1); 74 | 75 | 76 | return root; 77 | 78 | } 79 | 80 | 81 | Node *buildTree(int in[], int post[], int n) { 82 | // Your code here 83 | postInd = n-1; 84 | //Node *root = NULL; 85 | Node* root = fun(in,post,0,n-1); 86 | return root; 87 | } 88 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Searching/Count-repeated(AMAZON).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array arr[] of N positive integers, where elements are consecutive (sorted). Also, there is a single element which is repeating X (any variable) number of times. Now, the task is to find the element which is repeated and number of times it is repeated. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 5 8 | arr[] = {1,2,3,3,4} 9 | Output: 3 2 10 | Explanation: In the given array, 3 is 11 | occuring two times. 12 | 13 | Example 2: 14 | 15 | Input: 16 | N = 5 17 | arr[] = {2,3,4,5,5} 18 | Output: 5 2 19 | Explanation: In the given array, 5 is 20 | occuring two times. 21 | 22 | Your Task: 23 | Complete findRepeating function and return pair of element which is repeated and the number of times it is repeated. The printing is done by the driver code. 24 | 25 | Constraints: 26 | 1 <= N <= 107 27 | 1 <= arr[i] <= 1015 28 | 29 | Expected Time Complexity : O(logN) 30 | Expected Auxilliary Space : O(1) 31 | 32 | */ 33 | 34 | 35 | #include 36 | using namespace std; 37 | 38 | pair findRepeating(int *, int); 39 | 40 | int main() { 41 | 42 | int t; 43 | cin >> t; 44 | 45 | while(t--){ 46 | int n; 47 | cin >> n; 48 | int arr[n]; 49 | 50 | for(int i = 0;i> arr[i]; 52 | } 53 | 54 | pair ans = findRepeating(arr,n); 55 | 56 | cout << ans.first << " " << ans.second << endl; 57 | 58 | } 59 | 60 | } 61 | 62 | pair findRepeating(int *arr, int n) 63 | { 64 | pair ans; 65 | ans.first = -1; 66 | ans.second = -1; 67 | int low = 0; 68 | int high = n-1; 69 | while(low<=high) 70 | { 71 | int mid = (low+high)/2; 72 | if(mid == arr[mid]-arr[0]) 73 | { 74 | low = mid+1; 75 | } 76 | else if(arr[mid]==arr[mid+1] || arr[mid]==arr[mid-1]) 77 | { 78 | ans.first = arr[mid]; 79 | ans.second = n-(arr[n-1]-arr[0]); 80 | return ans; 81 | } 82 | else 83 | { 84 | high = mid-1; 85 | } 86 | } 87 | return ans; 88 | } 89 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Max-Path-Sum(IMP)(AMAZON,FACEBOOK,MICROSOFT,DIRECTI).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree in which each node element contains a number. Find the maximum possible sum from one leaf node to another. 3 | 4 | 5 | Example 1: 6 | 7 | Input : 8 | 3 9 | / \ 10 | 4 5 11 | / \ 12 | -10 4 13 | Output: 16 14 | Explanation : 15 | Maximum Sum lies between leaf node 4 and 5. 16 | 4 + 4 + 3 + 5 = 16. 17 | Example 2: 18 | 19 | Input : 20 | -15 21 | / \ 22 | 5 6 23 | / \ / \ 24 | -8 1 3 9 25 | / \ \ 26 | 2 -3 0 27 | / \ 28 | 4 -1 29 | / 30 | 10 31 | Output : 27 32 | Explanation: 33 | The maximum possible sum from one leaf node 34 | to another is (3 + 6 + 9 + 0 + -1 + 10 = 27) 35 | 36 | Your Task: 37 | You dont need to read input or print anything. Complete the function maxPathSum() which takes root node as input parameter and returns the maximum sum 38 | between 2 leaf nodes. 39 | 40 | 41 | Expected Time Complexity: O(N) 42 | Expected Auxiliary Space: O(Height of Tree) 43 | */ 44 | 45 | int util(Node* root, int &res) 46 | { 47 | if(root==NULL) return 0; 48 | 49 | if(!root->right && !root->left) return root->data; 50 | 51 | int lsum = util(root->left,res); 52 | int rsum = util(root->right,res); 53 | 54 | if(root->left && root->right) 55 | { 56 | res = max(res,lsum+rsum+root->data); 57 | return max(lsum,rsum)+root->data; 58 | } 59 | 60 | if(!root->left) 61 | { 62 | return rsum+root->data; 63 | } 64 | if(!root->right) 65 | { 66 | return lsum+root->data; 67 | } 68 | 69 | } 70 | 71 | 72 | int maxPathSum(Node* root) 73 | { 74 | // code here 75 | int res = INT_MIN; 76 | util(root,res); 77 | return res; 78 | } 79 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Tree/Children-Sum-Parent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a Binary Tree. Check whether all of its nodes have the value equal to the sum of their child nodes. 3 | 4 | Example 1: 5 | 6 | Input: 7 | 10 8 | / 9 | 10 10 | Output: 1 11 | Explanation: Here, every node is sum of 12 | its left and right child. 13 | Example 2: 14 | 15 | Input: 16 | 1 17 | / \ 18 | 4 3 19 | / \ 20 | 5 N 21 | Output: 0 22 | Explanation: Here, 1 is the root node 23 | and 4, 3 are its child nodes. 4 + 3 = 24 | 7 which is not equal to the value of 25 | root node. Hence, this tree does not 26 | satisfy the given conditions. 27 | Your Task: 28 | You don't need to read input or print anything. Your task is to complete the function isSumProperty() that takes the root Node of the Binary Tree as 29 | input and returns 1 if all the nodes in the tree satisfy the following properties. Else, it returns 0. 30 | For every node, data value must be equal to the sum of data values in left and right children. Consider data value as 0 for NULL child. Also, leaves 31 | are considered to follow the property. 32 | 33 | Expected Time Complexiy: O(N). 34 | Expected Auxiliary Space: O(Height of the Tree). 35 | */ 36 | 37 | 38 | struct Node 39 | { 40 | int data; 41 | struct Node* left; 42 | struct Node* right; 43 | 44 | Node(int x){ 45 | data = x; 46 | left = right = NULL; 47 | } 48 | }; 49 | 50 | 51 | // Return 1 if all the nodes in the tree satisfy the given property. Else return 0 52 | int isSumProperty(Node *root) 53 | { 54 | // Add your code here 55 | int left_data = 0; 56 | int right_data = 0; 57 | if(root==NULL || (root->left==NULL && root->right==NULL)) 58 | { 59 | return 1; 60 | } 61 | else 62 | { 63 | 64 | if(root->left!=NULL) 65 | left_data = root->left->data; 66 | 67 | if(root->right!=NULL) 68 | right_data = root->right->data; 69 | 70 | if((left_data+right_data==root->data) && isSumProperty(root->left) && isSumProperty(root->right)) 71 | { 72 | return 1; 73 | } 74 | else 75 | { 76 | return 0; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Sorting/Quick-Sort_(Lomuto-Partition).cpp: -------------------------------------------------------------------------------- 1 | /*Given an array of integers. Complete the partition() function used for the implementation of Quick Sort. 2 | 3 | Example 1: 4 | 5 | Input: N = 5, arr[] = { 4, 1, 3, 9, 7} 6 | Output: 1 3 4 7 9 7 | Example 2: 8 | 9 | Input: N = 10, 10 | arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1} 11 | Output: 1 2 3 4 5 6 7 8 9 10 12 | 13 | TIME: O(N^2) 14 | SPACE: O(1) 15 | */ 16 | 17 | 18 | #include 19 | #include 20 | using namespace std; 21 | 22 | int lomutoPartition (int arr[], int low, int high); 23 | /* The main function that implements QuickSort 24 | arr[] --> Array to be sorted, low --> Starting index, high --> Ending index */ 25 | 26 | void quickSort(int arr[], int low, int high) 27 | { 28 | if (low < high) 29 | { 30 | /* pi is partitioning index, arr[p] is now at right place */ 31 | int pi = lomutoPartition(arr, low, high); 32 | // Separately sort elements before partition and after partition 33 | quickSort(arr, low, pi - 1); 34 | quickSort(arr, pi + 1, high); 35 | } 36 | } 37 | 38 | void printArray(int arr[], int size) 39 | { 40 | int i; 41 | for (i=0; i < size; i++) 42 | printf("%d ", arr[i]); 43 | printf("\n"); 44 | } 45 | 46 | int main() 47 | { 48 | int arr[1000],n,T,i; 49 | scanf("%d",&T); 50 | while(T--){ 51 | scanf("%d",&n); 52 | for(i=0;i 19 | #include 20 | using namespace std; 21 | int partition (int arr[], int low, int high); 22 | 23 | 24 | 25 | /* Function to print an array */ 26 | void printArray(int arr[], int size) 27 | { 28 | int i; 29 | for (i=0; i < size; i++) 30 | printf("%d ", arr[i]); 31 | printf("\n"); 32 | } 33 | 34 | 35 | 36 | int main() 37 | { 38 | int arr[1000],n,T,i; 39 | scanf("%d",&T); 40 | while(T--){ 41 | scanf("%d",&n); 42 | for(i=0;ipivot); 85 | if(i>=j) 86 | { 87 | return j; 88 | } 89 | swap(arr[i],arr[j]); 90 | 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Greedy/N-Meetings(AMAZON,MICROSOFT,FLIPKART,MMT).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | There is one meeting room in a firm. There are N meetings in the form of (S[i], F[i]) where S[i] is start time of meeting i and F[i] is finish time of meeting i. 3 | What is the maximum number of meetings that can be accommodated in the meeting room when only one meeting can be held in the meeting room at a particular time? 4 | Also note start time of one chosen meeting can't be equal to the end time of the other chosen meeting. 5 | 6 | 7 | Example 1: 8 | 9 | Input: 10 | N = 6 11 | S[] = {1,3,0,5,8,5} 12 | F[] = {2,4,6,7,9,9} 13 | Output: 14 | 4 15 | Explanation: 16 | Four meetings can be held with 17 | given start and end timings. 18 | Example 2: 19 | 20 | Input: 21 | N = 8 22 | S[] = {75250, 50074, 43659, 8931, 11273, 23 | 27545, 50879, 77924} 24 | F[] = {112960, 114515, 81825, 93424, 54316, 25 | 35533, 73383, 160252} 26 | Output: 27 | 3 28 | Explanation: 29 | Only three meetings can be held 30 | with given start and end timings. 31 | 32 | Your Task : 33 | You don't need to read inputs or print anything. Complete the function maxMeetings() that recieves array S[ ] and F[ ] along with their size N as input 34 | parameters and returns the maximum number of meetings that can be held in the meeting room. 35 | 36 | 37 | Expected Time Complexity : O(N*LogN) 38 | Expected Auxilliary Space : O(N) 39 | */ 40 | 41 | class Solution{ 42 | public: 43 | 44 | 45 | static bool comp(pair a,pairb) 46 | { 47 | if(a.second==b.second) return a.first> arr; 55 | for(int i=0;iprevend) 61 | { 62 | prevend = arr[i].second; 63 | count++; 64 | } 65 | } 66 | return count; 67 | } 68 | }; 69 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Searching/Left-Index.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a sorted array of positive integers (elements may be repeated) and a number x. The task is to find the leftmost index of x in the given array. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 10 8 | arr[] = {1,1,2,2,3,4,5,5,6,7} 9 | X = 1 10 | Output: 0 11 | Explanation: 1 is present two times 12 | in the array and its leftmost index 13 | is 0. 14 | 15 | Example 2: 16 | 17 | Input: 18 | N = 7 19 | arr[] = {10,20,20,20,20,20,20} 20 | X = 20 21 | Output: 1 22 | Explanation: 20 is present 5 time, 23 | but its leftmost index is 1. 24 | 25 | Your Task: 26 | The task is to complete the function leftIndex() which takes the array arr[], its size N and an integer X as inputs and returns the index of leftmost occurrence 27 | of X in given input array. It returns -1 if element is not present in the array. 28 | 29 | Expected Time Complexity: O(LogN). 30 | Expected Auxiliary Space: O(1). 31 | */ 32 | 33 | 34 | 35 | 36 | #include 37 | using namespace std; 38 | 39 | 40 | int leftIndex(int n, int arr[], int X){ 41 | 42 | 43 | int low = 0; 44 | int high = n-1,ans=-1; 45 | while(low<=high) 46 | { 47 | int mid = (low+high)/2; 48 | if(arr[mid]==X ) 49 | { 50 | if(mid == 0 || arr[mid-1] < X ) 51 | { 52 | return mid; 53 | } 54 | else 55 | { 56 | high = mid-1; 57 | } 58 | } 59 | else if(arr[mid]>X) 60 | { 61 | high = mid-1; 62 | } 63 | else 64 | { 65 | low = mid+1; 66 | } 67 | } 68 | return -1; 69 | 70 | } 71 | 72 | 73 | int main() { 74 | 75 | 76 | int testcases; 77 | cin >> testcases; 78 | while(testcases--){ 79 | int sizeOfArray; 80 | cin >> sizeOfArray; 81 | 82 | int arr[sizeOfArray]; 83 | 84 | // Array input 85 | for(int index = 0; index < sizeOfArray; index++){ 86 | cin >> arr[index]; 87 | } 88 | 89 | int elemntToSearch; 90 | cin >> elemntToSearch; 91 | 92 | cout << leftIndex(sizeOfArray, arr, elemntToSearch) << endl; 93 | } 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Greedy/Fractional-Knapsack(MICROSOFT).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given weights and values of N items, we need to put these items in a knapsack of capacity W to get the maximum total value in the knapsack. 3 | Note: Unlike 0/1 knapsack, you are allowed to break the item. 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 3, W = 50 9 | values[] = {60,100,120} 10 | weight[] = {10,20,30} 11 | Output: 12 | 240.00 13 | Explanation:Total maximum value of item 14 | we can have is 240.00 from the given 15 | capacity of sack. 16 | Example 2: 17 | 18 | Input: 19 | N = 2, W = 50 20 | values[] = {60,100} 21 | weight[] = {10,20} 22 | Output: 23 | 160.00 24 | Explanation: 25 | Total maximum value of item 26 | we can have is 160.00 from the given 27 | capacity of sack. 28 | 29 | 30 | Your Task : 31 | Complete the function fractionalKnapsack() that receives maximum capacity , array of structure/class and size n and returns a double value representing the 32 | maximum value in knapsack. 33 | Note: The details of structure/class is defined in the comments above the given function. 34 | 35 | Expected Time Complexity : O(NlogN) 36 | Expected Auxilliary Space: O(1) 37 | 38 | */ 39 | 40 | class Solution 41 | { 42 | public: 43 | 44 | static bool comp(pair a, pair b) 45 | { 46 | return((a.first/a.second)>(b.first/b.second)); 47 | } 48 | 49 | double fractionalKnapsack(int W, Item arr[], int n) 50 | { 51 | // Your code here 52 | vector> vec; 53 | for(int i = 0;ileft),depth(root->right))+1; 54 | } 55 | 56 | 57 | int burnTree(Node* root, int target, int &dist) 58 | { 59 | if(root==NULL) return 0; 60 | 61 | if(root->data==target) 62 | { 63 | return 1; 64 | } 65 | 66 | int val = burnTree(root->left,target,dist); 67 | if(val) 68 | { 69 | dist = max(dist,val+depth(root->right)); 70 | return val+1; 71 | } 72 | 73 | val = burnTree(root->right,target,dist); 74 | if(val) 75 | { 76 | dist = max(dist,val+depth(root->left)); 77 | return val+1; 78 | } 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Hashing/Equal-Arrays-Using-Hash(GOLDMAN-SACHS).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given two arrays A and B of equal size N, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain 3 | same set of elements, arrangements (or permutation) of elements may be different though. 4 | Note : If there are repetitions, then counts of repeated elements must also be same for two array to be equal. 5 | 6 | Example 1: 7 | 8 | Input: 9 | N = 5 10 | A[] = {1,2,5,4,0} 11 | B[] = {2,4,5,0,1} 12 | Output: 1 13 | Explanation: Both the array can be 14 | rearranged to {0,1,2,4,5} 15 | Example 2: 16 | 17 | Input: 18 | N = 3 19 | A[] = {1,2,5} 20 | B[] = {2,4,15} 21 | Output: 0 22 | Explanation: A[] and B[] have only 23 | one common value. 24 | Your Task: 25 | Complete check() function which takes both the given array and their size as function arguments and returns true if the arrays are equal else returns false. 26 | The 0 and 1 printing is done by the driver code. 27 | 28 | Expected Time Complexity : O(N) 29 | Expected Auxilliary Space : O(N) 30 | */ 31 | 32 | 33 | 34 | #include 35 | 36 | using namespace std; 37 | #define ll long long 38 | 39 | bool check(vector , vector , int ); 40 | 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while(t--) { 46 | int n; 47 | cin>>n; 48 | 49 | vector arr(n,0),brr(n,0); 50 | 51 | for(ll i=0;i> arr[i]; 53 | 54 | for(ll i=0;i> brr[i]; 56 | 57 | cout << check(arr,brr,n) << "\n"; 58 | } 59 | return 0; 60 | } 61 | 62 | bool check(vector arr, vector brr, int n) 63 | { 64 | unordered_map mp; 65 | for(int i=0;i 42 | using namespace std; 43 | 44 | 45 | int maxWater(int height[], int n) 46 | { 47 | //Your code here 48 | int i=0,j=n-1,ans=0; 49 | while(i<=j) 50 | { 51 | ans = max(ans,((j-i-1)*min(height[i],height[j]))); 52 | if(height[i]>t; 69 | while(t--) 70 | { 71 | int n; 72 | cin>>n; 73 | int height[n]; 74 | 75 | for(int i=0;i>height[i]; 78 | } 79 | 80 | cout<<(maxWater(height, n))< findSpiral(Node *root) 41 | { 42 | //Your code here 43 | vector res; 44 | if(root==NULL) 45 | { 46 | 47 | return res; 48 | } 49 | 50 | queue q; 51 | q.push(root); 52 | int count = 0; 53 | while(1) 54 | { 55 | 56 | 57 | int size = q.size(); 58 | if(size==0) 59 | { 60 | break; 61 | } 62 | vector level; 63 | 64 | while(size>0) 65 | { 66 | Node* curr = q.front(); 67 | q.pop(); 68 | level.push_back(curr->data); 69 | if(curr->left!=NULL) 70 | q.push(curr->left); 71 | 72 | if(curr->right!=NULL) 73 | q.push(curr->right); 74 | 75 | size--; 76 | } 77 | if(count&1 || count==0) 78 | { 79 | for(int i=0;i=0;i--) 88 | { 89 | res.push_back(level[i]); 90 | } 91 | } 92 | count++; 93 | } 94 | return res; 95 | } 96 | -------------------------------------------------------------------------------- /GFG_DSA_Topicwise/Searching/Peak-Element(AMAZON,ADOBE,GOOGLE,VISA).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array A of N integers. The task is to find a peak element in A in O( log N ) . 3 | An array element is peak if it is not smaller than its neighbours. For corner elements, consider only one neighbour. 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 3 9 | A[] = {1,2,3} 10 | Output: 2 11 | Explanation: In the given array, 12 | 3 is the peak element as it is 13 | greater than its neighbour. 14 | Example 2: 15 | 16 | Input: 17 | N = 2 18 | A[] = {3,4} 19 | Output: 1 20 | Explanation: 4 is the peak element 21 | as it is greater than its neighbour 22 | elements. 23 | Your Task: 24 | You don't have to read input or print anything. Just complete the provided function peakElement() and return a valid index with respect to 0 based indexing. 25 | Note 1: Multiple peak element may be possible, in that case return any of the valid index . 26 | Note 2: If you click on Compile and Test the output will be 1 if the index that you return is correct. Otherwise output will be 0. 27 | 28 | Expected Time Complexity: O(log N). 29 | Expected Auxiliary Space: O(1). 30 | 31 | */ 32 | 33 | 34 | 35 | 36 | #include 37 | using namespace std; 38 | 39 | int peakElement(int arr[], int n) 40 | { 41 | // Your code here 42 | int low = 0; 43 | int high = n-1; 44 | while(low<=high) 45 | { 46 | int mid = (high+low)/2; 47 | if((mid==0 || arr[mid]>=arr[mid-1]) && (mid==(n-1)||arr[mid]>=arr[mid+1])) 48 | { 49 | return mid; 50 | } 51 | else if(mid!=0 && (arr[mid-1]>arr[mid])) 52 | { 53 | high = mid - 1; 54 | } 55 | else 56 | { 57 | low = mid+1; 58 | } 59 | 60 | } 61 | return -1; 62 | } 63 | 64 | int main() { 65 | int t; 66 | cin>>t; 67 | while(t--) 68 | { 69 | int n; 70 | cin>>n; 71 | int a[n]; 72 | for(int i=0;i>a[i]; 75 | } 76 | bool f=0; 77 | int A = peakElement(a,n); 78 | 79 | if(n==1) 80 | f=1; 81 | else 82 | if(A==0 and a[0]>=a[1]) 83 | f=1; 84 | else if(A==n-1 and a[n-1]>=a[n-2]) 85 | f=1; 86 | else if(a[A] >=a[A+1] and a[A]>= a[A-1]) 87 | f=1; 88 | else 89 | f=0; 90 | 91 | cout<