├── .gitignore ├── Adobe ├── best-time-to-buy-and-sell-stock.md ├── max-area-of-island.md ├── max-increase-to-keep-city-skyline.md ├── maximum-subarray.md ├── next-permutation.md ├── powx-n.md ├── readme.md ├── string-to-integer.md └── valid-parenthesis-string.md ├── Amazon ├── license-key-formatting.md ├── max-consecutive-ones.md ├── maximum-binary-tree.md ├── pascals-triangle-ii.md ├── pascals-triangle.md ├── path-sum.md ├── perfect-number.md ├── readme.md ├── reverse-bits.md ├── valid-palindrome.md └── valid-sudoku.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Facebook ├── convert-a-number-to-hexadecimal.md ├── decode-ways.md ├── divide-two-integers.md ├── permutations-ii.md ├── readme.md ├── reverse-words-in-a-string.md ├── set-matrix-zeroes.md ├── symmetric-tree.md ├── third-maximum-number.md ├── valid-number.md └── validate-ip-address.md ├── Google ├── buddy-strings.md ├── clone-graph.md ├── count-and-say.md ├── course-schedule.md ├── encode-and-decode-tiny-url.md ├── find-and-replace-in-string.md ├── fruit-into-baskets.md ├── gray-code.md ├── hand-of-straights.md ├── isomorphic-strings.md ├── lru-cache.md ├── nth-digit.md ├── number-of-islands.md ├── odd-even-jump.md ├── permutations.md ├── readme.md ├── toeplitz-matrix.md ├── two-sum.md ├── unique-email-addresses.md ├── valid-mountain-array.md ├── word-ladder-ii.md ├── word-ladder.md └── word-pattern.md ├── LICENSE ├── Microsoft ├── balanced-binary-tree.md ├── binary-tree-level-order-traversal-ii.md ├── excel-sheet-column-title.md ├── find-pivot-index.md ├── readme.md ├── rotate-string.md └── sort-colors.md ├── Miscellaneous ├── Codechef │ ├── aplong │ │ ├── 1.cpp │ │ ├── 2.cpp │ │ ├── 3.cpp │ │ ├── 4.cpp │ │ ├── 5.cpp │ │ ├── 6.cpp │ │ └── a.out │ ├── deccookoff │ │ ├── 1.c │ │ ├── 2.c │ │ └── a.out │ ├── declong │ │ ├── 1.c │ │ ├── 2.c │ │ ├── 3.c │ │ ├── 4.c │ │ ├── 5.c │ │ └── a.out │ ├── feblong │ │ ├── 1.cpp │ │ ├── 2.cpp │ │ ├── 3.cpp │ │ ├── 4.cpp │ │ ├── 5.cpp │ │ └── a.out │ ├── hashcode │ │ ├── .ipynb_checkpoints │ │ │ └── Hashcode-checkpoint.ipynb │ │ ├── Hashcode.ipynb │ │ ├── a.sample_sol.txt │ │ ├── a_example.txt │ │ └── hashcode_2020_online_qualification_round.pdf │ ├── icpc │ │ ├── 1.cpp │ │ └── 3.cpp │ ├── janlong │ │ ├── 1.cpp │ │ ├── 2.cpp │ │ ├── 3.cpp │ │ ├── 4.cpp │ │ ├── 5.cpp │ │ └── a.out │ ├── jlylong │ │ ├── 1.c │ │ ├── 2.c │ │ └── a.out │ ├── junecook │ │ ├── 1.c │ │ ├── 2.c │ │ ├── 3.c │ │ ├── 4.c │ │ └── a.out │ ├── junelong │ │ ├── 1.c │ │ ├── 2.c │ │ ├── 3.1.c │ │ ├── 3.c │ │ └── a.out │ ├── maylong │ │ ├── 1.cpp │ │ ├── 2.cpp │ │ └── a.out │ ├── novlong │ │ ├── 1.c │ │ ├── 2.c │ │ └── a.out │ ├── null │ │ ├── 1.c │ │ ├── 2.c │ │ ├── 3.c │ │ └── a.out │ ├── oclong │ │ ├── 1.c │ │ ├── 2.c │ │ ├── 3.c │ │ ├── 4.c │ │ ├── 4.cpp │ │ ├── 5.c │ │ └── a.out │ ├── oclunch │ │ ├── 1.cpp │ │ ├── 2.cpp │ │ └── 3.cpp │ ├── philips │ │ ├── 1.c │ │ └── a.out │ ├── sepcook │ │ ├── 1.c │ │ ├── 2.c │ │ └── a.out │ └── seplong │ │ ├── 1.c │ │ ├── 2.c │ │ ├── 2.py │ │ ├── 3.c │ │ ├── 4.c │ │ ├── 5.c │ │ ├── a.out │ │ └── hello.c ├── Dynamic Programming │ ├── a.out │ ├── decodeways2.cpp │ └── wildcard.cpp ├── FooBar │ ├── Foobar2_1.java │ ├── Foobar2_2.class │ ├── Foobar2_2.java │ ├── Foobar3_1.py │ ├── Foobar3_2.py │ ├── Solution.class │ └── foobar1.py ├── InterviewBit │ ├── Array │ │ ├── AntiDiagonals.cpp │ │ ├── FindDuplicateInArray.cpp │ │ ├── FindPermutaion.cpp │ │ ├── FirstMissingInteger.cpp │ │ ├── Flip.cpp │ │ ├── HotelBookingsPossible.cpp │ │ ├── InsertInterval.cpp │ │ ├── LargestNumber.cpp │ │ ├── MaxConsecutiveGap.cpp │ │ ├── MaxDistance.cpp │ │ ├── MaxNonNegativeSubArray.cpp │ │ ├── MaximumAbsoluteDifference.cpp │ │ ├── MaximumUnsortedSubarray.cpp │ │ ├── MergeInterval.cpp │ │ ├── MinStepsInfiniteGrid.cpp │ │ ├── NextPermutation.cpp │ │ ├── NobleInteger.cpp │ │ ├── PascalTriangle.cpp │ │ ├── RepeatAndMissingNumber.cpp │ │ ├── RotateImage.cpp │ │ ├── SetZeroes.cpp │ │ ├── Spiral.cpp │ │ ├── TripletsWithGivenSumRange.cpp │ │ ├── WaveArray.cpp │ │ ├── kthRowPascalTriangle.cpp │ │ └── nby3RepeatNumber.cpp │ ├── BinarySearch │ │ ├── PowerFunction.cpp │ │ └── SquareRoot.cpp │ └── Maths │ │ ├── CountGridPath.cpp │ │ ├── ExcelColumnNumber.cpp │ │ ├── ExcelColumnTitle.cpp │ │ ├── FizzBuzz.cpp │ │ ├── LargestCoprimeDivisor.cpp │ │ ├── Palindrome.cpp │ │ ├── PowerTwoIntegers.cpp │ │ ├── PrimeSum.cpp │ │ ├── RearrangeArray.cpp │ │ ├── SumOfHammingDistance.cpp │ │ ├── TraillingZeroesFactorial.cpp │ │ ├── gcd.cpp │ │ └── reverse.cpp ├── Labs │ ├── Lab1 │ │ ├── Assignment │ │ │ ├── .compare.h.swp │ │ │ ├── .compare_at.c.swp │ │ │ ├── .job.h.swp │ │ │ ├── .main.c.swp │ │ │ ├── .storage.h.swp │ │ │ ├── at │ │ │ ├── compare.h │ │ │ ├── compare_at.c │ │ │ ├── compare_at.o │ │ │ ├── compare_et.c │ │ │ ├── et │ │ │ ├── job.h │ │ │ ├── main.c │ │ │ ├── seqListOps.c │ │ │ ├── seqListOps.h │ │ │ └── storage.h │ │ ├── AssignmentFiles │ │ │ ├── .compare.h.swp │ │ │ ├── .compare_at.c.swp │ │ │ ├── .job.h.swp │ │ │ ├── .main.c.swp │ │ │ ├── .storage.h.swp │ │ │ ├── at │ │ │ ├── compare.h │ │ │ ├── compare_at.c │ │ │ ├── compare_at.o │ │ │ ├── compare_et.c │ │ │ ├── job.h │ │ │ ├── main.c │ │ │ ├── seqListOps.c │ │ │ ├── seqListOps.h │ │ │ └── storage.h │ │ ├── AssignmentSolution │ │ │ ├── compare.h │ │ │ ├── compare_at.c │ │ │ ├── compare_et.c │ │ │ ├── main.c │ │ │ ├── seqListOps.c │ │ │ ├── seqListOps.h │ │ │ └── storage.h │ │ ├── LabSheet1.pdf │ │ ├── Linking │ │ │ ├── a.out │ │ │ ├── add.c │ │ │ ├── add_pre.c │ │ │ └── output │ │ └── Search_max_min │ │ │ ├── main.c │ │ │ ├── search.h │ │ │ ├── search1 │ │ │ ├── search2 │ │ │ ├── search3 │ │ │ ├── search_max.c │ │ │ ├── search_max.o │ │ │ ├── search_min.c │ │ │ └── search_min.o │ ├── Lab2 │ │ ├── cmd_line_arg │ │ ├── cmd_line_arg.c │ │ ├── file │ │ ├── file.c │ │ ├── filecopy │ │ ├── filecopy.c │ │ ├── fileread │ │ ├── fileread.c │ │ ├── lab2.pdf │ │ ├── linked_list │ │ │ ├── driver.c │ │ │ ├── linkedlist │ │ │ ├── linkedlist.c │ │ │ ├── linkedlist.h │ │ │ ├── node.c │ │ │ ├── node.o │ │ │ ├── read.txt │ │ │ └── write.txt │ │ ├── program.txt │ │ ├── read.txt │ │ ├── stack │ │ │ ├── driver.c │ │ │ ├── linkedlist.c │ │ │ ├── linkedlist.h │ │ │ ├── node.c │ │ │ ├── node.o │ │ │ ├── read.txt │ │ │ ├── stack │ │ │ ├── stack.c │ │ │ ├── stack.h │ │ │ ├── stackInput.txt │ │ │ └── write.txt │ │ ├── test │ │ ├── test.c │ │ └── write.txt │ ├── Lab3 │ │ ├── gmon.out │ │ ├── input10.txt │ │ ├── input100.txt │ │ ├── input10000.txt │ │ ├── lab3.pdf │ │ ├── multiQ.c │ │ ├── multiQ.h │ │ ├── node.c │ │ ├── prof_output │ │ ├── que.c │ │ ├── que.h │ │ └── testMultiQ.c │ ├── Lab4 │ │ ├── a.out │ │ ├── cycle.c │ │ ├── cycle.h │ │ ├── cycle1 │ │ ├── cycle1.c │ │ ├── cycle1_output │ │ ├── cycle2 │ │ ├── cycle2.c │ │ ├── cycle2_output │ │ ├── cycle3 │ │ ├── cycle4 │ │ ├── dynalloc.c │ │ ├── dynalloc.h │ │ ├── gmon.out │ │ ├── lab4.pdf │ │ └── stackmem.c │ ├── Lab5 │ │ ├── 10 │ │ ├── 100 │ │ ├── 1000 │ │ ├── 10000 │ │ ├── 100000 │ │ ├── a.out │ │ ├── exer_1.c │ │ ├── generate.py │ │ ├── graph.ods │ │ ├── lab5.pdf │ │ ├── output.txt │ │ └── output_sort.txt │ ├── Lab6 │ │ ├── 1024.txt │ │ ├── 10240.txt │ │ ├── a.out │ │ ├── iterative_merge_sort.c │ │ ├── lab6.pdf │ │ ├── makefile │ │ ├── merge_sort.c │ │ ├── merge_sort.h │ │ └── recursive_merge_sort.c │ ├── Lab7 │ │ ├── 10 │ │ ├── 100 │ │ ├── 1000 │ │ ├── 10000 │ │ ├── 50000 │ │ ├── 100000 │ │ ├── a.out │ │ ├── complete.c │ │ ├── generate.py │ │ ├── graph.ods │ │ ├── lab7.pdf │ │ ├── quick_sort.c │ │ └── quick_sort.h │ └── Lab8 │ │ ├── a.out │ │ ├── aliceinwonderland.txt │ │ ├── hash.c │ │ ├── lab9.pdf │ │ └── stopwords.txt ├── LeetCode │ ├── April30DayChallenge │ │ ├── BSTFromPreorderTransversal.cpp │ │ ├── BackSpaceStringCompare.cpp │ │ ├── BestTimeToBuyAndSell.cpp │ │ ├── BinaryTreeMaxSumPath.cpp │ │ ├── BitWiseAndOfNumbersRange.cpp │ │ ├── ContagiousArray.cpp │ │ ├── CountSubArraysWithSumk.cpp │ │ ├── DiameterBinaryTree.cpp │ │ ├── FirstUniqueNumber.cpp │ │ ├── HappyNumber.cpp │ │ ├── JumpGame.cpp │ │ ├── LRUCache.cpp │ │ ├── LastStone.cpp │ │ ├── LeftMostColumnWithOne.cpp │ │ ├── LongestCommonSubsequence.cpp │ │ ├── MaximalSquare.cpp │ │ ├── MaximumSubarray.cpp │ │ ├── MiddleLinkedList.cpp │ │ ├── MinPathSum.cpp │ │ ├── MinStack.cpp │ │ ├── MoveZeroes.cpp │ │ ├── NumberOfIslands.cpp │ │ ├── ProductOfArrayExceptSelf.cpp │ │ ├── SearchInSortedRotatedArray.cpp │ │ ├── SingleNumber.cpp │ │ ├── StringShift.cpp │ │ ├── ValidParanthesisString.cpp │ │ ├── ValidSequenceBinaryTree.cpp │ │ ├── countingElements.cpp │ │ └── groupAnagrams.java │ ├── Array │ │ ├── FindDuplicates.cpp │ │ ├── PlusOne.cpp │ │ ├── RemoveDuplicates.cpp │ │ ├── RotateArray.cpp │ │ └── TwoSum.cpp │ └── May30DayChallenge │ │ ├── CheckIfItIsStraightLine.cpp │ │ ├── CousinsInBinaryTree.cpp │ │ ├── FindTheTownJudge.cpp │ │ ├── FirstBadVersion.cpp │ │ ├── FirstUniqueCharacter.cpp │ │ ├── FloodFill.cpp │ │ ├── JewelsAndStones.cpp │ │ ├── MajorityElement.cpp │ │ ├── NumberComplement.cpp │ │ ├── isValidSquare.cpp │ │ └── ransomNote.cpp └── slides │ ├── 01-introduction.pdf │ ├── 02-mathematics.pdf │ ├── 03-data-structures.pdf │ ├── 04-dynamic-programming.pdf │ ├── 05-combinatorial-games.pdf │ ├── 06-basic-graph-algorithms.pdf │ ├── 07-shortest-path-algorithms.pdf │ ├── 08-network-flow-problems.pdf │ ├── 09-computational-geometry.pdf │ ├── 10-string-algorithms.pdf │ └── suffix-array.pdf ├── README.md ├── _config.yml └── template.md /.gitignore: -------------------------------------------------------------------------------- 1 | pythonenv3.8/ 2 | **/.vscode 3 | **/.venv -------------------------------------------------------------------------------- /Adobe/best-time-to-buy-and-sell-stock.md: -------------------------------------------------------------------------------- 1 | ## [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int maxProfit(vector& prices) { 14 | int maxProfit = 0; 15 | int maxPrice = prices[prices.size() - 1]; 16 | for(int i = prices.size() - 2; i >= 0; i--) { 17 | maxProfit = max(maxProfit, maxPrice - prices[i]); 18 | maxPrice = max(maxPrice, prices[i]); 19 | } 20 | return maxProfit; 21 | } 22 | }; 23 | 24 | 25 | ``` -------------------------------------------------------------------------------- /Adobe/max-increase-to-keep-city-skyline.md: -------------------------------------------------------------------------------- 1 | ## [Max Increase to Keep City Skyline](https://leetcode.com/problems/max-increase-to-keep-city-skyline/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/max-increase-to-keep-city-skyline/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int maxIncreaseKeepingSkyline(vector>& grid) { 14 | vector top(grid[0].size(), 0); 15 | vector left(grid.size(), 0); 16 | int ans = 0; 17 | for(int i = 0; i < grid.size(); i++) { 18 | for(int j = 0; j < grid[i].size(); j++) { 19 | left[i] = max(grid[i][j], left[i]); 20 | } 21 | } 22 | for(int i = 0; i < grid[0].size(); i++) { 23 | for(int j = 0; j < grid.size(); j++) { 24 | top[i] = max(grid[j][i], top[i]); 25 | } 26 | } 27 | for(int i = 0; i < grid.size(); i++) { 28 | for(int j = 0; j < grid[i].size(); j++) { 29 | ans += min(top[j], left[i]) - grid[i][j]; 30 | } 31 | } 32 | return ans; 33 | } 34 | }; 35 | 36 | ``` -------------------------------------------------------------------------------- /Adobe/maximum-subarray.md: -------------------------------------------------------------------------------- 1 | ## [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/description/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/maximum-subarray/description/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int maxSubArray(vector& nums) { 14 | int maxSum = INT_MIN; 15 | int currSum = 0; 16 | for(int i = 0; i < nums.size(); i++) { 17 | currSum += nums[i]; 18 | if(currSum > maxSum) 19 | maxSum = currSum; 20 | if(currSum < 0) 21 | currSum = 0; 22 | } 23 | return maxSum; 24 | } 25 | }; 26 | 27 | 28 | ``` -------------------------------------------------------------------------------- /Adobe/powx-n.md: -------------------------------------------------------------------------------- 1 | ## [Pow(x, n)](https://leetcode.com/problems/powx-n/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/powx-n/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | double pow(double x, int n) { 14 | if(n == 0) 15 | return 1; 16 | if(n == 1) 17 | return x; 18 | double temp = pow(x, n / 2); 19 | if(n % 2 == 0) 20 | return temp * temp; 21 | else 22 | return temp * temp * x; 23 | } 24 | double myPow(double x, int n) { 25 | if(n >= 0) 26 | return pow(x, abs(n)); 27 | else 28 | return 1 / pow(x, abs(n)); 29 | } 30 | }; 31 | 32 | ``` -------------------------------------------------------------------------------- /Adobe/readme.md: -------------------------------------------------------------------------------- 1 | # Adobe 2 | 3 | ## Easy 4 | * [unique-email-addresses](../Google/unique-email-addresses.md) 5 | * [two-sum](../Google/two-sum.md) 6 | * [count-and-say](../Google/count-and-say.md) 7 | * [path-sum](../Amazon/path-sum.md) 8 | * [binary-tree-level-order-traversal-ii](../Microsoft/binary-tree-level-order-traversal-ii.md) 9 | * [symmetric-tree](../Facebook/symmetric-tree.md) 10 | * [pascals-triangle-ii](../Amazon/pascals-triangle-ii.md) 11 | * [best-time-to-buy-and-sell-stock](./best-time-to-buy-and-sell-stock.md) 12 | 13 | 14 | ## Medium 15 | * [number-of-islands](../Google/number-of-islands.md) 16 | * [lru-cache](../Google/lru-cache.md) 17 | * [permutations](../Google/permutations.md) 18 | * [permutations-ii](../Facebook/permutations-ii.md) 19 | * [string-to-integer](string-to-integer.md) 20 | * [powx-n](powx-n.md) 21 | * [divide-two-integers](../Facebook/divide-two-integers.md) 22 | * [decode-ways](../Facebook/decode-ways.md) 23 | * [max-increase-to-keep-city-skyline](max-increase-to-keep-city-skyline.md) 24 | * [encode-and-decode-tiny-url](../Google/encode-and-decode-tiny-url.md) 25 | * [max-area-of-island](max-area-of-island.md) 26 | * [valid-parenthesis-string](valid-parenthesis-string.md) 27 | * [sort-colors](../Microsoft/sort-colors.md) 28 | * [maximum-subarray](./maximum-subarray.md) 29 | * [next-permutation](./next-permutation.md) 30 | 31 | 32 | ## Hard 33 | 34 | 35 | -------------------------------------------------------------------------------- /Adobe/string-to-integer.md: -------------------------------------------------------------------------------- 1 | ## [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/string-to-integer-atoi/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int myAtoi(string s) { 14 | long int ans = 0; 15 | bool pos = true; 16 | int i = 0; 17 | while(s[i] == ' ') 18 | i++; 19 | if(s[i] == '-') 20 | pos = false; 21 | if(!pos || s[i] == '+') 22 | i++; 23 | while(s[i] >= '0' && s[i] <= '9') { 24 | ans = ans * 10 + (s[i] - '0'); 25 | i++; 26 | if(ans > INT_MAX && !pos) 27 | return INT_MIN; 28 | else if(ans > INT_MAX && pos) 29 | return INT_MAX; 30 | } 31 | return pos ? (int)ans : -(int)ans; 32 | } 33 | }; 34 | 35 | ``` -------------------------------------------------------------------------------- /Adobe/valid-parenthesis-string.md: -------------------------------------------------------------------------------- 1 | ## [Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/valid-parenthesis-string/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool checkValidString(string s) { 14 | stack s1; 15 | stack s2; 16 | 17 | for(int i = 0; i < s.length(); i++) { 18 | if(s[i] == '(') 19 | s1.push(i); 20 | else if(s[i] == '*') 21 | s2.push(i); 22 | else { 23 | if(!s1.empty()) 24 | s1.pop(); 25 | else if(!s2.empty()) 26 | s2.pop(); 27 | else 28 | return false; 29 | } 30 | } 31 | while(!s1.empty() && !s2.empty()) { 32 | if(s1.top() > s2.top()) 33 | return false; 34 | s1.pop(); 35 | s2.pop(); 36 | } 37 | if(!s1.empty()) 38 | return false; 39 | return true; 40 | 41 | } 42 | }; 43 | 44 | ``` -------------------------------------------------------------------------------- /Amazon/max-consecutive-ones.md: -------------------------------------------------------------------------------- 1 | ## [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/max-consecutive-ones/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int findMaxConsecutiveOnes(vector& nums) { 14 | int count = 0; 15 | int maxOnes = 0; 16 | for(int i = 0; i < nums.size(); i++) { 17 | if(nums[i] == 1) 18 | count++; 19 | else { 20 | maxOnes = max(maxOnes, count); 21 | count = 0; 22 | } 23 | } 24 | maxOnes = max(maxOnes, count); 25 | return maxOnes; 26 | } 27 | }; 28 | 29 | ``` -------------------------------------------------------------------------------- /Amazon/pascals-triangle-ii.md: -------------------------------------------------------------------------------- 1 | ## [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/pascals-triangle-ii/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | vector getRow(int rowIndex) { 14 | vector ans(rowIndex + 1); 15 | for(int i = 0; i <= rowIndex; i++) { 16 | if(i == 0 || i == rowIndex) 17 | ans[i] = 1; 18 | else 19 | ans[i] = (int)(((long)ans[i - 1] * (long)(rowIndex + 1 - i)) / i); 20 | } 21 | return ans; 22 | } 23 | }; 24 | 25 | ``` -------------------------------------------------------------------------------- /Amazon/pascals-triangle.md: -------------------------------------------------------------------------------- 1 | ## [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/pascals-triangle/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | vector> generate(int numRows) { 14 | vector> ans; 15 | for(int i = 0; i < numRows; i++) { 16 | vector temp; 17 | for(int j = 0; j < i + 1; j++) { 18 | if(j == 0 || j == i) 19 | temp.push_back(1); 20 | else 21 | temp.push_back(ans[i - 1][j - 1] + ans[i - 1][j]); 22 | } 23 | ans.push_back(temp); 24 | } 25 | return ans; 26 | } 27 | }; 28 | 29 | ``` -------------------------------------------------------------------------------- /Amazon/path-sum.md: -------------------------------------------------------------------------------- 1 | ## [Path Sum](https://leetcode.com/problems/path-sum/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/path-sum/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 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 | bool hasPathSum(TreeNode* root, int sum) { 25 | if(root == NULL) 26 | return false; 27 | if(sum - root -> val == 0 && root -> left == NULL && root -> right == NULL) 28 | return true; 29 | return hasPathSum(root -> left, sum - root -> val) || hasPathSum(root -> right, sum - root -> val); 30 | } 31 | }; 32 | 33 | ``` -------------------------------------------------------------------------------- /Amazon/perfect-number.md: -------------------------------------------------------------------------------- 1 | ## [Perfect Number](https://leetcode.com/problems/perfect-number/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/perfect-number/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool checkPerfectNumber(int num) { 14 | if(num == 1) 15 | return false; 16 | int sum = 1; 17 | for(int i = 2; i <= sqrt(num); i++) { 18 | if(num % i == 0) 19 | sum += i + num / i; 20 | } 21 | return sum == num; 22 | } 23 | }; 24 | 25 | ``` -------------------------------------------------------------------------------- /Amazon/reverse-bits.md: -------------------------------------------------------------------------------- 1 | ## [Reverse Bits](https://leetcode.com/problems/reverse-bits/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/reverse-bits/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | uint32_t reverseBits(uint32_t n) { 14 | uint32_t ans = 0; 15 | for(int i = 0; i < 32; i++) { 16 | int temp = (n >> i) & 1; 17 | ans |= temp << (31 - i); 18 | } 19 | return ans; 20 | } 21 | }; 22 | 23 | ``` -------------------------------------------------------------------------------- /Amazon/valid-palindrome.md: -------------------------------------------------------------------------------- 1 | ## [Valid Palindrme](https://leetcode.com/problems/valid-palindrome/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/valid-palindrome/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool isPalindrome(string s) { 14 | int start = 0, end = s.length() - 1; 15 | while(start < end) { 16 | if((s[start] > 'z' || s[start] < 'a') && (s[start] > 'Z' || s[start] < 'A') 17 | && (s[start] < '0' || s[start] > '9')) 18 | start++; 19 | else if((s[end] > 'z' || s[end] < 'a') && (s[end] > 'Z' || s[end] < 'A') 20 | && (s[end] < '0' || s[end] > '9')) 21 | end--; 22 | else if(tolower(s[start]) != tolower(s[end])) { 23 | return false; 24 | } 25 | else { 26 | start++; 27 | end--; 28 | } 29 | } 30 | return true; 31 | } 32 | }; 33 | 34 | 35 | ``` -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Thank you for your interest in the project. 4 | 5 | # Ways of Contributing 6 | 7 | ## Correct a solution 8 | 9 | Found a mistake? Please open an issue on GitHub having the format "[CORRECT] LC 1 Two Sum". 10 | 11 | ## Contribute a new solution 12 | 13 | Want to share your awesome solution? Please open an issue on GitHub having the format "[NEW] LC 1 Two Sum". 14 | 15 | ## Request a new solution 16 | 17 | Don't see your problem? Please open an issue on GitHub having the format "[REQUEST] LC 1 Two Sum". 18 | 19 | ## Community Contributions 20 | 21 | You can help others by sharing your experience using this project, whether that is through blog posts, or simply 22 | sharing your thoughts on Twitter and tagging [@chiragsinghal_](https://twitter.com/chiragsinghal_). 23 | 24 | Thanks for contributing! ❤️ 25 | -------------------------------------------------------------------------------- /Facebook/convert-a-number-to-hexadecimal.md: -------------------------------------------------------------------------------- 1 | ## [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/convert-a-number-to-hexadecimal/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | string toHex(int num) { 14 | if(num == 0) 15 | return "0"; 16 | string ans = ""; 17 | vector powTwo{1, 2, 4, 8}; 18 | for(int i = 0; i < 8; i++) { 19 | int temp = 0; 20 | for(int j = 0; j < 4; j++) { 21 | temp += powTwo[j] * (num & 1); 22 | num = num >> 1; 23 | } 24 | if(temp != 0 || num != 0) { 25 | if(temp < 10) 26 | ans = (char)(temp + '0') + ans; 27 | else 28 | ans = (char)(temp - 10 + 'a') + ans; 29 | } 30 | } 31 | return ans; 32 | } 33 | }; 34 | 35 | ``` -------------------------------------------------------------------------------- /Facebook/decode-ways.md: -------------------------------------------------------------------------------- 1 | ## [Decode Ways](https://leetcode.com/problems/decode-ways/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/decode-ways/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int numDecodings(string s) { 14 | if(s.length() == 0) 15 | return 0; 16 | if(s[0] == '0') 17 | return 0; 18 | vector arr(s.length() + 1, 0); 19 | arr[0] = 1; 20 | arr[1] = 1; 21 | for(int i = 1; i < s.length(); i++) { 22 | if(s[i] == '0' && (s[i - 1] == '1' || s[i - 1] == '2')) { 23 | arr[i + 1] += arr[i - 1]; 24 | } 25 | else if(s[i] == '0') { 26 | return 0; 27 | } 28 | else if(s[i + 1] != '0' && (s[i - 1] == '1' || 29 | (s[i - 1] == '2' && s[i] < '7'))) { 30 | arr[i + 1] = arr[i] + arr[i - 1]; 31 | } 32 | else { 33 | arr[i + 1] += arr[i]; 34 | } 35 | } 36 | return arr[s.length()]; 37 | } 38 | }; 39 | 40 | 41 | ``` -------------------------------------------------------------------------------- /Facebook/divide-two-integers.md: -------------------------------------------------------------------------------- 1 | ## [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/divide-two-integers/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int divide(int dividend, int divisor) { 14 | if(dividend == 0) 15 | return 0; 16 | if(divisor == 1) 17 | return dividend; 18 | long int quotient = 0; 19 | int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; 20 | if(divisor == -1 && dividend == INT_MIN) 21 | return INT_MAX; 22 | if(divisor == -1) 23 | return sign * abs(dividend); 24 | long int divid = abs(dividend); 25 | long int divis = abs(divisor); 26 | for(int i = 31; i >= 0; i--) { 27 | if(divid - (divis << i) >= 0) { 28 | divid -= (divis << i); 29 | quotient |= (1 << i); 30 | } 31 | } 32 | if(sign * quotient > INT_MAX || sign * quotient < INT_MIN) 33 | return INT_MAX; 34 | return sign * quotient; 35 | } 36 | }; 37 | 38 | ``` -------------------------------------------------------------------------------- /Facebook/permutations-ii.md: -------------------------------------------------------------------------------- 1 | ## [Permutations II](https://leetcode.com/problems/permutations-ii/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/permutations-ii/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool check(vector& nums, int idx, int curr) { 14 | for (int i = idx; i < curr; i++) 15 | if (nums[i] == nums[curr]) 16 | return 0; 17 | return 1; 18 | } 19 | void perm(vector>& ans, vector& nums, int idx) { 20 | if(idx == nums.size() - 1) { 21 | ans.push_back(nums); 22 | } 23 | else { 24 | for(int i = idx; i < nums.size(); i++) { 25 | if(check(nums, idx, i)) { 26 | swap(nums[i], nums[idx]); 27 | perm(ans, nums, idx + 1); 28 | swap(nums[i], nums[idx]); 29 | } 30 | } 31 | } 32 | } 33 | vector> permuteUnique(vector& nums) { 34 | vector> ans; 35 | perm(ans, nums, 0); 36 | return ans; 37 | } 38 | }; 39 | 40 | ``` -------------------------------------------------------------------------------- /Facebook/reverse-words-in-a-string.md: -------------------------------------------------------------------------------- 1 | ## [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/reverse-words-in-a-string/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | string reverseWords(string s) { 14 | int i = s.length() - 1; 15 | string ans = ""; 16 | while(i >= 0 && s[i] == ' ') 17 | i--; 18 | for(; i >= 0; ){ 19 | string temp = ""; 20 | while(i >= 0 && s[i] != ' ') { 21 | temp = s[i] + temp; 22 | i--; 23 | } 24 | while(i >= 0 && s[i] == ' ') 25 | i--; 26 | ans += temp; 27 | if(i >= 0) 28 | ans += ' '; 29 | } 30 | return ans; 31 | } 32 | }; 33 | 34 | ``` -------------------------------------------------------------------------------- /Facebook/set-matrix-zeroes.md: -------------------------------------------------------------------------------- 1 | ## [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/description/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/set-matrix-zeroes/description/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | void setZeroes(vector>& matrix) { 14 | unordered_map row; 15 | unordered_map col; 16 | 17 | for(int i = 0; i < matrix.size(); i++) { 18 | for (int j = 0; j < matrix[i].size(); j++) { 19 | if(matrix[i][j] == 0) { 20 | row[i] = 1; 21 | col[j] = 1; 22 | } 23 | } 24 | } 25 | 26 | for(int i = 0; i < matrix.size(); i++) { 27 | for (int j = 0; j < matrix[i].size(); j++) { 28 | if(row[i] == 1 || col[j] == 1) { 29 | matrix[i][j] = 0; 30 | } 31 | } 32 | } 33 | } 34 | }; 35 | 36 | 37 | ``` -------------------------------------------------------------------------------- /Facebook/symmetric-tree.md: -------------------------------------------------------------------------------- 1 | ## [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/symmetric-tree/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 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 | bool isMirror(TreeNode* A, TreeNode* B) { 25 | if(A == NULL && B == NULL) 26 | return true; 27 | if(A == NULL || B == NULL) 28 | return false; 29 | return A -> val == B -> val && isMirror(A -> left, B -> right) 30 | && isMirror(A -> right, B -> left); 31 | 32 | } 33 | bool isSymmetric(TreeNode* root) { 34 | return isMirror(root, root); 35 | } 36 | }; 37 | 38 | ``` -------------------------------------------------------------------------------- /Facebook/third-maximum-number.md: -------------------------------------------------------------------------------- 1 | ## Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/third-maximum-number/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int thirdMax(vector& nums) { 14 | long int a = (long)INT_MIN - 1, b = (long)INT_MIN - 1, c = (long)INT_MIN - 1; 15 | for(int i = 0; i < nums.size(); i++) { 16 | if(a < nums[i]) { 17 | c = b; 18 | b = a; 19 | a = nums[i]; 20 | } 21 | else if(a == nums[i]) { 22 | continue; 23 | } 24 | else if(b < nums[i]) { 25 | c = b; 26 | b = nums[i]; 27 | } 28 | else if(b == nums[i]) { 29 | continue; 30 | } 31 | else if (c < nums[i]) { 32 | c = nums[i]; 33 | } 34 | } 35 | if(c == (long)INT_MIN - 1) 36 | return a; 37 | return c; 38 | } 39 | }; 40 | 41 | ``` -------------------------------------------------------------------------------- /Google/buddy-strings.md: -------------------------------------------------------------------------------- 1 | ## [Buddy Strings](https://leetcode.com/problems/buddy-strings/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/buddy-strings/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool buddyStrings(string A, string B) { 14 | if(A.length() != B.length()) 15 | return false; 16 | int first = -1; 17 | int second = -1; 18 | unordered_map map; 19 | bool same = false; 20 | for(int i = 0; i < A.length(); i++) { 21 | if(A[i] != B[i]) { 22 | if(first == -1) 23 | first = i; 24 | else if(second == -1) 25 | second = i; 26 | else 27 | return false; 28 | } 29 | if(map[A[i]] != 0) 30 | same = true; 31 | map[A[i]] = 1; 32 | } 33 | if(first == -1 ^ second == -1) 34 | return false; 35 | if(first == -1 && second == -1 && same) 36 | return true; 37 | if(first == -1 && second == -1) 38 | return false; 39 | if(A[first] == B[second] && A[second] == B[first]) 40 | return true; 41 | return false; 42 | } 43 | }; 44 | 45 | ``` -------------------------------------------------------------------------------- /Google/count-and-say.md: -------------------------------------------------------------------------------- 1 | ## [Count And Say](https://leetcode.com/problems/count-and-say/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/count-and-say/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | string countAndSay(int n) { 14 | if(n == 1) 15 | return "1"; 16 | string ans = "1"; 17 | for(int i = 2; i <= n; i++) { 18 | string temp = ""; 19 | int count = 1; 20 | for(int j = 0; j < ans.length() - 1; j++) { 21 | if(ans[j] == ans[j + 1]) { 22 | count++; 23 | } 24 | else { 25 | temp += to_string(count) + ans[j]; 26 | count = 1; 27 | } 28 | } 29 | temp += to_string(count) + ans[ans.length() - 1]; 30 | ans = temp; 31 | } 32 | return ans; 33 | } 34 | }; 35 | 36 | ``` -------------------------------------------------------------------------------- /Google/find-and-replace-in-string.md: -------------------------------------------------------------------------------- 1 | ## [Find And Replace in String](https://leetcode.com/problems/find-and-replace-in-string/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/find-and-replace-in-string/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | string findReplaceString(string s, vector& indexes, vector& sources, vector& targets) { 14 | 15 | string ans = ""; 16 | vector> p; 17 | 18 | for(int i = 0; i < indexes.size(); i++) { 19 | p.push_back(make_pair(indexes[i], i)); 20 | } 21 | 22 | sort(p.begin(), p.end()); 23 | int j = 0; 24 | 25 | for(int i = 0; i < s.length(); i++) { 26 | if(j >= p.size() || i != p[j].first) 27 | ans += s[i]; 28 | else { 29 | int idx = s.find(sources[p[j].second], p[j].first); 30 | if(idx != string::npos && idx == p[j].first) { 31 | ans += targets[p[j].second]; 32 | i += sources[p[j].second].length() - 1; 33 | } 34 | else 35 | ans += s[i]; 36 | j++; 37 | } 38 | } 39 | return ans; 40 | } 41 | }; 42 | 43 | 44 | 45 | ``` -------------------------------------------------------------------------------- /Google/fruit-into-baskets.md: -------------------------------------------------------------------------------- 1 | ## [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/fruit-into-baskets/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int totalFruit(vector& tree) { 14 | int start = 0; 15 | int end = 0; 16 | int ans = 0; 17 | unordered_map map; 18 | int diff = 0; 19 | while(end < tree.size()) { 20 | if(map[tree[end]] == 0) { 21 | diff++; 22 | } 23 | if(diff <= 2) { 24 | map[tree[end]]++; 25 | ans = max(ans, end - start + 1); 26 | end++; 27 | } 28 | else { 29 | while(diff != 2) { 30 | map[tree[start]]--; 31 | if(map[tree[start]] == 0) 32 | diff--; 33 | start++; 34 | } 35 | map[tree[end]]++; 36 | ans = max(ans, end - start + 1); 37 | end++; 38 | } 39 | } 40 | return ans; 41 | } 42 | }; 43 | ``` -------------------------------------------------------------------------------- /Google/gray-code.md: -------------------------------------------------------------------------------- 1 | ## [Gray Code](https://leetcode.com/problems/gray-code/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/gray-code/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | vector grayCode(int n) { 14 | if(n == 0) { 15 | vector ans(1, 0); 16 | return ans; 17 | } 18 | vector ans; 19 | ans.push_back(0); 20 | for(int i = 1; i <= n; i++) { 21 | int num = 1 << (i - 1); 22 | for(int j = ans.size() - 1; j >=0; j--) { 23 | ans.push_back(ans[j] + num); 24 | } 25 | } 26 | return ans; 27 | } 28 | }; 29 | 30 | ``` -------------------------------------------------------------------------------- /Google/hand-of-straights.md: -------------------------------------------------------------------------------- 1 | ## [Hand of Straights](https://leetcode.com/problems/hand-of-straights/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/hand-of-straights/ 4 | 5 | > Note: This question is the same as 1296: 6 | 7 | --- 8 | 9 | ### Solution 10 | 11 | ```cpp 12 | 13 | class Solution { 14 | public: 15 | bool isNStraightHand(vector& hand, int W) { 16 | if(hand.size() % W != 0) 17 | return false; 18 | map freq; 19 | for(int i = 0; i < hand.size(); i++) 20 | freq[hand[i]]++; 21 | for(auto it = freq.begin(); it != freq.end(); it++) { 22 | int count = it -> second; 23 | int ele = it -> first; 24 | auto ij = it; 25 | if(count == 0) 26 | continue; 27 | for(int j = 0; j < W; j++, ij++) { 28 | if(ele + j != ij -> first) 29 | return false; 30 | if(count > ij -> second) 31 | return false; 32 | freq[ele + j] -= count; 33 | } 34 | } 35 | return true; 36 | } 37 | }; 38 | 39 | ``` -------------------------------------------------------------------------------- /Google/isomorphic-strings.md: -------------------------------------------------------------------------------- 1 | ## [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/isomorphic-strings/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool isIsomorphic(string s, string t) { 14 | unordered_map map; 15 | unordered_map visited; 16 | for(int i = 0; i < s.length(); i++) { 17 | if(map[s[i]] == 0) { 18 | if(visited[t[i]] == true) 19 | return false; 20 | visited[t[i]] = true; 21 | map[s[i]] = t[i]; 22 | } 23 | else { 24 | if(map[s[i]] != t[i]) 25 | return false; 26 | } 27 | } 28 | return true; 29 | } 30 | }; 31 | 32 | ``` -------------------------------------------------------------------------------- /Google/nth-digit.md: -------------------------------------------------------------------------------- 1 | ## [Nth Digit](https://leetcode.com/problems/nth-digit/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/nth-digit/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int findNthDigit(int n) { 14 | if(n < 10) 15 | return n; 16 | int dig = 1; 17 | int num = 1; 18 | int count = 9; 19 | while((long)dig * (long)count <= (long)n) { 20 | n -= dig * count; 21 | dig++; 22 | count *= 10; 23 | num *= 10; 24 | } 25 | if(n == 0) 26 | return 9; 27 | while(dig <= n) { 28 | n -= dig; 29 | num++; 30 | } 31 | if(n == 0) 32 | return (num - 1) % 10; 33 | for(int i = 0; i < dig - n; i++) 34 | num /= 10; 35 | return num % 10; 36 | } 37 | }; 38 | 39 | ``` -------------------------------------------------------------------------------- /Google/permutations.md: -------------------------------------------------------------------------------- 1 | ## [Permutations](https://leetcode.com/problems/permutations/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/permutations/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | void perm(vector>& ans, vector& nums, int idx) { 14 | if(idx == nums.size() - 1) 15 | ans.push_back(nums); 16 | else { 17 | for(int i = idx; i < nums.size(); i++) { 18 | swap(nums[i], nums[idx]); 19 | perm(ans, nums, idx + 1); 20 | swap(nums[i], nums[idx]); 21 | } 22 | } 23 | } 24 | vector> permute(vector& nums) { 25 | vector> ans; 26 | perm(ans, nums, 0); 27 | return ans; 28 | } 29 | }; 30 | 31 | ``` -------------------------------------------------------------------------------- /Google/toeplitz-matrix.md: -------------------------------------------------------------------------------- 1 | ## [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/toeplitz-matrix/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool isToeplitzMatrix(vector>& matrix) { 14 | for(int i = 0; i < matrix.size() - 1; i++) { 15 | int ele = matrix[i][0]; 16 | for(int j = i + 1, k = 1; j < matrix.size() && k < matrix[0].size(); j++, k++) { 17 | if(ele != matrix[j][k]) 18 | return false; 19 | } 20 | } 21 | for(int i = 1; i < matrix[0].size() - 1; i++) { 22 | int ele = matrix[0][i]; 23 | for(int j = i + 1, k = 1; j < matrix[0].size() && k < matrix.size(); j++, k++) { 24 | if(ele != matrix[k][j]) 25 | return false; 26 | } 27 | } 28 | return true; 29 | } 30 | }; 31 | 32 | 33 | ``` -------------------------------------------------------------------------------- /Google/two-sum.md: -------------------------------------------------------------------------------- 1 | ## [Two Sum](https://leetcode.com/problems/two-sum/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/two-sum/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | vector twoSum(vector& nums, int target) { 14 | unordered_map map; 15 | vector ans; 16 | for(int i = 0; i < nums.size(); i++) { 17 | if(map[target - nums[i]] != 0) { 18 | ans.push_back(map[target - nums[i]] - 1); 19 | ans.push_back(i); 20 | } 21 | map[nums[i]] = i + 1; 22 | } 23 | return ans; 24 | } 25 | }; 26 | ``` -------------------------------------------------------------------------------- /Google/valid-mountain-array.md: -------------------------------------------------------------------------------- 1 | ## [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/valid-mountain-array/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool validMountainArray(vector& arr) { 14 | if(arr.size() < 3) 15 | return false; 16 | int i = 0; 17 | while(i < arr.size() - 1 && arr[i] < arr[i + 1]) 18 | i++; 19 | if(i == arr.size() - 1 || i == 0) 20 | return false; 21 | while(i < arr.size() - 1 && arr[i] > arr[i + 1]) 22 | i++; 23 | if(i == arr.size() - 1) 24 | return true; 25 | return false; 26 | } 27 | }; 28 | 29 | ``` -------------------------------------------------------------------------------- /Google/word-pattern.md: -------------------------------------------------------------------------------- 1 | ## [Word Pattern](https://leetcode.com/problems/word-pattern/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/word-pattern/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool wordPattern(string pattern, string s) { 14 | unordered_map map; 15 | unordered_map count; 16 | int i = 0; 17 | for(int j = 0; j < pattern.length(); j++) { 18 | string word = ""; 19 | for(; i < s.length() && s[i] != ' '; i++) { 20 | word += s[i]; 21 | } 22 | i++; 23 | if(word == "") 24 | return false; 25 | if(map[pattern[j]] == "") { 26 | map[pattern[j]] = word; 27 | count[word]++; 28 | if(count[word] > 1) 29 | return false; 30 | } 31 | else if(map[pattern[j]] != word) 32 | return false; 33 | } 34 | if(i < s.length()) 35 | return false; 36 | return true; 37 | } 38 | }; 39 | 40 | 41 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 CHIRAG SINGHAL 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 | -------------------------------------------------------------------------------- /Microsoft/balanced-binary-tree.md: -------------------------------------------------------------------------------- 1 | ## [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/balanced-binary-tree/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 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 | bool isBal(TreeNode* root, int* h) { 25 | if(root == NULL) { 26 | *h = 0; 27 | return true; 28 | } 29 | int lh, rh; 30 | bool lb = isBal(root -> left, &lh); 31 | bool rb = isBal(root -> right, &rh); 32 | *h = max(lh, rh) + 1; 33 | if(!lb || !rb || abs(lh - rh) > 1) 34 | return false; 35 | return true; 36 | } 37 | bool isBalanced(TreeNode* root) { 38 | int height = 0; 39 | return isBal(root, &height); 40 | } 41 | }; 42 | 43 | ``` -------------------------------------------------------------------------------- /Microsoft/excel-sheet-column-title.md: -------------------------------------------------------------------------------- 1 | ## [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/excel-sheet-column-title/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | string convertToTitle(int n) { 14 | string ans = ""; 15 | while(n) { 16 | char ch = ('A' + (n - 1) % 26); 17 | ans = ch + ans; 18 | n = (n - 1) / 26; 19 | } 20 | return ans; 21 | } 22 | }; 23 | 24 | ``` -------------------------------------------------------------------------------- /Microsoft/find-pivot-index.md: -------------------------------------------------------------------------------- 1 | ## [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/find-pivot-index/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | int pivotIndex(vector& nums) { 14 | int sum = 0, currSum = 0; 15 | for(int i = 0; i < nums.size(); i++) 16 | sum += nums[i]; 17 | for(int i = 0; i < nums.size(); i++) { 18 | if(currSum == sum - currSum - nums[i]) 19 | return i; 20 | currSum += nums[i]; 21 | } 22 | return -1; 23 | } 24 | }; 25 | 26 | ``` -------------------------------------------------------------------------------- /Microsoft/rotate-string.md: -------------------------------------------------------------------------------- 1 | ## [Rotate String](https://leetcode.com/problems/rotate-string/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/rotate-string/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | bool rotateString(string A, string B) { 14 | if(A.length() != B.length()) 15 | return false; 16 | if(A.length() == 0) 17 | return true; 18 | for(int i = 0; i < A.length(); i++) { 19 | if(A[i] == B[0]) { 20 | int j = (i + 1) % A.length(), k = 1; 21 | while(j != i) { 22 | if(A[j] != B[k]) 23 | break; 24 | j = (j + 1) % A.length(); 25 | k++; 26 | } 27 | if(j == i) 28 | return true; 29 | } 30 | } 31 | return false; 32 | } 33 | }; 34 | 35 | ``` -------------------------------------------------------------------------------- /Microsoft/sort-colors.md: -------------------------------------------------------------------------------- 1 | ## [Sort Colors](https://leetcode.com/problems/sort-colors/description/) 2 | 3 | ### Problem Link - https://leetcode.com/problems/sort-colors/description/ 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | class Solution { 12 | public: 13 | void swap(vector& nums, int i, int j ) { 14 | int temp = nums[i]; 15 | nums[i] = nums[j]; 16 | nums[j] = temp; 17 | } 18 | void sortColors(vector& nums) { 19 | int lo = 0, hi = nums.size() - 1; 20 | int mid = 0; 21 | while(mid <= hi) { 22 | if(nums[mid] == 0) { 23 | swap(nums, lo, mid); 24 | lo++; 25 | mid++; 26 | } 27 | else if(nums[mid] == 2) { 28 | swap(nums, hi, mid); 29 | hi--; 30 | } 31 | else { 32 | mid++; 33 | } 34 | } 35 | } 36 | }; 37 | 38 | 39 | ``` -------------------------------------------------------------------------------- /Miscellaneous/Codechef/aplong/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | int n; 8 | std::cin>>n; 9 | long int arr[n]; 10 | for(int j = 0; j < n; j++) 11 | std::cin>>arr[j]; 12 | std::sort(arr, arr + n); 13 | long int sum = 0; 14 | for(int j = n - 1; j > -1; j--) { 15 | if(arr[j] + j - n + 1 > 0) 16 | sum += (arr[j] + j - n + 1) % 1000000007; 17 | } 18 | std::cout< 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | int n; 8 | int soc_dist = 1; 9 | int lastperson = -1; 10 | std::cin>>n; 11 | long int arr[n]; 12 | for(int j = 0; j < n; j++) { 13 | std::cin>>arr[j]; 14 | if(lastperson == -1 && arr[j] == 1) 15 | lastperson = j; 16 | else if (arr[j] == 1) { 17 | if(j - lastperson < 6) 18 | soc_dist = 0; 19 | lastperson = j; 20 | } 21 | } 22 | if(soc_dist) 23 | std::cout<<"YES"< 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | long int x, k; 8 | std::cin>>x>>k; 9 | long int count = 0; 10 | while(x % 2 == 0) { 11 | count++; 12 | x /= 2; 13 | } 14 | for(long int i = 3; i <= sqrt(x); i += 2) 15 | while(x % i == 0) { 16 | count++; 17 | x /= i; 18 | } 19 | if(x > 2) 20 | count++; 21 | if(count >= k) 22 | std::cout<<1< 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for( int k = 0; k < t; k++) { 7 | int n; 8 | std::cin>>n; 9 | if(n == 1) { 10 | std::cout<<1< 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | int n, m, k; 8 | std::cin>>n>>m>>k; 9 | int sol[n][k]; 10 | int ans[n]; 11 | for(int j = 0; j < n; j++) { 12 | int max = 0; 13 | int count[m]; 14 | int probans = 0; 15 | for(int l = 0; l < m; l++) 16 | count[l] = 0; 17 | for(int l = 0; l < k; l++) { 18 | std::cin>>sol[j][l]; 19 | count[sol[j][l]]++; 20 | if(count[sol[j][l]] > max) { 21 | max = count[sol[j][l]]; 22 | probans = sol[j][l]; 23 | } 24 | } 25 | ans[j] = probans; 26 | } 27 | for(int j = 0; j < n; j++) 28 | std::cout< 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | for (int i = 0; i < t; i++) { 7 | int n; 8 | scanf("%d", &n); 9 | int arr[101] = {0}; 10 | int max = 0; 11 | for(int j = 0; j < n; j++) { 12 | int num; 13 | scanf("%d", &num); 14 | arr[num]++; 15 | if(arr[num] > max) 16 | max = arr[num]; 17 | } 18 | printf("%d\n", n - max); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/deccookoff/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/deccookoff/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/declong/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long int t; 5 | scanf("%ld", &t); 6 | for (long int i = 0; i < t; i++) { 7 | long int n; 8 | scanf("%ld", &n); 9 | int arr[8] = {0}; 10 | int answer = 0; 11 | for(int j = 0; j < n; j++) { 12 | int ques, score; 13 | scanf("%d", &ques); 14 | scanf("%d", &score); 15 | if(ques > 8) 16 | continue; 17 | if(arr[ques - 1] < score) { 18 | answer = answer - arr[ques - 1] + score; 19 | arr[ques - 1] = score; 20 | } 21 | } 22 | printf("%d\n", answer); 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/declong/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long int t; 5 | scanf("%ld", &t); 6 | for (long int i = 0; i < t; i++) { 7 | long int n; 8 | long long int two = 0; 9 | long long int zero = 0; 10 | unsigned long long int ans = 0; 11 | scanf("%ld", &n); 12 | for(int j = 0; j < n; j++) { 13 | long long int num; 14 | scanf("%lld", &num); 15 | if(num == 2) 16 | two++; 17 | if(num == 0) 18 | zero++; 19 | } 20 | ans = two * (two - 1); 21 | ans = ans / 2; 22 | ans = ans + (zero * (zero - 1))/2; 23 | printf("%llu\n", ans); 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/declong/5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long int t; 5 | scanf("%ld", &t); 6 | for (long int i = 0; i < t; i++) { 7 | long int n; 8 | long long int two = 0; 9 | long long int zero = 0; 10 | unsigned long long int ans = 0; 11 | scanf("%ld", &n); 12 | for(int j = 0; j < n; j++) { 13 | long long int num; 14 | scanf("%lld", &num); 15 | if(num == 2) 16 | two++; 17 | if(num == 0) 18 | zero++; 19 | } 20 | ans = two * (two - 1); 21 | ans = ans / 2; 22 | ans = ans + (zero * (zero - 1))/2; 23 | printf("%llu\n", ans); 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/declong/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/declong/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/feblong/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int t; 6 | std::cin>>t; 7 | for(int i = 0; i < t; i++) { 8 | int n; 9 | std::cin>>n; 10 | long int arr[n], brr[n]; 11 | for(int j = 0; j < n; j++) 12 | std::cin>>arr[j]; 13 | for(int j = 0; j < n; j++) 14 | std::cin>>brr[j]; 15 | std::sort(arr, arr + n); 16 | std::sort(brr, brr + n); 17 | long long int sum = 0; 18 | for(int j = 0; j < n; j++) { 19 | if(arr[j] < brr[j]) 20 | sum += arr[j]; 21 | else 22 | sum += brr[j]; 23 | } 24 | std::cout< 2 | #include 3 | 4 | int main() { 5 | int t; 6 | std::cin>>t; 7 | for(int i = 0; i < t; i++) { 8 | long int n, k, max = 0; 9 | std::cin>>n>>k; 10 | long int arr[n], brr[n], crr[n]; 11 | for(long int j = 0; j < n; j++) { 12 | std::cin>>arr[j]; 13 | brr[j] = 0; 14 | if(arr[j] % k != 0){ 15 | max = j + 1; 16 | brr[j] = k - arr[j] % k; 17 | } 18 | } 19 | long long int sum = 0; 20 | for(long int j = n - 1; j != -1; j--) { 21 | sum += brr[j]; 22 | crr[j] = sum; 23 | } 24 | if(max == 0) { 25 | std::cout<<0<= crr[j]){ 31 | sum = 0; 32 | break; 33 | } 34 | c += arr[j] % k; 35 | // std::cout< 2 | #include 3 | 4 | int main() { 5 | int t; 6 | std::cin>>t; 7 | for(int k = 0; k < t; k++) { 8 | long int n, p; 9 | std::cin>>n>>p; 10 | int arr[n], brr[n]; 11 | for(int j = 0; j < n; j++) 12 | std::cin>>arr[j]; 13 | int flag = 1; 14 | for(int i = 0; i < n; i++) 15 | brr[i] = 0; 16 | for(int i = 0; i < n - 1; i++) 17 | if(arr[i + 1] % arr[i] != 0) 18 | flag = 0; 19 | if(flag == 1 && p % arr[n - 1] == 0) 20 | printf("NO\n"); 21 | else { 22 | for(int i = n - 1; i > -1; i--) { 23 | if(p % arr[i] != 0) { 24 | brr[i] = p / arr[i] + 1; 25 | p -= brr[i] * arr[i]; 26 | break; 27 | } 28 | else { 29 | brr[i] = p / arr[i] - 1; 30 | p -= brr[i] * arr[i]; 31 | } 32 | } 33 | printf("YES "); 34 | for(int i = 0; i < n; i++) 35 | printf("%d ", brr[i]); 36 | putchar('\n'); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/feblong/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/feblong/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/hashcode/a.sample_sol.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 0 5 3 | 3 4 2 1 0 4 | 1 1 5 | 5 6 | 7 | 8 | -------------------------------------------------------------------------------- /Miscellaneous/Codechef/hashcode/a_example.txt: -------------------------------------------------------------------------------- 1 | 6 2 7 2 | 1 2 3 6 5 4 3 | 5 2 2 4 | 0 1 2 3 4 5 | 4 3 1 6 | 0 2 3 5 7 | -------------------------------------------------------------------------------- /Miscellaneous/Codechef/hashcode/hashcode_2020_online_qualification_round.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/hashcode/hashcode_2020_online_qualification_round.pdf -------------------------------------------------------------------------------- /Miscellaneous/Codechef/icpc/1.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int main() { 5 | int t; 6 | char d; int dig, prev; 7 | long long N; 8 | std::cin >> t; 9 | std::cin.get(c); 10 | while(t) { 11 | bool removed = false; 12 | N = 0; 13 | std::cin.get(d); 14 | prev = d - '0'; 15 | while(std::cin.get(d)) { 16 | if (d == '\n') { 17 | if (removed) { 18 | N = N*10 + prev; 19 | } 20 | break; 21 | } 22 | else { 23 | dig = c - '0'; 24 | 25 | if (prev <= dig || removed) { 26 | 27 | N = N*10 + prev; 28 | } 29 | else { 30 | removed = true; 31 | } 32 | prev = dig; 33 | } 34 | } 35 | 36 | std::cout << N << "\n"; 37 | 38 | --t; 39 | } 40 | return 0; 41 | 42 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/icpc/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool compareTwoLine(lineSegment a, lineSegment b) 5 | { 6 | return a.V < b.V; 7 | } 8 | 9 | 10 | struct lineSegment { 11 | long int L; 12 | long int R; 13 | long int V; 14 | }; 15 | 16 | int main() { 17 | 18 | int t; 19 | std::cin>>t; 20 | for(int i = 0; i < t; i++) { 21 | int n; 22 | std::cin>>n; 23 | struct lineSegment prob[n]; 24 | for(int j = 0; j < n; j++) { 25 | std::cin>>prob[i].L; 26 | std::cin>>prob[i].R; 27 | std::cin>>prob[i].V; 28 | } 29 | std::sort(prob, prob + n, compareTwoLine); 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/janlong/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | int s, w1, w2, w3; 8 | std::cin>>s>>w1>>w2>>w3; 9 | if(w1 + w2 + w3 <= s) 10 | std::cout<<1< 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | unsigned long long int n, a, b, c, d, e, s; 8 | int res; 9 | std::cin>>n>>a; 10 | long long int ans = 1; 11 | for(int j = 0; j < n; j++) 12 | ans *= 10; 13 | 14 | s = 2 * ans + a; 15 | std::cout<>b; 17 | c = ans - b; 18 | std::cout<>d; 20 | e = ans - d; 21 | std::cout<>res; 23 | if(res == -1) 24 | break; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/janlong/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/janlong/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/jlylong/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int test; 5 | scanf("%d", &test); 6 | for (int i = 0; i < test; i++) 7 | { 8 | int n, steal = 0, found = 0; 9 | double mean = 0; 10 | scanf("%d", &n); 11 | long long int coins[n]; 12 | 13 | for (int j = 0; j < n; j++) 14 | { 15 | scanf("%lld", &coins[j]); 16 | mean += coins[j]; 17 | } 18 | mean = mean / n; 19 | for (int j = 0; j < n; j++){ 20 | if(!found && coins[j] == mean){ 21 | // printf("First found %d", j + 1); 22 | found = j + 1; 23 | break; 24 | } 25 | } 26 | if(!found) 27 | printf("Impossible\n"); 28 | else 29 | printf("%d\n", found); 30 | 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/jlylong/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // int cmpfunc (const void * a, const void * b) { 5 | // return ( *(int*)a - *(int*)b ); 6 | // } 7 | 8 | int max(int a, int b){ 9 | if(a < 0) 10 | a = -a; 11 | if (b < 0) 12 | b = -b; 13 | if(a > b) 14 | return a; 15 | else 16 | return b; 17 | } 18 | 19 | int main(){ 20 | int test; 21 | scanf("%d", &test); 22 | for (int i = 0; i < test; i++) 23 | { 24 | int n, k, sum = 0; 25 | scanf("%d", &n); 26 | scanf("%d", &k); 27 | int a[100000] = {0}; 28 | int j = 0, l = n; 29 | while(k != 0){ 30 | a[j] = k / l; 31 | // printf("%d\n", arr[j]); 32 | k = k - a[j]; 33 | l--; 34 | j++; 35 | } 36 | for(int j = 0; j < n; j++) 37 | printf("%d\n", a[j]); 38 | // qsort(arr, n, sizeof(int), cmpfunc); 39 | 40 | // sum += a[n - j - 1] - a[j]; 41 | for (int j = 0; j <= n / 2; j++) 42 | { 43 | sum += max(a[n - j - 1] - a[j] + a[n - j - 1] - a[j + 1], a[n - j -1] - a[j] + a[j] - a[n - j - 1]); 44 | printf("%d", sum); 45 | } 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/jlylong/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/jlylong/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junecook/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int test, num; 5 | char key[101], sol[101]; 6 | scanf("%d", &test); 7 | for(int i = 0; i < test; i++){ 8 | int score = 0; 9 | scanf("%d", &num); 10 | scanf("%s", sol); 11 | scanf("%s", key); 12 | for(int j = 0; j < num; j++){ 13 | if(sol[j] == key[j]) 14 | score += 1; 15 | else if(key[j] == 'N') 16 | continue; 17 | else 18 | j++; 19 | } 20 | printf("%d\n", score); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junecook/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int test; 5 | scanf("%d", &test); 6 | for(int i = 0; i < test; i++){ 7 | int n, m, d, v, smallest; 8 | int ad[2] = {0, 0}; 9 | int av[2] = {0, 0}; 10 | scanf("%d", &n); 11 | scanf("%d", &m); 12 | for(int j = 0; j < n; j++){ 13 | int sameday = 0; 14 | scanf("%d", &d); 15 | scanf("%d", &v); 16 | for(int k = 0; k < 2; k++){ 17 | if(d == ad[k]){ 18 | sameday = 1; 19 | if(v > av[k]){ 20 | ad[k] = d; 21 | av[k] = v; 22 | break; 23 | } 24 | } 25 | } 26 | if(!sameday){ 27 | if(av[0] < av[1]) 28 | smallest = 0; 29 | else 30 | smallest = 1; 31 | if(v > av[smallest]){ 32 | av[smallest] = v; 33 | ad[smallest] = d; 34 | } 35 | } 36 | } 37 | printf("%d\n", av[0] + av[1]); 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junecook/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int cas; 5 | scanf("%d", &cas); 6 | for (int i = 0; i < cas; i++) { 7 | int n, m, avail[100000], fav, order[100000] = {0}; 8 | long long int pricefav, priceother, profit = 0; 9 | scanf("%d", &n); 10 | scanf("%d", &m); 11 | for(int j = 0; j < m; j++) 12 | scanf("%d", &avail[j]); 13 | for (int j = 0; j < n; j++) 14 | { 15 | scanf("%d", &fav); 16 | scanf("%lld", &pricefav); 17 | scanf("%lld", &priceother); 18 | if(avail[fav - 1] > 0){ 19 | avail[fav - 1]--; 20 | profit += pricefav; 21 | order[j] = fav; 22 | } 23 | else { 24 | profit += priceother; 25 | } 26 | } 27 | for(int j = 0; j < n; j++){ 28 | if(order[j] != 0) 29 | continue; 30 | else 31 | for (int k = 0; k < n; k++) 32 | if(avail[k] > 0){ 33 | order[j] = k + 1; 34 | avail[k]--; 35 | break; 36 | } 37 | } 38 | printf("%lld\n", profit); 39 | for(int j = 0; j < n; j++) 40 | printf("%d ", order[j]); 41 | printf("\n"); 42 | } 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junecook/4.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/junecook/4.c -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junecook/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/junecook/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junelong/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int num, size; 5 | char atten[1001]; 6 | scanf("%d", &num); 7 | 8 | for(int i = 0; i < num; i++){ 9 | int count = 0; 10 | int proxy = 0; 11 | scanf("%d", &size); 12 | scanf("%s", atten); 13 | for(int j = 0; j < size; j++) 14 | if(atten[j] == 'P') 15 | count++; 16 | 17 | if(count < 0.75 * size) 18 | for(int i = 2; i < size - 2; i++){ 19 | if(atten[i] == 'A' && (atten[i-1] == 'P' || atten[i - 2] == 'P') && (atten[i + 1] == 'P' || atten[i + 2] == 'P')) 20 | proxy++; 21 | if(proxy + count >= 0.75 * size){ 22 | printf("%d\n", proxy); 23 | break; 24 | } 25 | } 26 | else if(count >= 0.75 * size) 27 | printf("%d\n", proxy); 28 | 29 | if(proxy + count < 0.75 * size) 30 | printf("-1\n"); 31 | } 32 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junelong/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int num; 5 | unsigned long long int choice; 6 | scanf("%d", &num); 7 | 8 | for(int i = 0; i < num; i++){ 9 | scanf("%llu", &choice); 10 | int sum = 0, add = 0; 11 | for(unsigned long long int x = choice; x > 0; x = x / 10) 12 | sum += x % 10; 13 | if(sum % 10 != 0) 14 | add = 10 - sum % 10; 15 | 16 | printf("%llu\n", choice * 10 + add); 17 | 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junelong/3.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MOD 1000000007 3 | 4 | int main(){ 5 | int num; 6 | unsigned long long int k; 7 | scanf("%d", &num); 8 | 9 | for(int i = 0; i < num; i++){ 10 | scanf("%llu", &k); 11 | unsigned long long int res = 1; 12 | k = k % (MOD - 1); 13 | unsigned long long int y = k - 1, x = 2; 14 | while (y > 0) { 15 | // If y is odd, multiply x with the result 16 | if (y & 1) 17 | res = (res * x) % MOD; 18 | 19 | // y must be even now 20 | y = y >> 1; // y = y/2 21 | x = (x * x) % MOD; 22 | } 23 | res = (res * 10) % MOD; 24 | printf("%llu\n", res); 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junelong/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int num; 6 | unsigned long long int k; 7 | scanf("%d", &num); 8 | 9 | for(int i = 0; i < num; i++){ 10 | scanf("%llu", &k); 11 | int total = 0; 12 | 13 | for(int j = 0; j < pow(10, k); j++){ 14 | int number[10] = {0}; 15 | int count = 0; 16 | 17 | if(j == 0 || j == pow(10, k) - 1 && number[0] == 0){ 18 | number[0]++; 19 | count++; 20 | } 21 | 22 | for(int x = j; x > 0; x = x / 10){ 23 | int divi = x % 10; 24 | if( number[divi] == 0){ 25 | number[divi]++; 26 | count++; 27 | } 28 | } 29 | for(int x = pow(10, k) - j - 1; x > 0; x = x / 10){ 30 | int divi = x % 10; 31 | if( number[divi] == 0){ 32 | number[divi]++; 33 | count++; 34 | } 35 | } 36 | if(count == 2){ 37 | total++; 38 | // printf("%d %d\n", j, (int)pow(10, k) - j - 1); 39 | } 40 | } 41 | 42 | printf("%d\n", total); 43 | 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/junelong/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/junelong/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/maylong/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | int n; 8 | std::cin>>n; 9 | int x_prev = -1, x_curr; 10 | int count = 1, min = INT_MAX, max = INT_MIN; 11 | 12 | for(int j = 0; j < n; j++) { 13 | if(x_prev == -1) { 14 | std::cin>>x_prev; 15 | continue; 16 | } 17 | std::cin>>x_curr; 18 | if(x_curr - x_prev < 3) { 19 | count++; 20 | x_prev = x_curr; 21 | } 22 | else { 23 | if(count < min) 24 | min = count; 25 | if(count > max) 26 | max = count; 27 | count = 1; 28 | x_prev = x_curr; 29 | } 30 | } 31 | 32 | if(count < min) 33 | min = count; 34 | if(count > max) 35 | max = count; 36 | 37 | std::cout< 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | int n, q, count = 0; 8 | std::cin>>n>>q; 9 | std::string s; 10 | std::cin>>s; 11 | std::vector arr(26, 0); 12 | std::vector brr(n + 1, 0); 13 | for(int j = 0; j < n; j++) { 14 | if(arr[s[j] - 'a'] == 0) 15 | count++; 16 | arr[s[j] - 'a']++; 17 | } 18 | // for(int j = 0; j < 26; j++) 19 | // std::cout<>c; 35 | if(c == 0) 36 | std::cout<= n) 38 | std::cout<<"0\n"; 39 | else if(n - count * c + brr[c - 1] > 0) 40 | std::cout< 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | int arr[128] = {0}; 7 | arr[1] = 0; 8 | int brr[128] = {0}; 9 | for(int i = 2; i < 128; i++) { 10 | for(int j = i - 2; j > -1; j--) { 11 | if(arr[i - 1] == arr[j]){ 12 | arr[i] = i - 1 - j; 13 | break; 14 | } 15 | } 16 | } 17 | // for(int i = 0; i < 128; i++) 18 | // printf("%d %d\n", arr[i], arr[i]); 19 | for(int i = 0; i < t; i++) { 20 | int n, count = 0; 21 | scanf("%d", &n); 22 | for(int j = 0; j < n; j++) 23 | if(arr[j] == arr[n - 1]) 24 | count++; 25 | printf("%d\n", count); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/novlong/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | for(int i = 0; i < t; i++) { 7 | int n; 8 | int arr[10] = {0}; 9 | scanf("%d", &n); 10 | for(int k = 0; k < n; k++) { 11 | char ch[10]; 12 | getchar(); 13 | scanf("%s", ch); 14 | for(int j = 0; j < 10; j++) { 15 | // printf("%c %d\n", ch, j); 16 | if(ch[j] == '1'){ 17 | // printf(" %d %d\n", j, arr[j]); 18 | arr[j]++; 19 | } 20 | } 21 | } 22 | int count = 0; 23 | // for(int k = 0; k < 10; k++) 24 | // printf("%d\n", arr[k]); 25 | for(int k = 0; k < 10; k++) 26 | if(arr[k] % 2) 27 | count++; 28 | printf("%d\n", count); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/novlong/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/novlong/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/null/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int num, a, b; 4 | scanf("%d", &num); 5 | 6 | for (int i = 0; i < num; i++){ 7 | scanf("%d", &a); 8 | scanf("%d", &b); 9 | printf("%d ", a + b -1); 10 | if(a > b) 11 | printf("%d\n", a - b + 1); 12 | else 13 | printf("%d\n", b - a + 1); 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/null/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define mod 1000000007 3 | 4 | int main(){ 5 | int num, a; 6 | scanf("%d", &num); 7 | 8 | for (int i = 0; i < num; i++){ 9 | unsigned long long int res = 1; 10 | scanf("%d", &a); 11 | if(a % 2) 12 | a = a + 1; 13 | 14 | for(int j = a - 1; j >= 3; j -= 2){ 15 | j = j % mod; 16 | res = (res * j) % mod; 17 | if (res == 0) 18 | break; 19 | } 20 | printf("%llu\n", res); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/null/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int count(int a, int option[]){ 4 | // int *p = option; 5 | // printf("%d %d\n", a,option[0]); 6 | if( a < 0 || option[0] == 0 ) return 0; 7 | else if (a == 0) return 1; 8 | else return count(a, option + 1) + count(a - option[0], option); 9 | } 10 | 11 | int main(){ 12 | int num, p, d; 13 | scanf("%d", &num); 14 | int choice[4] = {1, 2, 4, 0}; 15 | // printf("%d", choice[0]); 16 | for (int i = 0; i < num; i++){ 17 | scanf("%d", &p); 18 | scanf("%d", &d); 19 | int play = p - d; 20 | int res = count(play, choice); 21 | printf("%d\n", res); 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/null/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/null/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/oclong/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | for (int i = 0; i < t; i++) { 7 | int n, temp; 8 | int i = 0, count = 0; 9 | int arr[5]; 10 | scanf("%d", &n); 11 | for (int j = 0; j < n; j++) { 12 | int min = 0; 13 | scanf("%d", &temp); 14 | if(j == 0) { 15 | count++; 16 | } 17 | else if(j < 5) { 18 | min = arr[0]; 19 | for(int k = 0; k < i; k++) 20 | if(arr[k] < min) 21 | min = arr[k]; 22 | if(temp < min) 23 | count++; 24 | } 25 | else { 26 | min = arr[0]; 27 | for(int k = 0; k < 5; k++) 28 | if(arr[k] < min) 29 | min = arr[k]; 30 | if(temp < min) 31 | count++; 32 | } 33 | arr[i] = temp; 34 | i = ( i + 1 ) % 5; 35 | } 36 | printf("%d\n", count); 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/oclong/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | for(int i = 0; i < t; i++) { 7 | long long int n, k; 8 | scanf("%lld", &n); 9 | scanf("%lld", &k); 10 | long int arr[n], brr[n]; 11 | long long int l = k / n; 12 | l = l % 3; 13 | long long int p = k % n; 14 | for(int j = 0; j < n; j++) { 15 | scanf("%ld", &arr[j]); 16 | brr[j] = arr[j]; 17 | } 18 | if(n % 2 && (k - 1) >= n / 2) 19 | arr[n / 2] = 0; 20 | for(int j = 0; j <= n; j++) { 21 | if(l == 0) { 22 | break; 23 | } 24 | else if (l == 1) { 25 | arr[j] = arr[j] ^ arr[n - j - 1]; 26 | } 27 | else { 28 | arr[n - j - 1] = arr[j] ^ arr[n - j - 1]; 29 | } 30 | } 31 | for(int j = 0; j < p; j++) 32 | arr[j] = arr[j] ^ arr[n - j - 1]; 33 | 34 | // for(long long int j = 0; j < k; j++){ 35 | // brr[j % n] = brr[j % n] ^ brr[n - j % n - 1]; 36 | // printf("%ld\n", brr[j%n]); 37 | // } 38 | 39 | for(int j = 0; j < n ; j++) 40 | printf("%ld ", arr[j]); 41 | putchar('\n'); 42 | // for(int j = 0; j < n ; j++) 43 | // printf("%ld ", brr[j]); 44 | // putchar('\n'); 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/oclong/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | for(int i = 0; i < t; i++) { 7 | int n; 8 | scanf("%d", &n); 9 | int arr[n], brr[100000] = {0}, crr[100000] = {0}, max = -1; 10 | for(int j = 0; j < n; j++) 11 | brr[j] = j; 12 | for(int j = 0; j < n; j++) { 13 | int count = 0; 14 | scanf("%d", &arr[j]); 15 | for(int k = 0; k < j;) { 16 | if(arr[k] % arr[j] == 0) { 17 | // printf("%d %d\n", arr[j], arr[k]); 18 | brr[k] = j; 19 | count++; 20 | } 21 | } 22 | if(max < count) 23 | max = count; 24 | } 25 | printf("%d\n", max); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/oclong/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | for(int i = 0; i < t; i++) { 7 | int n; 8 | scanf("%d", &n); 9 | int arr[n], brr[100000] = {0}, crr[100000] = {0}, max = -1; 10 | vector arr[n]; 11 | for(int j = 0; j < n; j++) 12 | brr[j] = j; 13 | for(int j = 0; j < n; j++) { 14 | int count = 0; 15 | scanf("%d", &arr[j]); 16 | for(int k = 0; k < j;) { 17 | if(arr[k] % arr[j] == 0) { 18 | // printf("%d %d\n", arr[j], arr[k]); 19 | brr[k] = j; 20 | count++; 21 | } 22 | } 23 | if(max < count) 24 | max = count; 25 | } 26 | printf("%d\n", max); 27 | } 28 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/oclong/5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | for(int i = 0; i < t; i++) { 7 | int n; 8 | scanf("%d", &n); 9 | for(int j = 0; j < n; j++ { 10 | int b; 11 | char str[50]; 12 | scanf("%d", &b); 13 | scanf("%s", str); 14 | if(b != -1) { 15 | 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/oclong/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/oclong/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/oclunch/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | int n; 8 | std::cin>>n; 9 | int arr[n]; 10 | for(int j = 0; j < n; j++) { 11 | std::cin>>arr[j]; 12 | } 13 | std::sort(arr, arr + n); 14 | // for(int j = 0; j < n; j++) { 15 | // std::cout< 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | long long int n, k, count = 0; 8 | std::cin>>n>>k; 9 | long long int brr[n], crr[n]; 10 | long long int arr[n]; 11 | for(int j = 0; j < n; j++) { 12 | std::cin>>arr[j]; 13 | } 14 | for(int l = 0; l < n; l++){ 15 | int c = 0, c1 = 0; 16 | for(int j = 0; j < n; j++) { 17 | if(arr[l] > arr[j]){ 18 | c++; 19 | if(j > l) 20 | c1++; 21 | } 22 | } 23 | brr[l] = c; 24 | crr[l] = c1; 25 | } 26 | for(int j = 0; j < n; j++){ 27 | // std::cout< 2 | 3 | int main() { 4 | int t; 5 | std::cin>>t; 6 | for(int i = 0; i < t; i++) { 7 | long long int n, k; 8 | std::cin>>n>>k; 9 | long long int arr[n], brr[n], crr[n], drr[n]; 10 | for(int j = 0; j < n; j++) { 11 | std::cin>>drr[j]; 12 | } 13 | for(int a = 0; a < k; a++) { 14 | long long int count = 0; 15 | long long int realK; 16 | std::cin>>realK; 17 | for(int j = 0; j < n; j++) { 18 | arr[j] = drr[j] ^ realK; 19 | } 20 | // for(int j = 0; j < n; j++) { 21 | // std::cout< arr[j]){ 26 | count++; 27 | } 28 | } 29 | } 30 | std::cout< 2 | 3 | int count(int num, int a, int b, int tot[]) { 4 | if( num < 0 || a > b ) return 0; 5 | else if( num == 0) return 1; 6 | else{ 7 | return count(num, a + 1, b, tot) + count(num - a, a, b, tot); 8 | } 9 | } 10 | 11 | 12 | int main() { 13 | int n, a, b, c, d; 14 | int t[b - a]; 15 | scanf("%d", &n); 16 | scanf("%d", &a); 17 | scanf("%d", &b); 18 | scanf("%d", &c); 19 | scanf("%d", &d); 20 | 21 | printf("%d\n", count(n,a,b, t)); 22 | 23 | return 0; 24 | 25 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/philips/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/philips/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/sepcook/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | int arr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; 6 | scanf("%d", &t); 7 | for(int i = 0; i < t; i++) { 8 | int a; 9 | int b; 10 | scanf("%d %d", &a, &b); 11 | long int ans = a + b; 12 | long long int count = 0; 13 | for(long int i = ans; ans != 0; ans /= 10){ 14 | count += arr[ans % 10]; 15 | // printf("%lld\n", count); 16 | } 17 | printf("%lld\n", count); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/sepcook/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | for(int i = 0; i < t; i++) { 7 | long int a, b, c; 8 | scanf("%ld %ld %ld", &a, &b, &c); 9 | long int ans = a + 2 * b + 3 * c; 10 | // printf("%ld\n", ans); 11 | if(ans % 2) 12 | printf("%s\n", "NO"); 13 | else { 14 | ans /= 2; 15 | if(ans <= a + 2 * b) 16 | printf("%s\n", "YES"); 17 | else 18 | { 19 | 20 | } 21 | 22 | } 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/sepcook/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/sepcook/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/seplong/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MOD 1000000007 4 | 5 | int main() { 6 | int t; 7 | scanf("%d", &t); 8 | for(int i = 0; i < t; i++){ 9 | long long int A, B, C; 10 | long long int count = 0; 11 | scanf("%lld", &A); 12 | scanf("%lld", &B); 13 | scanf("%lld", &C); 14 | 15 | for(long long int a = 2; a <= A; a++){ 16 | if(a > B * B + 1) { 17 | count = ( count + ((A - a + 1) * (C - 1) * B) % MOD) % MOD; 18 | break; 19 | } 20 | for(long long int c = 2; c <= C; c++) 21 | { 22 | if(c > B * B + 1 && a > 1) { 23 | count = ( count + ((C - c + 1) * B) % MOD) % MOD; 24 | break; 25 | } 26 | if((a - 1) * (c -1) <= B * B) 27 | if((int)sqrt((a - 1) * (c - 1)) == sqrt((a - 1) * (c - 1)) && (int)sqrt((a - 1) * (c - 1)) > 0) 28 | count = ( count + (int)sqrt((a - 1) * (c - 1)) - 1) % MOD; 29 | else 30 | count = ( count + (int)sqrt((a - 1) * (c - 1))) % MOD; 31 | else 32 | count = (count + B) % MOD; 33 | // printf("%lld\n", count); 34 | } 35 | } 36 | 37 | 38 | 39 | printf("%lld\n", count); 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/seplong/5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | 7 | for(int i = 0; i < t; i++){ 8 | int n, m; 9 | scanf("%d", &n); 10 | scanf("%d", &m); 11 | 12 | if(m < n - 1 || m > n * (n + 1) / 2) 13 | printf("%d\n", -1); 14 | else { 15 | 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Miscellaneous/Codechef/seplong/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Codechef/seplong/a.out -------------------------------------------------------------------------------- /Miscellaneous/Codechef/seplong/hello.c: -------------------------------------------------------------------------------- 1 | int arr[51][51]; 2 | for (int i = 0; i < 51; ++i) arr[i][0] = 1; 3 | for (int i = 0; i < 51; ++i) arr[i][1] = i; 4 | for (int i = 1; i < 51; ++i) 5 | for (int j = 1; j < 51; ++J) 6 | arr[i][j] = arr[i-1][j-1] + arr[i-1][j]; 7 | -------------------------------------------------------------------------------- /Miscellaneous/Dynamic Programming/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Dynamic Programming/a.out -------------------------------------------------------------------------------- /Miscellaneous/Dynamic Programming/decodeways2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | return 0; 6 | } -------------------------------------------------------------------------------- /Miscellaneous/Dynamic Programming/wildcard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, m; 5 | std::cout<<"Enter length of string : "; 6 | std::cin>>n; 7 | 8 | char str[n + 1]; 9 | std::cout<<"Enter string : "; 10 | std::cin>>str; 11 | 12 | std::cout<<"Enter length of pattern : "; 13 | std::cin>>m; 14 | 15 | char pat[m + 1]; 16 | std::cout<<"Enter pattern : "; 17 | std::cin>>pat; 18 | 19 | bool arr[n + 1][m + 1]; 20 | 21 | for(int i = 0; i <= n; i++) 22 | for(int j = 0; j <=m; j++) 23 | arr[i][j] = false; 24 | 25 | arr[0][0] = true; 26 | 27 | for(int i = 1; i <= m; i++) 28 | if(pat[i - 1] == '*') 29 | arr[0][i] = arr[0][i - 1]; 30 | 31 | for(int i = 0; i <= n; i++) { 32 | for(int j = 0; j <=m; j++) { 33 | if(pat[j - 1] == '*') 34 | arr[i][j] = arr[i - 1][j] || arr[i][j - 1]; 35 | else if(pat[j - 1] == '?' || pat[j - 1] == str[i - 1]) 36 | arr[i][j] = arr[i - 1][j - 1]; 37 | else arr[i][j] = false; 38 | } 39 | } 40 | 41 | if(arr[n][m]) 42 | std::cout<<"TRUE"< 2 | 3 | std::vector > diagonal(std::vector > &A) { 4 | std::vector> ans; 5 | int r = 0, c = 0; 6 | for(int i = 0; i < 2 * A.size() - 1; i++) { 7 | int l = r, m = c; 8 | if(c < A.size() - 1) 9 | c++; 10 | else 11 | r++; 12 | std::vector a1; 13 | while(l < A.size() && m >= 0) { 14 | a1.push_back(A[l][m]); 15 | l++; 16 | m--; 17 | } 18 | ans.push_back(a1); 19 | } 20 | return ans; 21 | } 22 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/FindDuplicateInArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int repeatedNumber(const std::vector &A) { 4 | int sq = sqrt(A.size()); 5 | std::vector arr(A.size() / sq + 1, 0); 6 | for(int i = 0; i < A.size(); i++) { 7 | arr[(A[i] - 1) / sq]++; 8 | } 9 | int selected = A.size() / sq; 10 | for(int i = 0; i < arr.size() - 1; i++) { 11 | if(arr[i] > sq) 12 | selected = i; 13 | } 14 | std::unordered_map count; 15 | int maxRange = (selected + 1) * sq; 16 | int minRange = selected * sq; 17 | for(int i = 0; i < A.size(); i++) { 18 | if(A[i] <= maxRange && A[i] > minRange) { 19 | count[A[i]]++; 20 | if(count[A[i]] > 1) 21 | return A[i]; 22 | } 23 | } 24 | return -1; 25 | } 26 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/FindPermutaion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector findPerm(const std::string A, int B) { 4 | std::vector ans; 5 | for(int i = 0; i < B; i++) 6 | ans.push_back(i + 1); 7 | std::stack s; 8 | int k = 0; 9 | for(int i = 0; i < B - 1; i++) { 10 | if(A[i] == 'I') { 11 | s.push(i + 1); 12 | while(!s.empty()) { 13 | ans[k++] = s.top(); 14 | s.pop(); 15 | } 16 | } 17 | else 18 | s.push(i + 1); 19 | } 20 | s.push(B); 21 | while(!s.empty()) { 22 | ans[k++] = s.top(); 23 | s.pop(); 24 | } 25 | return ans; 26 | } 27 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/FirstMissingInteger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int shift(std::vector& A) { 4 | int j = 0; 5 | for(int i = 0; i < A.size(); i++) { 6 | if(A[i] > 0) { 7 | int temp = A[i]; 8 | A[i] = A[j]; 9 | A[j++] = temp; 10 | } 11 | } 12 | return j; 13 | } 14 | 15 | int firstMissingPositive(std::vector &A) { 16 | int pos_size = shift(A); 17 | for(int i = 0; i < pos_size; i++) { 18 | if(abs(A[i]) - 1 < pos_size && A[abs(A[i]) - 1] > 0) 19 | A[abs(A[i]) - 1] = -A[abs(A[i]) - 1]; 20 | } 21 | for(int i = 0; i < pos_size; i++) 22 | if(A[i] > 0) 23 | return i + 1; 24 | return pos_size + 1; 25 | } 26 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/Flip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector flip(std::string A) { 4 | int n = A.length(); 5 | std::vector B; 6 | int count = 0; 7 | for(int i = 0; i < n; i++) { 8 | if(A[i] == '1') 9 | B.push_back(-1); 10 | else { 11 | B.push_back(1); 12 | count++; 13 | } 14 | } 15 | std::vector ans; 16 | if(count == 0) 17 | return ans; 18 | int sum = 0, maxSum = 0; 19 | int currL = 0; 20 | int maxL = -1, maxR = -1; 21 | for(int i = 0; i < n; i++) { 22 | sum += B[i]; 23 | if(sum < 0){ 24 | sum = 0; 25 | currL = i + 1; 26 | } 27 | if(maxSum < sum) { 28 | maxSum = sum; 29 | maxL = currL; 30 | maxR = i; 31 | } 32 | } 33 | ans.push_back(++maxL); 34 | ans.push_back(++maxR); 35 | return ans; 36 | } 37 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/HotelBookingsPossible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool hotel(std::vector &arrive, std::vector &depart, int K) { 4 | std::vector> arr; 5 | for(int i = 0; i < arrive.size(); i++) { 6 | arr.push_back(std::make_pair(arrive[i], 0)); 7 | arr.push_back(std::make_pair(depart[i], 1)); 8 | } 9 | std::sort(arr.begin(), arr.end()); 10 | int count = 0; 11 | for(int i = 0; i < arr.size(); i++) { 12 | while(i < arr.size() - 1 && arr[i].first == arr[i + 1].first) { 13 | if(arr[i++].second == 0) 14 | count++; 15 | else count--; 16 | } 17 | if(arr[i].second == 0) 18 | count++; 19 | else count--; 20 | if(count > K) 21 | return false; 22 | } 23 | return true; 24 | } 25 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/LargestNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int comparator(int a, int b) { 4 | std::string s1 = std::to_string(a); 5 | std::string s2 = std::to_string(b); 6 | 7 | std::string x = s1 + s2; 8 | std::string y = s2 + s1; 9 | 10 | if(x.compare(y) > 0) 11 | return 1; 12 | return 0; 13 | } 14 | 15 | std::string largestNumber(const std::vector &A) { 16 | std::string ans; 17 | std::vector B = A; 18 | std::sort(B.begin(), B.end(), comparator); 19 | for(int i = 0; i < B.size(); i++) { 20 | if(B[i] == 0) 21 | continue; 22 | ans += std::to_string(B[i]); 23 | } 24 | if(ans.compare("") == 0) 25 | ans += "0"; 26 | return ans; 27 | } 28 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/MaxConsecutiveGap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int maximumGap(const std::vector &A) { 4 | if(A.size() < 2) 5 | return 0; 6 | int max = A[0], min = A[0]; 7 | for(int i = 1; i < A.size(); i++) { 8 | if(max < A[i]) 9 | max = A[i]; 10 | if(min > A[i]) 11 | min = A[i]; 12 | } 13 | std::vector maxB(A.size(), INT_MIN); 14 | std::vector minB(A.size(), INT_MAX); 15 | 16 | double range = (double)(max - min) / (double)(A.size() - 1); 17 | for(int i = 0; i < A.size(); i++) { 18 | if(A[i] == min || A[i] == max) 19 | continue; 20 | int index = (int)floor((double)(A[i] - min) / range); 21 | if(maxB[index] == INT_MIN) 22 | maxB[index] = A[i]; 23 | else if(maxB[index] < A[i]) 24 | maxB[index] = A[i]; 25 | if(minB[index] == INT_MAX) 26 | minB[index] = A[i]; 27 | else if(minB[index] > A[i]) 28 | minB[index] = A[i]; 29 | } 30 | int prev = min; 31 | int maxgap = 0; 32 | for(int i = 0; i < A.size() - 1; i++) { 33 | if(minB[i] == INT_MAX) 34 | continue; 35 | if(maxgap < minB[i] - prev) 36 | maxgap = minB[i] - prev; 37 | prev = maxB[i]; 38 | } 39 | if(maxgap < max - prev) 40 | maxgap = max - prev; 41 | return maxgap; 42 | } 43 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/MaxDistance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int max(int a, int b) { 4 | if(a > b) 5 | return a; 6 | return b; 7 | } 8 | 9 | int min(int a, int b) { 10 | if(a < b) 11 | return a; 12 | return b; 13 | } 14 | 15 | int maximumGap(const std::vector &A) { 16 | int n = A.size(); 17 | if(n == 0) 18 | return -1; 19 | int maxCount = -1; 20 | std::vector LMin(n, 0); 21 | std::vector RMax(n, 0); 22 | LMin[0] = A[0]; 23 | RMax[n - 1] = A[n - 1]; 24 | for(int i = 1; i < n; i++) 25 | LMin[i] = min(LMin[i - 1], A[i]); 26 | for(int i = n - 2; i > -1; i--) { 27 | RMax[i] = max(RMax[i + 1], A[i]); 28 | //cout< 2 | 3 | std::vector maxset(std::vector &A) { 4 | std::vector ans; 5 | bool found = false; 6 | long int sum = 0, maxSum = 0; 7 | int currStart = 0, maxStart = 0, end = 0; 8 | for(int i = 0; i < A.size(); i++) { 9 | if(A[i] >= 0) 10 | found = true; 11 | if(A[i] < 0) { 12 | sum = 0; 13 | currStart = i + 1; 14 | continue; 15 | } 16 | sum += A[i]; 17 | if(sum == maxSum && end - maxStart < i - currStart) { 18 | maxStart = currStart; 19 | end = i; 20 | } 21 | if(sum > maxSum) { 22 | maxStart = currStart; 23 | maxSum = sum; 24 | end = i; 25 | } 26 | } 27 | if(!found) 28 | return ans; 29 | for(int i = maxStart; i <= end; i++) 30 | ans.push_back(A[i]); 31 | return ans; 32 | } 33 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/MaximumAbsoluteDifference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int maxArr(std::vector &A) { 4 | int max1 = A[0] + 1, max2 = A[0] - 1; 5 | int min1 = A[0] + 1, min2 = A[0] - 1; 6 | for(int i = 0; i < A.size(); i++) { 7 | int a = A[i] + i + 1; 8 | int b = A[i] - i - 1; 9 | if(max1 < a) 10 | max1 = a; 11 | if(min1 > a) 12 | min1 = a; 13 | if(max2 < b) 14 | max2 = b; 15 | if(min2 > b) 16 | min2 = b; 17 | } 18 | if(max1 - min1 > max2 - min2) 19 | return max1 - min1; 20 | return max2 - min2; 21 | } 22 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/MinStepsInfiniteGrid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int mod(int x) { 4 | if (x < 0) 5 | return -x; 6 | return x; 7 | } 8 | 9 | int coverPoints(std::vector &A, std::vector &B) { 10 | int n = A.size(); 11 | int count = 0; 12 | for(int i = 0; i < n - 1; i++) { 13 | int xDiff = mod(A[i + 1] - A[i]); 14 | int yDiff = mod(B[i + 1] - B[i]); 15 | if(xDiff < yDiff) 16 | count += yDiff; 17 | else 18 | count += xDiff; 19 | } 20 | return count; 21 | } 22 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/NextPermutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void reverse(std::vector &A, int start) { 4 | for(int i = start; i < (A.size() + start) / 2; i++) { 5 | int temp = A[i]; 6 | A[i] = A[A.size() - i + start - 1]; 7 | A[A.size() - i + start - 1] = temp; 8 | } 9 | } 10 | 11 | std::vector nextPermutation(std::vector &A) { 12 | int sel = -1; 13 | for(int i = A.size() - 1; i > 0; i--) { 14 | if(A[i - 1] < A[i]) { 15 | sel = i - 1; 16 | break; 17 | } 18 | } 19 | if(sel == -1) { 20 | reverse(A, 0); 21 | return A; 22 | } 23 | int index = -1; 24 | int lo = sel + 1, hi = A.size() - 1; 25 | while(lo <= hi) { 26 | int mid = (lo + hi) / 2; 27 | if(A[mid] <= A[sel]) 28 | hi = mid - 1; 29 | else { 30 | lo = mid + 1; 31 | if(index == -1 || A[index] >= A[mid]) 32 | index = mid; 33 | } 34 | } 35 | int temp = A[index]; 36 | A[index] = A[sel]; 37 | A[sel] = temp; 38 | 39 | reverse(A, sel + 1); 40 | return A; 41 | } 42 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/NobleInteger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int solve(std::vector &A) { 4 | int n = A.size(); 5 | std::sort(A.begin(), A.end()); 6 | for(int i = 0; i < n - 1; i++) { 7 | if(A[i] != A[i + 1]) { 8 | if(A[i] == n - i - 1) 9 | return 1; 10 | } 11 | } 12 | if(A[n - 1] == 0) 13 | return 1; 14 | else 15 | return -1; 16 | } 17 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/PascalTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector > solve(int A) { 4 | std::vector> ans; 5 | for(int i = 0; i < A; i++) { 6 | std::vector a1; 7 | for(int j = 0; j <= i; j++) { 8 | if(j == 0 || j == i) { 9 | a1.push_back(1); 10 | } 11 | else 12 | a1.push_back(ans[i - 1][j] + ans[i - 1][j - 1]); 13 | } 14 | ans.push_back(a1); 15 | } 16 | return ans; 17 | } 18 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/RepeatAndMissingNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector repeatedNumber(const std::vector &A) { 4 | long int n = A.size(); 5 | long long int sum = (long) n * (n + 1); 6 | long long int squareSum = n * (n + 1) * (2 * n + 1); 7 | squareSum /= 6; 8 | sum /= 2; 9 | std::vector ans; 10 | for(int i = 0; i < n; i++) { 11 | squareSum -= (long long int) A[i] * (long long int) A[i]; 12 | sum -= (long long int) A[i]; 13 | } 14 | long long int eq1 = squareSum / sum; 15 | ans.push_back((eq1 - sum) / 2); 16 | ans.push_back((eq1 + sum) / 2); 17 | return ans; 18 | } 19 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/RotateImage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void rotate(std::vector > &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | for(int i = 0; i < A.size() / 2; i++) { 10 | for(int j = i; j < A.size() - i - 1; j++) { 11 | int temp = A[i][j]; 12 | A[i][j] = A[A.size() - 1 - j][i]; 13 | 14 | A[A.size() - j - 1][i] = A[A.size() - 1 - i][A.size() - j - 1]; 15 | 16 | A[A.size() - 1 - i][A.size() - j - 1] = A[j][A.size() - i - 1]; 17 | 18 | A[j][A.size() - i - 1] = temp; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/SetZeroes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setZeroes(std::vector > &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | int n = A.size(); 9 | int m = A[0].size(); 10 | std::map map; 11 | for(int i = 0; i < n; i++) { 12 | bool found = 0; 13 | for(int j = 0; j < m; j++) { 14 | if(A[i][j] == 0) { 15 | map[j] = 1; 16 | found = 1; 17 | } 18 | } 19 | for(int j = 0; found && j < m; j++) 20 | A[i][j] = 0; 21 | } 22 | for(int i = 0; i < m; i++) { 23 | if(map[i] == 1) { 24 | for(int j = 0; j < n; j++) { 25 | A[j][i] = 0; 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/Spiral.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector > generateMatrix(int A) { 4 | std::vector> spiral; 5 | for(int i = 1; i <= A; i++) { 6 | std::vector a; 7 | for(int j = 0; j < A; j++) 8 | a.push_back(0); 9 | spiral.push_back(a); 10 | } 11 | int r = 0, c = 0; 12 | int a = 1; 13 | int n = A, m = A; 14 | while(r < n && c < m) { 15 | for(int i = c; i < n; i++) 16 | spiral[r][i] = a++; 17 | r++; 18 | for(int i = r; i < m; i++) 19 | spiral[i][n - 1] = a++; 20 | n--; 21 | if(r < m) { 22 | for(int i = n - 1; i >= c; i--) 23 | spiral[m - 1][i] = a++; 24 | m--; 25 | } 26 | if(c < n) { 27 | for(int i = m - 1; i >= r; i--) 28 | spiral[i][c] = a++; 29 | c++; 30 | } 31 | } 32 | return spiral; 33 | } 34 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/TripletsWithGivenSumRange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int solve(std::vector &A) { 4 | std::vector a1; 5 | for(int i = 0; i < A.size(); i++) 6 | a1.push_back(stof(A[i])); 7 | float a = a1[0], b = a1[1], c = a1[2]; 8 | for(int i = 3; i < A.size(); i++) { 9 | if(a + b + c > 1 && a + b + c < 2) 10 | return 1; 11 | else { 12 | if(a + b + c > 2) { 13 | if(a > b && a > c) 14 | a = a1[i]; 15 | else if(b > c) 16 | b = a1[i]; 17 | else 18 | c = a1[i]; 19 | } 20 | else { 21 | if(a < b && a < c) 22 | a = a1[i]; 23 | else if(b < c) 24 | b = a1[i]; 25 | else 26 | c = a1[i]; 27 | } 28 | } 29 | } 30 | if(a + b + c < 2 && a + b + c > 1) 31 | return 1; 32 | else 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/WaveArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector wave(std::vector &A) { 4 | std::sort(A.begin(), A.end()); 5 | for(int i = 0; i < A.size() - 1; i += 2) { 6 | int temp = A[i]; 7 | A[i] = A[i + 1]; 8 | A[i + 1] = temp; 9 | } 10 | return A; 11 | } 12 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/kthRowPascalTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector getRow(int A) { 4 | std::vector ans; 5 | int k = 1; 6 | for(int i = 1; i <= A + 1; i++) { 7 | ans.push_back(k); 8 | k = (k * (A + 1 - i)) / i; 9 | } 10 | return ans; 11 | } 12 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Array/nby3RepeatNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int repeatedNumber(const std::vector &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | int n1 = INT_MAX, n2 = INT_MAX; 10 | int count1 = 0, count2 = 0; 11 | for(int i = 0; i < A.size(); i++) { 12 | if(A[i] == n1) { 13 | count1++; 14 | continue; 15 | } 16 | if(A[i] == n2) { 17 | count2++; 18 | continue; 19 | } 20 | if(count1 == 0) { 21 | n1 = A[i]; 22 | count1++; 23 | continue; 24 | } 25 | if(count2 == 0) { 26 | n2 = A[i]; 27 | count2++; 28 | continue; 29 | } 30 | else { 31 | count1--; 32 | count2--; 33 | } 34 | } 35 | count1 = 0, count2 = 0; 36 | int n = A.size(); 37 | for(int i = 0; i < A.size(); i++) { 38 | if(A[i] == n1) 39 | count1++; 40 | if(A[i] == n2) 41 | count2++; 42 | if(count1 > n / 3) 43 | return n1; 44 | 45 | if(count2 > n / 3) 46 | return n2; 47 | } 48 | return -1; 49 | } 50 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/BinarySearch/PowerFunction.cpp: -------------------------------------------------------------------------------- 1 | int Solution::pow(int x, int n, int d) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | if (n == 0) 7 | return 1 % d; 8 | 9 | long long ans = 1, base = x; 10 | while (n > 0) { 11 | if (n % 2 == 1) { 12 | ans = (ans * base) % d; 13 | n--; 14 | } 15 | else { 16 | base = (base * base) % d; 17 | n /= 2; 18 | } 19 | } 20 | if (ans < 0) 21 | ans = (ans + d) % d; 22 | return ans; 23 | } 24 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/BinarySearch/SquareRoot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int sqrt(int A) { 4 | int lo = 1; 5 | int hi = A; 6 | while(lo <= hi) { 7 | long int mid = ((long)lo + (long)hi) / 2; 8 | if(mid * mid == A) 9 | return mid; 10 | else if(mid * mid > A) 11 | hi = mid - 1; 12 | else 13 | lo = mid + 1; 14 | } 15 | return lo - 1; 16 | } 17 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/CountGridPath.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int uniquePaths(int A, int B) { 4 | std::vector v(B, 0); 5 | std::vector> dp(A, v); 6 | for(int i = 0; i < A; i++) 7 | dp[i][0] = 1; 8 | for(int i = 0; i < B; i++) 9 | dp[0][i] = 1; 10 | for(int i = 1; i < A; i++) 11 | for(int j = 1; j < B; j++) 12 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 13 | 14 | return dp[A - 1][B - 1]; 15 | } 16 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/ExcelColumnNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int titleToNumber(std::string A) { 4 | int ans = 0; 5 | for(int i = 0; i < A.length(); i++) { 6 | ans = ans * 26 + (A[i] - 'A' + 1); 7 | } 8 | return ans; 9 | } 10 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/ExcelColumnTitle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::string convertToTitle(int A) { 4 | std::string s; 5 | while(A > 0) { 6 | int r2 = A % 26; 7 | char c; 8 | if(r2 == 0) { 9 | c = 'Z'; 10 | A = A / 26 - 1; 11 | } 12 | else { 13 | c = 'A' + r2 - 1; 14 | A = A / 26; 15 | } 16 | std::string s1(1, c); 17 | s = s1 + s; 18 | } 19 | return s; 20 | } 21 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/FizzBuzz.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::vector fizzBuzz(int A) { 4 | std::vector ans; 5 | for(int i = 1; i <= A; i++) { 6 | if(i % 3 == 0 && i % 5 == 0) 7 | ans.push_back("FizzBuzz"); 8 | else if(i % 3 == 0) 9 | ans.push_back("Fizz"); 10 | else if(i % 5 == 0) 11 | ans.push_back("Buzz"); 12 | else 13 | ans.push_back(std::to_string(i)); 14 | } 15 | return ans; 16 | } 17 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/LargestCoprimeDivisor.cpp: -------------------------------------------------------------------------------- 1 | int gcd(int a, int b) { 2 | if(a == 0 || b == 0) 3 | return 0; 4 | if(a == b) 5 | return a; 6 | if(a > b) 7 | return gcd(a - b, b); 8 | return gcd(a, b - a); 9 | } 10 | 11 | int cpFact(int A, int B) { 12 | int g = gcd(A, B); 13 | while(g != 1) { 14 | A /= g; 15 | g = gcd(A, B); 16 | } 17 | return A; 18 | } 19 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/Palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int isPalindrome(int A) { 4 | if(A < 0) 5 | return 0; 6 | int rev = 0; 7 | int n = A; 8 | while(n != 0) { 9 | rev = rev * 10 + n % 10; 10 | n /= 10; 11 | } 12 | return rev == A; 13 | } 14 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/PowerTwoIntegers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int isPower(int A) { 4 | std::unordered_map map; 5 | std::vector keys; 6 | int n = A; 7 | for(int i = 2; i <= n && A != 1; i++) { 8 | if(A % i == 0) 9 | keys.push_back(i); 10 | while(A % i == 0) { 11 | map[i]++; 12 | A /= i; 13 | } 14 | } 15 | int min = INT_MAX; 16 | for(int i = 0; i < keys.size(); i++) { 17 | if(map[keys[i]] < min) 18 | min = map[keys[i]]; 19 | } 20 | if(min > 1) 21 | return true; 22 | else 23 | return false; 24 | } 25 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/PrimeSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void isPrime(std::vector &A) { 4 | int n = A.size(); 5 | A[0] = false, A[1] = false; 6 | 7 | for(int i = 2; i * i <= n; i++) { 8 | if(A[i] == true) { 9 | for(int j = i * i; j <=n; j += i) 10 | A[j] = false; 11 | } 12 | } 13 | } 14 | 15 | std::vector primesum(int A) { 16 | std::vector B(A + 1, true); 17 | std::vector ans; 18 | isPrime(B); 19 | for(int i = 2; i <= A; i++) { 20 | if(B[i] && B[A - i]) { 21 | ans.push_back(i); 22 | ans.push_back(A - i); 23 | return ans; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/RearrangeArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void arrange(std::vector &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | int n = A.size(); 10 | for(int i = 0; i < n; i++) { 11 | A[i] = A[i] + n * (A[A[i]] % n); 12 | } 13 | for(int i = 0; i < n; i++) { 14 | A[i] /= n; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/SumOfHammingDistance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int hammingDistance(const std::vector &A) { 4 | int count = 0; 5 | for(int i = 0; i < 31; i++) { 6 | int n_one = 0; 7 | for(int j = 0; j < A.size(); j++) { 8 | if(A[j] & (1 << i)) 9 | n_one++; 10 | } 11 | count = count % 1000000007 + (2 * (n_one) * (A.size() - n_one)) % 1000000007; 12 | count = count % 1000000007; 13 | } 14 | return count; 15 | } 16 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/TraillingZeroesFactorial.cpp: -------------------------------------------------------------------------------- 1 | int trailingZeroes(int A) { 2 | int nfive = 0; 3 | for(int i = 5; i <= A; i++) { 4 | int a = i; 5 | while(a != 0 && a % 5 == 0) { 6 | nfive++; 7 | a /= 5; 8 | } 9 | } 10 | return nfive; 11 | } 12 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/gcd.cpp: -------------------------------------------------------------------------------- 1 | int gcd(int A, int B) { 2 | if(A == 0 ) 3 | return B; 4 | else if(B == 0) 5 | return A; 6 | int dd, ds; 7 | if(A > B) { 8 | dd = A; 9 | ds = B; 10 | } 11 | else { 12 | dd = B; 13 | ds = A; 14 | } 15 | int rem = dd % ds; 16 | while(rem != 0) { 17 | dd = ds; 18 | ds = rem; 19 | rem = dd % ds; 20 | } 21 | return ds; 22 | } 23 | -------------------------------------------------------------------------------- /Miscellaneous/InterviewBit/Maths/reverse.cpp: -------------------------------------------------------------------------------- 1 | int Solution::reverse(int A) { 2 | bool pos = true; 3 | if(A < 0) { 4 | pos = false; 5 | A = -A; 6 | } 7 | long int rev = 0; 8 | while(A != 0) { 9 | if(rev > INT_MAX) 10 | return 0; 11 | int rem = A % 10; 12 | rev = rev * 10 + (long)rem; 13 | A /= 10; 14 | } 15 | if(rev > INT_MAX) 16 | return 0; 17 | if(!pos) 18 | return (int)(-rev); 19 | else 20 | return (int)rev; 21 | } 22 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/.compare.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Assignment/.compare.h.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/.compare_at.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Assignment/.compare_at.c.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/.job.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Assignment/.job.h.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/.main.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Assignment/.main.c.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/.storage.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Assignment/.storage.h.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/at: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Assignment/at -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/compare.h: -------------------------------------------------------------------------------- 1 | /***********file: compare.h *********/ 2 | typedef enum {LESSER , GREATER , EQUAL} ORDER; 3 | extern ORDER compare (Job j1,Job j2); 4 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/compare_at.c: -------------------------------------------------------------------------------- 1 | /***********file: compare_at.c *********/ 2 | #include "storage.h" 3 | #include "compare.h" 4 | 5 | ORDER compare (Job j1 , Job j2) 6 | { 7 | if (j1.at < j2.at) //2M 8 | return LESSER; 9 | else if (j1.at > j2.at) //2M 10 | return GREATER; 11 | else if (j1.at == j2.at) //2M 12 | return EQUAL; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/compare_at.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Assignment/compare_at.o -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/compare_et.c: -------------------------------------------------------------------------------- 1 | /***********file: compare_at.c *********/ 2 | 3 | #include "storage.h" 4 | #include "compare.h" 5 | 6 | ORDER compare (Job j1 , Job j2) 7 | { 8 | if (j1.et < j2.et) //2M 9 | return LESSER; 10 | else if (j1.et > j2.et) //2M 11 | return GREATER; 12 | else if (j1.et == j2.et) //2M 13 | return EQUAL; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/et: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Assignment/et -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/job.h: -------------------------------------------------------------------------------- 1 | /***********file: job.h *********/ 2 | #define MAX 10 3 | 4 | typedef enum {PRI_0, PRI_1, PRI_2} Priority; 5 | typedef int Job_Id; 6 | typedef int ExecutionTime; 7 | typedef int ArrivalTime; 8 | 9 | typedef struct 10 | { 11 | Job_Id id; 12 | Priority pri; 13 | ExecutionTime et; 14 | ArrivalTime at; 15 | } Job; 16 | 17 | typedef Job JobList[MAX]; 18 | 19 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/main.c: -------------------------------------------------------------------------------- 1 | /***********file: main.c *********/ 2 | #include 3 | #include "seqListOps.h" 4 | 5 | int main(void) 6 | { 7 | int i; int size; 8 | JobList list; 9 | size = inputJobs(list); 10 | printf("Size = %d\n", size); 11 | printf("The Jobs waiting on CPU are:\n"); 12 | printJobList(list, size); 13 | sortJobList(list, size); 14 | printf("The Jobs after sorting are:\n"); 15 | printJobList(list, size); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/seqListOps.h: -------------------------------------------------------------------------------- 1 | /***********file: seqListOps.h *********/ 2 | #include "storage.h" 3 | 4 | extern int inputJobs(JobList list); 5 | extern void sortJobList(JobList list, int size); 6 | extern void printJobList(JobList list, int size ); 7 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Assignment/storage.h: -------------------------------------------------------------------------------- 1 | /***********file: storage.h *********/ 2 | #include "job.h" 3 | 4 | typedef struct 5 | { 6 | Job ele; 7 | int next; 8 | } Location; 9 | 10 | typedef Location Store[MAX]; // MAX already defined in job.h 11 | 12 | typedef struct 13 | { 14 | int size; 15 | int head; 16 | } SeqList; 17 | 18 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/.compare.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/AssignmentFiles/.compare.h.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/.compare_at.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/AssignmentFiles/.compare_at.c.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/.job.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/AssignmentFiles/.job.h.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/.main.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/AssignmentFiles/.main.c.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/.storage.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/AssignmentFiles/.storage.h.swp -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/at: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/AssignmentFiles/at -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/compare.h: -------------------------------------------------------------------------------- 1 | /***********file: compare.h *********/ 2 | typedef enum {LESSER , GREATER , EQUAL} ORDER; 3 | extern ORDER compare (Job j1,Job j2); 4 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/compare_at.c: -------------------------------------------------------------------------------- 1 | /***********file: compare_at.c *********/ 2 | #include "storage.h" 3 | #include "compare.h" 4 | 5 | ORDER compare (Job j1 , Job j2) 6 | { 7 | if (j1.at < j2.at) //2M 8 | return LESSER; 9 | else if (j1.at > j2.at) //2M 10 | return GREATER; 11 | else if (j1.at == j2.at) //2M 12 | return EQUAL; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/compare_at.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/AssignmentFiles/compare_at.o -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/compare_et.c: -------------------------------------------------------------------------------- 1 | /***********file: compare_at.c *********/ 2 | 3 | #include "storage.h" 4 | #include "compare.h" 5 | 6 | ORDER compare (Job j1 , Job j2) 7 | { 8 | if (j1.et < j2.et) //2M 9 | return LESSER; 10 | else if (j1.et > j2.et) //2M 11 | return GREATER; 12 | else if (j1.et == j2.et) //2M 13 | return EQUAL; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/job.h: -------------------------------------------------------------------------------- 1 | /***********file: job.h *********/ 2 | #define MAX 10 3 | 4 | typedef enum {PRI_0, PRI_1, PRI_2} Priority; 5 | typedef int Job_Id; 6 | typedef int ExecutionTime; 7 | typedef int ArrivalTime; 8 | 9 | typedef struct 10 | { 11 | Job_Id id; 12 | Priority pri; 13 | ExecutionTime et; 14 | ArrivalTime at; 15 | } Job; 16 | 17 | typedef Job JobList[MAX]; 18 | 19 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/main.c: -------------------------------------------------------------------------------- 1 | /***********file: main.c *********/ 2 | #include 3 | #include "seqListOps.h" 4 | 5 | int main(void) 6 | { 7 | int i; int size; 8 | JobList list; 9 | size = inputJobs(list); 10 | printf("Size = %d\n", size); 11 | printf("The Jobs waiting on CPU are:\n"); 12 | printJobList(list, size); 13 | sortJobList(list, size); 14 | printf("The Jobs after sorting are:\n"); 15 | printJobList(list, size); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/seqListOps.h: -------------------------------------------------------------------------------- 1 | /***********file: seqListOps.h *********/ 2 | #include "storage.h" 3 | 4 | extern int inputJobs(JobList list); 5 | extern void sortJobList(JobList list, int size); 6 | extern void printJobList(JobList list, int size ); 7 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentFiles/storage.h: -------------------------------------------------------------------------------- 1 | /***********file: storage.h *********/ 2 | #include "job.h" 3 | 4 | typedef struct 5 | { 6 | Job ele; 7 | int next; 8 | } Location; 9 | 10 | typedef Location Store[MAX]; // MAX already defined in job.h 11 | 12 | typedef struct 13 | { 14 | int size; 15 | int head; 16 | } SeqList; 17 | 18 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentSolution/compare.h: -------------------------------------------------------------------------------- 1 | #include "storage.h" 2 | 3 | extern ORDER compare (Job j1,Job j2); 4 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentSolution/compare_at.c: -------------------------------------------------------------------------------- 1 | 2 | #include "storage.h" 3 | 4 | ORDER compare (Job j1 , Job j2) 5 | { 6 | if (j1.at < j2.at) return LESSER; 7 | else if (j1.at > j2.at) return GREATER; 8 | else if (j1.at == j2.at) return EQUAL; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentSolution/compare_et.c: -------------------------------------------------------------------------------- 1 | #include "storage.h" 2 | 3 | 4 | 5 | ORDER compare (Job j1 , Job j2) 6 | { 7 | if (j1.et < j2.et) return LESSER; 8 | else if (j1.et > j2.et) return GREATER; 9 | else if (j1.et == j2.et) return EQUAL; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentSolution/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "seqListOps.h" 3 | 4 | int main(void) 5 | { 6 | int i; 7 | seqarr seq; 8 | joblist arr; 9 | initialize_elements (arr); 10 | //printjoblist(arr); 11 | seq[0] = createlist(); 12 | seq[1] = createlist(); 13 | seq[2] = createlist(); 14 | insertelements (arr,seq); 15 | // printseqlist (seq[0]); 16 | // printseqlist (seq[1]); 17 | // printseqlist (seq[2]); 18 | copy_sorted_ele (seq , arr); 19 | //printf ("Sorted List\n"); 20 | printjoblist(arr); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentSolution/seqListOps.h: -------------------------------------------------------------------------------- 1 | #include "storage.h" 2 | 3 | extern seqlist createlist(); 4 | extern void initialize_elements (joblist arr); 5 | extern void insertelements (joblist arr,seqarr s); 6 | extern void printseqlist(seqlist); 7 | extern void copy_sorted_ele (seqarr , joblist); 8 | extern void printjoblist(joblist j); 9 | extern seqlist insert(Job j , seqlist sl); 10 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/AssignmentSolution/storage.h: -------------------------------------------------------------------------------- 1 | #define MAX 10 2 | #define MAX_SEQ_LIST 3 3 | 4 | typedef enum {PRI_0, PRI_1, PRI_2} PRIORITY; 5 | typedef enum {LESSER , GREATER , EQUAL} ORDER; 6 | typedef int JOB_ID; 7 | typedef int EXECUTION_TIME; 8 | typedef int ARRIVAL_TIME; 9 | 10 | typedef struct 11 | { 12 | JOB_ID id; 13 | PRIORITY pri; 14 | EXECUTION_TIME et; 15 | ARRIVAL_TIME at; 16 | } Job; 17 | 18 | typedef Job joblist[MAX]; 19 | 20 | typedef struct 21 | { 22 | Job ele; 23 | int next; 24 | } LOCATION; 25 | 26 | typedef LOCATION STORE[MAX]; 27 | 28 | typedef struct 29 | { 30 | int size; 31 | int head; 32 | } seqlist; 33 | 34 | typedef seqlist seqarr[MAX_SEQ_LIST]; 35 | 36 | STORE st; 37 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/LabSheet1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/LabSheet1.pdf -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Linking/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Linking/a.out -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Linking/add.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d", &a); 6 | scanf("%d", &b); 7 | printf("%d\n", a + b); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Linking/output: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Linking/output -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search.h" 3 | 4 | int main() { 5 | int a[] = {2, 4, 3, 1, 5, 9, 8, 10}; 6 | int ele = search(a, 8); 7 | printf("In function main , element = %d\n", ele); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/search.h: -------------------------------------------------------------------------------- 1 | extern int search(int arr[], int n); 2 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/search1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Search_max_min/search1 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/search2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Search_max_min/search2 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/search3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Search_max_min/search3 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/search_max.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | int search(int arr[], int n) { 4 | if( n > 0) { 5 | int max = arr[0]; 6 | for(int i = 0; i < n; i++) 7 | if(max < arr[i]) 8 | max = arr[i]; 9 | 10 | printf("In function max = %d\n", max); 11 | return max; 12 | } 13 | return -1; 14 | } 15 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/search_max.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Search_max_min/search_max.o -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/search_min.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | int search(int arr[], int n) { 4 | 5 | if(n > 0) { 6 | int min = arr[0]; 7 | for(int i = 0; i < n; i++) 8 | if(min > arr[i]) 9 | min = arr[i]; 10 | 11 | printf("In function min = %d\n", min); 12 | return min; 13 | } 14 | return -1; 15 | } 16 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab1/Search_max_min/search_min.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab1/Search_max_min/search_min.o -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/cmd_line_arg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/cmd_line_arg -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/cmd_line_arg.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]) { 4 | 5 | for(int i = 0; i < argc; i++) 6 | printf("%s\n", argv[i]); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/file: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/file -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/file.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num; 5 | FILE *fptr; 6 | fptr = fopen("program.txt","w"); 7 | if(fptr == NULL) { 8 | printf("Error!"); 9 | return 0; 10 | } 11 | printf("Enter num: "); 12 | scanf("%d",&num); 13 | fprintf(fptr,"%d",num); 14 | fclose(fptr); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/filecopy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/filecopy -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/filecopy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]) { 4 | 5 | if(argc < 3) { 6 | printf("Error provide two file names!!!\n"); 7 | return 0; 8 | } 9 | 10 | FILE* fread = fopen(argv[1], "r"); 11 | FILE* fwrite = fopen(argv[2], "w"); 12 | 13 | if(fread == NULL || fopen == NULL) { 14 | printf("Error opening file!!!\n"); 15 | return 0; 16 | } 17 | char str[30]; 18 | while(fgets(str, 30, fread) != NULL) 19 | fputs(str, fwrite); 20 | printf("File contents copied!!\n"); 21 | fclose(fread); 22 | fclose(fwrite); 23 | return 0; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/fileread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/fileread -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/fileread.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num; 5 | FILE *fptr; 6 | fptr = fopen ("program.txt", "r"); 7 | if (fptr == NULL){ 8 | printf("Error! opening file\n"); 9 | // Program exits if the file pointer returns NULL. 10 | return 0; 11 | } 12 | fscanf(fptr,"%d",&num); 13 | printf("Value of n=%d\n",num); 14 | fclose(fptr); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/lab2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/lab2.pdf -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/linked_list/linkedlist: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/linked_list/linkedlist -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/linked_list/linkedlist.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.c" 3 | 4 | // function declarations 5 | 6 | extern void insertFirst (struct linkedList * head, int ele); 7 | /* inserts a given element at the beginning of the list */ 8 | 9 | extern struct node * deleteFirst(struct linkedList * head); 10 | /* deletes the first element of the list and returns pointer to the deleted 11 | node. */ 12 | 13 | extern void printList (struct linkedList * head); 14 | /* prints all the elements in the list */ 15 | 16 | extern int search (struct linkedList * head, int ele); 17 | /* searches for a given element in the linked list. Returns 1 if found, 0 18 | otherwise. */ 19 | 20 | extern struct node * delete (struct linkedList * head, int ele); 21 | /* delets the first node in the list that contains the element = ele and 22 | If the element is not found it returns an error message saying 23 | found. */ 24 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/linked_list/node.c: -------------------------------------------------------------------------------- 1 | // structure definitions 2 | // structure of a linked list node. It contains an element 3 | typedef struct node { 4 | int element; 5 | struct node * next; 6 | } NODE; 7 | 8 | /* structure of a linked list / head. It stores the count of number of elements 9 | in the list and also a pointer link to the first node of the list. */ 10 | struct linkedList { 11 | int count; 12 | struct node * first; 13 | }; 14 | 15 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/linked_list/node.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/linked_list/node.o -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/linked_list/read.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 4 6 2 2 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/linked_list/write.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 4 6 2 2 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/program.txt: -------------------------------------------------------------------------------- 1 | 3 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/driver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stack.h" 4 | 5 | int main(int argc, char *argv[]) { 6 | 7 | if(argc < 2) { 8 | printf("Error provide file name!!\n"); 9 | return 0; 10 | } 11 | 12 | // Create a file pointer and open the file read as command line argument. 13 | FILE * fileptr = fopen(argv[1], "r"); 14 | if(fileptr == NULL) { 15 | printf("Error opening file!!\n"); 16 | return 0; 17 | } 18 | // Declare a pointer to a linked list (head) and allocate memory to it. 19 | struct linkedList * head = (struct linkedList *) malloc (sizeof(struct linkedList)); 20 | head -> first = NULL; 21 | head -> count = 0; 22 | 23 | int temp = 0; 24 | 25 | // In a loop read the file and insert elements into the linkedList. 26 | while ((fscanf(fileptr, "%d", &temp)) == 1) { 27 | 28 | // read the next element and store into the temp variable. 29 | 30 | // insert temp into the linked list. 31 | push(head, temp); 32 | printf("Element %d pushed in stack\n", temp); 33 | } 34 | 35 | // print the linked list. 36 | printf("\nElements in stack are - \n"); 37 | printList(head); 38 | 39 | // pop all elements of stack 40 | NODE* ptr = pop(head); 41 | while(ptr != NULL) { 42 | printf("Popped element = %d\n", ptr -> element); 43 | printList(head); 44 | ptr = pop(head); 45 | } 46 | 47 | // close the file pointer 48 | fclose(fileptr); 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/linkedlist.h: -------------------------------------------------------------------------------- 1 | #include "node.c" 2 | 3 | // function declarations 4 | 5 | extern void insertFirst (struct linkedList * head, int ele); 6 | /* inserts a given element at the beginning of the list */ 7 | 8 | extern struct node * deleteFirst(struct linkedList * head); 9 | /* deletes the first element of the list and returns pointer to the deleted 10 | node. */ 11 | 12 | extern void printList (struct linkedList * head); 13 | /* prints all the elements in the list */ 14 | 15 | extern int search (struct linkedList * head, int ele); 16 | /* searches for a given element in the linked list. Returns 1 if found, 0 17 | otherwise. */ 18 | 19 | extern struct node * delete (struct linkedList * head, int ele); 20 | /* delets the first node in the list that contains the element = ele and 21 | If the element is not found it returns an error message saying 22 | found. */ 23 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/node.c: -------------------------------------------------------------------------------- 1 | // structure definitions 2 | // structure of a linked list node. It contains an element 3 | typedef struct node { 4 | int element; 5 | struct node * next; 6 | } NODE; 7 | 8 | /* structure of a linked list / head. It stores the count of number of elements 9 | in the list and also a pointer link to the first node of the list. */ 10 | struct linkedList { 11 | int count; 12 | struct node * first; 13 | }; 14 | 15 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/node.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/stack/node.o -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/read.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 4 6 2 2 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/stack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/stack/stack -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/stack.c: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | 3 | void push(struct linkedList* head, int x) { 4 | insertFirst(head, x); 5 | } 6 | 7 | NODE* pop(struct linkedList* head) { 8 | return deleteFirst(head); 9 | } -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/stack.h: -------------------------------------------------------------------------------- 1 | #include "linkedlist.h" 2 | 3 | extern void push(struct linkedList* head, int x); 4 | 5 | extern NODE* pop(struct linkedList* head); -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/stackInput.txt: -------------------------------------------------------------------------------- 1 | 2 4 1 7 3 8 5 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/stack/write.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 4 6 2 2 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab2/test -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab2/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | printf("Hello World!\n"); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab3/gmon.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab3/gmon.out -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab3/input10.txt: -------------------------------------------------------------------------------- 1 | 1000000,6 2 | 1000001,6 3 | 1000002,1 4 | 1000003,10 5 | 1000004,3 6 | 1000005,4 7 | 1000006,3 8 | 1000007,8 9 | 1000008,9 10 | 1000009,2 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab3/lab3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab3/lab3.pdf -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab3/multiQ.c: -------------------------------------------------------------------------------- 1 | #include "multiQ.h" 2 | #define NUM 10 3 | 4 | MULTIQ createMQ(int num) { 5 | MULTIQ mq = (MULTIQ) malloc(num * sizeof(QUEUE)); 6 | for(int i = 0; i < num; i++) 7 | mq[i] = newQ(); 8 | 9 | return mq; 10 | } 11 | 12 | MULTIQ addMQ(MULTIQ mq, TASK t) { 13 | addQ(mq[t -> prior - 1], t); 14 | return mq; 15 | } 16 | 17 | TASK nextMQ(MULTIQ mq) { 18 | TASK t = NULL; 19 | int num = sizeof(mq) / sizeof(mq[0]) - 1; 20 | for(int i = NUM - 1; i >= 0; i--) { 21 | t = front(mq[i]); 22 | if(t != NULL) 23 | break; 24 | } 25 | return t; 26 | } 27 | 28 | TASK delNextMQ(MULTIQ mq) { 29 | TASK t = NULL; 30 | for(int i = NUM - 1; i >= 0; i--) { 31 | t = front(mq[i]); 32 | if(t != NULL){ 33 | delQ(mq[i]); 34 | break; 35 | } 36 | } 37 | return t; 38 | } 39 | 40 | int isEmptyMQ(MULTIQ mq) { 41 | for(int i = 0; i < NUM; i++) 42 | if(!isEmpty(mq[i])) 43 | return 0; 44 | return 1; 45 | } 46 | 47 | int sizeMQ(MULTIQ mq) { 48 | int size = 0; 49 | for(int i = 0; i < NUM; i++) 50 | size += lengthQ(mq[i]); 51 | return size; 52 | } 53 | 54 | int sizeMQbyPriority(MULTIQ mq, PRIORITY p) { 55 | return lengthQ(mq[p - 1]); 56 | } 57 | 58 | QUEUE getQueueFromMQ(MULTIQ mq, PRIORITY p) { 59 | return mq[p -1]; 60 | } 61 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab3/multiQ.h: -------------------------------------------------------------------------------- 1 | #include "que.h" 2 | 3 | extern MULTIQ createMQ(int num); 4 | 5 | extern MULTIQ addMQ(MULTIQ mq, TASK t); 6 | 7 | extern TASK nextMQ(MULTIQ mq); 8 | 9 | extern TASK delNextMQ(MULTIQ mq); 10 | 11 | extern int isEmptyMQ(MULTIQ mq); 12 | 13 | extern int sizeMQ(MULTIQ mq); 14 | 15 | extern int sizeMQbyPriority(MULTIQ mq, PRIORITY p); 16 | 17 | extern QUEUE getQueueFromMQ(MULTIQ, PRIORITY p); 18 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab3/node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //defining node and queue structure 6 | 7 | typedef int PRIORITY; 8 | 9 | struct task { 10 | int taskID; 11 | PRIORITY prior; 12 | }; 13 | 14 | typedef struct task* TASK; 15 | 16 | typedef TASK ELEMENT; 17 | 18 | typedef struct node { 19 | ELEMENT ele; 20 | struct node* next; 21 | } NODE; 22 | 23 | struct queue { 24 | NODE* head; 25 | NODE* tail; 26 | int count; 27 | }; 28 | 29 | 30 | typedef struct queue* QUEUE; 31 | 32 | 33 | typedef QUEUE* MULTIQ; 34 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab3/que.h: -------------------------------------------------------------------------------- 1 | #include "node.c" 2 | 3 | extern QUEUE newQ(); 4 | 5 | extern int isEmpty(QUEUE q); 6 | 7 | extern QUEUE delQ(QUEUE q); 8 | 9 | extern ELEMENT front(QUEUE q); 10 | 11 | extern QUEUE addQ(QUEUE q, ELEMENT e); 12 | 13 | extern int lengthQ(QUEUE q); 14 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab4/a.out -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/cycle.h: -------------------------------------------------------------------------------- 1 | #include "dynalloc.h" 2 | 3 | extern LinkedList createList(int n); 4 | 5 | extern LinkedList createCycle(LinkedList ls); 6 | 7 | extern int testCyclic(LinkedList ls); 8 | 9 | extern LinkedList makeCyclic(LinkedList ls, int ifCyclic); 10 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/cycle1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab4/cycle1 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/cycle1.c: -------------------------------------------------------------------------------- 1 | #include "cycle.h" 2 | 3 | int testCyclic(LinkedList ls) { 4 | if(ls -> count < 1) 5 | return 0; 6 | else if(ls -> count < 3 && ls -> tail -> next == ls -> head) 7 | return 1; 8 | else if (ls -> count < 3) 9 | return 0; 10 | 11 | NODE* hore; 12 | NODE* tort; 13 | hore = ls -> head -> next -> next; 14 | tort = ls -> head -> next; 15 | if(hore == tort) 16 | return 1; 17 | while(hore -> next != NULL && hore -> next -> next != NULL) { 18 | if(hore == tort || hore -> next == tort) 19 | return 1; 20 | hore = hore -> next -> next; 21 | tort = tort -> next; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/cycle2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab4/cycle2 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/cycle2.c: -------------------------------------------------------------------------------- 1 | #include "cycle.h" 2 | 3 | int testCyclic(LinkedList ls) { 4 | if(ls -> count < 1) 5 | return 0; 6 | 7 | NODE* prev = NULL; 8 | NODE* curr = ls -> head; 9 | NODE* next = curr -> next; 10 | 11 | if(curr == next) 12 | return 1; 13 | else { 14 | while(next != NULL) { 15 | if(next == ls -> head) 16 | return 1; 17 | 18 | curr -> next = prev; 19 | 20 | prev = curr; 21 | curr = next; 22 | next = next -> next; 23 | } 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/cycle3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab4/cycle3 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/cycle4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab4/cycle4 -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/dynalloc.c: -------------------------------------------------------------------------------- 1 | #include "dynalloc.h" 2 | 3 | unsigned long long int spacealloc = 0; 4 | 5 | void myfree(void* ptr, int size) { 6 | spacealloc -= size; 7 | // printf("Space allocated = %lld\n", spacealloc); 8 | free(ptr); 9 | } 10 | 11 | void* myalloc(int size) { 12 | spacealloc += size; 13 | //printf("Space allocated = %lld\n", spacealloc); 14 | return malloc(size); 15 | } 16 | 17 | void printUsedHeap() { 18 | printf("Size of used heap = %llu bytes\n", spacealloc); 19 | } 20 | 21 | /* 22 | int main() { 23 | int m = 0; 24 | srand(time(0)); 25 | for(int i = 0; i < 1; i++){ 26 | m = rand() % ( 15001 ) + 10000; 27 | int* arr = (int*) myalloc(m * sizeof(int)); 28 | printf("FIRST ADD -> %u LAST ADD -> %u\n", arr, &arr[m - 1]); 29 | //myfree(arr, sizeof(int) * m); 30 | } 31 | return 0; 32 | } 33 | */ 34 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/dynalloc.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef int ELEMENT; 7 | 8 | typedef struct node { 9 | ELEMENT ele; 10 | struct node* next; 11 | } NODE; 12 | 13 | struct linkedlist { 14 | int count; 15 | NODE* head; 16 | NODE* tail; 17 | }; 18 | 19 | typedef struct linkedlist* LinkedList; 20 | 21 | extern void myfree(void* ptr, int size); 22 | 23 | extern void* myalloc(int size); 24 | 25 | extern void printUsedHeap(); -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/gmon.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab4/gmon.out -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/lab4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab4/lab4.pdf -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab4/stackmem.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int bits; 4 | 5 | void p(int m); 6 | void d(); 7 | void g(); 8 | void h(); 9 | 10 | int main() { 11 | printf("MAIN -> %u\n", &bits); 12 | int a = 0; 13 | int hi = 10e2; 14 | int m; 15 | while(a < hi) { 16 | m = a + (hi - a) / 2; 17 | p(m); 18 | a = m + 1; 19 | } 20 | g(); 21 | h(); 22 | d(); 23 | 24 | return 0; 25 | } 26 | 27 | void p(int m) { 28 | if(!m) 29 | return; 30 | printf("PILANI %d -> %u\n", &m); 31 | p(m - 1); 32 | } 33 | 34 | void d() { 35 | int dub; 36 | printf("DUBAI -> %u\n", &dub); 37 | } 38 | 39 | void g() { 40 | int goa; 41 | printf("GOA -> %u\n", &goa); 42 | } 43 | 44 | void h() { 45 | int hyd; 46 | printf("HYDERABAD -> %u\n", &hyd); 47 | } 48 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab5/10: -------------------------------------------------------------------------------- 1 | "6416689589863898,DKOPE,02/2033,tkbxf,fdpax" 2 | "6248121580853183,VEIKL,09/2037,hibgh,mssra" 3 | "805446599973064,RDCXQ,10/2031,mvqmp,fatdl" 4 | "9552785475698098,VNDHM,10/2034,uyzec,exdjl" 5 | "2660814791251755,MTNYF,12/2022,qvcbd,dvvyf" 6 | "7159365242071851,YSETJ,09/2018,ygjqq,okrcy" 7 | "4404174139822423,JAEOC,09/2026,vnstn,wowlr" 8 | "4579684709597480,CGKPD,10/2038,ofrmk,xouto" 9 | "4214224217192200,MMHHU,03/2019,ruisq,kjzpy" 10 | "8748642289747866,DXBMX,07/2026,eukst,zuixl" 11 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab5/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab5/a.out -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab5/graph.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab5/graph.ods -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab5/lab5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab5/lab5.pdf -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab5/output.txt: -------------------------------------------------------------------------------- 1 | 10000, 9.000000ms 2 | 100000, 90.000000ms 3 | 1000000, 781.000000ms 4 | 10000000, 13654.000000ms 5 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab5/output_sort.txt: -------------------------------------------------------------------------------- 1 | 10, 0.000000ms, 476 2 | 100, 0.000000ms, 4796 3 | 1000, 3.000000ms, 47996 4 | 10000, 367.000000ms, 479996 5 | 100000, 35850.000000ms, 4799996 6 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab6/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab6/a.out -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab6/iterative_merge_sort.c: -------------------------------------------------------------------------------- 1 | #include "merge_sort.h" 2 | 3 | void mergeSort(Element Ls[], int n) { 4 | for(int i = 1; i < n; i *= 2) { 5 | for(int j = 0; j < n; j += 2 * i) { 6 | if(j + 2 * i > n) 7 | continue; 8 | int sz1 = i, sz2 = min(i, n - j); 9 | Element Ls1[sz1], Ls2[sz2], Ls3[sz1 + sz2]; 10 | for(int k = 0; k < i; k++) 11 | Ls1[k] = Ls[j + k]; 12 | for(int k = 0; k < sz2; k++) 13 | Ls2[k] = Ls[j + i + k]; 14 | 15 | merge(Ls1, sz1, Ls2, sz2, Ls3); 16 | 17 | for(int k = 0; k < sz1 + sz2; k++) 18 | Ls[j + k] = Ls3[k]; 19 | 20 | int end; 21 | endmem = &end; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab6/lab6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab6/lab6.pdf -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab6/makefile: -------------------------------------------------------------------------------- 1 | compRecMS : recursive_merge_sort.o merge_sort.o 2 | gcc merge_sort.o recursive_merge_sort.o -o recursive 3 | 4 | compIterMS : iterative_merge_sort.o merge_sort.o 5 | gcc merge_sort.o iterative_merge_sort.o -o iterative 6 | 7 | runRecMS : recursive 8 | ./recursive 9 | 10 | runIterMS : iterative 11 | ./iterative 12 | 13 | compare : iterative recursive 14 | ./iterative 15 | ./recursive 16 | 17 | recursive : compRecMS 18 | 19 | iterative : compIterMS 20 | 21 | merge_sort.o : merge_sort.c merge_sort.h 22 | gcc -c merge_sort.c 23 | 24 | recursive_merge_sort.o : recursive_merge_sort.o merge_sort.h 25 | gcc -c recursive_merge_sort.c 26 | 27 | iterative_merge_sort.o : iterative_merge_sort.o merge_sort.h 28 | gcc -c iterative_merge_sort.c 29 | 30 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab6/merge_sort.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct student { 6 | char name[20]; 7 | float cgpa; 8 | } STUDENT; 9 | 10 | typedef STUDENT Element; 11 | 12 | int* endmem; 13 | 14 | extern int min(int n1, int n2); 15 | 16 | extern void readData(FILE* f, Element Ls[], int n); 17 | 18 | extern void merge(Element Ls1[], int sz1, Element Ls2[], int sz2, Element Ls[]); 19 | 20 | extern void mergeSort(Element Ls[], int n); 21 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab6/recursive_merge_sort.c: -------------------------------------------------------------------------------- 1 | #include "merge_sort.h" 2 | 3 | void recursiveMergeSort(Element Ls[], int lo, int hi) { 4 | if(lo >= hi) { 5 | int end; 6 | endmem = &end; 7 | return; 8 | } 9 | 10 | int mid = lo + (hi - lo) / 2; 11 | 12 | int sz1 = mid - lo + 1; 13 | int sz2 = hi - mid; 14 | Element Ls1[sz1], Ls2[sz2], Ls3[sz1 + sz2]; 15 | 16 | recursiveMergeSort(Ls, lo, mid); 17 | recursiveMergeSort(Ls, mid + 1, hi); 18 | 19 | for(int i = 0; i < sz1; i++) 20 | Ls1[i] = Ls[lo + i]; 21 | for(int i = 0; i < sz2; i++) 22 | Ls2[i] = Ls[mid + 1 + i]; 23 | 24 | merge(Ls1, sz1, Ls2, sz2, Ls3); 25 | 26 | for(int i = 0; i < sz1 + sz2; i++) 27 | Ls[lo + i] = Ls3[i]; 28 | } 29 | 30 | void mergeSort(Element Ls[], int n) { 31 | recursiveMergeSort(Ls, 0, n - 1); 32 | } 33 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab7/10: -------------------------------------------------------------------------------- 1 | vxiadphhxe 594092 2 | wfodbzbshf 563837 3 | hbpswbdpfk 196559 4 | rfwraxcffu 554205 5 | qhcuywvglf 117790 6 | gjumzjylzg 663271 7 | ywsynponpt 588860 8 | mmykyrdpyn 10467 9 | ffazjscwai 166940 10 | adeirposuk 276916 11 | -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab7/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab7/a.out -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab7/graph.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab7/graph.ods -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab7/lab7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab7/lab7.pdf -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab7/quick_sort.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef struct employee { 7 | char name[20]; 8 | int empID; 9 | } EMPLOYEE; 10 | 11 | typedef EMPLOYEE Element; 12 | 13 | extern void readData(FILE* f, Element Ls[], int n); 14 | 15 | extern int partition(Element Ls[], int lo, int hi); 16 | 17 | extern void quickSort(Element Ls[], int lo, int hi, int s); 18 | 19 | extern void exchange(Element Ls[], int i, int j); 20 | 21 | extern void insertionSort(Element Ls[], int n); 22 | 23 | extern void sort(Element Ls[], int n, int s); 24 | 25 | extern double* testRun(Element Ls[], int n); 26 | 27 | extern int estimateCutoff(Element Ls[], int n); -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab8/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab8/a.out -------------------------------------------------------------------------------- /Miscellaneous/Labs/Lab8/lab9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/Labs/Lab8/lab9.pdf -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/BSTFromPreorderTransversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * Definition for a binary tree node. 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | class Solution { 21 | public: 22 | TreeNode* treeConstruction(std::vector& preorder, int *index, int min, int max) { 23 | if(*index >= preorder.size()) 24 | return NULL; 25 | 26 | TreeNode* x = NULL; 27 | 28 | if(preorder[*index] >= min && preorder[*index] <= max) { 29 | int key = preorder[*index]; 30 | x = new TreeNode(key); 31 | *index += 1; 32 | 33 | x -> left = treeConstruction(preorder, index, min, key); 34 | x -> right = treeConstruction(preorder, index, key, max); 35 | } 36 | return x; 37 | 38 | } 39 | TreeNode* bstFromPreorder(vector& preorder) { 40 | int index = 0; 41 | return treeConstruction(preorder, &index, INT_MIN, INT_MAX); 42 | } 43 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/BackSpaceStringCompare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | bool backspaceCompare(std::string S, std::string T) { 6 | std::stack S1; 7 | std::stack T1; 8 | int n1 = S.length(); 9 | int n2 = T.length(); 10 | for(int i = 0; i < n1; i++) { 11 | if(S[i] == '#'){ 12 | if(!S1.empty()) 13 | S1.pop(); 14 | } 15 | else 16 | S1.push(S[i]); 17 | } 18 | for(int i = 0; i < n2; i++) { 19 | if(T[i] == '#'){ 20 | if(!T1.empty()) 21 | T1.pop(); 22 | } 23 | else 24 | T1.push(T[i]); 25 | } 26 | bool same = true; 27 | while(!S1.empty() && !T1.empty()) { 28 | if(S1.top() != T1.top()) { 29 | same = false; 30 | break; 31 | } 32 | else { 33 | S1.pop(); 34 | T1.pop(); 35 | } 36 | } 37 | if(!S1.empty() || !T1.empty()) 38 | same = false; 39 | return same; 40 | } 41 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/BestTimeToBuyAndSell.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int maxProfit(std::vector& prices) { 6 | int n = prices.size(); 7 | int maxProfit = 0; 8 | for(int i = 1; i < n; i++) 9 | if(prices[i] > prices[i - 1]) 10 | maxProfit += prices[i] - prices[i - 1]; 11 | return maxProfit; 12 | } 13 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/BitWiseAndOfNumbersRange.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rangeBitwiseAnd(int m, int n) { 4 | int count = 0; 5 | if(n == m) 6 | return n; 7 | while(n / 2 != 0 && m / 2 != 0) { 8 | int q1 = 1, q2 = 1; 9 | int n1 = n, m1 = m; 10 | while(n1 / 2 != 0) { 11 | q1 *= 2; 12 | n1 /= 2; 13 | } 14 | while(m1 / 2 != 0) { 15 | q2 *= 2; 16 | m1 /= 2; 17 | } 18 | if(q1 == q2) 19 | count += q1; 20 | else 21 | break; 22 | n = n % q1; 23 | m = m % q2; 24 | } 25 | return count; 26 | } 27 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/ContagiousArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int findMaxLength(std::vector& nums) { 6 | if(nums.size() == 0) 7 | return 0; 8 | int max = -1; 9 | int sum[nums.size()]; 10 | int n = nums.size(); 11 | for(int i = 0; i < n; i++) 12 | if(nums[i] == 0) 13 | nums[i] = -1; 14 | 15 | int maxsum = nums[0], minsum = nums[0]; 16 | sum[0] = nums[0]; 17 | for(int i = 1; i < n; i++) { 18 | sum[i] = sum[i - 1] + nums[i]; 19 | if(maxsum < sum[i]) 20 | maxsum = sum[i]; 21 | if(minsum > sum[i]) 22 | minsum = sum[i]; 23 | if(sum[i] == 0) 24 | max = i + 1; 25 | } 26 | int map[maxsum - minsum + 1]; 27 | for(int i = 0; i < maxsum - minsum + 1; i ++) { 28 | map[i] = -1; 29 | } 30 | for(int i = 0; i < n; i++) { 31 | if(map[sum[i] - minsum] == -1) 32 | map[sum[i] - minsum] = i; 33 | else if(i - map[sum[i] - minsum] > max) 34 | max = i - map[sum[i] - minsum]; 35 | } 36 | if(max == -1) 37 | return 0; 38 | return max; 39 | } 40 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/CountSubArraysWithSumk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int subarraySum(std::vector& nums, int k) { 6 | int sum = 0; 7 | int count = 0; 8 | std::unordered_map map; 9 | for(int i = 0; i < nums.size(); i++) { 10 | sum += nums[i]; 11 | if(map[sum - k] != 0) 12 | count += map[sum - k]; 13 | if(sum == k) 14 | count++; 15 | map[sum]++; 16 | } 17 | return count; 18 | } 19 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/DiameterBinaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * Definition for a binary tree node. 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | //TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | class Solution { 21 | public: 22 | int diameterOfBinaryTree(TreeNode* root) { 23 | int height = 0; 24 | return diameter(root, &height); 25 | } 26 | 27 | int diameter(TreeNode* root, int * height) { 28 | int lh = 0, rh = 0; 29 | 30 | if(root == NULL) { 31 | *height = 0; 32 | return 0; 33 | } 34 | 35 | int ld = diameter(root -> left, &lh); 36 | int rd = diameter(root -> right, &rh); 37 | 38 | *height = max(lh, rh) + 1; 39 | 40 | return max(max(lh + rh, ld), rd); 41 | } 42 | 43 | int max(int a, int b) { 44 | if(a > b) 45 | return a; 46 | else 47 | return b; 48 | } 49 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/HappyNumber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isHappy(int n) { 4 | int slow, fast; 5 | slow = fast = n; 6 | do { 7 | slow = squareSum(slow); //same method as checking loop in a linked list 8 | fast = squareSum(squareSum(fast)); 9 | } while(slow != fast); 10 | 11 | return (slow == 1); 12 | } 13 | 14 | int squareSum(int n) { 15 | int sum = 0; 16 | 17 | while(n) { 18 | sum += (n % 10) * (n % 10); 19 | n /= 10; 20 | } 21 | 22 | return sum; 23 | } 24 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/JumpGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | bool canJump(std::vector& nums) { 6 | int n = nums.size(); 7 | int last = n - 1; 8 | for(int i = n - 2; i > -1; i--) 9 | if(nums[i] + i >= last) 10 | last = i; 11 | return last == 0; 12 | } 13 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/LastStone.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int lastStoneWeight(std::vector& stones) { 6 | make_heap(stones.begin(), stones.end()); 7 | while(stones.size() > 1) { 8 | int first = stones.front(); 9 | pop_heap(stones.begin(), stones.end()); 10 | stones.pop_back(); 11 | int sec = stones.front(); 12 | pop_heap(stones.begin(), stones.end()); 13 | stones.pop_back(); 14 | 15 | if(first == sec) 16 | continue; 17 | else { 18 | int dif = sec > first ? sec - first : first - sec; 19 | stones.push_back(dif); 20 | push_heap(stones.begin(), stones.end()); 21 | } 22 | } 23 | if(stones.size() == 0) 24 | return 0; 25 | else return stones.front(); 26 | } 27 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/LeftMostColumnWithOne.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * // This is the BinaryMatrix's API interface. 5 | * // You should not implement it, or speculate about its implementation 6 | * class BinaryMatrix { 7 | * public: 8 | * int get(int x, int y); 9 | * vector dimensions(); 10 | * }; 11 | */ 12 | 13 | class BinaryMatrix { 14 | public: 15 | int get(int x, int y); 16 | std::vector dimensions(); 17 | }; 18 | 19 | class Solution { 20 | public: 21 | int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { 22 | std::vector size = binaryMatrix.dimensions(); 23 | int n = size[0]; 24 | int m = size[1]; 25 | int leftColumn = -1; 26 | int r = 0; 27 | int c = m - 1; 28 | while(r < n && c > -1) { 29 | int x = binaryMatrix.get(r, c); 30 | if(x == 0) 31 | r++; 32 | else { 33 | leftColumn = c; 34 | c--; 35 | } 36 | } 37 | return leftColumn; 38 | } 39 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/LongestCommonSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int max(int a, int b) { 6 | if(a > b) 7 | return a; 8 | return b; 9 | } 10 | int longestCommonSubsequence(std::string text1, std::string text2) { 11 | int n1 = text1.length(); 12 | int n2 = text2.length(); 13 | int dp[n1 + 1][n2 + 1]; 14 | 15 | for(int i = 0; i <= n1; i++) { 16 | for(int j = 0; j <= n2; j++) { 17 | if(i == 0 || j == 0) 18 | dp[i][j] = 0; 19 | else if(text1[i - 1] == text2[j - 1]) 20 | dp[i][j] = 1 + dp[i - 1][j - 1]; 21 | else 22 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); 23 | } 24 | } 25 | return dp[n1][n2]; 26 | } 27 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/MaximalSquare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int maximalSquare(std::vector>& matrix) { 6 | int n1 = matrix.size(); 7 | if(n1 == 0) 8 | return 0; 9 | int n2 = matrix[0].size(); 10 | int max = 0; 11 | int square[n1][n2]; 12 | for(int i = 0; i < n1; i++) { 13 | square[i][0] = matrix[i][0] - '0'; 14 | if(square[i][0] == 1) 15 | max = 1; 16 | } 17 | for(int i = 0; i < n2; i++) { 18 | square[0][i] = matrix[0][i] - '0'; 19 | 20 | if(square[0][i] == 1) 21 | max = 1; 22 | } 23 | 24 | for(int i = 1; i < n1; i++ ) { 25 | for(int j = 1; j < n2; j++ ) { 26 | if(matrix[i][j] == '1'){ 27 | square[i][j] = 1 + min(square[i - 1][j], min(square[i][j - 1], square[i - 1][j - 1])); 28 | } 29 | else 30 | square[i][j] = 0; 31 | 32 | if(square[i][j] > max) 33 | max = square[i][j]; 34 | } 35 | } 36 | return max * max; 37 | } 38 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/MaximumSubarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int maxSubArray(std::vector& nums) { 6 | int max_sum, cur_sum; 7 | max_sum = cur_sum = 0; 8 | 9 | for(auto i = nums.begin(); i != nums.end(); i++) { 10 | cur_sum += *i; 11 | 12 | if(cur_sum < 0) 13 | cur_sum = 0; 14 | 15 | else if(cur_sum > max_sum) 16 | max_sum = cur_sum; 17 | } 18 | if(max_sum == 0) { 19 | max_sum = nums.front(); 20 | for(auto i = nums.begin(); i != nums.end(); i++) { 21 | if(max_sum < *i) 22 | max_sum = *i; 23 | } 24 | } 25 | return max_sum; 26 | } 27 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/MiddleLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct ListNode { 4 | int val; 5 | ListNode *next; 6 | }; 7 | 8 | /** 9 | * Definition for singly-linked list. 10 | * struct ListNode { 11 | * int val; 12 | * ListNode *next; 13 | * ListNode(int x) : val(x), next(NULL) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | ListNode* middleNode(ListNode* head) { 19 | ListNode* slow = head; 20 | ListNode* fast = head; 21 | while(fast != NULL && fast -> next != NULL) { 22 | fast = fast -> next -> next; 23 | slow = slow -> next; 24 | } 25 | return slow; 26 | } 27 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/MinPathSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int min(int a, int b) { 6 | if(a < b) 7 | return a; 8 | return b; 9 | } 10 | int minPathSum(std::vector>& grid) { 11 | int cost[grid.size()][grid[0].size()]; 12 | for(int i = 0; i < grid.size(); i++) { 13 | for(int j = 0; j < grid[0].size(); j++) { 14 | cost[i][j] = 0; 15 | } 16 | } 17 | cost[0][0] = grid[0][0]; 18 | for(int i = 1; i < grid.size(); i++) 19 | cost[i][0] = cost[i - 1][0] + grid[i][0]; 20 | for(int i = 1; i < grid[0].size(); i++) 21 | cost[0][i] = cost[0][i - 1] + grid[0][i]; 22 | 23 | for(int i = 1; i < grid.size(); i++) { 24 | for(int j = 1; j < grid[0].size(); j++) { 25 | cost[i][j] = grid[i][j] + min(cost[i - 1][j], cost[i][j - 1]); 26 | } 27 | } 28 | return cost[grid.size() - 1][grid[0].size() - 1]; 29 | } 30 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/MinStack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class MinStack { 4 | public: 5 | /** initialize your data structure here. */ 6 | std::stack s; 7 | std::stack min; 8 | int size; 9 | 10 | MinStack() { 11 | size = 0; 12 | } 13 | 14 | void push(int x) { 15 | s.push(x); 16 | if(min.empty()) 17 | min.push(x); 18 | else if(min.top() >= x) 19 | min.push(x); 20 | size++; 21 | } 22 | 23 | void pop() { 24 | if(s.empty()) 25 | return; 26 | if(min.top() == s.top()) 27 | min.pop(); 28 | s.pop(); 29 | } 30 | 31 | int top() { 32 | return s.top(); 33 | } 34 | 35 | int getMin() { 36 | return min.top(); 37 | } 38 | }; 39 | 40 | /** 41 | * Your MinStack object will be instantiated and called as such: 42 | * MinStack* obj = new MinStack(); 43 | * obj->push(x); 44 | * obj->pop(); 45 | * int param_3 = obj->top(); 46 | * int param_4 = obj->getMin(); 47 | */ -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/MoveZeroes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | void moveZeroes(std::vector& nums) { 6 | int start = 0; 7 | for(int i = 0; i < nums.size(); i++) 8 | if(nums[i] != 0) 9 | nums[start++] = nums[i]; 10 | while(start < nums.size()) 11 | nums[start++] = 0; 12 | } 13 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/NumberOfIslands.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | void travel_island(std::vector>& grid, int r, int c,std::vector>& visited) { 6 | visited[r][c] = true; 7 | int row[] = {-1, 1, 0, 0}; 8 | int col[] = {0, 0, -1, 1}; 9 | for(int i = 0; i < 4; i++) { 10 | if(r + row[i] < grid.size() && c + col[i] < grid[0].size() && !visited[r + row[i]][c + col[i]] && grid[r + row[i]][c + col[i]] == '1') { 11 | travel_island(grid, r + row[i], c + col[i], visited); 12 | } 13 | } 14 | } 15 | 16 | int numIslands(std::vector>& grid) { 17 | int count = 0; 18 | std::vector> visited; 19 | 20 | for(int i = 0; i < grid.size(); i++) { 21 | std::vector a; 22 | for(int j = 0; j < grid[i].size(); j++) { 23 | a.push_back(0); 24 | } 25 | visited.push_back(a); 26 | } 27 | for(int i = 0; i < grid.size(); i++) { 28 | for(int j = 0; j < grid[i].size(); j++) { 29 | if(!visited[i][j] && grid[i][j] == '1') { 30 | travel_island(grid, i, j, visited); 31 | count++; 32 | } 33 | } 34 | } 35 | return count; 36 | } 37 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/ProductOfArrayExceptSelf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | std::vector productExceptSelf(std::vector& nums) { 6 | std::vector ans; 7 | int pro = 1; 8 | for(int i = 0; i < nums.size(); i++) { 9 | ans.push_back(pro); 10 | pro *= nums[i]; 11 | } 12 | pro = 1; 13 | for(int i = nums.size() - 1; i > -1; i--) { 14 | ans[i] *= pro; 15 | pro *= nums[i]; 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/SearchInSortedRotatedArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int search(std::vector& nums, int target) { 6 | int pivot = 0; 7 | int lo = 0, hi = nums.size() - 1; 8 | 9 | while(lo <= hi) { 10 | int mid = (lo + hi) / 2; 11 | int num = nums[mid]; 12 | int l1 = nums[lo]; 13 | int l2 = nums[hi]; 14 | if(num == target) return mid; 15 | else if(l1 <= num) { 16 | if(target >= l1 && target < num) 17 | hi = mid - 1; 18 | else 19 | lo = mid + 1; 20 | } 21 | else { 22 | if(l2 >= target && target > num) 23 | lo = mid + 1; 24 | else 25 | hi = mid - 1; 26 | } 27 | } 28 | return -1; 29 | } 30 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/SingleNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int singleNumber(std::vector& nums) { 6 | int num = 0; 7 | for(auto i = nums.begin(); i != nums.end(); ++i) 8 | num = num ^ *i; 9 | 10 | return num; 11 | } 12 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/StringShift.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | std::string stringShift(std::string s, std::vector>& shift) { 6 | int rshift = 0, lshift = 0; 7 | for(int i = 0; i < shift.size(); i++) { 8 | if(shift[i][0] == 0) 9 | lshift += shift[i][1]; 10 | else 11 | rshift += shift[i][1]; 12 | } 13 | if(lshift == rshift) 14 | return s; 15 | else if(rshift > lshift) { 16 | int shift = rshift - lshift; 17 | rightShift(s, shift); 18 | } 19 | else { 20 | int shift = lshift - rshift; 21 | leftShift(s, shift); 22 | } 23 | return s; 24 | } 25 | 26 | void leftShift(std::string &s, int x) { 27 | x = x % s.length(); 28 | std::reverse(s.begin(), s.begin() + x); 29 | std::reverse(s.begin() + x, s.end()); 30 | std::reverse(s.begin(), s.end()); 31 | } 32 | 33 | void rightShift(std::string &s, int x) { 34 | x = x % s.length(); 35 | leftShift(s, s.length() - x); 36 | } 37 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/ValidParanthesisString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | bool checkValidString(std::string s) { 6 | std::stack s1; 7 | std::stack s2; 8 | 9 | for(int i = 0; i < s.length(); i++) { 10 | if(s[i] == '(') 11 | s1.push(i); 12 | else if(s[i] == '*') 13 | s2.push(i); 14 | else { 15 | if(!s1.empty()) 16 | s1.pop(); 17 | else if(!s2.empty()) 18 | s2.pop(); 19 | else 20 | return false; 21 | } 22 | } 23 | while(!s1.empty() && !s2.empty()) { 24 | if(s1.top() > s2.top()) 25 | return false; 26 | s1.pop(); 27 | s2.pop(); 28 | } 29 | if(!s1.empty()) 30 | return false; 31 | return true; 32 | 33 | } 34 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/countingElements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int countElements(std::vector& arr) { 6 | std::sort(arr.begin(), arr.end()); 7 | int n = arr.size(); 8 | int prevNum = arr[0]; 9 | int prevCount = 0, count = 0; 10 | for(int i = 0; i < n; i++) { 11 | if(prevNum == arr[i]) { 12 | prevCount++; 13 | } 14 | else if(arr[i] == prevNum + 1) { 15 | prevNum = arr[i]; 16 | count += prevCount; 17 | prevCount = 1; 18 | } 19 | else { 20 | prevNum = arr[i]; 21 | prevCount = 1; 22 | } 23 | } 24 | return count; 25 | } 26 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/April30DayChallenge/groupAnagrams.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public List> groupAnagrams(String[] strs) { 5 | HashMap , ArrayList > map = new HashMap , ArrayList >(); 6 | 7 | for(String str : strs) { 8 | HashMap freq = new HashMap (); 9 | for(int i = 0; i < str.length(); i++) { 10 | if(freq.containsKey(str.charAt(i))) { 11 | int x = freq.get(str.charAt(i)); 12 | freq.put(str.charAt(i), ++x); 13 | } 14 | else 15 | freq.put(str.charAt(i), 1); 16 | } 17 | if(map.containsKey(freq)){ 18 | ArrayList s = map.get(freq); 19 | s.add(str); 20 | } 21 | else { 22 | ArrayList s = new ArrayList(); 23 | s.add(str); 24 | map.put(freq, s); 25 | } 26 | } 27 | List> ans = new ArrayList>(); 28 | for(HashMap key : map.keySet()) { 29 | ArrayList s = map.get(key); 30 | ans.add(s); 31 | } 32 | return ans; 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/Array/FindDuplicates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | bool containsDuplicate(std::vector& nums) { 6 | std::unordered_map freq; 7 | for(int i = 0; i < nums.size(); i++) { 8 | if(freq[nums[i]] == 1) 9 | return true; 10 | freq[nums[i]]++; 11 | } 12 | return false; 13 | } 14 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/Array/PlusOne.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | std::vector plusOne(std::vector& digits) { 6 | for(int i = digits.size() - 1; i > -1; i--) { 7 | digits[i]++; 8 | if(digits[i] > 9) 9 | digits[i] = 0; 10 | else 11 | break; 12 | } 13 | if(digits[0] == 0) 14 | digits.insert(digits.begin(), 1); 15 | return digits; 16 | } 17 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/Array/RemoveDuplicates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int removeDuplicates(std::vector& nums) { 6 | if(nums.size() == 0) 7 | return 0; 8 | int k = 1; 9 | for(int i = 1; i < nums.size(); i++) { 10 | if(nums[i] != nums[i - 1]) 11 | nums[k++] = nums[i]; 12 | } 13 | return k; 14 | } 15 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/Array/RotateArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | 6 | void reverse(std::vector& nums, int start, int end) { 7 | for(int i = start; i <= (end + start) / 2; i++) { 8 | int temp = nums[i]; 9 | nums[i] = nums[start + end - i]; 10 | nums[end + start - i] = temp; 11 | } 12 | } 13 | 14 | void rotate(std::vector& nums, int k) { 15 | int n = nums.size(); 16 | k = k % n; 17 | reverse(nums, 0, n - 1 - k); 18 | reverse(nums, n - k, n - 1); 19 | reverse(nums, 0, n - 1); 20 | } 21 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/Array/TwoSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | std::vector twoSum(std::vector& nums, int target) { 6 | std::unordered_map map; 7 | std::vector ans; 8 | for(int i = 0; i < nums.size(); i++) { 9 | if(map[target - nums[i]] != 0) { 10 | ans.push_back(map[target - nums[i]] - 1); 11 | ans.push_back(i); 12 | break; 13 | } 14 | map[nums[i]] = i + 1; 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/CheckIfItIsStraightLine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | bool checkStraightLine(std::vector>& coordinates) { 6 | if(coordinates.size() == 2) 7 | return true; 8 | 9 | for(int i = 1; i < coordinates.size(); i++) { 10 | if((coordinates[i][1] - coordinates[0][1]) * (coordinates[1][0] - coordinates[0][0]) != 11 | (coordinates[i][0] - coordinates[0][0]) * (coordinates[1][1] - coordinates[0][1])) 12 | return false; 13 | } 14 | return true; 15 | } 16 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/FindTheTownJudge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int findJudge(int N, std::vector>& trust) { 6 | std::unordered_map map1, map2; 7 | for(int i = 0; i < trust.size(); i++) { 8 | map1[trust[i][0]]++; 9 | map2[trust[i][1]]++; 10 | } 11 | for(int i = 1; i <= N; i++) { 12 | if(map1[i] == 0 && map2[i] == N - 1) 13 | return i; 14 | } 15 | return -1; 16 | } 17 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/FirstBadVersion.cpp: -------------------------------------------------------------------------------- 1 | // The API isBadVersion is defined for you. 2 | // bool isBadVersion(int version); 3 | 4 | class Solution { 5 | public: 6 | int firstBadVersion(int n) { 7 | int lo = 1; 8 | int hi = n; 9 | while(lo < hi) { 10 | int mid = (int)(((long)lo + (long)hi) / 2); 11 | if(isBadVersion(mid)) 12 | hi = mid - 1; 13 | else 14 | lo = mid + 1; 15 | } 16 | if(isBadVersion(lo)) 17 | return lo; 18 | else 19 | return lo + 1; 20 | } 21 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/FirstUniqueCharacter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int firstUniqChar(std::string s) { 6 | std::list l; 7 | std::unordered_map::const_iterator> map; 8 | std::unordered_map a; 9 | for(int i = 0; i < s.length(); i++) { 10 | auto it = map.find(s[i]); 11 | if(it != map.end()) { 12 | if(a[s[i]] != 2) { 13 | l.erase(map[s[i]]); 14 | a[s[i]] = 2; 15 | } 16 | } 17 | else { 18 | l.push_front(s[i]); 19 | map[s[i]] = l.cbegin(); 20 | } 21 | } 22 | if(l.size() != 0) { 23 | char c = l.back(); 24 | for(int i = 0; i < s.length(); i++) 25 | if(c == s[i]) 26 | return i; 27 | } 28 | return -1; 29 | } 30 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/FloodFill.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | void fill(std::vector>& image, std::vector>& visited, int sr, int sc, int newColor, int oldColor) { 6 | if(sr >= image.size() || sc >= image[0].size() || sr < 0 || sc < 0) 7 | return; 8 | if(image[sr][sc] != oldColor) 9 | return; 10 | if(visited[sr][sc] == 1) 11 | return; 12 | image[sr][sc] = newColor; 13 | visited[sr][sc] = 1; 14 | fill(image, visited, sr - 1, sc, newColor, oldColor); 15 | fill(image, visited, sr + 1, sc, newColor, oldColor); 16 | fill(image, visited, sr, sc + 1, newColor, oldColor); 17 | fill(image, visited, sr, sc - 1, newColor, oldColor); 18 | } 19 | std::vector> floodFill(std::vector>& image, int sr, int sc, int newColor) { 20 | std::vector> visited; 21 | for(int i = 0; i < image.size(); i++) { 22 | std::vector v1(image[i].size(), 0); 23 | visited.push_back(v1); 24 | } 25 | fill(image, visited, sr, sc, newColor, image[sr][sc]); 26 | return image; 27 | } 28 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/JewelsAndStones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int numJewelsInStones(std::string J, std::string S) { 6 | std::unordered_map jewel; 7 | for(int i = 0; i < J.length(); i++) 8 | jewel[J[i]] = 1; 9 | int count = 0; 10 | for(int i = 0; i < S.length(); i++) 11 | if(jewel[S[i]] == 1) 12 | count++; 13 | return count; 14 | } 15 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/MajorityElement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int majorityElement(std::vector& nums) { 6 | int maj = 0; 7 | int count = 0; 8 | for(int i = 0; i < nums.size(); i++) { 9 | if(maj == nums[i]) 10 | count++; 11 | else if(count == 0) { 12 | maj = nums[i]; 13 | count++; 14 | } 15 | else 16 | count--; 17 | } 18 | count = 0; 19 | for(int i = 0; i < nums.size(); i++) { 20 | if(maj == nums[i]) 21 | count++; 22 | } 23 | if(count > nums.size() / 2) 24 | return maj; 25 | return -1; 26 | } 27 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/NumberComplement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | if(num == 0) 5 | return 1; 6 | long int k = 1; 7 | int n = num; 8 | while(n != 0) { 9 | k *= 2; 10 | n /= 2; 11 | } 12 | int A = k - 1; 13 | return (num ^ A); 14 | } 15 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/isValidSquare.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int num) { 4 | for(long long int i = 1; i * i <= num; i++) { 5 | if(i * i == num) 6 | return true; 7 | } 8 | return false; 9 | } 10 | }; -------------------------------------------------------------------------------- /Miscellaneous/LeetCode/May30DayChallenge/ransomNote.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | bool canConstruct(std::string ransomNote, std::string magazine) { 6 | std::vector count(26, 0); 7 | for(int i = 0; i < magazine.length(); i++) 8 | count[magazine[i] - 'a']++; 9 | for(int i = 0; i < ransomNote.length(); i++) 10 | count[ransomNote[i] - 'a']--; 11 | for(int i = 0; i < 26; i++) 12 | if(count[i] < 0) 13 | return false; 14 | return true; 15 | } 16 | }; -------------------------------------------------------------------------------- /Miscellaneous/slides/01-introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/01-introduction.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/02-mathematics.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/02-mathematics.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/03-data-structures.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/03-data-structures.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/04-dynamic-programming.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/04-dynamic-programming.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/05-combinatorial-games.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/05-combinatorial-games.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/06-basic-graph-algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/06-basic-graph-algorithms.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/07-shortest-path-algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/07-shortest-path-algorithms.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/08-network-flow-problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/08-network-flow-problems.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/09-computational-geometry.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/09-computational-geometry.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/10-string-algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/10-string-algorithms.pdf -------------------------------------------------------------------------------- /Miscellaneous/slides/suffix-array.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chirag-singhal/Coding-Interview-101/daa152471430b45025070ac2c480aa95a4273636/Miscellaneous/slides/suffix-array.pdf -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-architect -------------------------------------------------------------------------------- /template.md: -------------------------------------------------------------------------------- 1 | ## []() 2 | 3 | ### Problem Link - 4 | 5 | --- 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | 11 | 12 | 13 | ``` --------------------------------------------------------------------------------