├── .github └── workflows │ └── greetings.yml ├── Code Chef ├── A Problem On Sticks.cpp ├── Ada King.cpp ├── Ada Matrix.cpp ├── Ada and Dishes.cpp ├── Another Card Game Problem.cpp ├── Bowling Strategy.cpp ├── Cache Hit.cpp ├── Chef And Card Game.cpp ├── Chef And Demonetisation.cpp ├── Chef And Strings.cpp ├── Chef And Work.cpp ├── Chef Wars.cpp ├── Chef and Division 3.cpp ├── Chef and Easy Queries.cpp ├── Chef and Linear Chess.cpp ├── Chef and Wedding Arrangements.cpp ├── Chefina And Swaps.cpp ├── Chefina and Swap.cpp ├── Chopsticks.cpp ├── Compress The List.cpp ├── Covid Run.cpp ├── Doctor Chef.cpp ├── Encoded String.cpp ├── Even-tual Reduction.cpp ├── Iron, Magnet and Wall.cpp ├── Is this a give away.cpp ├── Khaled in HIT.cpp ├── Linked List-K Append.cpp ├── Maximum Production.cpp ├── Missing A Point.cpp ├── Mode Of Frequencies.cpp ├── Positive AND.cpp ├── Relativity.cpp ├── Replace for X.cpp ├── Restore Sequence.cpp ├── Smallest KMP.cpp ├── Usain Bolt.cpp ├── Vaccine Production.cpp ├── Weapon Value.cpp ├── What Is This a Crossover Episode.cpp ├── XxOoRr.cpp └── isolation_centres.cpp ├── Geeks For Geeks ├── Addition Under Modulo.cpp ├── Case-specific Sorting of Strings.cpp ├── Closing bracket index.cpp ├── Count Leaves in Binary Tree.cpp ├── Count the Zeros.cpp ├── Delete without head pointer.cpp ├── Digits In Factorial.cpp ├── Exactly 3 Divisors.cpp ├── Height of Binary Tree.cpp ├── Min Heap Implementation.cpp ├── Minimum indexed character.cpp ├── Minimum number to form the sum even.cpp ├── Red OR Green.cpp ├── Remove Duplicate Elements.cpp ├── Reverse Array In Groups.cpp ├── Reverse a string.cpp ├── Smallest Divisible Number.cpp ├── Sum of numbers in string.cpp └── Unique Numbers.cpp ├── Hacker Blocks ├── Activity Selection Problem.cpp ├── Aggressive Cows.cpp ├── All Indices Problem.cpp ├── Balanced Parenthesis.cpp ├── Binary To Decimal.cpp ├── Book Allocation Problem.cpp ├── Broken Calculator.cpp ├── Build BST.cpp ├── Can You Read This.cpp ├── Circular Linked List.cpp ├── Convert String To Integer.cpp ├── Count N Queen.cpp ├── Create tree from Inorder and Preorder.cpp ├── Cycle Detection & Removal In LL.cpp ├── Decimal to Octal.cpp ├── Dictionary Order(Larger).cpp ├── Dictionary Order(Smaller).cpp ├── Difference in ASCII Codes.cpp ├── Duplicate Character Formatting.cpp ├── Even After Odd.cpp ├── FIBONACCI PATTERN (PATTERN 4).cpp ├── Find Last Element.cpp ├── Find The Greater Element.cpp ├── Form Biggest Number.cpp ├── Generate Parentheses.cpp ├── Help Rahul To Search.cpp ├── Help Ramu.cpp ├── Hollow Diamond Pattern.cpp ├── Hostel Visit.cpp ├── I Am Busy Man.cpp ├── Importance Of Time.cpp ├── Intersection Point Of Two Linked List.cpp ├── Is Balanced Binary Tree.cpp ├── Is Palindrome.cpp ├── Keypad Codes.cpp ├── Kth Element from The Last.cpp ├── Kth Root.cpp ├── Largest BST in a Binary Tree.cpp ├── Level Order (ZigZag).cpp ├── Linked List K Reverse.cpp ├── Manmohan Loves Patterns - I.cpp ├── Manmohan Loves Patterns- II.cpp ├── Mapped Strings.cpp ├── Matrix Search.cpp ├── Max Frequency Characters.cpp ├── Maximum Circular Sum.cpp ├── Maximum Length Bitonic Subarray.cpp ├── Maximum Subarray Sum.cpp ├── Median of Sorted Arrays.cpp ├── Merge K sorted Arrays.cpp ├── Merge Sorted Linked List.cpp ├── Modular Exponentiation.cpp ├── Mountain Pattern.cpp ├── Move All X At End.cpp ├── Painter Partition Problem.cpp ├── Palindrome Linked List.cpp ├── Pattern Double Sided Arrow.cpp ├── Pattern Inverted Hour Glass.cpp ├── Pattern Triangle.cpp ├── Pattern With Zeroes.cpp ├── Piyush and Magical Park.cpp ├── Playing With Bits.cpp ├── Prateek Loves Candy.cpp ├── Prateek Sir And Coding.cpp ├── Print BST keys in the Given Range.cpp ├── Pythagorean Triplet.cpp ├── Queue Using Stack.cpp ├── Rain Water Harvesting.cpp ├── Rat In A Maze.cpp ├── Recover BST.cpp ├── Redundant Parentheses.cpp ├── Remove Duplicates.cpp ├── Replace PI.cpp ├── Replace With Sum of Greater Nodes.cpp ├── Rotate Image.cpp ├── Sanket And Strings.cpp ├── Smart Keypad - Advanced.cpp ├── Smart Keypad - I.cpp ├── Sorted Array.cpp ├── Sorting In Linear Time.cpp ├── Spiral Print Anticlockwise.cpp ├── Stock Span.cpp ├── String Compression.cpp ├── String Sort.cpp ├── Strongest Fighter.cpp ├── Subsequences.cpp ├── Subset Sum Easy.cpp ├── Sudoku Solver.cpp ├── Sum At Level K.cpp ├── Sum of Two Arrays.cpp ├── Target Sum Pairs.cpp ├── Target Sum Triplets.cpp ├── Towers Of Hanoi.cpp ├── Tree Bottom View.cpp ├── Tree Left View.cpp ├── Tree Right View.cpp ├── Tree Top View.cpp ├── Tricky Permutations.cpp ├── Ultra Fast Mathematics.cpp ├── Unique Number - I.cpp ├── Unique Number - II.cpp ├── Unique Number - III.cpp ├── Von Neuman Loves Binary.cpp ├── Wave Print Column Wise.cpp ├── Winning CB Scholarship.cpp └── XOR Profit Problem.cpp ├── Hacker Rank ├── Counting Valley.cpp ├── Jumping On Clouds.cpp ├── Mini Max Sum.cpp ├── Ransom Note.cpp ├── Repeated String.cpp ├── Simple Array Sum.cpp ├── Sock Merchant.cpp └── Two Strings.cpp ├── Interview Bit ├── Add One To Number.cpp ├── Largest Number.cpp ├── Longest Common Prefix.cpp ├── Max Non Negative Subarray.cpp ├── Max Sum Contiguous Subarray.cpp ├── Min Steps In Infinite Grid.cpp ├── Spiral Order Matrix II.cpp └── Triplets With Sum Between Given Range.cpp ├── LICENSE ├── Leet Code ├── 3sum.cpp ├── Add Binary.cpp ├── Add Digits.cpp ├── Add Two Numbers II.cpp ├── Add Two Numbers.cpp ├── All Elements in Two BST.cpp ├── All Paths From Source to Target.cpp ├── Alphabet Board Path.cpp ├── Angle Between Hands of a Clock.cpp ├── Append Characters to String to Make Subsequence.cpp ├── Arithmetic Subarrays.cpp ├── Balanced Binary Tree.cpp ├── Best Time to Buy and Sell Stock.cpp ├── Binary Search Tree Iterator.cpp ├── Binary Tree Inorder Traversal.cpp ├── Binary Tree Level Order Traversal II.cpp ├── Binary Tree Level Order Traversal.cpp ├── Binary Tree Postorder Traversal.cpp ├── Binary Tree Preorder Traversal.cpp ├── Binary Tree Right Side View.cpp ├── Binary Tree Zigzag Level Order Traversal.cpp ├── Bitwise AND of Numbers Range.cpp ├── Boats to Save People.cpp ├── Build an Array With Stack Operations.cpp ├── Buy Sell Stock.cpp ├── Climbing Stairs.cpp ├── Combination Sum.cpp ├── Combinations.cpp ├── Concatenation of Array.cpp ├── Construct BT from Preorder and Inorder Traversal.cpp ├── Construct Binary Tree from Inorder and Postorder Traversal.cpp ├── Container With Most Water.cpp ├── Contains Duplicate II.cpp ├── Contains Duplicate.cpp ├── Convert Binary Number in a Linked List to Integer.cpp ├── Convert Sorted Array to BST.cpp ├── Convert an Array Into a 2D Array With Conditions.cpp ├── Copy List with Random Pointer.cpp ├── Count Good Nodes in Binary Tree.cpp ├── Count Primes.cpp ├── Count the Number of Consistent Strings.cpp ├── Counting Bits.cpp ├── Course Schedule II.cpp ├── Custom Sort String.cpp ├── DI String Match.cpp ├── Daily Temperatures.cpp ├── Delete Node From BST.cpp ├── Delete Node in a Linked List.cpp ├── Design Hashset.cpp ├── Destination City.cpp ├── Detect Capital.cpp ├── Element Appearing More Than 25% In Sorted Array.cpp ├── Excel Sheet Column Number.cpp ├── Fibonacci Number.cpp ├── Final Prices With a Special Discount in a Shop.cpp ├── Find All Anagrams in a String.cpp ├── Find All Duplicates in an Array.cpp ├── Find First and Last Position in a Sorted Array.cpp ├── Find Minimum In Rotated Sorted Array II.cpp ├── Find Minimum In Rotated Sorted Array.cpp ├── Find Peak Element.cpp ├── Find The Duplicate Number.cpp ├── Find and Replace Pattern.cpp ├── Find the Pivot Integer.cpp ├── Find the Prefix Common Array of Two Arrays.cpp ├── First Missing Positive.cpp ├── First Unique Character in a String.cpp ├── FizzBuzz.cpp ├── Flatten a Multilevel Doubly Linked List.cpp ├── Gas Station.cpp ├── Generate Parentheses.cpp ├── Gray Code.cpp ├── Group the People Given the Group Size They Belong To.cpp ├── Hamming Distance.cpp ├── Happy Number.cpp ├── House Robber.cpp ├── Implement Queue using Stacks.cpp ├── Implement Stack using Queues.cpp ├── Implement strStr.cpp ├── Insert Interval.cpp ├── Insert into a BST.cpp ├── Invert Binary Tree.cpp ├── Is Subsequence.cpp ├── Island Perimeter.cpp ├── Isomorphic Strings.cpp ├── Iterator for Combination.cpp ├── Jump Game II.cpp ├── Jump Game.cpp ├── Kth Largest Element in an Array.cpp ├── Kth Smallest Element in a BST.cpp ├── LRU Cache.cpp ├── Largest Local Values in a Matrix.cpp ├── Left and Right Sum Differences.cpp ├── Length of Last Word.cpp ├── Length of the Longest Alphabetical Continuous Substring.cpp ├── Letter Case Permutation.cpp ├── Letter Combinations of a Phone Number.cpp ├── Linked List Cycle II.cpp ├── Linked List Cycle.cpp ├── Longest Common Prefix.cpp ├── Longest Consecutive Sequence.cpp ├── Longest Increasing Subsequence.cpp ├── Longest Mountain in Array.cpp ├── Longest Palindrome.cpp ├── Longest Repeating Character Replacement.cpp ├── Longest Substring Without Repeating Characters.cpp ├── Majority Element II.cpp ├── Majority Element.cpp ├── Make The String Great.cpp ├── Max Consecutive Ones III.cpp ├── Maximum Absolute Sum of Any Subarray.cpp ├── Maximum Depth of Binary Tree.cpp ├── Maximum Erasure Value.cpp ├── Maximum Ice Cream Bars.cpp ├── Maximum Non Negative Product in a Matrix.cpp ├── Maximum Number of Balloons.cpp ├── Maximum Number of Vowels in a Substring of Given Length.cpp ├── Maximum Number of Words You Can Type.cpp ├── Maximum Sub Array.cpp ├── Maximum Sum Circular Subarray.cpp ├── Maximum Sum With Exactly K Elements.cpp ├── Maximum Twin Sum of a Linked List.cpp ├── Maximum Value of a String in an Array.cpp ├── Maximum Width Of Binary Tree.cpp ├── Merge 2 Sorted Lists.cpp ├── Merge In Between Linked Lists.cpp ├── Merge Intervals.cpp ├── Merge Sorted Array.cpp ├── Merge k Sorted Lists.cpp ├── Minimum Add to Make Parentheses Valid.cpp ├── Minimum Flips to Make a OR b Equal to c.cpp ├── Minimum Number of Arrows to Burst Balloons.cpp ├── Minimum Number of Operations to Move All Balls to Each Box.cpp ├── Minimum Path Sum.cpp ├── Minimum Recolors to Get K Consecutive Black Blocks.cpp ├── Minimum Remove to Make Valid Parentheses.cpp ├── Minimum Size Subarray Sum.cpp ├── Minimum Window Substring.cpp ├── Missing Number.cpp ├── Move Zeroes.cpp ├── N-Queens II.cpp ├── N-Queens.cpp ├── N-ary Tree Level Order Traversal.cpp ├── N-ary Tree Postorder Traversal.cpp ├── N-ary Tree Preorder Traversal.cpp ├── Number of 1 Bits.cpp ├── Number of Good Ways to Split a String.cpp ├── Number of Recent Calls.cpp ├── Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold.cpp ├── Odd Even Linked List.cpp ├── Optimal Partition of String.cpp ├── Pallindrome Number.cpp ├── Partition List.cpp ├── Path Sum III.cpp ├── Permutation in String.cpp ├── Permutations II.cpp ├── Plus One.cpp ├── Populating Next Right Pointers in Each Node.cpp ├── PowXN.cpp ├── Power Of Four.cpp ├── Power of Two.cpp ├── Prison Cells After N Days.cpp ├── Product of Array Except Self.cpp ├── Ransom Note.cpp ├── Recover Binary Search Tree.cpp ├── Remove All Adjacent Duplicates In String.cpp ├── Remove All Adjacent Duplicates in String II.cpp ├── Remove Duplicates From A Sorted Array.cpp ├── Remove Duplicates From Sorted Array.cpp ├── Remove Duplicates from Sorted Array II.cpp ├── Remove Duplicates from Sorted List II.cpp ├── Remove Duplicates from Sorted List.cpp ├── Remove Element.cpp ├── Remove Linked LIst Elements.cpp ├── Remove Nth Node From End of List.cpp ├── Removing Stars From a String.cpp ├── Reverse Bits.cpp ├── Reverse Integer.cpp ├── Reverse Linked List.cpp ├── Reverse Nodes in k-Group.cpp ├── Reverse String.cpp ├── Reverse Words In A String.cpp ├── Roman to Integer.cpp ├── Rotate Array.cpp ├── Rotate Image.cpp ├── Rotate List.cpp ├── Same Tree.cpp ├── Search Insert Position.cpp ├── Search in Rotated Sorted Array.cpp ├── Sender With Largest Word Count.cpp ├── Set Matrix Zeroes.cpp ├── Single Number III.cpp ├── Single Number.cpp ├── Sliding Window Maximum.cpp ├── Sort Array By Parity.cpp ├── Sort Characters By Frequency.cpp ├── Spiral Matrix II.cpp ├── Spiral Matrix.cpp ├── Split Linked List in Parts.cpp ├── Subarray Product Less Than K.cpp ├── Subarray Sum Equals K.cpp ├── Subdomain Visit Count.cpp ├── Subsets.cpp ├── Substring with Concatenation of All Words.cpp ├── Substrings of Size Three with Distinct Characters.cpp ├── Sudoku Solver.cpp ├── Sum In A Matrix.cpp ├── Sum of All Odd Length Subarrays.cpp ├── Summary Ranges.cpp ├── Swap Nodes in Pairs.cpp ├── The k Strongest Values in an Array.cpp ├── Time Needed to Buy Tickets.cpp ├── Top K Frequent Elements.cpp ├── Top K Frequent Words.cpp ├── Trapping Rain Water.cpp ├── Two Sum II - Input array is sorted.cpp ├── Two Sum.cpp ├── Ugly Number II.cpp ├── Unique Paths II.cpp ├── Unique Paths.cpp ├── Valid Anagrams.cpp ├── Valid Mountain Array.cpp ├── Valid Parentheses.cpp ├── Valid Sudoku.cpp ├── Valid_Palindrome.cpp ├── Validate Binary Search Tree.cpp ├── Validate Stack Sequences.cpp ├── Watering Plants.cpp ├── Word Break II.cpp ├── Word Break.cpp ├── Word Pattern.cpp └── Words Search.cpp ├── README.md └── SPOJ ├── Aggressive Cows.cpp ├── Arranging Amplifiers.cpp ├── Biased Standings.cpp ├── Defense of a Kingdom.cpp ├── I Am Busy Man.cpp ├── Load Balancing.cpp ├── TEST.cpp ├── The last digit.cpp └── Transform The Expression.cpp /.github/workflows/greetings.yml: -------------------------------------------------------------------------------- 1 | name: Greetings 2 | 3 | on: [pull_request, issues] 4 | 5 | jobs: 6 | greeting: 7 | runs-on: ubuntu-latest 8 | permissions: 9 | issues: write 10 | pull-requests: write 11 | steps: 12 | - uses: actions/first-interaction@v1 13 | with: 14 | repo-token: ${{ secrets.GITHUB_TOKEN }} 15 | issue-message: 'Thanks for finding the issue! Lets solve this together!' 16 | pr-message: 'Congratulations!! This is your first pull request on this repo :)' 17 | -------------------------------------------------------------------------------- /Code Chef/Is this a give away.cpp: -------------------------------------------------------------------------------- 1 | /* Code Chef*/ 2 | /* Title - Is this a give away*/ 3 | //All submissions for this problem are available.You are given two integers l and r. 4 | //You have to choose k real numbers in the interval [l,r] uniform randomly. 5 | //What is the expected count of distinct numbers chosen by you? 6 | // 7 | //Input 8 | //The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. 9 | //The first and only line of each test case contains three space-separated integers l, r and k. 10 | //Output 11 | //For each test case, print a single line containing one integer - the expected count of distinct numbers chosen. It can be proved that the expected count is always an integer. 12 | 13 | #include 14 | using namespace std; 15 | 16 | int main() { 17 | // your code goes here 18 | int t; 19 | cin>>t; 20 | while(t--) 21 | { 22 | int l,r,k; 23 | cin>>l>>r>>k; 24 | if(l!=r) 25 | cout< 2 | #define int long long 3 | #define w(t) while(t--) 4 | #define bpc(n) __builtin_popcount(n) 5 | #define fr(init, range) for(int i = init ; i < range ; i++) 6 | #define judge freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 7 | #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) 8 | #define pr(a) for(auto x : a) cout<>t; 19 | 20 | w(t) 21 | { 22 | int n, q; 23 | cin>>n>>q; 24 | 25 | string s; 26 | cin>>s; 27 | 28 | map mp; 29 | 30 | for(int i = 0 ; i < n ; i++) 31 | { 32 | mp[s[i]]++; 33 | } 34 | 35 | w(q) 36 | { 37 | int c; 38 | cin>>c; 39 | 40 | int ans = 0; 41 | 42 | for(auto x : mp) 43 | { 44 | x.second -= c; 45 | 46 | if(x.second > 0) 47 | ans+=x.second; 48 | } 49 | cout< 25 | using namespace std; 26 | 27 | 28 | // } Driver Code Ends 29 | 30 | 31 | //User function Template for C++ 32 | 33 | 34 | int sumUnderModulo(long long a,long long b) 35 | { 36 | int M=1000000007; 37 | //your code here 38 | int ans = a%M; 39 | ans+=(b%M); 40 | ans = ans%M; 41 | return ans; 42 | } 43 | 44 | // { Driver Code Starts. 45 | 46 | 47 | int main() { 48 | int T; 49 | cin>>T; 50 | while(T--) 51 | { 52 | long long a; 53 | long long b; 54 | cin>>a>>b; 55 | cout<left == NULL and root->right == NULL){ 47 | return 1; 48 | } 49 | 50 | return countLeaves(root->left) + countLeaves(root->right); 51 | 52 | } -------------------------------------------------------------------------------- /Geeks For Geeks/Reverse a string.cpp: -------------------------------------------------------------------------------- 1 | /* Geeks for Geeks */ 2 | /* Title - Reverse a string */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 7/11/2020 */ 5 | 6 | 7 | // You are given a string s. You need to reverse the string. 8 | 9 | // Example 1: 10 | 11 | // Input: 12 | // s = Geeks 13 | // Output: skeeG 14 | // Example 2: 15 | 16 | // Input: 17 | // s = for 18 | // Output: rof 19 | // Your Task: 20 | 21 | // You only need to complete the function reverseWord() that takes s as parameter and returns the reversed string. 22 | 23 | // Expected Time Complexity: O(|S|). 24 | // Expected Auxiliary Space: O(1). 25 | 26 | // Constraints: 27 | // 1 <= |s| <= 10000 28 | 29 | // { Driver Code Starts 30 | //Initial Template for C++ 31 | 32 | 33 | #include 34 | using namespace std; 35 | 36 | 37 | string reverseWord(string str); 38 | 39 | 40 | int main() { 41 | 42 | int t; 43 | cin>>t; 44 | while(t--) 45 | { 46 | string s; 47 | cin >> s; 48 | 49 | cout << reverseWord(s) << endl; 50 | } 51 | return 0; 52 | 53 | } 54 | 55 | // } Driver Code Ends 56 | 57 | 58 | //User function Template for C++ 59 | 60 | string reverseWord(string str){ 61 | 62 | //Your code here 63 | int n = str.length(); 64 | int j = n-1; 65 | int i = 0; 66 | 67 | while(i 15 | using namespace std; 16 | 17 | long long getSmallestDivNum(long long n); 18 | 19 | int main() { 20 | // your code goes here 21 | int t; 22 | cin>>t; 23 | while(t--) 24 | { 25 | int n; 26 | cin>>n; 27 | cout< 31 | using namespace std; 32 | void print(int *a,int i,int n,int k){ 33 | if(i==n) 34 | return; 35 | if(a[i]==k) 36 | cout<>n; 42 | int a[n]; 43 | for(int i=0;i>a[i]; 45 | int m; 46 | cin>>m; 47 | print(a,0,n,m); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Hacker Blocks/Binary To Decimal.cpp: -------------------------------------------------------------------------------- 1 | /* Hacker Blocks */ 2 | /* Title - Binary To Decimal */ 3 | //Take N (number in binary format). Write a function that converts it to decimal format and Print the value returned. 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | int convert(int num) 9 | { 10 | int count = 0; 11 | int sum =0; 12 | int temp; 13 | while(num>0) 14 | { 15 | temp = (num%10)*pow(2,count++); 16 | sum += temp; 17 | num /= 10; 18 | } 19 | return sum; 20 | } 21 | int main() { 22 | int n,num; 23 | cin>>n; 24 | num = convert(n); 25 | cout< 22 | using namespace std; 23 | #define MAX 100000 24 | 25 | int calc(int n, int res[], int resultSize){ 26 | int i = 0; 27 | int carry = 0; 28 | while(i>n; 45 | int res[MAX]; 46 | int resultSize = 1; 47 | res[0] = 1; 48 | if(n==0 || n==1) 49 | cout<<1; 50 | for(int i = 2;i<=n;i++) 51 | resultSize = calc(i,res,resultSize); 52 | for(int i = resultSize-1;i>=0;i--) 53 | cout< 29 | using namespace std; 30 | int main() { 31 | string s; 32 | cin>>s; 33 | int n = s.length(); 34 | cout< 25 | #include 26 | using namespace std; 27 | int convert(char *a,int n){ 28 | if(n==0) 29 | return 0; 30 | int digit = a[n-1]-'0'; 31 | int ans = convert(a,n-1); 32 | return ans*10+digit; 33 | } 34 | int main() { 35 | char temp[1000]; 36 | cin>>temp; 37 | cout< 6 | using namespace std; 7 | int convert(int x){ 8 | int octal=0; 9 | int i=1; 10 | while(x>0) 11 | { 12 | octal = octal + (x%8) *i; 13 | i *= 10; 14 | x /= 8; 15 | } 16 | return octal; 17 | } 18 | int main() { 19 | int x; 20 | cin>>x; 21 | cout< 23 | #include 24 | using namespace std; 25 | int main() { 26 | string s; 27 | cin>>s; 28 | int i = 0; 29 | int n = s.length(); 30 | while(i 25 | #include 26 | using namespace std; 27 | void gen(char* inp,int i){ 28 | if(inp[i]=='\0' || inp[i+1]=='\0') 29 | { 30 | return ; 31 | } 32 | if(inp[i]==inp[i+1]){ 33 | // int j=i+1; 34 | for(int k=strlen(inp);k>i+1;k--) 35 | inp[k]=inp[k-1]; 36 | inp[i+1]='*'; 37 | gen(inp,i+2); 38 | } 39 | else 40 | gen(inp,i+1); 41 | } 42 | int main() { 43 | char inp[10000]; 44 | cin>>inp; 45 | 46 | gen(inp,0); 47 | cout< 9 | using namespace std; 10 | int main() { 11 | int a=0,b=1,c,n; 12 | cin>>n; 13 | for(int i=1;i<=n;i++) 14 | { 15 | for(int k=1;k<=i;k++) 16 | { 17 | cout< 32 | using namespace std; 33 | int fun(int *a,int n,int k){ 34 | if(n==0) 35 | return -1; 36 | int i=fun(a+1,n-1,k); 37 | if(i==-1){ 38 | if(a[0]==k) 39 | return 0; 40 | else 41 | return -1; 42 | } 43 | return i+1; 44 | } 45 | int main() { 46 | int n; 47 | cin>>n; 48 | int a[n]; 49 | for(int i=0;i>a[i]; 51 | int m; 52 | cin>>m; 53 | cout< 29 | using namespace std; 30 | int main() { 31 | int n; 32 | cin>>n; 33 | int a[n]; 34 | for(int i=0;i>a[i]; 36 | for(int i=0;i 28 | #include 29 | #include 30 | using namespace std; 31 | bool compare(string s, string r){ 32 | string sr = s.append(r); 33 | string rs = r.append(s); 34 | return sr.compare(rs)>0?true:false; 35 | } 36 | int main() { 37 | int t; 38 | cin>>t; 39 | while(t--){ 40 | int n; 41 | cin>>n; 42 | string a[n]; 43 | for(int i=0;i>a[i]; 45 | sort(a,a+n,compare); 46 | for(int i=0;i 25 | using namespace std; 26 | 27 | void generateParenthesis(int n, int openB, int closeB, string str) { 28 | if(closeB == n) { 29 | cout< closeB) { 33 | generateParenthesis(n, openB, closeB+1, str+')'); 34 | } 35 | if(openB < n) { 36 | generateParenthesis(n, openB+1, closeB, str+'('); 37 | } 38 | } 39 | 40 | int main() 41 | { 42 | int n; 43 | cin>>n; 44 | generateParenthesis(n, 0, 0, ""); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Hacker Blocks/Help Ramu.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashmodak97/Competitive-Coding-and-Interview-Problems/88a36ee43ccaeacfdd8a521beac04b12c28ffe8e/Hacker Blocks/Help Ramu.cpp -------------------------------------------------------------------------------- /Hacker Blocks/Hollow Diamond Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* Hacker Blocks */ 2 | /* Title - Hollow Diamond Pattern */ 3 | // Take N (number of rows), print the following pattern (for N = 5). 4 | 5 | // * * * * * 6 | // * * * * 7 | // * * 8 | // * * * * 9 | // * * * * * 10 | 11 | #include 12 | using namespace std; 13 | 14 | int main() { 15 | 16 | int n; 17 | cin>>n; 18 | for(int i=0;i 9 | using namespace std; 10 | 11 | bool compare(pair a,pair b){ 12 | return a.second>t; 18 | while(t--){ 19 | int n,s,e; 20 | cin>>n; 21 | 22 | vector > v; 23 | for(int i=0;i>s>>e; 25 | v.push_back(make_pair(s,e)); 26 | } 27 | sort(v.begin(),v.end(),compare); 28 | int activity=1; 29 | int taken_activity = v[0].second; 30 | for(int i=1;i=taken_activity){ 32 | activity++; 33 | taken_activity = v[i].second; 34 | } 35 | } 36 | cout< 27 | using namespace std; 28 | bool pal(int a[],int start,int end){ 29 | if(start==end) 30 | return true; 31 | else{ 32 | while(start<=end){ 33 | if(a[start]==a[end]) 34 | return pal(a,start+1,end-1); 35 | else 36 | return false; 37 | } 38 | } 39 | return true; 40 | } 41 | int main() { 42 | int n; 43 | cin>>n; 44 | int a[n]; 45 | for(int i=0;i>a[i]; 47 | pal(a,0,n-1)?cout<<"true":cout<<"false"; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Hacker Blocks/Kth Root.cpp: -------------------------------------------------------------------------------- 1 | /* Hacker Blocks */ 2 | /* Title - Kth Root */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 25/06/2020 */ 5 | 6 | // You are given two integers n and k. Find the greatest integer x, such that, x^k <= n. 7 | 8 | // Input Format 9 | // First line contains number of test cases, T. Next T lines contains integers, n and k. 10 | 11 | // Constraints 12 | // 1<=T<=10 1<=N<=10^15 1<=K<=10^4 13 | 14 | // Output Format 15 | // Output the integer x 16 | 17 | // Sample Input 18 | // 2 19 | // 10000 1 20 | // 1000000000000000 10 21 | // Sample Output 22 | // 10000 23 | // 31 24 | // Explanation 25 | // For the first test case, for x=10000, 10000^1=10000=n 26 | 27 | #include 28 | #include 29 | using namespace std; 30 | int main() { 31 | int t; 32 | cin>>t; 33 | while(t--){ 34 | long long int n,k; 35 | cin>>n>>k; 36 | long long int s=1,e=n; 37 | long long int ans = 0; 38 | while(s<=e){ 39 | long long int mid = (s+e)/2; 40 | if(pow(mid,k)<=n){ 41 | ans = max(ans,mid); 42 | s=mid+1; 43 | } 44 | else 45 | e=mid-1; 46 | 47 | } 48 | cout< 16 | using namespace std; 17 | int main() { 18 | int n; 19 | int col,row; 20 | cin>>n; 21 | row=1; 22 | while(row<=n) 23 | { 24 | col=1; 25 | if(row%2==0) 26 | { 27 | cout<<1; 28 | while(col<=row-2){ 29 | cout<<0; 30 | col++; 31 | } 32 | cout<<1; 33 | 34 | } 35 | else 36 | { 37 | while(col<=row) 38 | { 39 | cout<<1; 40 | col++; 41 | } 42 | } 43 | cout<1), total character is n. First and last character is n-1 and rest are 0. 25 | #include 26 | using namespace std; 27 | int main() { 28 | int n; 29 | int row,col; 30 | cin>>n; 31 | row = 1; 32 | while(row<=n) 33 | { 34 | col = 1; 35 | if(row==1) 36 | cout<<1; 37 | else 38 | { 39 | cout< 29 | using namespace std; 30 | void generate(char *inp, char *out,int i,int j){ 31 | if(inp[i]=='\0'){ 32 | out[j]='\0'; 33 | cout<>a; 61 | char out[1000000]; 62 | generate(a,out,0,0); 63 | return 0; 64 | } -------------------------------------------------------------------------------- /Hacker Blocks/Max Frequency Characters.cpp: -------------------------------------------------------------------------------- 1 | /* Hacker Blocks */ 2 | /* Title - Max Frequency Characters */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 23/6/2020 */ 5 | 6 | // Take as input S, a string. Write a function that returns the character with maximum frequency. Print the value returned. 7 | 8 | // Input Format 9 | // String 10 | 11 | // Constraints 12 | // A string of length between 1 to 1000. 13 | 14 | // Output Format 15 | // Character 16 | 17 | // Sample Input 18 | // aaabacb 19 | // Sample Output 20 | // a 21 | // Explanation 22 | // For the given input string, a appear 4 times. Hence, it is the most frequent character. 23 | 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | int maxFrequency(){ 29 | string str; 30 | cin>>str; 31 | vector ar(26,0); 32 | for(int i=0;i 36 | using namespace std; 37 | 38 | 39 | int main() { 40 | int t; 41 | cin>>t; 42 | while(t--){ 43 | int n1,n2; 44 | list l1,l2; 45 | cin>>n1; 46 | for(int i=0;i>temp; 50 | l1.push_back(temp); 51 | } 52 | cin>>n2; 53 | for(int i=0;i>temp; 57 | l2.push_back(temp); 58 | } 59 | l1.merge(l2); 60 | for(auto x = l1.begin();x!=l1.end();x++){ 61 | cout<<(*x)<<" "; 62 | } 63 | cout< 25 | #include 26 | using namespace std; 27 | long long int power(long long int x, unsigned int y, int p) 28 | { 29 | long long int res = 1; // Initialize result 30 | 31 | x = x % p; // Update x if it is more than or 32 | // equal to p 33 | 34 | if (x == 0) return 0; // In case x is divisible by p; 35 | 36 | while (y > 0) 37 | { 38 | // If y is odd, multiply x with result 39 | if (y & 1) 40 | res = (res*x) % p; 41 | 42 | // y must be even now 43 | y = y>>1; // y = y/2 44 | x = (x*x) % p; 45 | } 46 | return res; 47 | } 48 | 49 | int main() { 50 | int a,b,c; 51 | cin>>a>>b>>c; 52 | // long long int x = pow(a%c,b); 53 | 54 | cout< 13 | using namespace std; 14 | int main() { 15 | int n; 16 | cin>>n; 17 | int i,j,k; 18 | for(i=1;i<=n;i++) 19 | { 20 | for(j=1;j<=i;j++) 21 | cout<0;k--) 23 | cout<<" "; 24 | if(i==n) 25 | j--; 26 | for(int l=j-1;l>=1;l--) 27 | cout< 27 | using namespace std; 28 | void moveatend(char *inp, int i, int l) 29 | { 30 | if(i>=l){ 31 | return; 32 | } 33 | char x=inp[i]; 34 | if(x!='x'){ 35 | cout<>inp; 46 | int l = strlen(inp); 47 | moveatend(inp,0,l); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Hacker Blocks/Pattern Double Sided Arrow.cpp: -------------------------------------------------------------------------------- 1 | /* Hacker Blocks */ 2 | /* Title - Pattern Double Sided Arrow */ 3 | // Take N as input. For a value of N=7, we wish to draw the following pattern : 4 | 5 | // 1 6 | // 2 1 1 2 7 | // 3 2 1 1 2 3 8 | // 4 3 2 1 1 2 3 4 9 | // 3 2 1 1 2 3 10 | // 2 1 1 2 11 | // 1 12 | 13 | #include 14 | using namespace std; 15 | int main() { 16 | int n; 17 | cin>>n; 18 | for(int i=1;i<=(n+1)/2;i++){ 19 | for(int j=1;j<=(2*(n+1-2*i));j++){ 20 | cout<<" "; 21 | } 22 | for(int j = i;j>0;j--){ 23 | cout<1){ 30 | for(int j=1;j<=i;j++){ 31 | cout<0;i--){ 37 | for(int j=1;j<=(2*(n+1-2*i));j++){ 38 | cout<<" "; 39 | } 40 | for(int j = i;j>0;j--){ 41 | cout<1){ 48 | for(int j=1;j<=i;j++){ 49 | cout< 18 | using namespace std; 19 | 20 | 21 | int main(){ 22 | 23 | int n; 24 | cin>>n; 25 | 26 | //Upper 27 | 28 | for(int i=n;i>=1;i--){ 29 | for(int j=n;j>=i;j--){ 30 | cout<=1;j--){ 34 | cout<<" "; 35 | } 36 | 37 | for(int j=i;j<=n;j++){ 38 | cout<=0;i--) 43 | cout<=i;j--){ 51 | cout< 12 | using namespace std; 13 | int main() { 14 | int n; 15 | cin>>n; 16 | for(int i=1;i<=n;i++) 17 | { 18 | for(int j=i;j 11 | using namespace std; 12 | int main() { 13 | int n; 14 | int row,col; 15 | cin>>n; 16 | row = 1; 17 | while(row<=n) 18 | { 19 | col = 1; 20 | if(row==1) 21 | cout<<1; 22 | else 23 | { 24 | cout< 26 | using namespace std; 27 | int countSetBits(int n){ 28 | int ans=0; 29 | while(n>0){ 30 | if(n&1==1) 31 | ans++; 32 | n=n>>1; 33 | } 34 | return ans; 35 | } 36 | int main() { 37 | int q,a,b; 38 | cin>>q; 39 | while(q--){ 40 | cin>>a>>b; 41 | int count =0; 42 | for(int i=a;i<=b;i++){ 43 | count+= countSetBits(i); 44 | } 45 | cout< 4 | #include 5 | using namespace std; 6 | int main() { 7 | int N; 8 | cin>>N; 9 | if( N == 1 || N == 2 ) 10 | cout<<-1; 11 | else if( N % 2 == 0 ) 12 | { 13 | int temp = pow(N/2,2); 14 | cout< 22 | using namespace std; 23 | 24 | int main() { 25 | stack a,b,c; 26 | int n; 27 | cin>>n; 28 | for(int i=0;i 27 | #include 28 | using namespace std; 29 | void gen(char* inp,int i){ 30 | if(inp[i]=='\0' or inp[i+1]=='\0') 31 | return ; 32 | if(inp[i]==inp[i+1]){ 33 | for(int j=i+1;j>inp; 43 | gen(inp,0); 44 | cout< 31 | using namespace std; 32 | void replacepi(char *a,int i){ 33 | if(a[i]=='\0' or a[i+1]=='\0') 34 | return; 35 | if(a[i]=='p' and a[i+1]=='i'){ 36 | int j=i; 37 | while(a[j]!='\0') 38 | j++; 39 | while(j>=i+2){ 40 | a[j+2]=a[j]; 41 | j--; 42 | } 43 | a[i]='3'; 44 | a[i+1]='.'; 45 | a[i+2]='1'; 46 | a[i+3]='4'; 47 | replacepi(a,i+4); 48 | } 49 | else 50 | replacepi(a,i+1); 51 | } 52 | int main() { 53 | int t; 54 | cin>>t; 55 | while(t--){ 56 | char a[1000]; 57 | cin>>a; 58 | // int n = strlen(a); 59 | replacepi(a,0); 60 | cout< 29 | #include 30 | using namespace std; 31 | 32 | int main() { 33 | int n; 34 | cin>>n; 35 | int a[n][n]; 36 | for(int i=0;i>a[i][j]; 39 | for(int i=0;i 25 | using namespace std; 26 | bool isSorted(int ar[],int N) 27 | { 28 | if(N==1) 29 | { 30 | return true; 31 | } 32 | if(ar[0]<=ar[1] && isSorted(ar+1,N-1)) 33 | { 34 | return true; 35 | } 36 | return false; 37 | } 38 | int main() 39 | { 40 | int N; 41 | cin>>N; 42 | int ar[N]; 43 | for(int i=0;i>ar[i]; 46 | } 47 | bool y=isSorted(ar,N); 48 | if(y==1) 49 | cout<<"true"; 50 | else 51 | cout<<"false"; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Hacker Blocks/Sorting In Linear Time.cpp: -------------------------------------------------------------------------------- 1 | /* Hacker Blocks */ 2 | /* Title - Sorting In Linear Time */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 27/06/2020 */ 5 | 6 | // You will be given an array containing only 0s, 1s and 2s. you have sort the array in linear time that is O(N) where N is the size of the array. 7 | 8 | // Input Format 9 | // The first line contains N, which is the size of the array. The following N lines contain either 0, or 1, or 2. 10 | 11 | // Constraints 12 | // 1 <= N <= 10^6 13 | // Each input element x, such that x ∈ { 0, 1, 2 }. 14 | 15 | // Output Format 16 | // Output the sorted array with each element separated by a newline. 17 | 18 | // Sample Input 19 | // 5 20 | // 0 21 | // 1 22 | // 2 23 | // 1 24 | // 2 25 | // Sample Output 26 | // 0 27 | // 1 28 | // 1 29 | // 2 30 | // 2 31 | 32 | #include 33 | using namespace std; 34 | void swap(int &a,int &b) 35 | { 36 | a = a + b; 37 | b = a - b; 38 | a = a - b; 39 | } 40 | int main() { 41 | int n; 42 | cin>>n; 43 | int a[n]; 44 | for(int i=0;i>a[i]; 46 | int low=0,mid=0,high=n-1; 47 | while(mid<=high){ 48 | if(a[mid]==0){ 49 | swap(a[mid],a[low]); 50 | low++; 51 | mid++; 52 | } 53 | else if(a[mid]==1) 54 | mid++; 55 | else{ 56 | swap(a[mid],a[high]); 57 | high--; 58 | } 59 | } 60 | for(int i=0;i 24 | using namespace std; 25 | int main() { 26 | int m,n; 27 | cin>>m>>n; 28 | int a[m][n]; 29 | int i,j,sr,sc,er,ec; 30 | for(i=0;i>a[i][j]; 33 | sc=sr=0; 34 | er=m-1; 35 | ec=n-1; 36 | while(sr<=er && sc<=ec){ 37 | for(i=sr;i<=er;i++) 38 | cout<sr;i--) 43 | cout<sc;i--) 45 | cout< 25 | using namespace std; 26 | void compressed() 27 | { 28 | string str; 29 | cin>>str; 30 | int n = str.length(); 31 | int i=0; 32 | int count = 0; 33 | char current; 34 | while(i=1) 53 | cout< 30 | #include 31 | using namespace std; 32 | int main() { 33 | int n,t; 34 | cin>>n; 35 | int a[n]; 36 | for(int i=0;i>a[i]; 38 | cin>>t; 39 | sort(a,a+n); 40 | int l=0,r=n-1; 41 | while(lt) 45 | r--; 46 | else{ 47 | cout< 27 | #include 28 | #include 29 | using namespace std; 30 | set s; 31 | void permute(char *inp,int i){ 32 | if(inp[i]=='\0') 33 | { 34 | s.insert(inp); 35 | return; 36 | } 37 | for(int j=i;inp[j]!='\0';j++){ 38 | swap(inp[i],inp[j]); 39 | permute(inp,i+1); 40 | swap(inp[i],inp[j]); 41 | } 42 | return; 43 | } 44 | int main() { 45 | char inp[10000]; 46 | cin>>inp; 47 | permute(inp,0); 48 | for(auto x: s){ 49 | cout< 7 | using namespace std; 8 | int main() { 9 | int n; 10 | cin>>n; 11 | int x = 0; 12 | for(int i=1;i<=n;i++){ 13 | int m; 14 | cin>>m; 15 | x^=m; 16 | } 17 | cout< 9 | using namespace std; 10 | int main() { 11 | int n; 12 | cin>>n; 13 | int x=0; 14 | int a[n]; 15 | for(int i=0;i>a[i]; 17 | x = x^a[i]; 18 | } 19 | int temp = x; 20 | int pos=0; 21 | while((temp&1)==0){ 22 | pos++; 23 | temp=temp>>1; 24 | } 25 | int mask = (1<0) 29 | res=res^a[i]; 30 | } 31 | int ans = res^x; 32 | cout< 28 | #include 29 | #include 30 | using namespace std; 31 | int main() { 32 | int n; 33 | cin>>n; 34 | int a[n]; 35 | for(int i=0;i>a[i]; 37 | unordered_set s(a,a+n); 38 | int sum_array = accumulate(a,a+n,0); 39 | int sum_set = accumulate(s.begin(),s.end(),0); 40 | cout<<(3*sum_set-sum_array)/2; //(k*sum_set-sum_array)/(k-1) where is number of similar elements 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Hacker Blocks/Von Neuman Loves Binary.cpp: -------------------------------------------------------------------------------- 1 | /*Coding Blocks Hacker Blocks*/ 2 | /* Title - Von Neuman Loves Binary */ 3 | 4 | //Given a binary number ,help Von Neuman to find out its decimal representation. For eg 000111 in binary is 7 in decimal. 5 | // 6 | //Input Format 7 | //The first line contains N , the number of binary numbers. Next N lines contain N integers each representing binary represenation of number. 8 | #include 9 | #include 10 | using namespace std; 11 | int main() { 12 | int n,num; 13 | cin>>n; 14 | int i; 15 | for(i=1;i<=n;i++) 16 | { 17 | cin>>num; 18 | int count = 0; 19 | int sum =0; 20 | int temp; 21 | while(num>0) 22 | { 23 | temp = (num%10)*pow(2,count++); 24 | sum += temp; 25 | num /= 10; 26 | } 27 | cout< 25 | using namespace std; 26 | int main() { 27 | int m,n; 28 | cin>>m>>n; 29 | int i,j; 30 | int a[m][n]; 31 | for(i=0;i>a[i][j]; 34 | for(i=0;i=0;j--) 42 | cout< 30 | using namespace std; 31 | int main () { 32 | int a,b; 33 | cin>>a>>b; 34 | int x = 0; 35 | int i,j; 36 | for(i=a;i 2 -> 4 -> 6 or 0 -> 2 -> 3 -> 4 ->6. The first path takes 3 jumps while the second takes 4. 6 | // 7 | //Function Description 8 | // 9 | //Complete the jumpingOnClouds function in the editor below. It should return the minimum number of jumps required, as an integer. 10 | // 11 | //jumpingOnClouds has the following parameter(s): 12 | // 13 | //c: an array of binary integers 14 | 15 | int jumpingOnClouds(vector c) { 16 | int jumps=0; 17 | int i=0; 18 | while(i arr) { 10 | int minSum = INT_MAX; 11 | int maxSum = INT_MIN; 12 | int currSum = 0; 13 | for(int i=0;i ar) { 16 | int sum = 0; 17 | for(int i=0;i ar) { 16 | 17 | int count=0; 18 | for(int i=0;i Solution::plusOne(vector &A) { 18 | 19 | int n = A.size(); 20 | int carry = 0; 21 | int s = A[n-1]; 22 | s+=1; 23 | A[n-1]=s%10; 24 | carry = s/10; 25 | 26 | // cout<=0;i--) 29 | { 30 | if(carry==1){ 31 | s = A[i]+carry; 32 | A[i] = s%10; 33 | carry = s/10; 34 | } 35 | } 36 | vector ans; 37 | if(carry == 1) 38 | { 39 | ans.push_back(carry); 40 | for(int i=0;i &A) { 14 | 15 | vector B; 16 | bool allZero = true; 17 | for(int i=0;i0?1:0; 39 | } -------------------------------------------------------------------------------- /Interview Bit/Longest Common Prefix.cpp: -------------------------------------------------------------------------------- 1 | /* Interview Bit */ 2 | /* Title - Longest Common Prefix */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 26/09/2020 */ 5 | 6 | // Given the array of strings A, 7 | // you need to find the longest string S which is the prefix of ALL the strings in the array. 8 | 9 | // Longest common prefix for a pair of strings S1 and S2 is the longest string S which is the prefix of both S1 10 | // and S2. 11 | 12 | // For Example, longest common prefix of "abcdefgh" and "abcefgh" is "abc". 13 | 14 | 15 | 16 | // Input Format 17 | 18 | // The only argument given is an array of strings A. 19 | // Output Format 20 | 21 | // Return longest common prefix of all strings in A. 22 | // For Example 23 | 24 | // Input 1: 25 | // A = ["abcdefgh", "aefghijk", "abcefgh"] 26 | // Output 1: 27 | // "a" 28 | // Explanation 1: 29 | // Longest common prefix of all the strings is "a". 30 | 31 | // Input 2: 32 | // A = ["abab", "ab", "abcd"]; 33 | // Output 2: 34 | // "ab" 35 | // Explanation 2: 36 | // Longest common prefix of all the strings is "ab". 37 | 38 | string Solution::longestCommonPrefix(vector &A) { 39 | sort(A.begin(),A.end()); 40 | int n = A.size(); 41 | int minval = min(A[0].length(),A[n-1].length()); 42 | int i=0; 43 | string first = A[0], last = A[n-1]; 44 | while(i &A) { 37 | int n = A.size(); 38 | int sum = INT_MIN; 39 | int currentSum = 0; 40 | for(int i=0;i &A, vector &B) { 29 | int count = 0; 30 | int a = 0; int b = 0; 31 | for (int i=0; i < (A.size()-1); ++i) { 32 | a = abs((A[i+1]-A[i])); 33 | b = abs((B[i+1]-B[i])); 34 | count += max(a,b); 35 | } 36 | return count; 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Akash Modak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Leet Code/Add Digits.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Add Digits */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 26/7/2020 */ 5 | 6 | // Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 7 | 8 | // Example: 9 | 10 | // Input: 38 11 | // Output: 2 12 | // Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. 13 | // Since 2 has only one digit, return it. 14 | // Follow up: 15 | // Could you do it without any loop/recursion in O(1) runtime? 16 | 17 | class Solution { 18 | public: 19 | int addDigits(int num) { 20 | if(num==0) 21 | return 0; 22 | else if(num%9==0) 23 | return 9; 24 | else 25 | return num%9; 26 | } 27 | }; -------------------------------------------------------------------------------- /Leet Code/All Paths From Source to Target.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - All Paths From Source to Target */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 25/7/2020 */ 5 | 6 | // Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and return them in any order. 7 | 8 | // The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists. 9 | 10 | // Example: 11 | // Input: [[1,2], [3], [3], []] 12 | // Output: [[0,1,3],[0,2,3]] 13 | // Explanation: The graph looks like this: 14 | // 0--->1 15 | // | | 16 | // v v 17 | // 2--->3 18 | // There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3. 19 | // Note: 20 | 21 | // The number of nodes in the graph will be in the range [2, 15]. 22 | // You can print different paths in any order, but you should keep the order of nodes inside one path. 23 | 24 | class Solution { 25 | public: 26 | void dfs(vector> graph,int target,int i,vector& t,vector>& ans){ 27 | if(i==target) 28 | ans.push_back(t); 29 | for(auto x:graph[i]){ 30 | t.push_back(x); 31 | dfs(graph,target,x,t,ans); 32 | t.pop_back(); 33 | } 34 | } 35 | vector> allPathsSourceTarget(vector>& graph) { 36 | vector> res; 37 | int target = graph.size()-1; 38 | vector t; 39 | t.push_back(0); 40 | dfs(graph,target,0,t,res); 41 | return res; 42 | } 43 | }; -------------------------------------------------------------------------------- /Leet Code/Angle Between Hands of a Clock.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Angle Between Hands of a Clock */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 14/07/2020 */ 5 | 6 | // Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand. 7 | 8 | 9 | 10 | // Example 1: 11 | 12 | 13 | 14 | // Input: hour = 12, minutes = 30 15 | // Output: 165 16 | // Example 2: 17 | 18 | 19 | 20 | // Input: hour = 3, minutes = 30 21 | // Output: 75 22 | // Example 3: 23 | 24 | 25 | 26 | // Input: hour = 3, minutes = 15 27 | // Output: 7.5 28 | // Example 4: 29 | 30 | // Input: hour = 4, minutes = 50 31 | // Output: 155 32 | // Example 5: 33 | 34 | // Input: hour = 12, minutes = 0 35 | // Output: 0 36 | 37 | 38 | // Constraints: 39 | 40 | // 1 <= hour <= 12 41 | // 0 <= minutes <= 59 42 | // Answers within 10^-5 of the actual value will be accepted as correct. 43 | 44 | class Solution { 45 | public: 46 | double angleClock(int hour, int minutes) { 47 | double hourAngle = (hour%12)*30+minutes*0.5; 48 | double minAngle = minutes*6; 49 | double diffAngle = abs(hourAngle-minAngle); 50 | return min(diffAngle,360-diffAngle); 51 | } 52 | }; -------------------------------------------------------------------------------- /Leet Code/Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Best Time to Buy and Sell Stock */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 24/09/2020 */ 5 | 6 | // Say you have an array for which the ith element is the price of a given stock on day i. 7 | 8 | // If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. 9 | 10 | // Note that you cannot sell a stock before you buy one. 11 | 12 | // Example 1: 13 | 14 | // Input: [7,1,5,3,6,4] 15 | // Output: 5 16 | // Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. 17 | // Not 7-1 = 6, as selling price needs to be larger than buying price. 18 | // Example 2: 19 | 20 | // Input: [7,6,4,3,1] 21 | // Output: 0 22 | // Explanation: In this case, no transaction is done, i.e. max profit = 0. 23 | 24 | class Solution { 25 | public: 26 | int maxProfit(vector& prices) { 27 | int n = prices.size(); 28 | if(n<=1) 29 | return 0; 30 | if(n==2 and prices[1]>prices[0]) 31 | return prices[1]-prices[0]; 32 | int minp = INT_MAX; 33 | int maxp =0; 34 | for(int i=0;imaxp) 39 | maxp = prices[i]-minp; 40 | } 41 | return maxp; 42 | } 43 | }; -------------------------------------------------------------------------------- /Leet Code/Binary Tree Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Binary Tree Inorder Traversal */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 21/09/2020 */ 5 | 6 | // Given the root of a binary tree, return the inorder traversal of its nodes' values. 7 | 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * struct TreeNode { 12 | * int val; 13 | * TreeNode *left; 14 | * TreeNode *right; 15 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 16 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 17 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 18 | * }; 19 | */ 20 | class Solution { 21 | public: 22 | vector v; 23 | vector inorderTraversal(TreeNode* root) { 24 | if(root==NULL) 25 | return v; 26 | 27 | inorderTraversal(root->left); 28 | v.push_back(root->val); 29 | inorderTraversal(root->right); 30 | 31 | return v; 32 | } 33 | }; -------------------------------------------------------------------------------- /Leet Code/Binary Tree Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Binary Tree Postorder Traversal */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 21/09/2020 */ 5 | 6 | // Given the root of a binary tree, return the postorder traversal of its nodes' values. 7 | 8 | // Follow up: Recursive solution is trivial, could you do it iteratively? 9 | 10 | 11 | /** 12 | * Definition for a binary tree node. 13 | * struct TreeNode { 14 | * int val; 15 | * TreeNode *left; 16 | * TreeNode *right; 17 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 18 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 19 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 20 | * }; 21 | */ 22 | class Solution { 23 | public: 24 | vector v; 25 | vector postorderTraversal(TreeNode* root) { 26 | if(root==NULL) 27 | return v; 28 | 29 | postorderTraversal(root->left); 30 | postorderTraversal(root->right); 31 | v.push_back(root->val); 32 | 33 | return v; 34 | } 35 | }; 36 | 37 | -------------------------------------------------------------------------------- /Leet Code/Binary Tree Preorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Binary Tree Preorder Traversal */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 21/09/2020 */ 5 | 6 | // Given a binary tree, return the preorder traversal of its nodes' values. 7 | 8 | // Example: 9 | 10 | // Input: [1,null,2,3] 11 | // 1 12 | // \ 13 | // 2 14 | // / 15 | // 3 16 | 17 | // Output: [1,2,3] 18 | // Follow up: Recursive solution is trivial, could you do it iteratively? 19 | 20 | /** 21 | * Definition for a binary tree node. 22 | * struct TreeNode { 23 | * int val; 24 | * TreeNode *left; 25 | * TreeNode *right; 26 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 27 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 28 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 29 | * }; 30 | */ 31 | class Solution { 32 | public: 33 | vector v; 34 | vector preorderTraversal(TreeNode* root) { 35 | if(root==NULL) 36 | return v; 37 | 38 | v.push_back(root->val); 39 | preorderTraversal(root->left); 40 | preorderTraversal(root->right); 41 | return v; 42 | } 43 | }; -------------------------------------------------------------------------------- /Leet Code/Bitwise AND of Numbers Range.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Bitwise AND of Numbers Range */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 17/10/2020 */ 5 | 6 | // Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. 7 | 8 | // Example 1: 9 | 10 | // Input: [5,7] 11 | // Output: 4 12 | // Example 2: 13 | 14 | // Input: [0,1] 15 | // Output: 0 16 | 17 | class Solution { 18 | public: 19 | int rangeBitwiseAnd(int m, int n) { 20 | if(m==0 or n==0) 21 | return 0; 22 | if(m==n) 23 | return m; 24 | int num1 = log2(m)+1; 25 | int num2 = log2(n)+1; 26 | 27 | if(num1==num2){ 28 | if(m+1==n) 29 | return m&n; 30 | int x = m^n; 31 | int count = 0; 32 | while(num1--){ 33 | if(x&1) 34 | count = 0; 35 | else 36 | count++; 37 | x = x>>1; 38 | } 39 | int ans = 0; 40 | int diff = num2 - count; 41 | while(count--){ 42 | ans = ans<<1; 43 | ans = ans | 1; 44 | } 45 | ans = ans<& people, int limit) { 31 | sort(people.begin(), people.end()); 32 | int i=0,j=people.size()-1,count=0; 33 | while(i<=j){ 34 | if(people[i]+people[j]<=limit) 35 | i++; 36 | j--; 37 | count++; 38 | } 39 | return count; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Leet Code/Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Climbing stairs */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 31/7/2020 */ 5 | 6 | // You are climbing a stair case. It takes n steps to reach to the top. 7 | 8 | // Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 9 | 10 | // Example 1: 11 | 12 | // Input: 2 13 | // Output: 2 14 | // Explanation: There are two ways to climb to the top. 15 | // 1. 1 step + 1 step 16 | // 2. 2 steps 17 | // Example 2: 18 | 19 | // Input: 3 20 | // Output: 3 21 | // Explanation: There are three ways to climb to the top. 22 | // 1. 1 step + 1 step + 1 step 23 | // 2. 1 step + 2 steps 24 | // 3. 2 steps + 1 step 25 | 26 | 27 | // Constraints: 28 | 29 | // 1 <= n <= 45 30 | 31 | class Solution{ 32 | public: 33 | int climbingStairs(int n){ 34 | if(n==1) 35 | return 1; 36 | int a=1; 37 | int b=2; 38 | for(int i=3;i<=n;i++){ 39 | int c = a+b; 40 | a=b; 41 | b=c; 42 | } 43 | return b; 44 | } 45 | }; -------------------------------------------------------------------------------- /Leet Code/Combinations.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Combinations */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 06/06/2023 */ 5 | 6 | // Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. 7 | 8 | // You may return the answer in any order. 9 | 10 | 11 | 12 | // Example 1: 13 | 14 | // Input: n = 4, k = 2 15 | // Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] 16 | // Explanation: There are 4 choose 2 = 6 total combinations. 17 | // Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination. 18 | // Example 2: 19 | 20 | // Input: n = 1, k = 1 21 | // Output: [[1]] 22 | // Explanation: There is 1 choose 1 = 1 total combination. 23 | 24 | class Solution { 25 | public: 26 | void backtrack(int i, int j, int k, int n, vector &temp, vector> & res){ 27 | if(i==k){ 28 | res.push_back(temp); 29 | return; 30 | } 31 | for(int l=j;l<=n;l++){ 32 | temp.push_back(l); 33 | j++; 34 | backtrack(i+1,j,k,n,temp,res); 35 | temp.pop_back(); 36 | } 37 | 38 | } 39 | vector> combine(int n, int k) { 40 | vector temp; 41 | vector> res; 42 | backtrack(0,1,k,n,temp,res); 43 | return res; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /Leet Code/Concatenation of Array.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Concatenation of Array */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 05/08/2021 */ 5 | 6 | // Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). 7 | 8 | // Specifically, ans is the concatenation of two nums arrays. 9 | 10 | // Return the array ans. 11 | 12 | 13 | 14 | // Example 1: 15 | 16 | // Input: nums = [1,2,1] 17 | // Output: [1,2,1,1,2,1] 18 | // Explanation: The array ans is formed as follows: 19 | // - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]] 20 | // - ans = [1,2,1,1,2,1] 21 | // Example 2: 22 | 23 | // Input: nums = [1,3,2,1] 24 | // Output: [1,3,2,1,1,3,2,1] 25 | // Explanation: The array ans is formed as follows: 26 | // - ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]] 27 | // - ans = [1,3,2,1,1,3,2,1] 28 | 29 | 30 | class Solution { 31 | public: 32 | vector getConcatenation(vector& nums) { 33 | vector ans(2*nums.size()); 34 | 35 | for(int i=0;i& height) { 15 | int sum = 0; 16 | int i = 0; 17 | int j = height.size() - 1; 18 | int current = 0; 19 | while(i& nums, int k) { 24 | unordered_map> m; 25 | for(int i=0;i0){ 27 | for(auto x: m[nums[i]]){ 28 | if(abs(x-i)<=k) 29 | return true; 30 | } 31 | } 32 | m[nums[i]].push_back(i); 33 | } 34 | return false; 35 | } 36 | }; -------------------------------------------------------------------------------- /Leet Code/Contains Duplicate.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Contains Duplicate */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 27/08/2020 */ 5 | 6 | // Given an array of integers, find if the array contains any duplicates. 7 | 8 | // Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 9 | 10 | // Example 1: 11 | 12 | // Input: [1,2,3,1] 13 | // Output: true 14 | // Example 2: 15 | 16 | // Input: [1,2,3,4] 17 | // Output: false 18 | // Example 3: 19 | 20 | // Input: [1,1,1,3,3,4,3,2,4,2] 21 | // Output: true 22 | 23 | class Solution { 24 | public: 25 | bool containsDuplicate(vector& nums) { 26 | unordered_map m; 27 | for(auto x: nums){ 28 | m[x]++; 29 | if(m[x]>1) 30 | return true; 31 | 32 | } 33 | return false; 34 | } 35 | }; -------------------------------------------------------------------------------- /Leet Code/Count Primes.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Count Primes */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 27/12/2020 */ 5 | 6 | // Count the number of prime numbers less than a non-negative number, n. 7 | 8 | 9 | 10 | // Example 1: 11 | 12 | // Input: n = 10 13 | // Output: 4 14 | // Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 15 | // Example 2: 16 | 17 | // Input: n = 0 18 | // Output: 0 19 | // Example 3: 20 | 21 | // Input: n = 1 22 | // Output: 0 23 | 24 | class Solution { 25 | public: 26 | int countPrimes(int n) { 27 | if(n==0 || n==1 || n==2){ 28 | return 0; 29 | } 30 | vector prime(n+1,1); 31 | prime[0] = 0; 32 | prime[1] = 0; 33 | // prime[2] = 0; 34 | for(long long int i=2;i<=n;i++){ 35 | if(prime[i]==1){ 36 | for(long long int j=i*i;j<=n;j+=i) 37 | prime[j] = 0; 38 | } 39 | } 40 | int count = 0; 41 | for(int i=2;i& words) { 31 | bool arr[26] = {}; 32 | bool flag = false; 33 | int count=0; 34 | for(int i=0;i countBits(int num) { 25 | vector dp(num+1); 26 | dp[0] = 0; 27 | if(num==0) 28 | return dp; 29 | dp[1] = 1; 30 | for(int i=2;i<=num;i++){ 31 | if(i%2==0) 32 | dp[i] = dp[i/2]; 33 | else 34 | dp[i] = dp[i-1] + 1; 35 | } 36 | return dp; 37 | } 38 | }; -------------------------------------------------------------------------------- /Leet Code/DI String Match.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - DI String Match */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 02/05/2023 */ 5 | 6 | // A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where: 7 | 8 | // s[i] == 'I' if perm[i] < perm[i + 1], and 9 | // s[i] == 'D' if perm[i] > perm[i + 1]. 10 | // Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them. 11 | 12 | 13 | 14 | // Example 1: 15 | 16 | // Input: s = "IDID" 17 | // Output: [0,4,1,3,2] 18 | // Example 2: 19 | 20 | // Input: s = "III" 21 | // Output: [0,1,2,3] 22 | // Example 3: 23 | 24 | // Input: s = "DDI" 25 | // Output: [3,2,0,1] 26 | 27 | class Solution { 28 | public: 29 | vector diStringMatch(string s) { 30 | int low = 0, high = s.length(); 31 | vector res; 32 | 33 | for(int i=0;i dailyTemperatures(vector& temperatures) { 26 | vector res(temperatures.size()); 27 | stack s; 28 | for(int i=0;i "New York" -> "Lima" -> "Sao Paulo". 17 | // Example 2: 18 | 19 | // Input: paths = [["B","C"],["D","B"],["C","A"]] 20 | // Output: "A" 21 | // Explanation: All possible trips are: 22 | // "D" -> "B" -> "C" -> "A". 23 | // "B" -> "C" -> "A". 24 | // "C" -> "A". 25 | // "A". 26 | // Clearly the destination city is "A". 27 | // Example 3: 28 | 29 | // Input: paths = [["A","Z"]] 30 | // Output: "Z" 31 | 32 | class Solution { 33 | public: 34 | string destCity(vector>& paths) { 35 | unordered_set mp; 36 | for(int i=0;i& arr) { 26 | unordered_map m; 27 | if(arr.size()==0) 28 | return 0; 29 | float avg = arr.size()/4; 30 | int ans = 0; 31 | for(int i=0;iavg) 34 | return ans = arr[i]; 35 | } 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /Leet Code/Excel Sheet Column Number.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Excel Sheet Column Number */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 10/8/2020 */ 5 | 6 | // Given a column title as appear in an Excel sheet, return its corresponding column number. 7 | 8 | // For example: 9 | 10 | // A -> 1 11 | // B -> 2 12 | // C -> 3 13 | // ... 14 | // Z -> 26 15 | // AA -> 27 16 | // AB -> 28 17 | // ... 18 | // Example 1: 19 | 20 | // Input: "A" 21 | // Output: 1 22 | // Example 2: 23 | 24 | // Input: "AB" 25 | // Output: 28 26 | // Example 3: 27 | 28 | // Input: "ZY" 29 | // Output: 701 30 | 31 | 32 | // Constraints: 33 | 34 | // 1 <= s.length <= 7 35 | // s consists only of uppercase English letters. 36 | // s is between "A" and "FXSHRXW". 37 | 38 | class Solution { 39 | public: 40 | int titleToNumber(string s) { 41 | 42 | int cell = 0; 43 | int n = s.length()-1; 44 | for(int i=0;s[i]!='\0';i++){ 45 | int temp = s[i] - 'A' + 1; 46 | cell += pow(26,n) * temp; 47 | n--; 48 | } 49 | return cell; 50 | } 51 | }; -------------------------------------------------------------------------------- /Leet Code/Fibonacci Number.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Fibonacci Number */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 29/05/2023 */ 5 | 6 | // The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, 7 | 8 | // F(0) = 0, F(1) = 1 9 | // F(n) = F(n - 1) + F(n - 2), for n > 1. 10 | // Given n, calculate F(n). 11 | 12 | 13 | 14 | // Example 1: 15 | 16 | // Input: n = 2 17 | // Output: 1 18 | // Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. 19 | // Example 2: 20 | 21 | // Input: n = 3 22 | // Output: 2 23 | // Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. 24 | // Example 3: 25 | 26 | // Input: n = 4 27 | // Output: 3 28 | // Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3. 29 | 30 | class Solution { 31 | public: 32 | int topDown(int n, vector &dp){ 33 | if(n==0 || n==1) return n; 34 | if(n<0) return 0; 35 | if(dp[n]) return dp[n]; 36 | return dp[n]=topDown(n-1,dp)+topDown(n-2,dp); 37 | } 38 | int fib(int n) { 39 | vector dp(n+1); 40 | return topDown(n,dp); 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /Leet Code/Find All Duplicates in an Array.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Find All Duplicates in an Array */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 6/8/2020 */ 5 | 6 | // Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. 7 | 8 | // Find all the elements that appear twice in this array. 9 | 10 | // Could you do it without extra space and in O(n) runtime? 11 | 12 | // Example: 13 | // Input: 14 | // [4,3,2,7,8,2,3,1] 15 | 16 | // Output: 17 | // [2,3] 18 | 19 | class Solution { 20 | public: 21 | vector findDuplicates(vector& nums) { 22 | map m; 23 | for(auto x: nums){ 24 | m[x]++; 25 | } 26 | vector v; 27 | for(auto x: m){ 28 | if(x.second==2) 29 | v.push_back(x.first); 30 | } 31 | return v; 32 | } 33 | }; -------------------------------------------------------------------------------- /Leet Code/Find Minimum In Rotated Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Find Minimum In Rotated Sprted Array II */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 25/7/2020 */ 5 | 6 | // Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. 7 | 8 | // (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). 9 | 10 | // Find the minimum element. 11 | 12 | // The array may contain duplicates. 13 | 14 | // Example 1: 15 | 16 | // Input: [1,3,5] 17 | // Output: 1 18 | // Example 2: 19 | 20 | // Input: [2,2,2,0,1] 21 | // Output: 0 22 | // Note: 23 | 24 | // This is a follow up problem to Find Minimum in Rotated Sorted Array. 25 | // Would allow duplicates affect the run-time complexity? How and why? 26 | 27 | class Solution { 28 | public: 29 | 30 | int findMin(vector& nums) { 31 | // int ans = findArr(nums,0,nums.size()-1); 32 | int low=0; 33 | int high = nums.size()-1; 34 | while(low nums,int low, int high){ 26 | if(low>high) 27 | return nums[0]; 28 | if(high==low) 29 | return nums[low]; 30 | 31 | int mid = low +(high-low)/2; 32 | if(midlow && nums[mid]& nums) { 41 | int ans = findArr(nums,0,nums.size()-1); 42 | return ans; 43 | } 44 | }; -------------------------------------------------------------------------------- /Leet Code/Find The Duplicate Number.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Find The Duplicate Number */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 22/8/2020 */ 5 | 6 | // Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. 7 | 8 | // Example 1: 9 | 10 | // Input: [1,3,4,2,2] 11 | // Output: 2 12 | // Example 2: 13 | 14 | // Input: [3,1,3,4,2] 15 | // Output: 3 16 | // Note: 17 | 18 | // You must not modify the array (assume the array is read only). 19 | // You must use only constant, O(1) extra space. 20 | // Your runtime complexity should be less than O(n2). 21 | // There is only one duplicate number in the array, but it could be repeated more than once. 22 | 23 | class Solution { 24 | public: 25 | int findDuplicate(vector& nums) { 26 | sort(nums.begin(),nums.end()); 27 | int i; 28 | for(i=0;i v(n+1); 34 | v[1]=1; 35 | for(int i=2;i<=n;i++) 36 | v[i]=v[i-1]+i; 37 | for(int i=1;i<=n;i++) 38 | if(v[i]==(v[n]-v[i]+i)) return i; 39 | return -1; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Leet Code/First Missing Positive.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - First Missing Positive */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 16/07/2021 */ 5 | 6 | // Given an unsorted integer array nums, find the smallest missing positive integer. 7 | 8 | // You must implement an algorithm that runs in O(n) time and uses constant extra space. 9 | 10 | 11 | 12 | // Example 1: 13 | 14 | // Input: nums = [1,2,0] 15 | // Output: 3 16 | // Example 2: 17 | 18 | // Input: nums = [3,4,-1,1] 19 | // Output: 2 20 | // Example 3: 21 | 22 | // Input: nums = [7,8,9,11,12] 23 | // Output: 1 24 | 25 | 26 | class Solution { 27 | public: 28 | int firstMissingPositive(vector& nums) { 29 | if(nums.size()==0) 30 | return 1; 31 | int n = nums.size(); 32 | int count = 0; 33 | for(int i=0;i0){ 35 | nums[count++] = nums[i]; 36 | } 37 | } 38 | 39 | for(int i=0;i0){ 41 | nums[abs(nums[i])-1] = -nums[abs(nums[i])-1]; 42 | } 43 | } 44 | 45 | for(int i=0;i0) 47 | return i+1; 48 | } 49 | return count+1; 50 | } 51 | }; -------------------------------------------------------------------------------- /Leet Code/First Unique Character in a String.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - First Unique Character in a String */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 21/09/2020 */ 5 | 6 | // Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1. 7 | 8 | // Examples: 9 | 10 | // s = "leetcode" 11 | // return 0. 12 | 13 | // s = "loveleetcode" 14 | // return 2. 15 | 16 | 17 | // Note: You may assume the string contains only lowercase English letters. 18 | 19 | class Solution { 20 | public: 21 | int firstUniqChar(string s) { 22 | vector v(256,0); 23 | 24 | for(int i=0;i fizzBuzz(int n) { 36 | vector s; 37 | for(int i=1;i<=n;i++){ 38 | if(i%3==0 && i%5==0){ 39 | s.push_back("FizzBuzz"); 40 | } 41 | else if(i%3==0) 42 | s.push_back("Fizz"); 43 | else if(i%5==0) 44 | s.push_back("Buzz"); 45 | else 46 | s.push_back(to_string(i)); 47 | } 48 | return s; 49 | } 50 | }; -------------------------------------------------------------------------------- /Leet Code/Generate Parentheses.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Generate Parentheses */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 26/09/2020 */ 5 | 6 | // Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 7 | 8 | // For example, given n = 3, a solution set is: 9 | 10 | // [ 11 | // "((()))", 12 | // "(()())", 13 | // "(())()", 14 | // "()(())", 15 | // "()()()" 16 | // ] 17 | 18 | 19 | class Solution { 20 | public: 21 | vector s; 22 | void generate(char *str,int idx,int n,int open, int close){ 23 | if(idx==2*n){ 24 | str[idx]='\0'; 25 | s.push_back(str); 26 | return ; 27 | } 28 | if(open generateParenthesis(int n) { 39 | if(n==0){ 40 | return s; 41 | } 42 | char str[10000000]; 43 | generate(str,0,n,0,0); 44 | return s; 45 | } 46 | }; -------------------------------------------------------------------------------- /Leet Code/Gray Code.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Gray Code */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 01/07/2021 */ 5 | 6 | // An n-bit gray code sequence is a sequence of 2n integers where: 7 | 8 | // Every integer is in the inclusive range [0, 2n - 1], 9 | // The first integer is 0, 10 | // An integer appears no more than once in the sequence, 11 | // The binary representation of every pair of adjacent integers differs by exactly one bit, and 12 | // The binary representation of the first and last integers differs by exactly one bit. 13 | // Given an integer n, return any valid n-bit gray code sequence. 14 | 15 | 16 | 17 | // Example 1: 18 | 19 | // Input: n = 2 20 | // Output: [0,1,3,2] 21 | // Explanation: 22 | // The binary representation of [0,1,3,2] is [00,01,11,10]. 23 | // - 00 and 01 differ by one bit 24 | // - 01 and 11 differ by one bit 25 | // - 11 and 10 differ by one bit 26 | // - 10 and 00 differ by one bit 27 | // [0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01]. 28 | // - 00 and 10 differ by one bit 29 | // - 10 and 11 differ by one bit 30 | // - 11 and 01 differ by one bit 31 | // - 01 and 00 differ by one bit 32 | // Example 2: 33 | 34 | // Input: n = 1 35 | // Output: [0,1] 36 | 37 | 38 | // Constraints: 39 | 40 | // 1 <= n <= 16 41 | 42 | class Solution { 43 | public: 44 | vector grayCode(int n) { 45 | vector res(1<>1); 48 | 49 | return res; 50 | } 51 | }; -------------------------------------------------------------------------------- /Leet Code/Hamming Distance.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Hamming Distance */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 05/07/2020 */ 5 | 6 | // The Hamming distance between two integers is the number of positions at which the corresponding bits are different. 7 | 8 | // Given two integers x and y, calculate the Hamming distance. 9 | 10 | // Note: 11 | // 0 ≤ x, y < 231. 12 | 13 | // Example: 14 | 15 | // Input: x = 1, y = 4 16 | 17 | // Output: 2 18 | 19 | // Explanation: 20 | // 1 (0 0 0 1) 21 | // 4 (0 1 0 0) 22 | // ↑ ↑ 23 | 24 | // The above arrows point to positions where the corresponding bits are different. 25 | 26 | class Solution { 27 | public: 28 | int hammingDistance(int x, int y) { 29 | int count = 0; 30 | while(x!=0 || y!=0){ 31 | int x1 = x&1; 32 | int y1 = y&1; 33 | if(x1^y1){ 34 | count++; 35 | } 36 | x=x>>1; 37 | y=y>>1; 38 | } 39 | return count; 40 | } 41 | }; -------------------------------------------------------------------------------- /Leet Code/Happy Number.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Happy Number */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 27/12/2020 */ 5 | 6 | // Write an algorithm to determine if a number n is happy. 7 | 8 | // A happy number is a number defined by the following process: 9 | 10 | // Starting with any positive integer, replace the number by the sum of the squares of its digits. 11 | // Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. 12 | // Those numbers for which this process ends in 1 are happy. 13 | // Return true if n is a happy number, and false if not. 14 | 15 | class Solution { 16 | public: 17 | bool isHappy(int n) { 18 | if(n<1) 19 | return false; 20 | set s; 21 | int num = n; 22 | while(s.find(num)==s.end()){ 23 | s.insert(num); 24 | num = digitSquaredSum(num); 25 | } 26 | return num==1; 27 | } 28 | int digitSquaredSum(int n){ 29 | int sum = 0; 30 | while(n>0){ 31 | int t = n%10; 32 | sum += (t*t); 33 | n/=10; 34 | } 35 | return sum; 36 | } 37 | }; -------------------------------------------------------------------------------- /Leet Code/Invert Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Invert Binary Tree */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 24/09/2020 */ 5 | 6 | // Invert a binary tree. 7 | 8 | // Example: 9 | 10 | // Input: 11 | 12 | // 4 13 | // / \ 14 | // 2 7 15 | // / \ / \ 16 | // 1 3 6 9 17 | // Output: 18 | 19 | // 4 20 | // / \ 21 | // 7 2 22 | // / \ / \ 23 | // 9 6 3 1 24 | // Trivia: 25 | // This problem was inspired by this original tweet by Max Howell: 26 | 27 | // Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off. 28 | 29 | /** 30 | * Definition for a binary tree node. 31 | * struct TreeNode { 32 | * int val; 33 | * TreeNode *left; 34 | * TreeNode *right; 35 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 36 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 37 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 38 | * }; 39 | */ 40 | class Solution { 41 | public: 42 | TreeNode* invertTree(TreeNode* root) { 43 | if(root==NULL) 44 | return root; 45 | TreeNode* leftTree = invertTree(root->left); 46 | TreeNode* rightTree = invertTree(root->right); 47 | 48 | root->left = rightTree; 49 | root-> right = leftTree; 50 | return root; 51 | } 52 | }; -------------------------------------------------------------------------------- /Leet Code/Is Subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Is Subsequence */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 22/05/2023 */ 5 | 6 | // Given two strings s and t, return true if s is a subsequence of t, or false otherwise. 7 | 8 | // A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not). 9 | 10 | 11 | 12 | // Example 1: 13 | 14 | // Input: s = "abc", t = "ahbgdc" 15 | // Output: true 16 | // Example 2: 17 | 18 | // Input: s = "axc", t = "ahbgdc" 19 | // Output: false 20 | 21 | class Solution { 22 | public: 23 | bool isSubsequence(string s, string t) { 24 | int j=0; 25 | for(int i=0;i& nums) { 29 | vector dp(nums.size(),0); 30 | int dist=0; 31 | dp[0]=nums[0]; 32 | for(int i=1;i& nums) { 26 | int n = nums.size(); 27 | vector dp(n,0); 28 | dp[0]=nums[0]; 29 | if(nums[0]==0 and nums.size()>1) return false; 30 | for(int i=1;i& nums, int k) { 25 | sort(nums.begin(), nums.end(), greater()); 26 | return nums[k-1]; 27 | } 28 | }; -------------------------------------------------------------------------------- /Leet Code/Length of Last Word.cpp: -------------------------------------------------------------------------------- 1 | /* Leetcode */ 2 | /* Title - Length of Last Word */ 3 | /* Created by - Akash Modak */ 4 | /* Date - 17/04/2021 */ 5 | 6 | // Given a string s consists of some words separated by spaces, return the length of the last word in the string. If the last word does not exist, return 0. 7 | 8 | // A word is a maximal substring consisting of non-space characters only. 9 | 10 | 11 | 12 | // Example 1: 13 | 14 | // Input: s = "Hello World" 15 | // Output: 5 16 | // Example 2: 17 | 18 | // Input: s = " " 19 | // Output: 0 20 | 21 | class Solution { 22 | public: 23 | int lengthOfLastWord(string s) { 24 | int length = s.length(); 25 | int len = 0; 26 | if(length==1 and isalpha(s[0])) 27 | return 1; 28 | for(int i=length-1; i>=0;i--){ 29 | if(isalpha(s[i])){ 30 | for(int j=i; j>=0;j--){ 31 | if(!isalpha(s[j])){ 32 | return len; 33 | } 34 | len++; 35 | } 36 | return len; 37 | } 38 | } 39 | return 0; 40 | } 41 | }; -------------------------------------------------------------------------------- /Leet Code/Length of the Longest Alphabetical Continuous Substring.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Length of the Longest Alphabetical Continuous Substring */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 23/05/2023 */ 5 | 6 | // An alphabetical continuous string is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string "abcdefghijklmnopqrstuvwxyz". 7 | 8 | // For example, "abc" is an alphabetical continuous string, while "acb" and "za" are not. 9 | // Given a string s consisting of lowercase letters only, return the length of the longest alphabetical continuous substring. 10 | 11 | 12 | 13 | // Example 1: 14 | 15 | // Input: s = "abacaba" 16 | // Output: 2 17 | // Explanation: There are 4 distinct continuous substrings: "a", "b", "c" and "ab". 18 | // "ab" is the longest continuous substring. 19 | // Example 2: 20 | 21 | // Input: s = "abcde" 22 | // Output: 5 23 | // Explanation: "abcde" is the longest continuous substring. 24 | 25 | 26 | class Solution { 27 | public: 28 | int longestContinuousSubstring(string s) { 29 | int i=0,count=1, total=1; 30 | for(i=1;i &res, string s, int i){ 24 | if(i==s.length()){ 25 | res.push_back(s); 26 | return; 27 | }else if(isalpha(s[i])){ 28 | s[i]=tolower(s[i]); 29 | perm(res,s,i+1); 30 | s[i]=toupper(s[i]); 31 | perm(res,s,i+1); 32 | }else{ 33 | perm(res,s,i+1); 34 | } 35 | 36 | } 37 | vector letterCasePermutation(string s) { 38 | vector res; 39 | perm(res,s,0); 40 | return res; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /Leet Code/Letter Combinations of a Phone Number.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Letter Combinations of a Phone Number */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 26/09/2020 */ 5 | 6 | // Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. 7 | 8 | // A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. 9 | 10 | 11 | 12 | // Example: 13 | 14 | // Input: "23" 15 | // Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 16 | // Note: 17 | 18 | // Although the above answer is in lexicographical order, your answer could be in any order you want. 19 | 20 | class Solution { 21 | public: 22 | string table[20] = {"","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 23 | vector s; 24 | void generate(string inp,int i,char *out,int j){ 25 | if(inp[i]=='\0'){ 26 | out[j] = '\0'; 27 | s.push_back(out); 28 | return ; 29 | } 30 | 31 | int key = inp[i]-'0'; 32 | key--; 33 | for(int t=0;table[key][t]!='\0';t++){ 34 | out[j] = table[key][t]; 35 | generate(inp,i+1,out,j+1); 36 | } 37 | } 38 | vector letterCombinations(string digits) { 39 | if(digits.length()==0) 40 | return s; 41 | char out[10000000]; 42 | generate(digits,0,out,0); 43 | return s; 44 | } 45 | }; -------------------------------------------------------------------------------- /Leet Code/Longest Common Prefix.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Longest Common Prefix */ 3 | 4 | // Write a function to find the longest common prefix string amongst an array of strings. 5 | 6 | // If there is no common prefix, return an empty string "". 7 | 8 | // Example 1: 9 | 10 | // Input: ["flower","flow","flight"] 11 | // Output: "fl" 12 | // Example 2: 13 | 14 | // Input: ["dog","racecar","car"] 15 | // Output: "" 16 | // Explanation: There is no common prefix among the input strings. 17 | // Note: 18 | 19 | // All given inputs are in lowercase letters a-z. 20 | 21 | class Solution { 22 | public: 23 | string longestCommonPrefix(vector& strs) { 24 | string s = ""; 25 | int n = strs.size(); 26 | if(n==0) 27 | return s; 28 | if(n==1) 29 | return strs[0]; 30 | 31 | sort(strs.begin(),strs.end()); 32 | int minimumLength = min(strs[0].length(),strs[n-1].length()); 33 | string first = strs[0], last = strs[n-1]; 34 | int i=0; 35 | while(i& nums) { 25 | unordered_set s; 26 | int maxCount=0; 27 | for(int i=0;i& nums) { 21 | if(nums.size()==0) 22 | return 0; 23 | int n = nums.size(); 24 | int lis[n]; 25 | for(int i=0;inums[j] and lis[i] m; 28 | int ans =0; 29 | for(int i=0;ians?ans+1:ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Leet Code/Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Longest Substring Without Repeating Characters */ 3 | // Given a string, find the length of the longest substring without repeating characters. 4 | 5 | // Example 1: 6 | 7 | // Input: "abcabcbb" 8 | // Output: 3 9 | // Explanation: The answer is "abc", with the length of 3. 10 | // Example 2: 11 | 12 | // Input: "bbbbb" 13 | // Output: 1 14 | // Explanation: The answer is "b", with the length of 1. 15 | // Example 3: 16 | 17 | // Input: "pwwkew" 18 | // Output: 3 19 | // Explanation: The answer is "wke", with the length of 3. 20 | // Note that the answer must be a substring, "pwke" is a subsequence and not a substring. 21 | 22 | class Solution { 23 | public: 24 | int lengthOfLongestSubstring(string s) { 25 | vector lastIndex(256,-1); 26 | int i = 0; 27 | int j; 28 | int res = 0; 29 | int n = s.length(); 30 | for(j=0;j majorityElement(vector& nums) { 28 | if(nums.size()==1){ 29 | return vector(1,nums[0]); 30 | } 31 | vector res; 32 | int length = nums.size(); 33 | int oneThird = length/3; 34 | int count = 1; 35 | sort(nums.begin(), nums.end()); 36 | int currentElement = nums[0]; 37 | for(int i=1;i oneThird) 40 | { 41 | res.push_back(currentElement); 42 | } 43 | count = 0; 44 | } 45 | currentElement = nums[i]; 46 | count++; 47 | } 48 | if(count > oneThird) 49 | { 50 | res.push_back(currentElement); 51 | } 52 | return res; 53 | } 54 | }; -------------------------------------------------------------------------------- /Leet Code/Majority Element.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Majority Element */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 24/06/2021 */ 5 | 6 | // Given an array nums of size n, return the majority element. 7 | 8 | // The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. 9 | // Example 1: 10 | 11 | // Input: nums = [3,2,3] 12 | // Output: 3 13 | // Example 2: 14 | 15 | // Input: nums = [2,2,1,1,1,2,2] 16 | // Output: 2 17 | 18 | class Solution { 19 | public: 20 | int majorityElement(vector& nums) { 21 | if(nums.size() == 1) 22 | return nums[0]; 23 | sort(nums.begin(), nums.end()); 24 | int middleElement = nums.size()/2; 25 | return nums[middleElement]; 26 | 27 | } 28 | }; -------------------------------------------------------------------------------- /Leet Code/Maximum Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Maximum Depth of Binary Tree */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 24/09/2020 */ 5 | 6 | // Given a binary tree, find its maximum depth. 7 | 8 | // The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 9 | 10 | // Note: A leaf is a node with no children. 11 | 12 | // Example: 13 | 14 | // Given binary tree [3,9,20,null,null,15,7], 15 | 16 | // 3 17 | // / \ 18 | // 9 20 19 | // / \ 20 | // 15 7 21 | // return its depth = 3. 22 | 23 | /** 24 | * Definition for a binary tree node. 25 | * struct TreeNode { 26 | * int val; 27 | * TreeNode *left; 28 | * TreeNode *right; 29 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 30 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 31 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 32 | * }; 33 | */ 34 | class Solution { 35 | public: 36 | int depth = 0; 37 | int maxDepth(TreeNode* root) { 38 | if(root==NULL) 39 | return 0; 40 | int h1 = maxDepth(root->left)+1; 41 | int h2 = maxDepth(root->right)+1; 42 | 43 | depth = max(h1,h2); 44 | return depth; 45 | } 46 | }; -------------------------------------------------------------------------------- /Leet Code/Maximum Erasure Value.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Maximum Erasure Value */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 14/06/2023 */ 5 | 6 | // You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements. 7 | 8 | // Return the maximum score you can get by erasing exactly one subarray. 9 | 10 | // An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r). 11 | 12 | 13 | 14 | // Example 1: 15 | 16 | // Input: nums = [4,2,4,5,6] 17 | // Output: 17 18 | // Explanation: The optimal subarray here is [2,4,5,6]. 19 | // Example 2: 20 | 21 | // Input: nums = [5,2,1,2,5,2,1,2,5] 22 | // Output: 8 23 | // Explanation: The optimal subarray here is [5,2,1] or [1,2,5]. 24 | 25 | class Solution { 26 | public: 27 | int maximumUniqueSubarray(vector& nums) { 28 | unordered_set s; 29 | int sum=0, maxSum=INT_MIN,start=0; 30 | for(int i=0;i m; 35 | 36 | int num = 0; 37 | 38 | for(int i=0;i ch; 32 | ch.insert('a'); 33 | ch.insert('e'); 34 | ch.insert('i'); 35 | ch.insert('o'); 36 | ch.insert('u'); 37 | int count=0, maxCount=0; 38 | for(int i=0;i& nums) { 14 | 15 | int maxSum=INT_MIN; 16 | int currSum=0; 17 | for(int i=0;i& nums1, int m, vector& nums2, int n) { 20 | int i=m-1; 21 | int j=n-1; 22 | int k=m+n-1; 23 | while(i>=0 && j>=0) 24 | { 25 | nums1[k--]=nums1[i]>nums2[j]?nums1[i--]:nums2[j--]; 26 | } 27 | while(i>=0) 28 | nums1[k--]=nums1[i--]; 29 | while(j>=0) 30 | nums1[k--]=nums2[j--]; 31 | 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Leet Code/Minimum Add to Make Parentheses Valid.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Minimum Add to Make Parentheses Valid */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 01/06/2023 */ 5 | 6 | // A parentheses string is valid if and only if: 7 | 8 | // It is the empty string, 9 | // It can be written as AB (A concatenated with B), where A and B are valid strings, or 10 | // It can be written as (A), where A is a valid string. 11 | // You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string. 12 | 13 | // For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))". 14 | // Return the minimum number of moves required to make s valid. 15 | 16 | 17 | 18 | // Example 1: 19 | 20 | // Input: s = "())" 21 | // Output: 1 22 | // Example 2: 23 | 24 | // Input: s = "(((" 25 | // Output: 3 26 | 27 | class Solution { 28 | public: 29 | int minAddToMakeValid(string s) { 30 | int open=0,add=0,close=0; 31 | for(int i=0;i0) open--; 34 | else add++; 35 | } 36 | return add+open; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /Leet Code/Minimum Flips to Make a OR b Equal to c.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Minimum Flips to Make a OR b Equal to c */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 07/06/2023 */ 5 | 6 | // Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation). 7 | // Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation. 8 | 9 | // Example 1: 10 | 11 | // Input: a = 2, b = 6, c = 5 12 | // Output: 3 13 | // Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c) 14 | // Example 2: 15 | 16 | // Input: a = 4, b = 2, c = 7 17 | // Output: 1 18 | // Example 3: 19 | 20 | // Input: a = 1, b = 2, c = 3 21 | // Output: 0 22 | 23 | class Solution { 24 | public: 25 | int minFlips(int a, int b, int c) { 26 | int at, bt, ct,count=0; 27 | while(c!=0 || b!=0 || a!=0){ 28 | at=a&1; 29 | bt=b&1; 30 | ct=c&1; 31 | a=a>>1; 32 | b=b>>1; 33 | c=c>>1; 34 | if(ct==1 and at!=bt) continue; 35 | else if(ct==0 and at!=bt) count++; 36 | else if(ct==1 and at==0) count++; 37 | else if(ct==0 and at==1) count+=2; 38 | } 39 | return count; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Leet Code/Minimum Path Sum.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Minimum Path Sum */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 23/09/2020 */ 5 | 6 | // Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. 7 | 8 | // Note: You can only move either down or right at any point in time. 9 | 10 | // Example: 11 | 12 | // Input: 13 | // [ 14 | // [1,3,1], 15 | // [1,5,1], 16 | // [4,2,1] 17 | // ] 18 | // Output: 7 19 | // Explanation: Because the path 1→3→1→1→1 minimizes the sum. 20 | 21 | class Solution { 22 | public: 23 | 24 | 25 | int minPathSum(vector>& grid) { 26 | if(grid.size()==0) 27 | return 0; 28 | int m = grid.size(); 29 | int n = grid[0].size(); 30 | 31 | if(m==0 and n==0) 32 | return 0; 33 | 34 | int dp[m][n]; 35 | dp[0][0] = grid[0][0]; 36 | for(int i=1;i& nums) { 19 | int ans = INT_MAX; 20 | int start = 0; 21 | int sum = 0; 22 | for(int i=0;i=s){ 26 | while(sum-nums[start]>=s){ 27 | sum-=nums[start]; 28 | start++; 29 | } 30 | int len = i-start+1; 31 | ans = min(ans,len); 32 | 33 | } 34 | } 35 | if(ans==INT_MAX) 36 | return 0; 37 | return ans; 38 | 39 | } 40 | }; -------------------------------------------------------------------------------- /Leet Code/Missing Number.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Missing Number */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 22/8/2020 */ 5 | 6 | // Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. 7 | 8 | // Example 1: 9 | 10 | // Input: [3,0,1] 11 | // Output: 2 12 | // Example 2: 13 | 14 | // Input: [9,6,4,2,3,5,7,0,1] 15 | // Output: 8 16 | 17 | 18 | class Solution { 19 | public: 20 | int missingNumber(vector& nums) { 21 | int n = nums.size(); 22 | int sumn = (n*(n+1))/2; 23 | int sum = 0; 24 | for(int i=0;i& nums) { 20 | int count = 0; 21 | int i,j=0; 22 | if(nums.empty()) 23 | return; 24 | for(i=0;i& arr, int k, int threshold) { 24 | int count=0,sum=0; 25 | for(int i=0;i=k){ 28 | sum-=arr[i-k]; 29 | } 30 | if(i>=k-1 and sum/k>=threshold) count++; 31 | } 32 | return count; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leet Code/Optimal Partition of String.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Optimal Partition of String */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 18/05/2023 */ 5 | 6 | // Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once. 7 | 8 | // Return the minimum number of substrings in such a partition. 9 | 10 | // Note that each character should belong to exactly one substring in a partition. 11 | 12 | 13 | 14 | // Example 1: 15 | 16 | // Input: s = "abacaba" 17 | // Output: 4 18 | // Explanation: 19 | // Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba"). 20 | // It can be shown that 4 is the minimum number of substrings needed. 21 | // Example 2: 22 | 23 | // Input: s = "ssssss" 24 | // Output: 6 25 | // Explanation: 26 | // The only valid partition is ("s","s","s","s","s","s"). 27 | 28 | class Solution { 29 | public: 30 | int partitionString(string s) { 31 | int maxVal=1; 32 | int letters[26]={0}; 33 | for(int i=0;i0) 22 | { 23 | int pop = x % 10; 24 | x /= 10; 25 | if( rev > INT_MAX / 10 || ( rev == INT_MAX / 10 && pop > 7 ) ) 26 | return false; 27 | if( rev < INT_MIN / 10 || ( rev == INT_MIN/10 && pop < -8 ) ) 28 | return false; 29 | rev = ( rev * 10 ) + pop; 30 | 31 | } 32 | if( rev == s ) 33 | return true; 34 | else 35 | return false; 36 | } 37 | }; 38 | 39 | -------------------------------------------------------------------------------- /Leet Code/Permutations II.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Permutations II */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 03/06/2023 */ 5 | 6 | // Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order. 7 | 8 | 9 | 10 | // Example 1: 11 | 12 | // Input: nums = [1,1,2] 13 | // Output: 14 | // [[1,1,2], 15 | // [1,2,1], 16 | // [2,1,1]] 17 | // Example 2: 18 | 19 | // Input: nums = [1,2,3] 20 | // Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 21 | 22 | class Solution { 23 | public: 24 | void swap(vector &nums, int i, int j) { 25 | int t=nums[i]; 26 | nums[i]=nums[j]; 27 | nums[j]=t; 28 | } 29 | void perm(vector nums, int i, set> &s){ 30 | if(i==nums.size()){ 31 | s.insert(nums); 32 | return; 33 | } 34 | 35 | for(int j=i;j> permuteUnique(vector& nums) { 42 | set> st; 43 | perm(nums,0,st); 44 | vector> res(st.begin(),st.end()); 45 | return res; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /Leet Code/Plus One.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Plus One */ 3 | 4 | // Given a non-empty array of digits representing a non-negative integer, plus one to the integer. 5 | 6 | // The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. 7 | 8 | // You may assume the integer does not contain any leading zero, except the number 0 itself. 9 | 10 | // Example 1: 11 | 12 | // Input: [1,2,3] 13 | // Output: [1,2,4] 14 | // Explanation: The array represents the integer 123. 15 | // Example 2: 16 | 17 | // Input: [4,3,2,1] 18 | // Output: [4,3,2,2] 19 | // Explanation: The array represents the integer 4321. 20 | 21 | class Solution { 22 | public: 23 | vector plusOne(vector& digits) { 24 | int n = digits.size(); 25 | int carry = 0; 26 | int temp = digits[n-1]+1; 27 | digits[n-1] = temp%10; 28 | carry = temp/10; 29 | for(int i=n-2;i>=0;i--){ 30 | int temp = digits[i]+carry; 31 | digits[i] = temp%10; 32 | carry = temp/10; 33 | } 34 | if(carry) 35 | digits.insert(digits.begin(),carry); 36 | return digits; 37 | } 38 | }; -------------------------------------------------------------------------------- /Leet Code/PowXN.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Pow(x,n) */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 16/7/2020 */ 5 | 6 | // Implement pow(x, n), which calculates x raised to the power n (xn). 7 | 8 | // Example 1: 9 | 10 | // Input: 2.00000, 10 11 | // Output: 1024.00000 12 | // Example 2: 13 | 14 | // Input: 2.10000, 3 15 | // Output: 9.26100 16 | // Example 3: 17 | 18 | // Input: 2.00000, -2 19 | // Output: 0.25000 20 | // Explanation: 2-2 = 1/2^2 = 1/4 = 0.25 21 | // Note: 22 | 23 | // -100.0 < x < 100.0 24 | // n is a 32-bit signed integer, within the range [−2^31, 2^31 − 1] 25 | 26 | class Solution { 27 | public: 28 | double power(double x, long long n){ 29 | if(n==0) 30 | return 1.0; 31 | if(n%2==0) 32 | { 33 | double ans = power(x,n/2); 34 | return ans*ans; 35 | } 36 | else 37 | return x*power(x,n-1); 38 | } 39 | double myPow(double x, int n) { 40 | 41 | if(n<0) 42 | return 1/power(x,-1*(long long)n); 43 | return power(x,(long long)n); 44 | } 45 | }; -------------------------------------------------------------------------------- /Leet Code/Power Of Four.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Power of Four */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 4/8/2020 */ 5 | 6 | // Given an integer (signed 32 bits), write a function to check whether it is a power of 4. 7 | 8 | // Example 1: 9 | 10 | // Input: 16 11 | // Output: true 12 | // Example 2: 13 | 14 | // Input: 5 15 | // Output: false 16 | // Follow up: Could you solve it without loops/recursion? 17 | 18 | class Solution { 19 | public: 20 | bool isPowerOfFour(long long int num) { 21 | if(num==1) 22 | return true; 23 | if(num<=0) 24 | return false; 25 | int setBits = __builtin_popcount(num); 26 | int numOfDigits = floor(log2(num))+1; 27 | // cout< 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. 7 | 8 | // Example: 9 | 10 | // Input: [1,2,3,4] 11 | // Output: [24,12,8,6] 12 | // Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer. 13 | 14 | // Note: Please solve it without division and in O(n). 15 | 16 | // Follow up: 17 | // Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.) 18 | 19 | class Solution { 20 | public: 21 | vector productExceptSelf(vector& nums) { 22 | if(nums.size()==0 || nums.size()==1) 23 | return nums; 24 | 25 | int left=1,right=1; 26 | vector res(nums.size(),1); 27 | for(int i=0;i m; 29 | for(int i=0;i0 && res[res.length()-1]==s[i]) 31 | res.pop_back(); 32 | else 33 | res.push_back(s[i]); 34 | } 35 | return res; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Leet Code/Remove Duplicates From A Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Remove Duplicates From A Sorted Array */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 22/8/2020 */ 5 | 6 | // Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. 7 | 8 | // Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. 9 | 10 | // Example 1: 11 | 12 | // Given nums = [1,1,2], 13 | 14 | // Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. 15 | 16 | // It doesn't matter what you leave beyond the returned length. 17 | // Example 2: 18 | 19 | // Given nums = [0,0,1,1,1,2,2,3,3,4], 20 | 21 | // Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. 22 | 23 | // It doesn't matter what values are set beyond the returned length. 24 | 25 | class Solution { 26 | public: 27 | int removeDuplicates(vector& nums) { 28 | if(nums.size()==0) 29 | return 0; 30 | int i=0,j; 31 | for(j=1;j& nums) { 26 | int n = nums.size(); 27 | if(n==0) 28 | return 0; 29 | int i=0; 30 | int j=1; 31 | while(jval; 30 | ListNode* node = new ListNode(head->val); 31 | ListNode* temp = node; 32 | ListNode* output = temp; 33 | while(head != NULL){ 34 | 35 | if(head->val != current){ 36 | node = new ListNode(head->val); 37 | temp->next = node; 38 | temp = temp->next; 39 | current = head->val; 40 | } 41 | 42 | head = head->next; 43 | } 44 | 45 | return output; 46 | } 47 | }; -------------------------------------------------------------------------------- /Leet Code/Remove Element.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Remove Element */ 3 | 4 | // Given an array nums and a value val, remove all instances of that value in-place and return the new length. 5 | 6 | // Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. 7 | 8 | // The order of elements can be changed. It doesn't matter what you leave beyond the new length. 9 | 10 | // Example 1: 11 | 12 | // Given nums = [3,2,2,3], val = 3, 13 | 14 | // Your function should return length = 2, with the first two elements of nums being 2. 15 | 16 | // It doesn't matter what you leave beyond the returned length. 17 | // Example 2: 18 | 19 | // Given nums = [0,1,2,2,3,0,4,2], val = 2, 20 | 21 | // Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4. 22 | 23 | // Note that the order of those five elements can be arbitrary. 24 | 25 | // It doesn't matter what values are set beyond the returned length. 26 | 27 | class Solution { 28 | public: 29 | int removeElement(vector& nums, int val) { 30 | int n = nums.size(); 31 | int i=0,j=0; 32 | while(j2->6->3->4->5->6, val = 6 11 | Output: 1->2->3->4->5 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode() : val(0), next(nullptr) {} 19 | * ListNode(int x) : val(x), next(nullptr) {} 20 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | ListNode* removeElements(ListNode* head, int val) { 26 | if(head==NULL or head->next==NULL){ 27 | if(head==NULL) 28 | return head; 29 | else if(head->next==NULL and head->val==val){ 30 | head= head->next; 31 | return head; 32 | } 33 | return head; 34 | 35 | } 36 | if(head->val==val) 37 | head = removeElements(head->next,val); 38 | else 39 | head->next = removeElements(head->next,val); 40 | return head; 41 | } 42 | }; -------------------------------------------------------------------------------- /Leet Code/Removing Stars From a String.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Removing Stars From a String */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 08/05/2023 */ 5 | 6 | // You are given a string s, which contains stars *. 7 | 8 | // In one operation, you can: 9 | 10 | // Choose a star in s. 11 | // Remove the closest non-star character to its left, as well as remove the star itself. 12 | // Return the string after all stars have been removed. 13 | 14 | // Note: 15 | 16 | // The input will be generated such that the operation is always possible. 17 | // It can be shown that the resulting string will always be unique. 18 | 19 | 20 | // Example 1: 21 | 22 | // Input: s = "leet**cod*e" 23 | // Output: "lecoe" 24 | // Explanation: Performing the removals from left to right: 25 | // - The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e". 26 | // - The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e". 27 | // - The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe". 28 | // There are no more stars, so we return "lecoe". 29 | // Example 2: 30 | 31 | // Input: s = "erase*****" 32 | // Output: "" 33 | // Explanation: The entire string is removed, so we return an empty string. 34 | 35 | class Solution { 36 | public: 37 | string removeStars(string s) { 38 | int j=0; 39 | for(int i=0;i> 8) & 0xff ]<<16 | 33 | lookuptable[ (n >> 16 )& 0xff ]<< 8 | 34 | lookuptable[ (n >>24 ) & 0xff ] ; 35 | return rev; 36 | 37 | } 38 | }; -------------------------------------------------------------------------------- /Leet Code/Reverse Integer.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Reverse Number */ 3 | //Given a 32-bit signed integer, reverse digits of an integer. 4 | // 5 | //Example 1: 6 | // 7 | //Input: 123 8 | //Output: 321 9 | //Example 2: 10 | // 11 | //Input: -123 12 | //Output: -321 13 | //Example 3: 14 | // 15 | //Input: 120 16 | //Output: 21 17 | //Note: 18 | //Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [-231, 231 - 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 19 | 20 | 21 | 22 | class Solution { 23 | public: 24 | int reverse(int x) { 25 | int sum=0; 26 | 27 | while(x!=0) { 28 | int pop = (x%10); 29 | x/=10; 30 | if(sum > INT_MAX/10 || (sum == INT_MAX / 10 && pop > 7)) 31 | return 0; 32 | if(sum < INT_MIN /10 || (sum == INT_MIN/10 && pop < -8)) 33 | return 0; 34 | sum = (sum*10)+pop; 35 | 36 | } 37 | 38 | cout<2->3->4->5->NULL 11 | // Output: 5->4->3->2->1->NULL 12 | // Follow up: 13 | 14 | // A linked list can be reversed either iteratively or recursively. Could you implement both? 15 | 16 | /** 17 | * Definition for singly-linked list. 18 | * struct ListNode { 19 | * int val; 20 | * ListNode *next; 21 | * ListNode() : val(0), next(nullptr) {} 22 | * ListNode(int x) : val(x), next(nullptr) {} 23 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 24 | * }; 25 | */ 26 | class Solution { 27 | public: 28 | ListNode* reverseList(ListNode* head) { 29 | if(head==NULL) 30 | return head; 31 | ListNode* current = head ; 32 | ListNode* prev = NULL; 33 | ListNode* nextNode = NULL; 34 | while(current!=NULL){ 35 | nextNode = current->next; 36 | current->next = prev; 37 | prev = current; 38 | current = nextNode; 39 | } 40 | return prev; 41 | } 42 | }; -------------------------------------------------------------------------------- /Leet Code/Reverse String.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Reverse String */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 21/09/2020 */ 5 | 6 | // Write a function that reverses a string. The input string is given as an array of characters char[]. 7 | 8 | // Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. 9 | 10 | // You may assume all the characters consist of printable ascii characters. 11 | 12 | 13 | 14 | // Example 1: 15 | 16 | // Input: ["h","e","l","l","o"] 17 | // Output: ["o","l","l","e","h"] 18 | // Example 2: 19 | 20 | // Input: ["H","a","n","n","a","h"] 21 | // Output: ["h","a","n","n","a","H"] 22 | 23 | class Solution { 24 | public: 25 | void reverseString(vector& s) { 26 | int i,j,n = s.size(); 27 | if(n==0 ||n==1) 28 | return ; 29 | for(i=0,j=n-1;i>word) 38 | ans = word+" "+ans; 39 | return ans.substr(0,ans.length()-1); 40 | } 41 | }; -------------------------------------------------------------------------------- /Leet Code/Rotate Array.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Rotate Array */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 27/08/2020 */ 5 | 6 | // Given an array, rotate the array to the right by k steps, where k is non-negative. 7 | 8 | // Follow up: 9 | 10 | // Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. 11 | // Could you do it in-place with O(1) extra space? 12 | 13 | 14 | // Example 1: 15 | 16 | // Input: nums = [1,2,3,4,5,6,7], k = 3 17 | // Output: [5,6,7,1,2,3,4] 18 | // Explanation: 19 | // rotate 1 steps to the right: [7,1,2,3,4,5,6] 20 | // rotate 2 steps to the right: [6,7,1,2,3,4,5] 21 | // rotate 3 steps to the right: [5,6,7,1,2,3,4] 22 | // Example 2: 23 | 24 | // Input: nums = [-1,-100,3,99], k = 2 25 | // Output: [3,99,-1,-100] 26 | // Explanation: 27 | // rotate 1 steps to the right: [99,-1,-100,3] 28 | // rotate 2 steps to the right: [3,99,-1,-100] 29 | 30 | 31 | // Constraints: 32 | 33 | // 1 <= nums.length <= 2 * 10^4 34 | // It's guaranteed that nums[i] fits in a 32 bit-signed integer. 35 | // k >= 0 36 | 37 | class Solution { 38 | public: 39 | void rotate(vector& nums, int k) { 40 | int n = nums.size(); 41 | while(k-- && n){ 42 | int temp = nums[n-1]; 43 | nums.pop_back(); 44 | nums.insert(nums.begin(),temp); 45 | } 46 | } 47 | }; -------------------------------------------------------------------------------- /Leet Code/Search Insert Position.cpp: -------------------------------------------------------------------------------- 1 | /*Leet Code*/ 2 | /*Title - Search Insert Position*/ 3 | //Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 4 | // 5 | //You may assume no duplicates in the array. 6 | // 7 | //Example 1: 8 | // 9 | //Input: [1,3,5,6], 5 10 | //Output: 2 11 | //Example 2: 12 | // 13 | //Input: [1,3,5,6], 2 14 | //Output: 1 15 | 16 | 17 | // class Solution { 18 | // public: 19 | // int searchInsert(vector& nums, int target) { 20 | // int pos=0,i=0; 21 | // while(inums[i]) 24 | // pos++; 25 | // else if(target==nums[i]) 26 | // return pos; 27 | // i++; 28 | // } 29 | // return pos; 30 | // } 31 | // }; 32 | 33 | // optimized solution 34 | class Solution { 35 | public: 36 | int searchInsert(vector& nums, int target) { 37 | int low = 0, high = nums.size()-1, mid = low; 38 | 39 | while(low<=high) { 40 | mid = low + (high-low)/2; 41 | if(nums[mid]==target ) 42 | return mid; 43 | else if(target < nums[mid]) { 44 | high = mid-1; 45 | }else { 46 | low = mid+1; 47 | } 48 | } 49 | return low; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Leet Code/Single Number III.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Single Number III */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 23/7/2020 */ 5 | 6 | // Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. 7 | 8 | // Example: 9 | 10 | // Input: [1,2,1,3,2,5] 11 | // Output: [3,5] 12 | // Note: 13 | 14 | // The order of the result is not important. So in the above example, [5, 3] is also correct. 15 | // Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity? 16 | 17 | class Solution { 18 | public: 19 | vector singleNumber(vector& nums) { 20 | int x=0; 21 | int n = nums.size(); 22 | for(int i=0;i>1; 29 | } 30 | int mask = (1<0) 35 | m^=nums[i]; 36 | } 37 | // cout< v; 39 | // cout<& nums) { 24 | int x = 0; 25 | for(auto i:nums){ 26 | x^=i; 27 | } 28 | return x; 29 | } 30 | }; -------------------------------------------------------------------------------- /Leet Code/Sort Array By Parity.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Sort Array By Parity */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 21/8/2020 */ 5 | 6 | // Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. 7 | 8 | // You may return any answer array that satisfies this condition. 9 | 10 | 11 | 12 | // Example 1: 13 | 14 | // Input: [3,1,2,4] 15 | // Output: [2,4,3,1] 16 | // The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. 17 | 18 | 19 | class Solution { 20 | public: 21 | vector sortArrayByParity(vector& A) { 22 | if(A.empty()){ 23 | return vector{}; 24 | } 25 | vector v; 26 | for(int i=0;i& nums, int k) { 19 | if(nums.empty()) 20 | return 0; 21 | else if(nums.size()==1){ 22 | if(nums[0]=k){ 33 | while(mult>=k and start& nums, int k) { 22 | int count = 0; 23 | if(nums.empty()) 24 | return count; 25 | int sum = 0; 26 | unordered_map m; 27 | m[0] = 1; 28 | for(int i=0;i& nums,int i,int n,vector>& ans){ 28 | if(i==n) return; //n is end of nums array if we reach there return 29 | int v = nums[i]; // The current element we are looking at 30 | vector>t = ans; // temp array to store the arrays for current element 31 | for(auto x: t){ 32 | x.push_back(v); // add the current element 33 | ans.push_back(x); // insert it to ans array 34 | } 35 | fillPowerSet(nums,i+1,n,ans); // go for the next element (i = i+1) 36 | } 37 | 38 | vector> subsets(vector& nums) { 39 | int i = 0, n=nums.size(); 40 | if(n==0) return {}; // if empty array 41 | vector>ans; 42 | ans.push_back({}); 43 | fillPowerSet(nums,i,n,ans); 44 | return ans; 45 | } 46 | }; -------------------------------------------------------------------------------- /Leet Code/Substrings of Size Three with Distinct Characters.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Substrings of Size Three with Distinct Characters */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 11/05/2023 */ 5 | 6 | // A string is good if there are no repeated characters. 7 | 8 | // Given a string s, return the number of good substrings of length three in s, 9 | 10 | // Note that if there are multiple occurrences of the same substring, every occurrence should be counted. 11 | 12 | // A substring is a contiguous sequence of characters in a string. 13 | 14 | 15 | 16 | // Example 1: 17 | 18 | // Input: s = "xyzzaz" 19 | // Output: 1 20 | // Explanation: There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". 21 | // The only good substring of length 3 is "xyz". 22 | // Example 2: 23 | 24 | // Input: s = "aababcabc" 25 | // Output: 4 26 | // Explanation: There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc". 27 | // The good substrings are "abc", "bca", "cab", and "abc". 28 | 29 | class Solution { 30 | public: 31 | int countGoodSubstrings(string s) { 32 | int count=0; 33 | for(int i=1;i2->3->4, you should return the list as 2->1->4->3. 15 | 16 | /** 17 | * Definition for singly-linked list. 18 | * struct ListNode { 19 | * int val; 20 | * ListNode *next; 21 | * ListNode() : val(0), next(nullptr) {} 22 | * ListNode(int x) : val(x), next(nullptr) {} 23 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 24 | * }; 25 | */ 26 | class Solution { 27 | public: 28 | ListNode* swapPairs(ListNode* head) { 29 | int k = 2; 30 | ListNode* current = head; 31 | ListNode* prev = NULL; 32 | ListNode* nextNode = NULL; 33 | 34 | while(current!=NULL and k>0){ 35 | nextNode = current->next; 36 | current->next = prev; 37 | prev = current; 38 | current = nextNode; 39 | k--; 40 | } 41 | if(current!=NULL) 42 | head->next = swapPairs(current); 43 | return prev; 44 | } 45 | }; -------------------------------------------------------------------------------- /Leet Code/Top K Frequent Elements.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Top K Frequent Elements */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 17/7/2020 */ 5 | 6 | // Given a non-empty array of integers, return the k most frequent elements. 7 | 8 | // Example 1: 9 | 10 | // Input: nums = [1,1,1,2,2,3], k = 2 11 | // Output: [1,2] 12 | // Example 2: 13 | 14 | // Input: nums = [1], k = 1 15 | // Output: [1] 16 | // Note: 17 | 18 | // You may assume k is always valid, 1 ≤ k ≤ number of unique elements. 19 | // Your algorithm's time complexity must be better than O(n log n), where n is the array's size. 20 | // It's guaranteed that the answer is unique, in other words the set of the top k frequent elements is unique. 21 | // You can return the answer in any order. 22 | 23 | class Solution { 24 | public: 25 | vector topKFrequent(vector& nums, int k) { 26 | map m,n; 27 | for(auto x: nums){ 28 | m[x]++; 29 | } 30 | vector v; 31 | priority_queue> j; 32 | 33 | for(auto x: m){ 34 | j.push({x.second,x.first}); 35 | } 36 | while(k--){ 37 | v.push_back(j.top().second); 38 | j.pop(); 39 | } 40 | return v; 41 | } 42 | }; -------------------------------------------------------------------------------- /Leet Code/Trapping Rain Water.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code*/ 2 | /* Title - Trapping Rain Water */ 3 | //Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. 4 | 5 | class Solution { 6 | public: 7 | int trap(vector& height) { 8 | if(height.empty()) 9 | return 0; 10 | int sum = 0; 11 | vector left(height.size()); 12 | vector right(height.size()); 13 | left[0] = height[0]; 14 | right[height.size()-1] = height[height.size()-1]; 15 | for(int i=1;iheight[i]) 18 | left[i] = left[i-1]; 19 | else 20 | left[i] = height[i]; 21 | } 22 | for(int i=height.size()-2;i>=0;i--) 23 | { 24 | if(right[i+1]>height[i]) 25 | right[i] = right[i+1]; 26 | else 27 | right[i] = height[i]; 28 | } 29 | for(int i=0;i twoSum(vector& nums, int target) { 17 | map hashmap; 18 | vector output; 19 | for( int i=0;i(nums[i],i)); 23 | 24 | if(hashmap.find(secondEle)!=hashmap.end() && hashmap.find(secondEle)->second!=i) 25 | { 26 | output.push_back(hashmap.find(secondEle)->second); 27 | 28 | output.push_back(i); 29 | return output; 30 | } 31 | } 32 | return output; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leet Code/Ugly Number II.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Ugly Number II */ 3 | /* Created By - Akash Modak */ 4 | /* Date - 4/7/2020 */ 5 | 6 | // Write a program to find the n-th ugly number. 7 | 8 | // Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. 9 | 10 | // Example: 11 | 12 | // Input: n = 10 13 | // Output: 12 14 | // Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. 15 | // Note: 16 | 17 | // 1 is typically treated as an ugly number. 18 | // n does not exceed 1690. 19 | 20 | 21 | class Solution { 22 | public: 23 | int nthUglyNumber(int n) { 24 | int mul2=2,mul3=3,mul5=5,next=1; 25 | int i2=0,i3=0,i5=0; 26 | int a[n]; 27 | a[0]=1; 28 | for(int i=1;i m1,m2; 23 | if(s.length()!= t.length()) 24 | return false; 25 | for(int i=0;i= 3 11 | // There exists some i with 0 < i < arr.length - 1 such that: 12 | // arr[0] < arr[1] < ... < arr[i - 1] < arr[i] 13 | // arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 14 | 15 | 16 | 17 | // Example 1: 18 | 19 | // Input: arr = [2,1] 20 | // Output: false 21 | // Example 2: 22 | 23 | // Input: arr = [3,5,5] 24 | // Output: false 25 | // Example 3: 26 | 27 | // Input: arr = [0,3,2,1] 28 | // Output: true 29 | 30 | class Solution { 31 | public: 32 | bool validMountainArray(vector& arr) { 33 | bool increasing = false; 34 | bool decreasing = false; 35 | if(arr.size()<3) 36 | return false; 37 | int i=0; 38 | bool flag = false; 39 | while(iarr[i+1] and flag){ 48 | i++; 49 | } 50 | 51 | if(i!=arr.size()-1) 52 | return false; 53 | 54 | return true; 55 | } 56 | }; -------------------------------------------------------------------------------- /Leet Code/Valid_Palindrome.cpp: -------------------------------------------------------------------------------- 1 | /* Leet Code */ 2 | /* Title - Valid Palindrome */ 3 | /* Date - 02/10/2020 */ 4 | 5 | // Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 6 | 7 | 8 | 9 | //Example 1: 10 | 11 | //Input: "A man, a plan, a canal: Panama" 12 | //Output: true 13 | 14 | //Example 2: 15 | 16 | //Input: "race a car" 17 | //Output: false 18 | 19 | //Note: For the purpose of this problem, we define empty string as valid palindrome. 20 | 21 | 22 | class Solution { 23 | public: 24 | bool isPalindrome(string s) { 25 | int i=0, j=s.length()-1; 26 | while (i 4, 17 | // push(5), 18 | // pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 19 | // Example 2: 20 | 21 | // Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2] 22 | // Output: false 23 | // Explanation: 1 cannot be popped before 2. 24 | 25 | class Solution { 26 | public: 27 | bool validateStackSequences(vector& pushed, vector& popped) { 28 | int i=0,j=0; 29 | stack s; 30 | for(;i 11 | using namespace std; 12 | 13 | int main(){ 14 | int t; 15 | cin>>t; 16 | while(t--){ 17 | int n,w,h; 18 | int x[40010],y[40010]; 19 | cin>>w>>h>>n; 20 | if(n==0){ 21 | cout<>x[i]>>y[i]; 26 | } 27 | sort(x,x+n); 28 | sort(y,y+n); 29 | 30 | int dx = x[0]-1; 31 | int dy = y[0]-1; 32 | 33 | for(int i=1;i 9 | using namespace std; 10 | 11 | bool compare(pair a,pair b){ 12 | return a.second>t; 18 | while(t--){ 19 | int n,s,e; 20 | cin>>n; 21 | 22 | vector > v; 23 | for(int i=0;i>s>>e; 25 | v.push_back(make_pair(s,e)); 26 | } 27 | sort(v.begin(),v.end(),compare); 28 | int activity=1; 29 | int taken_activity = v[0].second; 30 | for(int i=1;i=taken_activity){ 32 | activity++; 33 | taken_activity = v[i].second; 34 | } 35 | } 36 | cout< 24 | using namespace std; 25 | 26 | int main() { 27 | // your code goes here 28 | int n; 29 | cin>>n; 30 | while(n!=42){ 31 | cout<>n; 33 | } 34 | return 0; 35 | } --------------------------------------------------------------------------------