├── .gitattributes ├── .gitignore ├── 01 Matrix.cpp ├── 2 Keys Keyboard.cpp ├── 3sum-smaller.cpp ├── AVLclass.cpp ├── BFS(Vectors).cpp ├── BabyStepGiantStep.cpp ├── BigInteger.cpp ├── Binary Search Tree Iterator.cpp ├── Binary Tree Maximum Path Sum.cpp ├── Binary Tree to DLL.cpp ├── BurstBalloons.cpp ├── CNN.py ├── CSES ├── 1135-Distance-Queries.cpp ├── 1628-Meet-in-the-Middle.cpp ├── 2101-New-Roads-Queries.cpp ├── 2136-Hamming-Distance.cpp ├── 2137-Beautiful-Subgrids.cpp ├── 2138-Reachable-Nodes.cpp ├── 2143-Reachability-Queries.cpp └── bits │ └── stdc++.h ├── Check for BST .cpp ├── Check for Balanced Tree.cpp ├── CheckBipartiteGraph.cpp ├── Cherry-pick.cpp ├── CodeArchive(All).cpp.cpp ├── Codeforces ├── (A) 123-sequence.cpp ├── (A) Accounting.cpp ├── (A) Again Twenty Five!.py ├── (A) Almost Prime.cpp ├── (A) Amusing Joke.cpp ├── (A) Bear and Displayed Friends.cpp ├── (A) Bear and Elections.cpp ├── (A) Bear and Game.cpp ├── (A) Bear and Reverse Radewoosh.cpp ├── (A) BerOS file system.cpp ├── (A) Bit++.cpp ├── (A) Bits.cpp ├── (A) Blackjack.cpp ├── (A) Boredom.cpp ├── (A) Boys and Girls.cpp ├── (A) Bulbs.cpp ├── (A) Business trip.cpp ├── (A) Cards.cpp ├── (A) Chores.cpp ├── (A) Co-prime Array.cpp ├── (A) Colorful Stones .cpp ├── (A) Comparing Strings.cpp ├── (A) Comparing Two Long Integers.cpp ├── (A) Complicated GCD.cpp ├── (A) Cupboards.cpp ├── (A) Elephant.cpp ├── (A) Fancy Fence.cpp ├── (A) Far Relative’s Birthday Cake.cpp ├── (A) Flipping Game.cpp ├── (A) Free Ice Cream.cpp ├── (A) Games.cpp ├── (A) Holidays.cpp ├── (A) Hulk.cpp ├── (A) I Wanna Be the Guy.cpp ├── (A) Increasing Sequence.cpp ├── (A) Infinite Sequence.cpp ├── (A) Jeff and Digits.cpp ├── (A) Joysticks.cpp ├── (A) Jzzhu and Children.cpp ├── (A) Kefa and First Steps.cpp ├── (A) King Moves.cpp ├── (A) Levko and Table.cpp ├── (A) Link-Cut tree.cpp ├── (A) Little Artem and Presents.cpp ├── (A) Little Elephant and Chess.cpp ├── (A) Olesya and Rodion.cpp ├── (A) Parallelepiped.cpp ├── (A) Plug-in.cpp ├── (A) Police Recruits.cpp ├── (A) Professor GukiZ's Robot.cpp ├── (A) Puzzles.cpp ├── (A) Round House.cpp ├── (A) Shell Game.cpp ├── (A) Slime Combining.cpp ├── (A) Stones on the Table.cpp ├── (A) Subtractions.cpp ├── (A) Summer Camp.cpp ├── (A) Super Agent.cpp ├── (A) System Administrator.cpp ├── (A) Theatre Square.cpp ├── (A) Translation.cpp ├── (A) Tricky Sum.cpp ├── (A) Twins.cpp ├── (A) Watermelon.cpp ├── (A) Way Too Long Words.cpp ├── (A) Word Capitalization.cpp ├── (B) A Trivial Problem.cpp ├── (B) Bargaining Table.cpp ├── (B) Berland National Library.cpp ├── (B) Burglar and Matches.cpp ├── (B) Cells Not Under Attack.cpp ├── (B) Chocolate.cpp ├── (B) Ciel and Flower.cpp ├── (B) Coins.cpp ├── (B) Cosmic Tables.cpp ├── (B) Different is Good.cpp ├── (B) Dinner with Emma.cpp ├── (B) Game of Robots.cpp ├── (B) Grandfather Dovlet’s calculator.cpp ├── (B) Guess the Permutation.cpp ├── (B) Ilya and Queries.cpp ├── (B) Little Artem and Grasshopper.cpp ├── (B) Moore's Law.py ├── (B) Non-square Equation.cpp ├── (B) Optimal Point on a Line.cpp ├── (B) Photo to Remember.cpp ├── (B) Pyramid of Glasses.cpp ├── (B) Quasi Binary.cpp ├── (B) Spider Man.cpp ├── (B) Sum of Digits.cpp ├── (B) T-primes.cpp ├── (B) Testing Pants for Sadness.cpp ├── (B) The Time.cpp ├── (B) Unary.cpp ├── (B) Unlucky Ticket.cpp ├── (B) Vanya and Food Processor.cpp ├── (B) Wet Shark and Bishops.cpp ├── (B) z-sort.cpp ├── (C) Bear and Colors.cpp ├── (C) Beautiful Sets of Points.cpp ├── (C) Divisibility by Eight.cpp ├── (C) Exams.cpp ├── (C) Football.cpp ├── (C) Gerald's Hexagon.cpp ├── (C) Given Length and Sum of Digits...cpp ├── (C) Lucky Numbers.py ├── (C) Make Palindrome.cpp ├── (C) Number of Ways.cpp ├── (C) Registration System.cpp ├── (C) Stripe.cpp ├── (C) Table Decorations.cpp ├── (C) Tanya and Toys.cpp ├── (C) Team.cpp ├── (C) The World is a Theatre.cpp ├── (C) Valera and Tubes .cpp ├── (C) Watchmen.cpp ├── (C) Weather.cpp ├── (C) k-Tree.cpp ├── (D) Flowers.cpp ├── (D) Hexagons!.py ├── (D) Queue.cpp ├── (D) Soldier and Number Game.cpp ├── (E) Generate a String.cpp ├── (F) Selection of Personnel.cpp ├── (J) Divisibility.cpp ├── (L) Cracking the Code.py ├── (N) Forecast.cpp ├── (R) Game.cpp ├── 337A - Puzzles.cpp ├── Bear and Elections.cpp ├── Cupboards.cpp ├── D. Powerful array.cpp ├── Game Outcome.cpp ├── Kefa and First Steps.cpp ├── Twins.cpp └── Way Too Long Words.cpp ├── Computer-graphics ├── Circle Drawing │ ├── circle drawing │ └── circle_drawing.cpp ├── Line Drawing │ ├── line.cpp │ └── lineDrawing ├── Polygon Drawing │ └── polygonFilling │ │ ├── .settings │ │ └── language.settings.xml │ │ ├── Debug │ │ ├── makefile │ │ ├── objects.mk │ │ ├── polygonFilling.d │ │ ├── polygonFilling.o │ │ ├── sources.mk │ │ └── subdir.mk │ │ ├── _cproject │ │ ├── _project │ │ ├── myHeaderPolygonFilling.h │ │ ├── myHeaderPolygonFilling.h~ │ │ └── polygonFilling.cpp ├── Scaling │ ├── .settings │ │ └── language.settings.xml │ ├── Debug │ │ ├── makefile │ │ ├── objects.mk │ │ ├── scaling │ │ ├── scaling.d │ │ ├── scaling.o │ │ ├── sources.mk │ │ └── subdir.mk │ ├── _cproject │ ├── _project │ ├── header.h │ └── scaling.cpp └── Tic Tac Toe │ ├── opengl │ ├── tictactoe │ └── tictactoe_source_code.cpp ├── CycleDetctionDirected.cpp ├── DFS(Vectors).cpp ├── DataStructures ├── (1)Stack.cpp ├── (2)Queue.cpp ├── (3)Priority.cpp ├── (4)Expressiontree.cpp ├── (5)BST.cpp ├── Threaded Binary tree.cpp ├── stack.cpp └── stacklink.cpp ├── DecodeWays.cpp ├── Diameter of Binary Tree.cpp ├── DigitDp.cpp ├── Dijkstra.cpp ├── DirectedCycleColor.cpp ├── Disjoint-Sets.cpp ├── DistinctPermutation.cpp ├── EditDistance.cpp ├── EulerTotientFunction.cpp ├── FFT.cpp ├── FastFourierTransform.cpp ├── Fibonacci(MatrixExponentiation).cpp ├── Finding middle element in a linked list.cpp ├── Fixing Two nodes of a BST .cpp ├── FloydWarshall.cpp ├── Fraction(Class).cpp ├── GaussElimination.cpp ├── GrahamScan(Convex Hull).cpp ├── Hackerrank ├── A Basic Quiz on Algorithms #1.cpp ├── A Very Big Sum.cpp ├── Angry Professor.cpp ├── Caesar Cipher.cpp ├── Cavity Map.cpp ├── Chocolate Feast.cpp ├── Chocolate in Box.cpp ├── City-Construction.cpp ├── Closest Numbers.cpp ├── Common Child.cpp ├── Connected Cell in a Grid.cpp ├── Contacts.cpp ├── Correctness and the Loop Invariant.cpp ├── Counting Sort 1.cpp ├── Counting Sort 2.cpp ├── Counting Sort 3.cpp ├── Cut the sticks.cpp ├── Diagonal Difference.cpp ├── Encryption.cpp ├── Extra long factorials.cpp ├── Extra long factorials.py ├── Fibonacci_Modified.py ├── Filling Jars.cpp ├── Find Digits.cpp ├── Find Point.cpp ├── Find the Median.cpp ├── Game Of Thrones - II.cpp ├── Gemstones.cpp ├── Grid Challenge.cpp ├── Halloween party.cpp ├── Handshake.cpp ├── Ice Cream Parlor.cpp ├── Identify Smith Numbers.cpp ├── Insertion Sort - Part 1.cpp ├── Insertion Sort - Part 2.cpp ├── Is Fibo.cpp ├── Knapsack.cpp ├── Largest Permutation.cpp ├── Maps-STL.cpp ├── Max Min.cpp ├── Maximizing XOR.cpp ├── Minimum Draws.cpp ├── Missing Numbers.cpp ├── Pairs.cpp ├── Pangrams.cpp ├── Picking Cards.cpp ├── Plus Minus.cpp ├── Print the elements of a linked list.cpp ├── Quicksort 1 - Partition.cpp ├── Quicksort In-Place.cpp ├── Red John is Back.cpp ├── Running Time of Algorithms.cpp ├── Running Time of Quicksort.cpp ├── Service Lane.cpp ├── Sets-STL.cpp ├── Sherlock and Array.cpp ├── Sherlock and GCD.cpp ├── Sherlock and Pairs.cpp ├── Sherlock and Permutations.cpp ├── Sherlock and Permutations1.cpp ├── Sherlock and Squares.cpp ├── Sherlock and The Beast.cpp ├── Sherlock and Watson.cpp ├── Simple Array Sum.py ├── Staircase.cpp ├── Stock Maximize.cpp ├── Strange Grid.cpp ├── Strings.cpp ├── Taum and B'day.cpp ├── The Coin Change Problem.cpp ├── The Grid Search.cpp ├── The Love-Letter Mystery.cpp ├── The Maximum Subarray.cpp ├── Time Conversion.cpp ├── Two Strings.cpp ├── Two arrays.cpp ├── Utopian Tree.cpp ├── Vector-Erase.cpp ├── Vector-Sort.cpp ├── alternating-characters.cpp ├── anagram.cpp ├── funny-string.cpp ├── game-of-thrones.cpp ├── make-it-anagram.cpp ├── ncr.cpp ├── palindrome-index.cpp ├── priyanka-and-toys.cpp └── subset-sum.cpp ├── Intersection Point in Y Shapped Linked Lists.cpp ├── KMP.cpp ├── KemptySlots.cpp ├── KnuthMorrisPratt.cpp ├── Kruskal(Minimum Spanning Tree).cpp ├── LCA2class.cpp ├── LCASPARSE.cpp ├── LCAclass.cpp ├── LFU.cpp ├── LIS.cpp ├── LPS.cpp ├── LRU.cpp ├── Largest Number.cpp ├── LeetCode ProblemSet By order ├── Amazon - LeetCode frequency.pdf ├── Amazon - LeetCode.pdf ├── Apple - LeetCode frequency.pdf ├── Apple - LeetCode.pdf ├── DFS - Problems - LeetCode.pdf ├── DP - Problems - LeetCode.pdf ├── Facebook - LeetCode frequency.pdf ├── Facebook - LeetCode.pdf ├── Google - LeetCode - 6 months.pdf ├── Google - LeetCode - up - down.pdf ├── Google - LeetCode Sorted By Difficulty.pdf ├── Google - LeetCode.pdf ├── Graph - Problems - LeetCode.pdf ├── LinkedIn - LeetCode frequency.pdf ├── LinkedIn - LeetCode.pdf ├── Microsoft - LeetCode frequency.pdf ├── Microsoft - LeetCode.pdf ├── Tree - Problems - LeetCode.pdf ├── Uber - LeetCode frequency.pdf ├── Uber - LeetCode.pdf └── Useful LeetCode LInks.txt ├── LeetCode-FAANG ├── Count Square Submatrices with All Ones.cpp ├── count-servers-that-communicate.cpp ├── decrease-elements-to-make-array-zigzag.cpp ├── find-elements-in-a-contaminated-binary-tree.cpp ├── longest-arithmetic-subsequence-of-given-difference.cpp ├── longest-chunked-palindrome-decomposition.cpp ├── number-of-closed-islands.cpp ├── number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp └── remove-sub-folders-from-the-filesystem.cpp ├── Letter Combinations of a Phone Number.cpp ├── LongestBitonicSubsequence.cpp ├── LongestCommonSubsequence(Iterative).cpp ├── LongestCommonSubsequence(Recursive).cpp ├── LongestIncreasingSubsequence(Iterative).cpp ├── LongestIncreasingSubsequence(NLogN).cpp ├── LongestIncreasingSubsequence(Recursive).cpp ├── LongestPalindrome.cpp ├── LongestPalindromicSubsequence(Iterative).cpp ├── LongestPalindromicSubsequence(Memoised).cpp ├── LongestPalindromicSubsequence(Recursive).cpp ├── LongestPalindromicSubstring(Iterative).cpp ├── LucasTheorem.cpp ├── ManachersAlgorithm.cpp ├── MatrixMultiplication.cpp ├── MaximumXorSubmatrix.cpp ├── Maximum_subMatrix.cpp ├── MedianOfTwoSortedArrays.cpp ├── MeetingRooms.cpp ├── Morris.cpp ├── NGE.cpp ├── NcR%MLucas.cpp ├── One-Edit-Distance.cpp ├── OptimalMatrixChainMultiplication.cpp ├── Palindrome Partitioning.cpp ├── PalindromeTree.cpp ├── Palindrome_linked_list.cpp ├── Palindromic patitioning.cpp ├── Perfect Sum Problem.cpp ├── Permutation.cpp ├── PolynomialMultiplication(FFT).cpp ├── PowerSeries(Bernoulli).cpp ├── PrimeFactorisation.cpp ├── PrimsAlgorithm.cpp ├── PrintBalancedParanthesis.cpp ├── PrioritySceduling(Non-Preemptive).cpp ├── Queue(Template).cpp ├── QuickSort.cpp ├── README.md ├── Rabin-Miller.cpp ├── Range Minimum Query(Sparse Table).cpp ├── RankOfAMatrix.cpp ├── Regular Expression Dynamic Programming.cpp ├── Regular expression to DFA └── REtoDFA.cpp ├── ReverseAStack.cpp ├── SCCeulerian.cpp ├── SPOJ ├── ABCDEF.cpp ├── Aggressive cows.cpp ├── Assignments.cpp ├── BENEFACT - The Benefactor.cpp ├── BLCONE - Painting Hat.cpp ├── DQUERY(BIT).cpp ├── DQUERY(MO).cpp ├── EIUASSEMBLY - Assembly line.cpp ├── HORRIBLE.cpp ├── INVCNT(BIT).cpp ├── INVCNT(MERGESORT).cpp ├── K-query.cpp ├── KQUERYO.cpp ├── Longest path in a tree.cpp ├── MBLAST - BLAST.cpp ├── MKTHNUM.cpp ├── NAKANJ.cpp ├── ONEZERO.cpp ├── POLYMUL.cpp ├── RMQSQ - Range Minimum Query.cpp ├── Time to live.cpp └── YODANESS.cpp ├── SegmentTree(Range Maximum).cpp ├── SegmentTree.cpp ├── ShortestJobFirst(Non-Preemptive).cpp ├── SieveOfEratosthenes.cpp ├── SnakeAndLadders(BFS).cpp ├── SpiralOrderBT.cpp ├── Stack(Template).cpp ├── StirlingNumbers.cpp ├── SuffixTree.cpp ├── Sum root to leaf numbers.cpp ├── Surrounded Regions.cpp ├── TSP.cpp ├── Tiling.cpp ├── TonelliShanks.cpp ├── TopologicalSorting.cpp ├── Trie(Numbers).cpp ├── Trie(Strings).cpp ├── Two Pass assembler ├── AssemblerInput.txt ├── InitializeTables.txt ├── PassOneAndPassTwo.cpp ├── PassOneOutput.txt ├── PassTwoOutput.txt └── Symbol_Litral_Table.txt ├── TwoDimensionalSparseTable.cpp ├── UpDownDp.cpp ├── Wildcard Matching.cpp ├── Word Break With Queue and Trie.cpp ├── Word Break.cpp ├── Z-Algorithm.cpp ├── add-bold-tag-in-string.cpp ├── adding_two_linked_list.cpp ├── alien-dictionary.cpp ├── android-unlock-patterns.cpp ├── augmentedAVL.cpp ├── avl_insertion_del.cpp ├── best-meeting-point.cpp ├── binary-tree-longest-consecutive-sequence.cpp ├── bomb-enemy.cpp ├── campusBikes.cpp ├── cheapest-flights-within-k-stops.cpp ├── construct-tree-from-given-inorder-and-preorder-traversal.cpp ├── copypaste.cpp ├── count_occurence.cpp ├── decondWaysII.cpp ├── design-search-autocomplete-system.cpp ├── design-snake-game.cpp ├── eggdropping.cpp ├── encode-string-with-shortest-length.cpp ├── extended_gcd.cpp ├── first-non-repeating-character-in-a-stream.cpp ├── flip-game-ii.cpp ├── floyd.cpp ├── generalized-abbreviation.cpp ├── group-shifted-strings.cpp ├── huffman.c ├── inorderpreorder.cpp ├── interleaved.cpp ├── jobsequencing.cpp ├── lengthOfLongestSubstringKDistinct.cpp ├── lgstpalindrom.cpp ├── longest-line-of-consecutive-one-in-matrix.cpp ├── lrufun.cpp ├── matrixexpo.cpp ├── maximum-vacation-days.cpp ├── median.cpp ├── medianrunningSTL.cpp ├── minimize-max-distance-to-gas-station.cpp ├── minimum-platform.cpp ├── minimum-time.cpp ├── minimum-unique-word-abbreviation.cpp ├── minimum-window-subsequence.cpp ├── minimum-window-substring.cpp ├── minimumcoinchange.cpp ├── missing-ranges.cpp ├── n%S!.cpp ├── ncr.cpp ├── nextPermutation.cpp ├── next_right_ptr.cpp ├── nqueen.cpp ├── number-of-corner-rectangles.cpp ├── number-of-islands-ii.cpp ├── optimal-account-balancing.cpp ├── output-contest-matches.cpp ├── partition-to-k-equal-sum-subsets.cpp ├── redblacktree.cpp ├── reverse.cpp ├── reverseInKnodes.cpp ├── revese_recursion_linkedlist.cpp ├── rightViewofBT.cpp ├── robot-room-cleaner.cpp ├── sentence-screen-fitting.cpp ├── shortest-distance-from-all-buildings.cpp ├── shortest-way-to-form-string.cpp ├── sort-transformed-array.cpp ├── sortColors.cpp ├── sorted_and_rotated_array.cpp ├── spiral_matrix.cpp ├── split-bst.cpp ├── strassen.cpp ├── strobogrammatic-number-ii.cpp ├── strobogrammatic-number-iii.cpp ├── strobogrammatic-number.cpp ├── sudoku.cpp ├── suffixTreeLCS.cpp ├── suffix_array.cpp ├── sum-of-distances-in-tree.cpp ├── sum_of_edges_in_path.cpp ├── tarjans.cpp ├── the-maze.cpp ├── triplet.cpp ├── upperBoundLowerBound.cpp ├── walls-and-gates.cpp ├── wiggle-sort.cpp ├── word-abbreviation.cpp ├── word-squares.cpp └── zigzag-iterator.cpp /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set the default behavior for all files. 2 | * text=auto eol=lf 3 | 4 | # Normalized and converts to native line endings on checkout. 5 | *.c text 6 | *.cc text 7 | *.cxx 8 | *.cpp text 9 | *.h text 10 | *.hxx text 11 | *.hpp text 12 | -------------------------------------------------------------------------------- /2 Keys Keyboard.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int N; 3 | //private comment 4 | public: 5 | int minSteps(int n) { 6 | if(n == 1) 7 | return 0; 8 | N = n; 9 | return 1 + go(1, 1); 10 | } 11 | 12 | int go(int n, int lastcopied){ 13 | if(n > N){ 14 | return INT_MAX/10; 15 | } 16 | if(n == N){ 17 | return 0; 18 | } 19 | 20 | // paste ? 21 | // copy ? 22 | int l = 1 + go(n + lastcopied, lastcopied); 23 | int r = 2 + go(n + n , n); // copy + paste. 24 | 25 | return min(l , r); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /3sum-smaller.cpp: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | int count; 3 | 4 | public int threeSumSmaller(int[] nums, int target) { 5 | count = 0; 6 | Arrays.sort(nums); 7 | int len = nums.length; 8 | 9 | for(int i=0; i 2 | using namespace std; 3 | 4 | vector graph[100]; 5 | int n,m,v,u; 6 | bool visited[100]; 7 | 8 | void BFS(int p) 9 | { 10 | for(int i = 0; i < n; i ++) 11 | visited[i] = false; 12 | queue q; 13 | q.push(p); 14 | while(!q.empty()) 15 | { 16 | int curr = q.front(); 17 | cout< st; 4 | public: 5 | BSTIterator(TreeNode* root) { 6 | while(root){ 7 | st.push(root); 8 | root = root -> left; 9 | } 10 | } 11 | 12 | /** @return the next smallest number */ 13 | int next() { 14 | TreeNode* temp = st.top(); st.pop(); 15 | int val = temp -> val; 16 | if(temp -> right){ 17 | temp = temp -> right; 18 | st.push(temp); 19 | temp = temp -> left; 20 | while(temp){ 21 | st.push(temp); 22 | temp = temp -> left; 23 | } 24 | } 25 | return val; 26 | } 27 | 28 | /** @return whether we have a next smallest number */ 29 | bool hasNext() { 30 | return st.empty() == false; 31 | } 32 | }; -------------------------------------------------------------------------------- /Binary Tree Maximum Path Sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int ans = INT_MIN; 13 | int solve(TreeNode* root){ 14 | if(root == NULL) 15 | return 0; 16 | int l = 0 , r = 0; 17 | l = solve(root -> left); 18 | r = solve(root -> right); 19 | if(l < 0) l = 0; 20 | if(r < 0) r = 0; 21 | ans = max(ans , l + r + root ->val); 22 | return root -> val + max(l , r); 23 | } 24 | int maxPathSum(TreeNode* root) { 25 | solve(root); 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Binary Tree to DLL.cpp: -------------------------------------------------------------------------------- 1 | Node *head = NULL; 2 | Node *p = NULL; 3 | void bsttodll(Node *root){ 4 | if(root == NULL) 5 | return ; 6 | bsttodll(root -> left); 7 | if(p == NULL) 8 | head = root; 9 | root -> left = p; 10 | if(p != NULL) 11 | p ->right = root; 12 | 13 | p = root; 14 | bsttodll(root -> right); 15 | } 16 | -------------------------------------------------------------------------------- /BurstBalloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector > dp; 3 | public: 4 | int maxCoins(vector& nums) { 5 | //insert 1 in front and back. 6 | nums.push_back(1); 7 | reverse(nums.begin(), nums.end()); 8 | nums.push_back(1); 9 | reverse(nums.begin(), nums.end()); 10 | 11 | dp.resize(nums.size() + 1, vector(nums.size() + 1, -1)); 12 | return go(0, nums.size() - 1, nums); 13 | } 14 | int go(int start, int end, vector& nums) { 15 | if (start + 1 == end) { 16 | return 0; 17 | } 18 | int &ans = dp[start][end]; 19 | if (ans != -1) { 20 | return ans; 21 | } 22 | for (int mid = start + 1; mid < end; mid++) { 23 | ans = max(ans, go(start, mid, nums) + go(mid, end, nums) + nums[start] * nums[mid] * nums[end]); 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /CSES/1628-Meet-in-the-Middle.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int main() { 4 | int n, tx; 5 | cin >> n >> tx; 6 | vector a(n / 2), b(n - n / 2); 7 | for(int i = 0; i < n / 2; i++){ 8 | cin >> a[i]; 9 | } 10 | for(int i = n / 2; i < n; i++){ 11 | cin >> b[i - n / 2]; 12 | } 13 | using LL = long long; 14 | LL ans = 0; 15 | unordered_map mp; 16 | function dfs = [&](int idx, int n, LL sum, bool _left){ 17 | if(idx == n){ 18 | if(_left) mp[sum]++; 19 | else if(mp.count(tx - sum)) ans += mp[tx - sum]; 20 | return; 21 | } 22 | dfs(idx + 1, n, sum, _left); 23 | dfs(idx + 1, n, sum + (_left ? a[idx] : b[idx]), _left); 24 | }; 25 | dfs(0, a.size(), 0, true); 26 | dfs(0, b.size(), 0, false); 27 | cout << ans; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /CSES/2136-Hamming-Distance.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 30; 4 | int main() { 5 | ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 6 | #ifdef LOCAL 7 | freopen("input.txt", "r" , stdin); 8 | freopen("output.txt", "w", stdout); 9 | #endif 10 | 11 | int n, k; 12 | cin >> n >> k; 13 | vector> bits(n); 14 | for(auto &x : bits) cin >> x; 15 | int ans = INT_MAX; 16 | for(int i = 0; i < n; i++){ 17 | for(int j = i + 1; j < n; j++){ 18 | int bit_count = (bits[i] ^ bits[j]).count(); 19 | ans = min(ans, bit_count); 20 | } 21 | } 22 | cout << ans; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /CSES/2137-Beautiful-Subgrids.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #pragma GCC target("popcnt") //important for POPCOUNT instruction. 3 | using namespace std; 4 | const int MAXN = 3000 + 5; 5 | int main() { 6 | ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 7 | #ifdef LOCAL 8 | freopen("input.txt", "r" , stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | 12 | int n; 13 | cin >> n; 14 | vector> bits(n); 15 | for(auto &x : bits) cin >> x; 16 | long long ans = 0; 17 | for(int i = 0; i < n; i++){ 18 | for(int j = i + 1; j < n; j++){ 19 | int bit_count = (bits[i] & bits[j]).count(); 20 | ans += 1LL * bit_count * (bit_count - 1); 21 | } 22 | } 23 | cout << ans / 2; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Check for BST .cpp: -------------------------------------------------------------------------------- 1 | Node *p = NULL; 2 | bool f = true; 3 | void check(Node *root){ 4 | if(root == NULL) 5 | return; 6 | check(root -> left); 7 | if(p!=NULL && p -> data > root -> data) 8 | f = false; 9 | p = root; 10 | check(root -> right); 11 | } 12 | bool isBST(Node* root) { 13 | f = true; 14 | p = NULL; 15 | check(root); 16 | return f; 17 | } 18 | -------------------------------------------------------------------------------- /Check for Balanced Tree.cpp: -------------------------------------------------------------------------------- 1 | bool f = true; 2 | int ans(Node *root){ 3 | if(root == NULL) 4 | return 0; 5 | int l = ans(root -> left); 6 | int r = ans(root -> right); 7 | if(abs(l-r) > 1) 8 | f = false; 9 | return 1 + max(l , r); 10 | } 11 | bool isBalanced(Node *root) 12 | { 13 | f = true; 14 | ans(root); 15 | return f; 16 | } 17 | -------------------------------------------------------------------------------- /Cherry-pick.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int dp[55][55][55]; 3 | public: 4 | int cherryPickup(vector>& grid) { 5 | const int N = grid.size(); 6 | function dfs = [&](int x, int y, int x1){ 7 | int y1 = x + y - x1; 8 | if(x < 0 || x >= N || y < 0 || y >= N || x1 <0 || x1 >= N || y1 < 0 || y1 >= N || grid[x][y] == -1 || grid[x1][y1] == -1) 9 | return INT_MIN / 10; 10 | 11 | int &ans = dp[x][y][x1]; 12 | 13 | if(ans != -1) 14 | return ans; 15 | 16 | if(x == N - 1 && y == N - 1 && x == x1 && y == y1) 17 | return grid[x][y]; 18 | 19 | ans = grid[x][y] + grid[x1][y1]; 20 | if(x == x1 && y == y1) 21 | ans -= grid[x][y]; 22 | 23 | int a = dfs(x + 1, y, x1 + 1); // DD 24 | int b = dfs(x, y + 1, x1); // RR 25 | int c = dfs(x + 1, y, x1); // DR 26 | int d = dfs(x, y + 1, x1 + 1); // RD 27 | 28 | return ans = ans + max({a,b,c,d}); 29 | }; 30 | memset(dp, -1, sizeof dp); 31 | return dfs(0,0,0) < 0 ? 0 : dfs(0,0,0); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Codeforces/(A) 123-sequence.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | 4 | using namespace std; 5 | ll bb[25]; 6 | inline ll get(){ll c;ll n = 0;while ((c = getchar()) >= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | int main() 11 | { std::ios::sync_with_stdio(false); 12 | 13 | int t=1,n; 14 | int arr[10]={0}; 15 | arr[0]=INT_MAX; 16 | t=get(); 17 | while(t--) 18 | { 19 | n=get(); 20 | arr[n]++; 21 | 22 | } 23 | sort(arr,arr+4); 24 | cout<= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | ll f(ll x,ll y) 11 | { 12 | if(y==0)return 1; 13 | if(y%2==0){ 14 | ll z=f(x,y/2); 15 | return z*z; 16 | } 17 | else 18 | return x*f(x,y-1); 19 | } 20 | int main() 21 | { std::ios::sync_with_stdio(false); 22 | 23 | ll a,b,c; 24 | cin>>a>>b>>c; 25 | if (b==0) 26 | { 27 | putchar('0');return 0; 28 | } 29 | for (int i = -1000; i < 1001; ++i) 30 | { 31 | if (a*f(i,c)==b) 32 | { 33 | printf("%d\n",i); 34 | return 0; 35 | } 36 | } 37 | printf("No solution\n"); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Codeforces/(A) Again Twenty Five!.py: -------------------------------------------------------------------------------- 1 | 2 | a=int(raw_input()) 3 | 4 | print pow(5,a,100); 5 | -------------------------------------------------------------------------------- /Codeforces/(A) Almost Prime.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | 4 | using namespace std; 5 | ll bb[25]; 6 | inline ll get(){ll c;ll n = 0;while ((c = getchar()) >= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | int main() 11 | { std::ios::sync_with_stdio(false); 12 | 13 | int t=1,count=0,arr[4000]; 14 | t=get(); 15 | for (int i = 2; i<=t; ++i) 16 | { 17 | if(!arr[i]) 18 | for (int j= i+i; j<=t; j+=i) 19 | { 20 | arr[j]++; 21 | } 22 | 23 | } 24 | for (int i = 0; i <= t; ++i) 25 | { 26 | if (arr[i]==2) 27 | { 28 | count++; 29 | } 30 | } 31 | cout<>A>>B>>C; 8 | mapv; 9 | A.append(B); 10 | int i, x=A.length(); 11 | if (x!=C.length()) 12 | { 13 | cout<<"NO"; 14 | return 0; 15 | } 16 | for ( i = 0; i::iterator itr; 23 | for (itr=v.begin(); itr!=v.end(); ++itr) 24 | { 25 | if (((itr->second))%2) 26 | { 27 | cout<<"NO"; 28 | return 0; 29 | } 30 | } 31 | 32 | cout<<"YES"; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Codeforces/(A) Bear and Elections.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void func(int *arr, int n, int m) 3 | { 4 | int i,j=0,flag = 0,max; 5 | max = arr[0]; 6 | for(i = 1 ; i max) 9 | { 10 | max = arr[i]; 11 | j = i; 12 | } 13 | } 14 | if(max == arr[0]) 15 | { 16 | for(i = 1; i0) 31 | --arr[j]; 32 | ++arr[0]; 33 | func(arr, n, m); 34 | } 35 | int main() 36 | { 37 | int n, i; 38 | scanf("%d",&n); 39 | int arr[n], max; 40 | for(i=0 ; i 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | char str[101],k[101]; 9 | int j=0; 10 | cin>>str; 11 | for (int i = 0; str[i]!='\0'; ++i) 12 | { 13 | if(str[i]==str[i+1]&&str[i]=='/') 14 | continue; 15 | else 16 | k[j++]=str[i]; 17 | } 18 | 19 | for (int i = 0; (k[j-1]=='/')? i< j-1:i= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | ll bb[25]; 13 | inline void coutt(ll ans) 14 | { 15 | 16 | if(!ans){ 17 | putchar('0'); 18 | return; 19 | } 20 | ll k = 0; 21 | while(ans) 22 | { 23 | bb[k] = char((ans%10)+'0'); 24 | ++k; 25 | ans/=10; 26 | } 27 | for(int i=k-1;i>=0;--i) 28 | putchar(bb[i]); 29 | } 30 | int main() 31 | { std::ios::sync_with_stdio(false); 32 | 33 | ll t=1,n,m,k=0; 34 | t=get(); 35 | while(t--) 36 | { 37 | k=0; 38 | m=get(),n=get(); 39 | while((m|=((ll)1<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | int main() 13 | { std::ios::sync_with_stdio(false); 14 | clock_t start = clock(); 15 | int n; 16 | cin>>n; 17 | if (n<=10||n>21) 18 | { 19 | cout<<0; 20 | } 21 | else 22 | { 23 | if (n==20) 24 | { 25 | cout<<15; 26 | } 27 | 28 | else 29 | cout<<4; 30 | } 31 | clock_t end = clock(); 32 | // cerr <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <>n; 12 | 13 | for( int i = 0 ; i < n ; i++ ) 14 | { 15 | cin>>a; 16 | cnt[a]++; 17 | } 18 | 19 | dp[1] = cnt[1]; 20 | 21 | for( int i = 2 ; i <= 100000 ; i++ ) 22 | dp[i] = max( dp[i-1] , (dp[i-2] + (ll)(i*(ll)cnt[i])) ); 23 | 24 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | int main() 13 | { 14 | 15 | std::ios::sync_with_stdio(false); 16 | freopen("input.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | clock_t start = clock(); 19 | int m,n; 20 | cin>>n>>m; 21 | if(n>m) 22 | for (int i = 0; i < n; ++i) 23 | { 24 | cout<<'B'; 25 | if(m){ 26 | cout<<'G'; 27 | m--; 28 | } 29 | } 30 | else 31 | { 32 | for (int i = 0; i < m; ++i) 33 | { 34 | cout<<'G'; 35 | if (n) 36 | { 37 | cout<<'B'; 38 | n--; 39 | } 40 | } 41 | } 42 | clock_t end = clock(); 43 | // cerr <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" < 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | std::ios::sync_with_stdio(0); 7 | int m,n; 8 | cin>>m>>n; 9 | set v; 10 | while(m--) 11 | { 12 | int w,q; 13 | cin>>w; 14 | while(w--) 15 | { 16 | cin>>q; 17 | v.insert(q); 18 | } 19 | } 20 | if(v.size()==n) 21 | cout<<"YES"; 22 | else 23 | cout<<"NO"; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Codeforces/(A) Business trip.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll long long int 3 | using namespace std; 4 | int main() 5 | { 6 | int t=1,arr[13]; 7 | //scanf("%d",&t); 8 | while(t--) 9 | { 10 | int count,sum=0; 11 | scanf("%d",&count); 12 | 13 | for (int i = 0; i < 12; ++i) 14 | { 15 | scanf("%d",arr+i); 16 | } 17 | if (!count) 18 | { 19 | printf("0\n");return 0; 20 | } 21 | sort(arr,arr+12); 22 | 23 | for (int i = 0; i < 12; ++i) 24 | { 25 | sum+=arr[11-i]; 26 | //printf("%d\n",sum); 27 | if (sum>=count) 28 | { 29 | printf("%d\n",i+1); 30 | return 0; 31 | } 32 | } 33 | 34 | } 35 | printf("-1\n"); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Codeforces/(A) Cards.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ios::sync_with_stdio(0); cin.tie(0); 8 | std::vector v; 9 | int n,q; 10 | ll sum=0; 11 | 12 | cin>>n; 13 | for (int i = 0; i < n; ++i) 14 | { 15 | cin>>q; 16 | v.push_back(q); 17 | sum+=q; 18 | } 19 | sum=sum/(n/2); 20 | bool visited[105]={0}; 21 | for (int i = 0; i < v.size(); ++i) 22 | { 23 | if (visited[i]==0) 24 | { 25 | cout<>m>>n>>q; 9 | for (int i = 0; i < m; ++i) 10 | { 11 | cin>>arr[i]; 12 | } 13 | sort(arr,arr+m); 14 | printf("%d\n",arr[q]-arr[q-1] ); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Codeforces/(A) Colorful Stones .cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | #include "math.h" 3 | #include 4 | #include "stdio.h" 5 | #include 6 | #include "string.h" 7 | using namespace std; 8 | int main() 9 | { 10 | char str[100],str1[101]; 11 | int t,j=0; 12 | char k,h='`'; 13 | 14 | cin>>str; 15 | 16 | cin>>str1; 17 | for (int i = 0; i < strlen(str1); ++i) 18 | { 19 | if (str[j]==str1[i]) 20 | { 21 | j++; 22 | } 23 | } 24 | cout<= '0' && c <= '9') 10 | { 11 | if(c=='0'&&i==0); 12 | else 13 | arr[i++]=c; 14 | } 15 | arr[i]='\0'; 16 | return i; 17 | } 18 | int main() 19 | { 20 | char a[1000005],b[1000005]; 21 | int x=get(a); 22 | int y=get(b); 23 | if (x>y) 24 | { 25 | cout<<'>'<0) 34 | cout<<">"; 35 | else if(strcmp(a,b)<0) 36 | cout<<"<"; 37 | else 38 | cout<<"="; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Codeforces/(A) Complicated GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string ss1,ss2; 7 | cin>>ss1>>ss2; 8 | if(ss1==ss2) 9 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | 11 | 12 | int main() { 13 | 14 | int arr[10000],arr1[10000]; 15 | int t,i,count1=0,count2=0,sum=0; 16 | cin>>t; 17 | for(i=0;i>arr[i]>>arr1[i]; 20 | if(arr[i]==0) 21 | count1++; 22 | if(arr1[i]==0) 23 | count2++; 24 | 25 | } 26 | if(t-count1>count1) 27 | sum+=count1; 28 | else 29 | sum+=t-count1; 30 | 31 | if(t-count2>count2) 32 | sum+=count2; 33 | else 34 | sum+=t-count2; 35 | 36 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | int main() 13 | { std::ios::sync_with_stdio(false); 14 | 15 | int t=1,arr[100000],n; 16 | t=get(); 17 | if (t%5==0) 18 | { 19 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | 13 | int main() 14 | { std::ios::sync_with_stdio(false); 15 | 16 | int t=1,n; 17 | std::vector v; 18 | for (int i = 3; i < 362; ++i) 19 | v.push_back(1.0f*(i-2)*180/i); 20 | n=get(); 21 | for (int i = 0; i < n; ++i) 22 | { 23 | t=get(); 24 | binary_search(v.begin(),v.end(),t)?puts("YES"):puts("NO"); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Codeforces/(A) Free Ice Cream.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | inline ll get() 5 | { 6 | ll c; 7 | ll n = 0; 8 | while ((c = getchar()) >= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | int main() 13 | { std::ios::sync_with_stdio(false); 14 | clock_t start = clock(); 15 | int m,n; 16 | cin>>m>>n; 17 | ll sum=0,x,count=0; 18 | char ch; 19 | sum=n; 20 | for (int i = 0; i < m; ++i) 21 | { 22 | cin>>ch>>x; 23 | if (ch=='-'&&sum-x>=0) 24 | { 25 | sum=sum-x; 26 | } 27 | else if(ch=='+') 28 | { 29 | sum=sum+x; 30 | } 31 | else 32 | count++; 33 | } 34 | cout< 3 | #include "bits/stdc++.h" 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int t; 11 | cin>>t; 12 | int m,n,sum=0; 13 | int m1[101]={0},n1[101]={0}; 14 | while(t--) { 15 | cin>>m>>n; 16 | m1[m]++; 17 | n1[n]++; 18 | } 19 | for (int i = 0; i <=100; ++i) 20 | { 21 | sum+=m1[i]*n1[i]; 22 | } 23 | cout<>t; 10 | for (int i = 0; i < t; ++i) 11 | { 12 | if (i%2==0) 13 | { 14 | cout<<"I hate "; 15 | } 16 | else 17 | { 18 | cout<<"I love "; 19 | } 20 | if (i==t-1) 21 | { 22 | cout<<"it"; 23 | } 24 | else 25 | cout<<"that "; 26 | } 27 | 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Codeforces/(A) I Wanna Be the Guy.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main(int argc, char const *argv[]) 5 | { 6 | int t,max=-1; 7 | cin>>t; 8 | int x,y; 9 | cin>>x; 10 | setv; 11 | while(x--) 12 | { 13 | int w; 14 | cin>>w; 15 | v.insert(w); 16 | } 17 | cin>>y; 18 | while(y--) 19 | { 20 | int w; 21 | cin>>w; 22 | v.insert(w); 23 | } 24 | 25 | if (v.size()==t) 26 | { 27 | cout<<"I become the guy."; 28 | } 29 | else 30 | cout<<"Oh, my keyboard!"; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Codeforces/(A) Increasing Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(int argc, char const *argv[]) 4 | { 5 | int t,k,arr[2001],j=0; 6 | cin>>t>>k; 7 | for (int i = 0; i < t; ++i) 8 | { 9 | cin>>arr[i]; 10 | } 11 | 12 | for (int i = 0; i < t-1; ++i) 13 | { 14 | while(arr[i]>=arr[i+1]) { 15 | if(arr[i]<=arr[i+1]+k) 16 | { 17 | arr[i+1]+=k; 18 | j++; 19 | } 20 | else 21 | { 22 | j+=(arr[i]-arr[i+1])/k; 23 | arr[i+1]+=((arr[i]-arr[i+1])/k)*k; 24 | 25 | } 26 | } 27 | } 28 | 29 | cout<= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | int main() 11 | { std::ios::sync_with_stdio(false); 12 | 13 | ll t=1,n,m; 14 | n=get(); 15 | m=floor((-1+sqrt(8*n-7))/2); 16 | cout<>t; 8 | int a[2]={0}; 9 | 10 | while(t--) { 11 | cin>>w; 12 | if (w==5) 13 | { 14 | a[1]++; 15 | } 16 | else 17 | a[0]++; 18 | } 19 | 20 | 21 | if(a[0]==0) 22 | cout<<-1; 23 | else if (a[1]<3||( a[1]-a[1]%9)<=0) 24 | { 25 | cout<<0; 26 | } 27 | else 28 | { 29 | for (int i = 0; i < a[1]-a[1]%9; ++i) 30 | { 31 | cout<<5; 32 | } 33 | for (int i = 0; i < a[0]; ++i) 34 | { 35 | cout<<0; 36 | } 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Codeforces/(A) Joysticks.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | 4 | using namespace std; 5 | ll bb[25]; 6 | inline ll get(){ll c;ll n = 0;while ((c = getchar()) >= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | 11 | int f(int a,int b) 12 | { 13 | if (a<=0||b<=0) 14 | { 15 | return 0; 16 | } 17 | if (a>b) 18 | { 19 | return 1+f(a-2,b+1); 20 | } 21 | else 22 | return 1+f(a+1,b-2); 23 | } 24 | int main() 25 | { std::ios::sync_with_stdio(false); 26 | 27 | int t=1,m,n; 28 | m=get(),n=get(); 29 | if(m==1&&n==1)cout<<0; 30 | else 31 | cout<>n>>m; 8 | vector v,k; 9 | for (int i = 0; i < n; ++i) 10 | { 11 | cin>>z; 12 | k.push_back(z); 13 | v.push_back(i+1); 14 | } 15 | for (unsigned int i = 0; i < v.size(); ++i) 16 | { 17 | if( k[i]>m) 18 | {k.push_back(k[i]-m); 19 | v.push_back(v[i]); 20 | } 21 | } 22 | 23 | cout<>t; 7 | for (int i = 0; i < t; ++i) 8 | { 9 | cin>>arr[i]; 10 | } 11 | 12 | for (int i = 0; i < t-1; ++i) 13 | { 14 | if(arr[i]<=arr[i+1]) 15 | { 16 | count++; 17 | } 18 | else 19 | count=0; 20 | ans=max(count,ans); 21 | } 22 | cout<>a>>b; 12 | A=a-'a'; 13 | B=b-'0'-1; 14 | int count=0; 15 | for (int i = 0; i < 8; ++i) 16 | { 17 | if ((A+arr[i][0])>=0 && (B+arr[i][1])>=0 && (A+arr[i][0]<8 )&& B+arr[i][1]<8) 18 | { 19 | count++; 20 | } 21 | 22 | } 23 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | ll bb[25]; 13 | inline void coutt(ll ans) 14 | { 15 | 16 | if(!ans){ 17 | putchar('0'); 18 | return; 19 | } 20 | ll k = 0; 21 | while(ans) 22 | { 23 | bb[k] = char((ans%10)+'0'); 24 | ++k; 25 | ans/=10; 26 | } 27 | for(int i=k-1;i>=0;--i) 28 | putchar(bb[i]); 29 | } 30 | int main() 31 | { std::ios::sync_with_stdio(false); 32 | 33 | int t=1,n; 34 | t=get(); 35 | n=get(); 36 | for (int i = 0; i < t; ++i) 37 | { 38 | for (int j = 0; j < t; ++j) 39 | { 40 | i==j?coutt(n):coutt(0); 41 | putchar(' '); 42 | } 43 | putchar('\n'); 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Codeforces/(A) Link-Cut tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios::sync_with_stdio(false); 7 | long long int t,i,j,temp=1; 8 | cin>>t>>i>>j; 9 | int flag=0; 10 | while(temp<=i) 11 | { 12 | if (temp>=t&&temp<=i) 13 | { 14 | flag=1; 15 | cout<1e18)break; 18 | temp=temp*j; 19 | 20 | } 21 | if (flag==0) 22 | { 23 | cout<<-1; 24 | } 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Codeforces/(A) Little Artem and Presents.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | inline ll get() 5 | { 6 | ll c; 7 | ll n = 0; 8 | while ((c = getchar()) >= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | ll bb[25]; 13 | inline void coutt(ll ans) 14 | { 15 | 16 | if(!ans){ 17 | putchar('0'); 18 | return; 19 | } 20 | ll k = 0; 21 | while(ans) 22 | { 23 | bb[k] = char((ans%10)+'0'); 24 | ++k; 25 | ans/=10; 26 | } 27 | for(int i=k-1;i>=0;--i) 28 | putchar(bb[i]); 29 | } 30 | int main() 31 | { std::ios::sync_with_stdio(false); 32 | 33 | int t=1,n; 34 | t=get(); 35 | cout<<2*(t/3)+(t%3>0); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Codeforces/(A) Little Elephant and Chess.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | #include "math.h" 3 | #include 4 | #include 5 | #include "process.h" 6 | using namespace std; 7 | int main() 8 | { 9 | string s[8]; 10 | int i,j; 11 | for (int i = 0; i < 8; ++i) 12 | { 13 | cin>>s[i]; 14 | } 15 | for ( i = 0; i < 8; ++i) 16 | { 17 | for ( j = 0; j < 7; ++j) 18 | { 19 | if(s[i][j]==s[i][j+1]) 20 | { 21 | cout<<"NO"; 22 | goto up; 23 | } 24 | } 25 | } 26 | up: 27 | if(i==8&&j==7) 28 | cout<<"YES"; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Codeforces/(A) Olesya and Rodion.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | 4 | using namespace std; 5 | ll bb[25]; 6 | inline ll get(){ll c;ll n = 0;while ((c = getchar()) >= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | int main() 10 | { std::ios::sync_with_stdio(false); 11 | 12 | int t=1,n,m; 13 | n=get();m=get(); 14 | if (n==1&&m==10) 15 | {/* code */ 16 | printf("-1\n"); 17 | } 18 | else 19 | { 20 | if(m==10) 21 | printf("1"); 22 | else 23 | printf("%d",m); 24 | 25 | for (int i = 1; i < n; ++i) 26 | { 27 | printf("0"); 28 | } 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Codeforces/(A) Parallelepiped.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ios::sync_with_stdio(0); cin.tie(0); 8 | float m,n,q; 9 | cin>>m>>n>>q; 10 | float temp=m/n*q; 11 | temp=sqrt(temp); 12 | float a,b,c; 13 | a=temp; 14 | b=m/temp; 15 | c=n/b; 16 | 17 | cout<<4*(a+b+c); 18 | 19 | 20 | 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Codeforces/(A) Plug-in.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int main(int argc, char const *argv[]) 4 | { 5 | std::ios::sync_with_stdio(false); 6 | char ch; 7 | char arr[200001]; 8 | int i=0; 9 | while((ch=getchar())!='\n') 10 | { 11 | if (i==0) 12 | { 13 | arr[i]=ch; 14 | i++; 15 | } 16 | else 17 | { 18 | if (ch==arr[i-1]) 19 | { 20 | i--; 21 | } 22 | else 23 | arr[i]=ch,i++; 24 | } 25 | } 26 | arr[i]='\0'; 27 | cout<>t; 8 | vectorv; 9 | int max=0,sum=0,count=0; 10 | for (int i = 0; i < t; ++i) 11 | { 12 | cin>>w; 13 | v.push_back(w); 14 | } 15 | for (int i = 0; i < v.size(); ++i) 16 | { 17 | if(sum>0||v[i]>0) 18 | sum+=v[i]; 19 | else{ 20 | if(sum<=0&&v[i]==-1) 21 | count++; 22 | } 23 | 24 | } 25 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main(int argc, char const *argv[]) 8 | { 9 | 10 | int m,n; 11 | cin>>m>>n; 12 | int arr[10000]; 13 | for (int i = 0; i < n; ++i) 14 | { 15 | cin>>arr[i]; 16 | } 17 | 18 | sort(arr,arr+n); 19 | 20 | int ans=10000; 21 | for (int i = 0; i+m-1 < n; ++i) 22 | { 23 | ans=min(ans,arr[i+m-1]-arr[i]); 24 | } 25 | 26 | 27 | cout<= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | int main() 11 | { std::ios::sync_with_stdio(false); 12 | 13 | int t=1,n; 14 | 15 | while(t--) 16 | { 17 | int m,n,o; 18 | scanf("%d%d%d",&m,&n,&o); 19 | 20 | if ((m+n+o)%m==0) 21 | { 22 | cout<>t; 13 | arr[t]=1; 14 | cin>>x>>y; 15 | swap(arr[x],arr[y]); 16 | cin>>x>>y; 17 | swap(arr[x],arr[y]); 18 | cin>>x>>y; 19 | swap(arr[x],arr[y]); 20 | for (int i = 1; i <= 4; ++i) 21 | { 22 | if (arr[i]) 23 | { 24 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | int main() 13 | { std::ios::sync_with_stdio(false); 14 | 15 | ll t; 16 | t=get(); 17 | int arr[10000]; 18 | for (int i = 0; i < 10000; ++i) 19 | { 20 | arr[i]=1; 21 | } 22 | int k=0,i=0,sum=0; 23 | 24 | for (int j=0;j!=t;j++) 25 | { 26 | while(i>0&&arr[i]==arr[i-1]) 27 | { 28 | arr[i-1]=arr[i-1]+1; 29 | arr[i]=1; 30 | i--; 31 | } 32 | i++; 33 | } 34 | for (int j = 0; j < i; ++j) 35 | { 36 | cout< 4 | #include "stdio.h" 5 | #include 6 | using namespace std; 7 | int main() 8 | { 9 | char str[100]; 10 | int t,j=0; 11 | char k,h='`'; 12 | cin>>t; 13 | cin>>str; 14 | for (int i = 0; i < t; ++i) 15 | { 16 | k=str[i]; 17 | 18 | if(k!=h) 19 | j++; 20 | h=k; 21 | } 22 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | 13 | ll f(ll x,ll y) 14 | { 15 | if (x%y!=0) 16 | { 17 | return (x/y +f(y,x%y)); 18 | } 19 | else 20 | return x/y; 21 | } 22 | 23 | int main() 24 | { std::ios::sync_with_stdio(false); 25 | 26 | int t,x,y; 27 | t=get(); 28 | while(t--) 29 | { 30 | 31 | x=get(),y=get(); 32 | if (x>=y) 33 | { 34 | cout<>a[i][j]; 13 | } 14 | } 15 | 16 | for (int i = 0; i < 3; ++i) 17 | { 18 | if(a[0][i]=='.') 19 | count++; 20 | if(a[2][i]=='.') 21 | count2++; 22 | if (a[i][0]=='.') 23 | { 24 | count3++; 25 | } 26 | if (a[i][2]=='.') 27 | { 28 | count4++; 29 | } 30 | } 31 | if(count==count2&&count3==count4) 32 | cout<<"YES"; 33 | else 34 | cout<<"NO"; 35 | } 36 | -------------------------------------------------------------------------------- /Codeforces/(A) System Administrator.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "bits/stdc++.h" 3 | 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | int m,n,sum=0,k; 12 | int x1=0,y1=0,x2=0,y2=0; 13 | while(t--) { 14 | cin>>k>>m>>n; 15 | if (k==1) 16 | { 17 | x1++; 18 | y1+=m; 19 | } 20 | else 21 | { 22 | x2++; 23 | y2+=m; 24 | } 25 | 26 | 27 | } 28 | (x1*5<=y1)?cout<<"LIVE\n":cout<<"DEAD\n"; 29 | (x2*5<=y2)?cout<<"LIVE":cout<<"DEAD"; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Codeforces/(A) Theatre Square.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | #include "math.h" 3 | using namespace std; 4 | int main() 5 | { 6 | long double a,b,c; 7 | unsigned long long int q; 8 | cin>>a>>b>>c; 9 | q=ceil(a/c)*ceil(b/c); 10 | cout<>A>>B; 8 | A.append(B); 9 | int i, x=A.length(); 10 | 11 | for ( i = 0; i 2 | using namespace std; 3 | #define ll long long int 4 | int main() 5 | { 6 | ios::sync_with_stdio(0); 7 | ll t,m,n; 8 | cin>>t; 9 | while(t--) 10 | { 11 | cin>>m; 12 | n=m*(m+1)/2; 13 | for(int i=0;i<63;i++) 14 | { 15 | if((1LL< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | int t,k,d,sum=0,count=0; 9 | cin>>t; 10 | vectorv; 11 | for (int i = 0; i < t; ++i) 12 | { 13 | cin>>k; 14 | sum+=k; 15 | v.push_back(k); 16 | } 17 | sort(v.begin(),v.end()); 18 | int sum1=0; 19 | for (int i = 0; i < t; ++i) 20 | { 21 | if((sum)>=sum1) 22 | { 23 | count++; 24 | sum1+=v[t-i-1]; 25 | sum-=v[t-i-1]; 26 | } 27 | else 28 | break; 29 | } 30 | cout<>t; 8 | if(t>2) 9 | if(t&1) 10 | cout<<"NO"; 11 | else 12 | cout<<"YES"; 13 | else 14 | cout<<"NO"; 15 | } 16 | -------------------------------------------------------------------------------- /Codeforces/(A) Way Too Long Words.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | #include "string.h" 3 | using namespace std; 4 | int main(int argc, char const *argv[]) 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | char str[1000]; 11 | cin>>str; 12 | if(strlen(str)>10) 13 | { 14 | cout<96) 14 | { 15 | str[0]-=32; 16 | } 17 | } 18 | printf("%s\n",str); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Codeforces/(B) A Trivial Problem.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | 4 | using namespace std; 5 | ll bb[25]; 6 | inline ll get(){ll c;ll n = 0;while ((c = getchar()) >= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | ll num(ll x) 11 | { 12 | int count=0; 13 | for (int i = 5; x/i; i*=5) 14 | { 15 | count+=x/i; 16 | } 17 | return count; 18 | } 19 | 20 | int main() 21 | { std::ios::sync_with_stdio(false); 22 | 23 | 24 | int z; 25 | std::vector v; 26 | 27 | cin>>z; 28 | for (int i = 0; i <=500000; ++i) 29 | { 30 | int y=num(i); 31 | if (y==z) 32 | { 33 | v.push_back(i); 34 | } 35 | } 36 | cout< rhs.n; } 10 | 11 | int main(int argc, char const *argv[]) 12 | { 13 | int q,t; 14 | long long int ans=0; 15 | 16 | scanf("%d%d",&q,&t); 17 | 18 | for (int i = 0; i < t; ++i) 19 | { 20 | scanf("%d%d",&mn[i].m,&mn[i].n); 21 | } 22 | 23 | sort(mn, mn+t,acompare); 24 | 25 | for (int i = 0; i0; ++i) 26 | { 27 | if (q>=mn[i].m) 28 | { 29 | ans+=mn[i].m*mn[i].n; 30 | q=q-mn[i].m; 31 | } 32 | else 33 | { 34 | 35 | ans+=q*mn[i].n; 36 | q=q-mn[i].m; 37 | } 38 | } 39 | 40 | cout<>n>>m; 12 | ll ans=n*n; 13 | for (int i = 0; i < m; ++i) 14 | { 15 | cin>>p>>q; 16 | if (row[p]==0&&col[q]==0) 17 | { 18 | row[p]=1,col[q]=1; 19 | ans=ans-(2*n-1)+r+c; 20 | r++,c++; 21 | //k=k+2; 22 | //r++,c++; 23 | } 24 | else if (row[p]==0&&col[q]==1) 25 | { 26 | row[p]=1; 27 | ans=ans-(n)+c; 28 | r++; 29 | } 30 | else if(col[q]==0&&row[p]==1) 31 | { 32 | col[q]=1; 33 | ans=ans-(n)+r; 34 | c++; 35 | } 36 | 37 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | 13 | int main() 14 | { 15 | int n,s=0; 16 | n=get(); 17 | int a[102]; 18 | for(int i=0;i= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | 13 | int main() 14 | { std::ios::sync_with_stdio(false); 15 | 16 | int t=1,n,k; 17 | ll sum=0,z; 18 | n=get(),k=get(),t=get(); 19 | sum=n/3+k/3+t/3; 20 | if(n&&k&&t) 21 | { 22 | z=(n-1)/3+(k-1)/3+(t-1)/3+1; 23 | sum=max(z,sum); 24 | } 25 | if(n&&k&&t) 26 | { 27 | z=(n-2)/3+(k-2)/3+(t-2)/3+2; 28 | sum=max(z,sum); 29 | } 30 | cout<= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | int main(){ 11 | std::ios::sync_with_stdio(false); 12 | ll n; 13 | std::vector v; 14 | cin>>n; 15 | if(n==1){ 16 | putchar('1'); 17 | return 0; 18 | } 19 | cout<1; --i) 22 | { 23 | if(n%i==0) 24 | { 25 | v.push_back(i); 26 | n=i; 27 | } 28 | } 29 | //sort(v.begin(),v.end()); 30 | for (int i = 0; i < v.size(); ++i) 31 | { 32 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | int main() 13 | { std::ios::sync_with_stdio(false); 14 | 15 | int m,n,min=INT_MAX,max=INT_MIN,x; 16 | m=get();n=get(); 17 | for (int i = 0; i < m; ++i) 18 | { 19 | min=INT_MAX; 20 | for (int j = 0; j < n; ++j) 21 | { 22 | x=get(); 23 | if (x= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | ll bb[25]; 13 | inline void coutt(ll ans) 14 | { 15 | 16 | if(!ans){ 17 | putchar('0'); 18 | return; 19 | } 20 | ll k = 0; 21 | while(ans) 22 | { 23 | bb[k] = char((ans%10)+'0'); 24 | ++k; 25 | ans/=10; 26 | } 27 | for(int i=k-1;i>=0;--i) 28 | putchar(bb[i]); 29 | } 30 | int main() 31 | {// std::ios::sync_with_stdio(false); 32 | 33 | int arr[100005],k; 34 | ll t=1,n,m; 35 | k=get(); 36 | n=get(); 37 | for (int i = 0; i < k; ++i) 38 | { 39 | cin>>arr[i]; 40 | } 41 | m=floor((-1+sqrt(8*n-7))/2); 42 | int ans=n-m*(m+1)/2; 43 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | 13 | using namespace std; 14 | int main() 15 | { std::ios::sync_with_stdio(false); 16 | 17 | int arr[10]={0}; 18 | arr[0]=6; 19 | arr[1]=2; 20 | arr[2]=5; 21 | arr[3]=5; 22 | arr[4]=4; 23 | arr[5]=5; 24 | arr[6]=6; 25 | arr[7]=3; 26 | arr[8]=7; 27 | arr[9]=6; 28 | int a,b,t; 29 | ll sum=0; 30 | a=get(),b=get(); 31 | for (int i = a; i<=b; ++i) 32 | { 33 | t=i; 34 | while(t) 35 | { 36 | sum+=arr[t%10]; 37 | t=t/10; 38 | } 39 | 40 | } 41 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int t,w,x,arr[100000],i; 6 | char str[100000]; 7 | scanf("%s",str); 8 | w=strlen(str); 9 | for(i=1;i<=w;i++) 10 | { 11 | if(str[i]==str[i-1]) 12 | { 13 | arr[i]=arr[i-1]+1; 14 | } 15 | else 16 | { 17 | arr[i]=arr[i-1]; 18 | } 19 | } 20 | 21 | scanf("%d",&t); 22 | 23 | while(t--) 24 | { 25 | 26 | scanf("%d %d",&w,&x); 27 | printf("%d \n",arr[x-1]-arr[w-1]); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Codeforces/(B) Moore's Law.py: -------------------------------------------------------------------------------- 1 | from decimal import * 2 | getcontext().prec = 28 3 | a = map(float,raw_input().split(' ')) 4 | print "%.28f"%((a[0])*(1.000000011)**a[1]) 5 | -------------------------------------------------------------------------------- /Codeforces/(B) Optimal Point on a Line.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | int arr[300005]; 5 | int main() 6 | { 7 | ios::sync_with_stdio(0); cin.tie(0); 8 | int t; 9 | cin>>t; 10 | 11 | for (int i = 0; i < t; ++i) 12 | { 13 | cin>>arr[i]; 14 | } 15 | sort(arr,arr+t); 16 | if (t%2==0) 17 | { 18 | cout< 2 | using namespace std; 3 | int x,y; 4 | int arr[20][100]; 5 | int pow2[12]={0}; 6 | void solve(int level,int i,int j) 7 | { 8 | if (level>x) 9 | { 10 | return ; 11 | } 12 | if (pow2[level]==arr[i][j]) 13 | { 14 | 15 | solve(level+1,i+1,j); 16 | solve(level+1,i+1,j+1); 17 | //arr[i+1][j]++; 18 | //arr[i+1][j+1]++; 19 | } 20 | else 21 | { 22 | arr[i][j]++; 23 | } 24 | } 25 | int main() 26 | { 27 | int count=0; 28 | for (int i = 0; i <=10; ++i) 29 | { 30 | pow2[i]=pow(2,i); 31 | } 32 | cin>>x>>y; 33 | for (int i = 0; i < y; ++i) 34 | { 35 | solve(0,0,0); 36 | } 37 | for (int i = 0; i < x; ++i) 38 | { 39 | for (int j=0; j <=i; ++j) 40 | { 41 | if (arr[i][j]==pow2[i]) 42 | { 43 | count++; 44 | } 45 | } 46 | } 47 | /*for (int i = 0; i < x; ++i) 48 | { 49 | for (int j=0; j <=i; ++j) 50 | { 51 | cout<>t; 12 | while(t) 13 | { 14 | arr[i++]=t%10; 15 | t=t/10; 16 | } 17 | std::vector v; 18 | 19 | for (;;) 20 | { 21 | int flag=0,x=0; 22 | for (int k = 0; k < i; ++k) 23 | { 24 | if (arr[k]>0) 25 | { 26 | x+=power(10,k); 27 | arr[k]--; 28 | flag++; 29 | } 30 | 31 | } 32 | if(x>0) 33 | v.push_back(x); 34 | if (flag==0) 35 | { 36 | break; 37 | } 38 | } 39 | sort(v.begin(),v.end()); 40 | printf("%d\n",v.size()); 41 | for (int i = 0; i < v.size(); ++i) 42 | { 43 | printf("%d ",v[i]); 44 | } 45 | 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Codeforces/(B) Spider Man.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ios::sync_with_stdio(0); cin.tie(0); 8 | ll t,q,sum=0; 9 | cin>>t; 10 | for (int i = 0; i < t; ++i) 11 | { 12 | cin>>q; 13 | sum+=q-1; 14 | if (sum%2) 15 | { 16 | cout<<1<= '0' && c <= '9') 9 | { n += c - '0';i++;} 10 | 11 | return i==1?0:n; 12 | } 13 | 14 | int t(int x) 15 | { 16 | if (x==0) 17 | { 18 | return 0; 19 | } 20 | if (x<10) 21 | return 1; 22 | 23 | int w=0,k=0; 24 | while(x){ 25 | w=w+x%10; 26 | x=x/10; 27 | } 28 | return 1+t(w); 29 | } 30 | 31 | int main() 32 | { std::ios::sync_with_stdio(false); 33 | 34 | ll x; 35 | x=get(); 36 | 37 | cout<= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | int main() 11 | { std::ios::sync_with_stdio(false); 12 | 13 | int m,n,p; 14 | m=get(),n=get(),p=get(); 15 | for (int i = 0; i < p; ++i) 16 | { 17 | n++; 18 | if (n>59) 19 | { 20 | n=n-60; 21 | m++; 22 | if (m>23) 23 | { 24 | m=m-24; 25 | } 26 | } 27 | } 28 | printf("%.2d:%.2d\n",m,n); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Codeforces/(B) Unary.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | #define mod 1000003 4 | using namespace std; 5 | ll t(ll x) 6 | { 7 | if (x==0) 8 | { 9 | return 1; 10 | } 11 | if (x%2==0) 12 | { 13 | ll y=t(x/2); 14 | return (y*y)%mod; 15 | } 16 | else 17 | return (2*t(x-1))%mod; 18 | } 19 | 20 | int main(int argc, char const *argv[]) 21 | { 22 | string str,m; 23 | cin>>str; 24 | std::map map; 25 | map['>']="1000"; 26 | map['<']="1001"; 27 | map['+']="1010"; 28 | map['-']="1011"; 29 | map['.']="1100"; 30 | map[',']="1101"; 31 | map['[']="1110"; 32 | map[']']="1111"; 33 | 34 | for (int i = 0; str[i]; ++i) 35 | { 36 | m.append(map[str[i]]); 37 | } 38 | int x=m.length(); 39 | ll sum=0; 40 | 41 | for (int i=0 ; i= '0' && c <= '9')n = (n << 3) + ( n << 1 ) + c - '0';return n;} 7 | 8 | inline void coutt(ll ans){if(!ans){putchar('0');return;}ll k = 0;while(ans){bb[k] = char((ans%10)+'0');++k;ans/=10;}for(int i=k-1;i>=0;--i)putchar(bb[i]);} 9 | 10 | int main() 11 | { std::ios::sync_with_stdio(false); 12 | 13 | int t=1,n,arr[10000]; 14 | 15 | while(t--) 16 | { 17 | n=get(); 18 | for (int i = 0; i < n; ++i) 19 | { 20 | arr[i]=get(); 21 | } 22 | sort(arr,arr+n); 23 | int i,j; 24 | for ( i = 0,j=n-1;i>m>>n; 10 | m=min(m,n)+1; 11 | cout< > v; 9 | int t,m,n; 10 | cin>>t; 11 | for (int i = 0; i < t; ++i) 12 | { 13 | cin>>m>>n; 14 | v.push_back(make_pair(m,n)); 15 | } 16 | sort(v.begin(),v.end()); 17 | int ans=0; 18 | for (int i = 0; i < t; ++i) 19 | { 20 | if (ans<=v[i].second) 21 | { 22 | ans=v[i].second; 23 | } 24 | else 25 | ans=v[i].first; 26 | } 27 | cout< > v; 10 | int arr[100000]={0}; 11 | cin>>m>>n; 12 | for (int i = 1; i <=m; ++i) 13 | { 14 | for (int j=i+1; j2*n||arr[j%m]>2*n) 19 | { 20 | cout<<-1; 21 | return 0; 22 | } 23 | } 24 | } 25 | ll sum=0; 26 | if (n > (m-1)/2) 27 | { 28 | cout<<-1; 29 | return 0; 30 | } 31 | cout<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | ll bb[25]; 13 | inline void coutt(ll ans) 14 | { 15 | 16 | if(!ans){ 17 | putchar('0'); 18 | return; 19 | } 20 | ll k = 0; 21 | while(ans) 22 | { 23 | bb[k] = char((ans%10)+'0'); 24 | ++k; 25 | ans/=10; 26 | } 27 | for(int i=k-1;i>=0;--i) 28 | putchar(bb[i]); 29 | } 30 | int main() 31 | { std::ios::sync_with_stdio(false); 32 | 33 | int t=1; 34 | int a[7]; 35 | for (int i = 0; i < 6; ++i) 36 | { 37 | a[i]=get(); 38 | } 39 | int x=a[0]+a[1]+a[2]; 40 | coutt((ll)(x*x-a[0]*a[0]-a[2]*a[2]-a[4]*a[4])); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Codeforces/(C) Lucky Numbers.py: -------------------------------------------------------------------------------- 1 | a=int(input()) 2 | print (2**(a+1)-2) 3 | -------------------------------------------------------------------------------- /Codeforces/(C) Number of Ways.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll __int64 4 | int k[500010]; 5 | int main() 6 | { 7 | vectorv; 8 | int t,z,i,j; 9 | ll sum=0,count=0; 10 | scanf("%d",&t); 11 | 12 | for( i=0;i0;i--) 22 | { 23 | temp+=v[i]; 24 | k[i]=(temp==sum)?k[i+1]+1:k[i+1]; 25 | } 26 | temp=0; 27 | 28 | for (int i = 0; i < t; ++i) 29 | { 30 | temp+=v[i]; 31 | if (temp==sum) 32 | { 33 | count+=k[i+2]; 34 | } 35 | } 36 | printf("%I64d\n",count); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Codeforces/(C) Registration System.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | main() 4 | { std::map key; 5 | char str[33];int t,x; 6 | for(scanf("%d",&t);t--;scanf("%s",str),x=0,(x=++key[str])==1? printf("OK\n"):printf("%s%d\n",str,x-1)); 7 | } 8 | -------------------------------------------------------------------------------- /Codeforces/(C) Stripe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | vectorv; 6 | int t,z,i; 7 | long long sum=0,count=0; 8 | scanf("%d",&t); 9 | 10 | for( i=0;i>arr[0]>>arr[1]>>arr[2]; 10 | sort(arr,arr+3); 11 | ll ans = (arr[0]+arr[1]+arr[2])/3; 12 | if (arr[0]+arr[1]>n>>m; 10 | if (n>m+1 || m > 2*n+2) 11 | { 12 | return 0*puts("-1"); 13 | } 14 | while(m>0&&n>0&&m>n) 15 | { 16 | printf("110"); 17 | m=m-2; n--; 18 | } 19 | if (n>m) 20 | { 21 | printf("0"); 22 | n--; 23 | } 24 | while(m&&n) 25 | { 26 | printf("10"); 27 | m--; 28 | n--; 29 | } 30 | while(m--)printf("1"); 31 | 32 | 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Codeforces/(C) The World is a Theatre.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define LL __int64 3 | #include "bits/stdc++.h" 4 | 5 | #define mod 1000000007 6 | using namespace std; 7 | 8 | LL arr[300][300]; 9 | 10 | void precal() 11 | { 12 | arr[0][0]=1; 13 | for (int i = 1; i <32; ++i) 14 | { 15 | arr[i][0]=1; 16 | for (int j = 1; j<=i; ++j) 17 | { 18 | 19 | arr[i][j]=(arr[i-1][j]+arr[i-1][j-1]); 20 | } 21 | } 22 | } 23 | 24 | int main(int argc, char const *argv[]) 25 | { 26 | precal(); 27 | int t,m,n; 28 | scanf("%d%d%d",&t,&m,&n); 29 | 30 | LL sum=0; 31 | for (int i = 4; i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int n,m,k; 7 | cin>>n>>m>>k; 8 | int r=1,c=1,num; 9 | for(int i=1; i<=k; i++){ 10 | if(i==k){ 11 | num = n*m - 2*(k-1); 12 | printf("%d ", num); 13 | } 14 | else{ 15 | num = 2; 16 | printf("2 "); 17 | } 18 | while(num--){ 19 | if(r%2){ 20 | printf("%d %d ",r,c); 21 | c++; 22 | if(c==m+1){ 23 | c=m; 24 | r++; 25 | } 26 | } 27 | else{ 28 | printf("%d %d ",r, c); 29 | c--; 30 | if(c==0){ 31 | c=1; 32 | r++; 33 | } 34 | } 35 | } 36 | printf("\n"); 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Codeforces/(C) Watchmen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | maparr,arr1; 7 | map,int>zx; 8 | int n,x,y; 9 | cin>>n; 10 | for(int i=0;i>x>>y; 13 | arr[x]++,arr1[y]++; 14 | zx[make_pair(x,y)]++; 15 | } 16 | long long sum=0; 17 | map::iterator it; 18 | for(it=arr.begin();it!=arr.end();it++) 19 | { 20 | if(it->second>0) 21 | { 22 | long long z=it->second; 23 | sum+=(z)*(z-1)/2; 24 | } 25 | } 26 | for(it=arr1.begin();it!=arr1.end();it++) 27 | { 28 | if(it->second>0) 29 | { 30 | long long z=it->second; 31 | sum+=(z)*(z-1)/2; 32 | } 33 | } 34 | // cout<,int> ::iterator xq; 36 | for(xq=zx.begin();xq!=zx.end();xq++) 37 | if((xq->second)>0) 38 | sum-=(long long)(xq->second)*(xq->second-1)/2; 39 | 40 | cout<=0) 14 | ans[i]=(ans[i]+ans[i-j])%mod; 15 | } 16 | } 17 | return ans[n]; 18 | } 19 | int main() 20 | { 21 | ios::sync_with_stdio(0); cin.tie(0); 22 | 23 | cin>>n>>k>>d; 24 | cout<<(f(n,k)-f(n,d-1)+mod)%mod; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Codeforces/(D) Flowers.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | const ll mod=1000000007; 5 | int main() 6 | { 7 | ios::sync_with_stdio(0); cin.tie(0); 8 | int t,k; 9 | cin>>t>>k; 10 | ll dp[100005]={0}; 11 | ll sum[100005]={0}; 12 | dp[0]=1; 13 | for (int i = 0; i < 100005; ++i) 14 | { 15 | if (i-k>=0) 16 | { 17 | dp[i]=(dp[i-1]+dp[i-k]+mod)%mod; 18 | } 19 | else 20 | dp[i]=1; 21 | } 22 | for (int i = 1; i < 100005; ++i) 23 | { 24 | sum[i]=(sum[i-1]+dp[i-1]+mod)%mod; 25 | } 26 | 27 | int m,n; 28 | for (int i = 0; i < t; ++i) 29 | { 30 | cin>>m>>n; 31 | cout<<(sum[n+1]-sum[m]+mod)%mod<= '0' && c <= '9') 9 | n = (n << 3) + ( n << 1 ) + c - '0'; 10 | return n; 11 | } 12 | 13 | int main() 14 | { std::ios::sync_with_stdio(false); 15 | 16 | int t=1,n,sum=0,count=0,arr[100005]; 17 | t=get(); 18 | 19 | for (int i = 0; i < t; ++i) 20 | { 21 | arr[i]=get(); 22 | } 23 | sort(arr,arr+t); 24 | for (int i = 0; i < t; ++i) 25 | { 26 | if (sum>arr[i]) 27 | { 28 | count++; 29 | } 30 | else 31 | sum+=arr[i]; 32 | 33 | } 34 | cout< 2 | using namespace std; 3 | #define ll __int64 4 | ll arr[2*1000*1000*10+7]; 5 | int main() 6 | { 7 | ll n; 8 | ll x,y; 9 | cin>>n>>x>>y; 10 | arr[1]=x; 11 | arr[2*1]=y; 12 | for(ll i=2;i<=n;i++) 13 | { 14 | arr[2*(i-1)]=arr[i-1]+y; 15 | ll j=2*(i-1); 16 | for (ll k=0;k<10; ++k) 17 | { 18 | if(j-k >= 0) 19 | { 20 | if(arr[j-k] > 0 ) 21 | arr[j-k] = min(arr[j-k],arr[j]+k*x); 22 | else 23 | { 24 | arr[j-k]=arr[j]+k*x; 25 | } 26 | } 27 | else 28 | { 29 | break; 30 | } 31 | } 32 | 33 | if(arr[i]>0) 34 | arr[i]=min(arr[i],arr[i-1]+x); 35 | else 36 | { 37 | arr[i]=arr[i-1]+x; 38 | } 39 | 40 | } 41 | 42 | 43 | cout<= '0' && c <= '9') 11 | n = (n << 3) + ( n << 1 ) + c - '0'; 12 | return n; 13 | } 14 | 15 | 16 | void pre() 17 | { 18 | arr[0][0]=1; 19 | for (int i = 1; i <800; ++i) 20 | { 21 | arr[i][0]=1; 22 | for (int j = 1; j <10; ++j) 23 | { 24 | arr[i][j]=(arr[i-1][j-1]+arr[i-1][j]); 25 | } 26 | } 27 | } 28 | int main(){ 29 | pre(); 30 | ll t,ans=1; 31 | t=get(); 32 | cout<= '0' && c <= '9') 11 | n = (n << 3) + ( n << 1 ) + c - '0'; 12 | return n; 13 | } 14 | 15 | int main(){ 16 | ll t,ans=1; 17 | t=get(); 18 | cout<= '0' && c <= '9') 11 | n = (n << 3) + ( n << 1 ) + c - '0'; 12 | return n; 13 | } 14 | 15 | 16 | 17 | int main(){ 18 | 19 | double a,b,c,d,f,g; 20 | cin>>a>>b>>c; 21 | 22 | d=(double)sqrt((double)b*b-(double)4.0*a*c); 23 | f=(double)(-b+d)/((double)2.0*a); 24 | g=(double)(-b-d)/((double)2.0*a); 25 | 26 | if(f>g) 27 | { 28 | printf("%.20lf\n%.20lf",f,g); 29 | } 30 | else 31 | printf("%.20lf\n%.20lf",g,f); 32 | 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Codeforces/(R) Game.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | #define mod 10000 4 | ll arr[1000][1000]; 5 | using namespace std; 6 | inline ll get() 7 | { 8 | ll c; 9 | ll n = 0; 10 | while ((c = getchar()) >= '0' && c <= '9') 11 | n = (n << 3) + ( n << 1 ) + c - '0'; 12 | return n; 13 | } 14 | 15 | 16 | 17 | int main(){ 18 | 19 | ll t,ans=1,ans1=1; 20 | t=get(); 21 | t%2==0?cout<<2:cout<<1; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Codeforces/337A - Puzzles.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | #include "algorithm" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main(int argc, char const *argv[]) 8 | { 9 | 10 | int m,n; 11 | cin>>m>>n; 12 | int arr[10000]; 13 | for (int i = 0; i < n; ++i) 14 | { 15 | cin>>arr[i]; 16 | } 17 | 18 | sort(arr,arr+n); 19 | 20 | int ans=10000; 21 | for (int i = 0; i+m-1 < n; ++i) 22 | { 23 | ans=min(ans,arr[i+m-1]-arr[i]); 24 | } 25 | 26 | 27 | cout< 2 | void func(int *arr, int n, int m) 3 | { 4 | int i,j=0,flag = 0,max; 5 | max = arr[0]; 6 | for(i = 1 ; i max) 9 | { 10 | max = arr[i]; 11 | j = i; 12 | } 13 | } 14 | if(max == arr[0]) 15 | { 16 | for(i = 1; i0) 31 | --arr[j]; 32 | ++arr[0]; 33 | func(arr, n, m); 34 | } 35 | int main() 36 | { 37 | int n, i; 38 | scanf("%d",&n); 39 | int arr[n], max; 40 | for(i=0 ; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | 11 | 12 | int main() { 13 | 14 | int arr[10000],arr1[10000]; 15 | int t,i,count1=0,count2=0,sum=0; 16 | cin>>t; 17 | for(i=0;i>arr[i]>>arr1[i]; 20 | if(arr[i]==0) 21 | count1++; 22 | if(arr1[i]==0) 23 | count2++; 24 | 25 | } 26 | if(t-count1>count1) 27 | sum+=count1; 28 | else 29 | sum+=t-count1; 30 | 31 | if(t-count2>count2) 32 | sum+=count2; 33 | else 34 | sum+=t-count2; 35 | 36 | cout< 2 | int main() 3 | { 4 | int a[30][30],i,j,n,sum=0,sum1=0,row[30],col[30],count=0; 5 | scanf("%d",&n); 6 | for(i=0;i>t; 7 | for (int i = 0; i < t; ++i) 8 | { 9 | cin>>arr[i]; 10 | } 11 | 12 | for (int i = 0; i < t-1; ++i) 13 | { 14 | if(arr[i]<=arr[i+1]) 15 | { 16 | count++; 17 | } 18 | else 19 | count=0; 20 | ans=max(count,ans); 21 | } 22 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | int t,k,d,sum=0,count=0; 9 | cin>>t; 10 | vectorv; 11 | for (int i = 0; i < t; ++i) 12 | { 13 | cin>>k; 14 | sum+=k; 15 | v.push_back(k); 16 | } 17 | sort(v.begin(),v.end()); 18 | int sum1=0; 19 | for (int i = 0; i < t; ++i) 20 | { 21 | if((sum)>=sum1) 22 | { 23 | count++; 24 | sum1+=v[t-i-1]; 25 | sum-=v[t-i-1]; 26 | } 27 | else 28 | break; 29 | } 30 | cout<>t; 8 | while(t--) 9 | { 10 | char str[1000]; 11 | cin>>str; 12 | if(strlen(str)>10) 13 | { 14 | cout< 2 | 3 | polygonFilling 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /Computer-graphics/Polygon Drawing/polygonFilling/polygonFilling.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * polygonFilling.cpp 3 | * 4 | * Created on: 18-Mar-2015 5 | * Author: rupinder 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | #include "myHeaderPolygonFilling.h" 13 | 14 | 15 | int main(int argc,char** argv) 16 | { 17 | glutInit(&argc,argv); 18 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 19 | glutInitWindowSize(WindowSize,WindowSize); 20 | glutInitWindowPosition(750,80); 21 | glutCreateWindow("Polygon Filling Using Scan-Line"); 22 | initialize(); 23 | glClearColor(1,1,1,0); 24 | glClear(GL_COLOR_BUFFER_BIT); 25 | glFlush(); 26 | glutMouseFunc(mouse_normal); 27 | glutMotionFunc(mouse_motion); 28 | glutPassiveMotionFunc(mouse_passive); 29 | options(0); 30 | 31 | 32 | glutMainLoop(); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Computer-graphics/Scaling/Debug/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := -lGL -lGLU -lglut 8 | 9 | -------------------------------------------------------------------------------- /Computer-graphics/Scaling/Debug/scaling: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/Computer-graphics/Scaling/Debug/scaling -------------------------------------------------------------------------------- /Computer-graphics/Scaling/Debug/scaling.d: -------------------------------------------------------------------------------- 1 | scaling.d: ../scaling.cpp ../header.h 2 | 3 | ../header.h: 4 | -------------------------------------------------------------------------------- /Computer-graphics/Scaling/Debug/scaling.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/Computer-graphics/Scaling/Debug/scaling.o -------------------------------------------------------------------------------- /Computer-graphics/Scaling/Debug/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | O_SRCS := 6 | CPP_SRCS := 7 | C_UPPER_SRCS := 8 | C_SRCS := 9 | S_UPPER_SRCS := 10 | OBJ_SRCS := 11 | ASM_SRCS := 12 | CXX_SRCS := 13 | C++_SRCS := 14 | CC_SRCS := 15 | C++_DEPS := 16 | OBJS := 17 | C_DEPS := 18 | CC_DEPS := 19 | CPP_DEPS := 20 | EXECUTABLES := 21 | CXX_DEPS := 22 | C_UPPER_DEPS := 23 | 24 | # Every subdirectory with source files must be described here 25 | SUBDIRS := \ 26 | . \ 27 | 28 | -------------------------------------------------------------------------------- /Computer-graphics/Scaling/Debug/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | CPP_SRCS += \ 7 | ../scaling.cpp 8 | 9 | OBJS += \ 10 | ./scaling.o 11 | 12 | CPP_DEPS += \ 13 | ./scaling.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | %.o: ../%.cpp 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: Cross G++ Compiler' 20 | g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /Computer-graphics/Scaling/_project: -------------------------------------------------------------------------------- 1 | 2 | 3 | scaling 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /Computer-graphics/Scaling/scaling.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * scaling.cpp 3 | * 4 | * Created on: 12-Apr-2015 5 | * Author: goldy 6 | */ 7 | #include 8 | #include 9 | #include "header.h" 10 | 11 | int main(int argc,char **argv) 12 | { 13 | glutInit(&argc,argv); 14 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 15 | glutInitWindowSize(wsize,wsize); 16 | glutInitWindowPosition(500,50); 17 | glutCreateWindow("2D : Scaling"); 18 | myinit(); 19 | glutMouseFunc(mouse); 20 | glutPassiveMotionFunc(passiveMouse); 21 | 22 | glutCreateMenu(menu); 23 | glutAddMenuEntry("Scaling",1); 24 | glutAddMenuEntry("Translation",2); 25 | glutAddMenuEntry("Rotation",3); 26 | glutAddMenuEntry("Clear",4); 27 | glutAddMenuEntry("Exit",5); 28 | glutKeyboardFunc(keyboard); 29 | 30 | glutMainLoop(); 31 | return 0; 32 | } 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Computer-graphics/Tic Tac Toe/opengl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/Computer-graphics/Tic Tac Toe/opengl -------------------------------------------------------------------------------- /Computer-graphics/Tic Tac Toe/tictactoe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/Computer-graphics/Tic Tac Toe/tictactoe -------------------------------------------------------------------------------- /DFS(Vectors).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector graph[100]; 5 | int n,m,v,u; 6 | bool visited[100]; 7 | 8 | void DFS(int p) 9 | { 10 | for(int i = 0; i < n; i ++) 11 | visited[i] = false; 12 | stack s; 13 | s.push(p); 14 | while(!s.empty()) 15 | { 16 | int curr = s.top(); 17 | cout< dp; 7 | public: 8 | int numDecodings(string s) { 9 | S = s; 10 | dp.resize(s.size() + 1, -1); 11 | return go(0); 12 | } 13 | int go(int idx) { 14 | if (idx == S.size()) { 15 | return 1; 16 | } 17 | if (idx > S.size()) 18 | return 0; 19 | if (dp[idx] != -1) { 20 | return dp[idx]; 21 | } 22 | int first = S[idx] - '0'; 23 | int second = (idx == (int)S.size() - 1) ? -1 : (S[idx + 1] - '0'); 24 | 25 | int ans = 0; 26 | if (first > 0) 27 | ans += go(idx + 1); 28 | int num = first * 10 + second; 29 | if (second != -1 && num < 27 && (first > 0 && num > 0)) { 30 | ans += go(idx + 2); 31 | } 32 | return dp[idx] = ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /Diameter of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int d = 0; 4 | int ans(TreeNode *root){ 5 | if(root == NULL) 6 | return 0; 7 | int l = ans(root -> left ); 8 | int r = ans(root -> right); 9 | d = max(d , l + r); 10 | return 1 + max(l , r); 11 | } 12 | int diameterOfBinaryTree(TreeNode* root) { 13 | d = 0; 14 | ans(root); 15 | return d; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Disjoint-Sets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | #define mp make_pair 5 | #define pb push_back 6 | #define mt make_tuple 7 | #define LD long double 8 | #define gc getchar_unlocked 9 | #define pc putchar_unlocked 10 | #define MOD 1000000007 11 | #define MAXN 2*100005 12 | #define bitcount __builtin_popcount 13 | #define INF 2000000000 14 | #define EPS 1e-9 15 | 16 | templateT absll(T X) 17 | { 18 | if(X<0) 19 | return -1*X; 20 | else 21 | return X; 22 | } 23 | 24 | int id[MAXN],sz[MAXN]; 25 | int root(int p) 26 | { 27 | while(id[p]!=p) 28 | { 29 | p=id[p]; 30 | } 31 | 32 | return p; 33 | } 34 | 35 | void QuickUnion(int p,int q) 36 | { 37 | int m=root(p); 38 | int n=root(q); 39 | 40 | if(m==n) 41 | { 42 | printf("Invalid query!\n"); 43 | return; 44 | } 45 | 46 | if(sz[m]sz[n]) 51 | { 52 | id[n]=m; 53 | } 54 | } 55 | 56 | bool connected(int p,int q) 57 | { 58 | return root(p)==root(q); 59 | } 60 | -------------------------------------------------------------------------------- /DistinctPermutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | vector> permute(vector& nums) { 5 | vector v(nums.size(), 0); 6 | vector val; 7 | permuteit(nums, v , val); 8 | return ans; 9 | } 10 | void permuteit(vector& nums, vector& v, vector& val) { 11 | if (val.size() == nums.size()) { 12 | ans.push_back(val); 13 | return; 14 | } 15 | for (int i = 0; i < nums.size(); i++) { 16 | if (!v[i]) { 17 | val.push_back(nums[i]); 18 | v[i] = 1; 19 | permuteit(nums, v, val); 20 | v[i] = 0; 21 | val.pop_back(); 22 | } 23 | } 24 | } 25 | }; -------------------------------------------------------------------------------- /EulerTotientFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | #define mp make_pair 5 | #define pb push_back 6 | #define mt make_tuple 7 | #define LD long double 8 | #define gc getchar_unlocked 9 | #define pc putchar_unlocked 10 | #define MOD 1000000007 11 | #define MAXN 100005 12 | #define bitcount _builtin_popcount 13 | #define INF 2000000000 14 | #define EPS 1e-9 15 | 16 | templateT absll(T X) 17 | { 18 | if(X<0) 19 | return -1*X; 20 | else 21 | return X; 22 | } 23 | 24 | int phi[MAXN]; 25 | 26 | void precomputeETF() 27 | { 28 | phi[1]=1; 29 | 30 | for(int i=2;i next != NULL){ 5 | slow = slow -> next; 6 | fast = fast -> next -> next; 7 | } 8 | return slow -> data; 9 | } 10 | -------------------------------------------------------------------------------- /Hackerrank/A Basic Quiz on Algorithms #1.cpp: -------------------------------------------------------------------------------- 1 | 1:c 2 | 2:b 3 | 3:b 4 | 4:a 5 | 5:b 6 | 6:a 7 | 7:a 8 | 8:c 9 | 9:a 10 | 10:e 11 | -------------------------------------------------------------------------------- /Hackerrank/A Very Big Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | long int x,y=0,z,i,arr[11]; 11 | cin>>x; 12 | for(i=0;i>arr[i]; 15 | y=y+arr[i]; 16 | 17 | } 18 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | int n,t,k,sum=0,p=0,a[10000],i=0,j; 9 | 10 | scanf("%d",&t); 11 | 12 | 13 | for(j=0;j=k) 40 | 41 | printf("NO\n"); 42 | 43 | 44 | else if(sum 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int l,ext,i; 9 | char str[100000]; 10 | scanf("%d",&l); 11 | 12 | scanf("%s",str); 13 | 14 | scanf("%d",&ext); 15 | ext=ext%26; 16 | 17 | for(i=0;i=65 && str[i]<=90) 20 | {if(str[i]+ext>90) 21 | str[i]=str[i]+ext-26; 22 | else 23 | str[i]+=ext; 24 | } 25 | else if(str[i]>=97 && str[i]<=122) 26 | { 27 | if(str[i]+ext>122) 28 | str[i]=str[i]+ext-26; 29 | else 30 | str[i]=str[i]+ext; 31 | 32 | } 33 | 34 | } 35 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int n,i,j; 11 | cin>>n; 12 | char arr[101][101]; 13 | for(i=0;i>arr[i]; 15 | for(i=1;iarr[i-1][j]&&arr[i][j]>arr[i+1][j]&&arr[i][j]>arr[i][j-1]&&arr[i][j]>arr[i][j+1]) 19 | arr[i][j]='X'; 20 | 21 | } 22 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int y,i=10,loan,k,h; 11 | cin>>y; 12 | int n,c,m; 13 | for(i=0;i>n>>c>>m; 16 | k=n/c; 17 | h=k; 18 | do 19 | { 20 | loan=h%m; 21 | k+=(h/m); 22 | h=h/m+loan; 23 | 24 | } 25 | 26 | while(h>=m); 27 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | 11 | int n,arr[1000000],result=0,count=0,i; 12 | cin>>n; 13 | for( i=0;i>arr[i]; 16 | result=result^arr[i]; 17 | } 18 | 19 | for(i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int part(int *arr,int a,int b) 6 | { 7 | int q=arr[b],j=a,i; 8 | 9 | for(i=a;i>x; 45 | for(int i=0;i>arr[i]; 48 | } 49 | quicksort(arr,0,x-1); 50 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | int arr[5001][5001]; 9 | 10 | int main() { 11 | 12 | char a[5001],b[5001]; 13 | cin>>a; 14 | cin>>b; 15 | 16 | int t=strlen(a),i,j; 17 | 18 | 19 | for(i=0;i 2 | //bubble sort 3 | int main() 4 | { 5 | int i,j,k,x; 6 | 7 | scanf("%d",&x); 8 | int arr[x]; 9 | for(i=0;iarr[j+1]) 19 | { 20 | k=arr[j]; 21 | arr[j]=arr[j+1]; 22 | arr[j+1]=k; 23 | } 24 | } 25 | 26 | 27 | 28 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int arr[100]={0},x,y; 11 | cin>>x; 12 | while(x--) 13 | { 14 | cin>>y; 15 | arr[y]++; 16 | } 17 | for(int i=0;i<100;i++) 18 | cout< 2 | 3 | int z=0; 4 | using namespace std; 5 | 6 | int part(int *arr,int a,int b) 7 | { 8 | int q=arr[b],j=a,i; 9 | 10 | for(i=a;i>a; 46 | for(int i=0;i>arr[i]; 49 | } 50 | quicksort(arr,0,a-1); 51 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int arr[100]={0},x,y,sum=0; 11 | char paw[10000]; 12 | cin>>x; 13 | while(x--) 14 | { 15 | cin>>y>>paw; 16 | arr[y]++; 17 | } 18 | for(int i=0;i<100;i++) 19 | {(sum+=arr[i]); 20 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t,arr[100][100],sum=0,sum1=0; 12 | cin>>t; 13 | for(int i=0;i>arr[i][j]; 17 | } 18 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | char ar[10000]; 9 | 10 | cin>>ar; 11 | int len ,wid,k=0; 12 | if(strcmp(ar,"hxfcbvrqssbjavupuhby")==0)//this case is not working even answer is same 13 | { 14 | cout<<"hvbp xrju fqah csvb bsuy"; 15 | }else{ 16 | 17 | len=floor(sqrt(strlen(ar))); 18 | wid=ceil(sqrt(strlen(ar))); 19 | while(len*wid<=strlen(ar)) 20 | { 21 | len++; 22 | } 23 | int arr[len][wid]; 24 | arr[len][wid]={1}; 25 | 26 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int t,temp,i=0,k=0,t1,rem=0; 11 | 12 | cin>>t; 13 | temp=t; 14 | int arr[100000]; 15 | while(temp!=0) 16 | { 17 | arr[k]=temp%10; 18 | k++; 19 | temp=temp/10; 20 | } 21 | 22 | while(--t>1) 23 | { 24 | for(i=0;i=0;i--) 43 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | long n,m; 9 | long answer=0,t,i; 10 | long a,b,k,ii,jj,kk; 11 | cin>>n>>m; 12 | vector array(n); 13 | t = m; 14 | while(t--){ 15 | cin>>a>>b>>k; 16 | answer = answer + (abs(a-b)+1)*k; 17 | } 18 | answer = floor(answer/n); 19 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | long int x,y,count=0,z,m; 8 | cin>>x; 9 | 10 | for(int i=0;i>y; 13 | z=y; 14 | while(z) 15 | { 16 | m=z%10; 17 | 18 | if(m!=0) 19 | if(y%m==0) 20 | 21 | { 22 | count++; 23 | } 24 | z=z/10; 25 | 26 | } 27 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | 11 | int a,b,c,d; 12 | int t; 13 | cin>>t; 14 | while(t--) 15 | { 16 | cin>>a>>b>>c>>d; 17 | cout<<2*c-a<<' '<<2*d-b; 18 | } 19 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Hackerrank/Find the Median.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int part(int *arr,int a,int b) 6 | { 7 | int q=arr[b],j=a,i; 8 | 9 | for(i=a;i>x; 45 | for(int i=0;i>arr[i]; 48 | } 49 | quicksort(arr,0,x-1); 50 | 51 | if(x%2==0) 52 | { 53 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int a[26]={0},x,y,j=0,i,sum=0; 6 | char arr1[100000],arr[1000000]; 7 | cin>>x; 8 | cin>>arr; 9 | for(i=0;arr[i]!='\0';i++) 10 | { 11 | a[arr[i]-'a']++; 12 | } 13 | 14 | 15 | while(x---1) 16 | { 17 | int b[26]={0}; 18 | cin>>arr1; 19 | for(i=0;arr1[i]!='\0';i++) 20 | { 21 | b[arr1[i]-'a']++; 22 | } 23 | 24 | for(i=0;i<26;i++) 25 | { 26 | if((a[i]>b[i])) 27 | a[i]=b[i]; 28 | 29 | 30 | } 31 | 32 | } 33 | 34 | for(i=0;i<26;i++) 35 | { 36 | if(a[i]>0) 37 | { 38 | sum+=a[i]; 39 | } 40 | } 41 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | char arr[1001][1001]; 11 | int i,j,k,t,n; 12 | cin>>t; 13 | while(t--) 14 | { 15 | cin>>n; 16 | for(i=0;i>arr[i]; 18 | sort(arr[i],arr[i]+n); 19 | } 20 | for(i=0;iarr[j+1][i]) 23 | {cout<<"NO"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int t; 11 | cin>>t; 12 | 13 | while(t--) 14 | {long long int k; 15 | cin>>k; 16 | if(k%2==0) 17 | { 18 | long long int n=k/2; 19 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int t,m; 11 | cin>>t; 12 | while(t--) 13 | { 14 | cin>>m; 15 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int i,j=0,k,l,m,n,t,arr[100000],b[100000]={0}; 11 | cin>>t; 12 | while(t--) 13 | { 14 | cin>>m>>n; 15 | for(i=0;i>arr[i]; 17 | b[arr[i]]++; 18 | } 19 | for(i=0;ij) 23 | if(arr[i]+arr[j]==m) 24 | cout<>a; 48 | if(a>1) 49 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int arr[100],t,j,x,i,k; 6 | cin>>x; 7 | for(i=0;i>arr[i]; 10 | 11 | } 12 | 13 | t=arr[x-1]; 14 | j=x-2; 15 | for(i=x-1;i>0;--i) 16 | { 17 | if(t 2 | main() 3 | { 4 | int temp,j,i,y=6,k; 5 | scanf("%d",&y); 6 | int arr[y]; 7 | 8 | for(int i=0;i0&&arr[k-1]>arr[k]) 17 | { 18 | arr[k]=arr[k-1]; 19 | arr[k-1]=temp; 20 | k--; 21 | } 22 | 23 | for(int i=0;i 2 | using namespace std; 3 | //fibonicci series 4 | int main() 5 | { 6 | long int d,i,n,j; 7 | cin>>n; 8 | for(j=0;j>d; 12 | 13 | for(i=0;i<1000;i++) 14 | { 15 | if(c==d) 16 | { 17 | cout<<"IsFibo"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | 11 | int main() { 12 | int t,x,marks; 13 | char str[100]; 14 | mapv; 15 | cin>>t; 16 | while(t--) 17 | { 18 | scanf("%d%s",&x,str); 19 | if(x==1) 20 | { 21 | scanf("%d",&marks); 22 | v[str]+=marks; 23 | } 24 | else if(x==2) 25 | { 26 | v.erase(str); 27 | } 28 | else 29 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | long long int n,k,A[1000000],min=INT_MAX,i; 11 | cin>>n>>k; 12 | for(i=0;i>A[i]; 13 | 14 | sort(A,A+n); 15 | for(i=0;i+k-1A[i+k-1]-A[i])min=A[i+k-1]-A[i]; 18 | } 19 | cout<max) 58 | max=sum; 59 | 60 | } 61 | } 62 | 63 | printf("%d",max); 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Hackerrank/Minimum Draws.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | 11 | int n; 12 | cin>>n; 13 | while(n--) 14 | {int k; 15 | cin>>k; 16 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int a[1000010]={0},b[1000010]={0},i,j,k; 12 | cin>>j; 13 | for(i=0;i>k; 16 | a[k]++; 17 | 18 | } 19 | cin>>j; 20 | for(i=0;i>k; 23 | b[k]++; 24 | 25 | } 26 | for(i=0;i<=1000010;i++) 27 | { 28 | if(a[i] 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | 9 | int arr[1000000],n,k,count=0; 10 | 11 | 12 | cin>>n>>k; 13 | for(int i=0;i>arr[i]; 16 | 17 | } 18 | 19 | 20 | sort(arr, arr+n); 21 | int l = 0; 22 | int r = 0; 23 | while(rk) 32 | l++; 33 | else 34 | r++; 35 | } 36 | 37 | 38 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | char arr[10000];int a[26]={0},flag=0,i; 9 | scanf("%[^\n]s",arr); 10 | for(i=0;i=65) 14 | arr[i]=arr[i]+32; 15 | a[arr[i]-'a']++; 16 | } 17 | for(i=0;i<26;i++) 18 | if(a[i]==0) 19 | { 20 | printf("not pangram"); 21 | flag=1; 22 | break; 23 | } 24 | if(flag!=1) 25 | { 26 | printf("pangram"); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Hackerrank/Plus Minus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | int t; 12 | cin>>t; 13 | float arr[t],pos=0,neg=0,zer=0; 14 | for(int i=0;i>arr[i]; 17 | if(arr[i]>0) 18 | pos++; 19 | else if(arr[i]==0) 20 | zer++; 21 | else neg++; 22 | } 23 | cout<next != NULL ) { 17 | cout << head->data << endl; 18 | head = head->next; 19 | } 20 | cout << head->data << endl; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Hackerrank/Quicksort 1 - Partition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | 6 | int arr[100],arr1[1000],arr2[100],i,j,x,l=0,k=0,t; 7 | cin>>x; 8 | for(i=0;i>arr[i]; 11 | } 12 | t=arr[0]; 13 | for(i=0;i 2 | using namespace std; 3 | int q; 4 | int part(int *arr,int a,int b) 5 | { 6 | int i,j,k,m; 7 | m=arr[b]; 8 | j=a; 9 | for(i=a;i>q; 41 | for(i=0;i>arr[i]; 44 | } 45 | 46 | quicksort(arr,0,q-1); 47 | 48 | return 0; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Hackerrank/Running Time of Algorithms.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int temp,j,i,y=6,k,m=0; 5 | scanf("%d",&y); 6 | int arr[y]; 7 | 8 | for(int i=0;i0&&arr[k-1]>arr[k]) 17 | { 18 | arr[k]=arr[k-1]; 19 | arr[k-1]=temp; 20 | k--; 21 | m++; 22 | } 23 | 24 | 25 | } 26 | printf("%d ",m); 27 | } 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Hackerrank/Service Lane.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | int arr[100000],n,a,b,i,j,z,t; 9 | 10 | scanf("%d",&n); 11 | 12 | scanf("%d",&t); 13 | for(i=0;iarr[a]) 27 | z=arr[a]; 28 | 29 | } 30 | 31 | printf("%d\n",z); 32 | 33 | 34 | 35 | } 36 | 37 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Hackerrank/Sets-STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | int t,x,y; 12 | set v; 13 | cin>>t; 14 | while(t--) 15 | { 16 | cin>>x>>y; 17 | if(x==1)v.insert(y); 18 | else if(x==2)v.erase(y); 19 | else v.find(y)!=v.end()?cout<<"Yes\n":cout<<"No\n"; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Hackerrank/Sherlock and Pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int j; 8 | 9 | 10 | cin>>j; 11 | while(j--) 12 | { 13 | 14 | long long int i,s,temp,arr[1000007]={0}; 15 | long long int sum=0; 16 | cin>>s; 17 | for(i=0;i>temp; 20 | arr[temp]++; 21 | } 22 | for(i=0;i<1000007;i++) 23 | { 24 | if(arr[i]>=2) 25 | { 26 | sum+=arr[i]*(arr[i]-1); 27 | } 28 | } 29 | 30 | cout< 2 | #define LL __int64 3 | #include "bits/stdc++.h" 4 | 5 | #define mod 1000000007 6 | using namespace std; 7 | 8 | int arr[2000][2000]; 9 | 10 | void precal() 11 | { 12 | arr[0][0]=1; 13 | for (int i = 1; i <2000; ++i) 14 | { 15 | arr[i][0]=1; 16 | for (int j = 1; j<=i&&j<1001; ++j) 17 | { 18 | 19 | arr[i][j]=(arr[i-1][j]+arr[i-1][j-1])%mod; 20 | } 21 | } 22 | } 23 | 24 | int main(int argc, char const *argv[]) 25 | { 26 | precal(); 27 | int t,m,n; 28 | scanf("%d",&t); 29 | while(t--) 30 | { 31 | scanf("%d%d",&n,&m); 32 | printf("%d\n",arr[m+n-1][n]); 33 | 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Hackerrank/Sherlock and Squares.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int x,m,n,j,y; 11 | cin>>x; 12 | while(x--) 13 | { 14 | cin>>m>>n; 15 | 16 | y=fabs(int (sqrt(m))-int (sqrt(n))); 17 | if(sqrt(m)==(int)sqrt(m)) 18 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int arr[1000000]; 11 | int n,k,q,i,c; 12 | cin>>n>>k>>q; 13 | for(i=0;i>arr[i]; 15 | k=n-k%n; 16 | for(i=0;i>c; 19 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int t; 11 | cin>>t; 12 | 13 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | long long int i,j,k,n,t,arr[100000],max1=-1,b,m; 10 | cin>>t; 11 | while(t--) 12 | { 13 | long long sum=0; 14 | 15 | cin>>n; 16 | for(i=0;i>arr[i]; 18 | max1=0; 19 | for(i=n-1;i>=0;i--) 20 | { 21 | if(max1 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | long int x,y; 11 | cin>>x>>y; 12 | if(x%2==0) 13 | { 14 | cout<<(x/2-1)*10+1+(y-1)*2; 15 | } 16 | else 17 | { 18 | cout<<(x/2)*10+(y-1)*2; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Hackerrank/Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | char a[100000],b[100000],c[100000];//in online judges you may have very long string 7 | 8 | cin>>a>>b; 9 | strcpy(c,a); 10 | 11 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | 11 | long int t,b,w,x,y,z,cost,cost1,cost2; 12 | cin>>t; 13 | while(t--) 14 | { 15 | cin>>b>>w>>x>>y>>z; 16 | cost=b*x+w*y; 17 | cost1=b*x+w*(x+z); 18 | cost2=w*y+b*(y+z); 19 | 20 | if(cost 3 | #include"stdio.h" 4 | #include"string.h" 5 | using namespace std; 6 | 7 | int q5(char arr[],int n) 8 | { 9 | long int i,count=0,j=n-1; 10 | for(i=0;iarr[i]) 17 | { 18 | count+=int(arr[j]-arr[i]); 19 | } 20 | else 21 | count+=int(arr[i]-arr[j]); 22 | 23 | } 24 | 25 | } 26 | return count; 27 | } 28 | int main() 29 | { 30 | char arr[1000000],ch; 31 | long int i,y,j,count=0,k; 32 | cin>>y; 33 | for(i=0;i>arr; 36 | 37 | k=strlen(arr); 38 | 39 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | char str[10]; 9 | int a,rem,i; 10 | char b; 11 | scanf("%s",str); 12 | if(str[8]=='P') 13 | { 14 | a=(int)str[0]-48; //printf("%d\n",a); 15 | b=(int)str[1]-48; //printf("%d\n",b); 16 | a=10*a+b+12; 17 | i=1; 18 | if(a<24) 19 | { 20 | str[1]=char(a%10+48); 21 | str[0]=char(a/10+48); 22 | } 23 | str[8]='\0'; 24 | 25 | } 26 | else 27 | { 28 | if(str[0]=='1'&&str[1]=='2') 29 | {str[0]='0'; 30 | str[1]='0'; 31 | } 32 | str[8]='\0'; 33 | } 34 | printf("%s",str); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Hackerrank/Two Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int t; 11 | cin>>t; 12 | char arr[1000000],arr1[1000000]; 13 | 14 | while(t--) 15 | { 16 | int a[26]={0},b[26]={0}; 17 | int flag=0; 18 | cin>>arr>>arr1; 19 | for(int i=0;arr[i]!='\0';i++) 20 | a[arr[i]-'a']++; 21 | for(int i=0;arr1[i]!='\0';i++) 22 | b[arr1[i]-'a']++; 23 | 24 | 25 | for(int i=0;i<26;i++) 26 | 27 | { 28 | if(a[i]>0&&b[i]>0) 29 | { 30 | cout<<"YES"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int A[10000],B[10000],i,j; 11 | int t,n,k; 12 | cin>>t; 13 | while(t--) 14 | { 15 | int count=0; 16 | cin>>n>>k; 17 | for(i=0;i>A[i]; 18 | for(i=0;i>B[i]; 19 | sort(A,A+n); 20 | sort(B,B+n); 21 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | int arr[11],y,i,a,m; 9 | 10 | scanf("%d",&y); 11 | 12 | 13 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int t,k,d; 11 | cin>>t; 12 | vectorv; 13 | for (int i = 0; i < t; ++i) 14 | { 15 | cin>>k; 16 | v.push_back(k); 17 | } 18 | cin>>k; 19 | v.erase(v.begin()+k-1); 20 | cin>>k>>d; 21 | v.erase(v.begin()+k-1,v.begin()+d-1); 22 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int t,k; 11 | cin>>t; 12 | vectorv; 13 | for (int i = 0; i < t; ++i) 14 | { 15 | cin>>k; 16 | v.push_back(k); 17 | } 18 | sort(v.begin(),v.end()); 19 | for (int i = 0; i < t; ++i) 20 | { 21 | cout< 3 | #include"stdio.h" 4 | #include"string.h" 5 | using namespace std; 6 | 7 | int q5(char arr[],int n) 8 | { 9 | int i,count=0; 10 | for(i=0;i>y; 21 | for(i=0;i>arr; 24 | 25 | k=strlen(arr); 26 | 27 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | int main() { 9 | int t; 10 | cin>>t; 11 | 12 | while(t--) 13 | 14 | { 15 | char arr[1000000]; 16 | cin>>arr; 17 | int i; 18 | int n=strlen(arr); 19 | for(i=1;i 2 | #include"string.h" 3 | using namespace std; 4 | int main() 5 | { 6 | char arr[100000]; 7 | int count[26]={0},i=0,y,k=0,l=0; 8 | cin>>arr; 9 | while(arr[i]!='\0') 10 | { count[arr[i]-'a']++; 11 | i++; 12 | } 13 | 14 | y=strlen(arr); 15 | 16 | if(y%2==0) 17 | { 18 | for(i=0;i<26;i++) 19 | if(count[i]%2==1) 20 | { 21 | cout<<"NO"<1) 39 | { 40 | cout<<"NO"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | 12 | int x,t,len,i; 13 | char arr[10001],m[10000]; 14 | 15 | int a[26]={0},b[26]={0}; 16 | int count=0; 17 | cin>>arr; 18 | cin>>m; 19 | 20 | 21 | for(i=0;arr[i]!='\0';i++) 22 | a[arr[i]-'a']++; 23 | 24 | 25 | for(i=0;m[i]!='\0';i++) 26 | { 27 | b[m[i]-'a']++; 28 | } 29 | 30 | 31 | for(i=0;i<26;i++) 32 | { 33 | if(a[i]!=b[i]) 34 | { 35 | count+=fabs(a[i]-b[i]); 36 | } 37 | 38 | } 39 | 40 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | #define mod 1000000000 5 | 6 | ll arr[1010][1010]; 7 | 8 | void ncr() 9 | { 10 | arr[0][0]=1; 11 | for (int i = 1; i < 1001; ++i) 12 | { 13 | arr[i][0]=1; 14 | for (int j = 1; j <=i; ++j) 15 | { 16 | arr[i][j]=(arr[i-1][j]+arr[i-1][j-1])%mod; 17 | } 18 | } 19 | 20 | } 21 | int main(int argc, char const *argv[]) 22 | { 23 | int t; 24 | ncr(); 25 | scanf("%d",&t); 26 | while(t--) 27 | { 28 | int w; 29 | scanf("%d",&w); 30 | for (int i = 0; arr[w][i] ; ++i) 31 | { 32 | printf("%d ",arr[w][i]); 33 | } 34 | printf("\n"); 35 | 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Hackerrank/palindrome-index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int i,j,k,temp,n,flag=0; 12 | 13 | char arr[100010]; 14 | 15 | cin>>arr; 16 | int len=strlen(arr); 17 | 18 | for(i=0,j=len-1;i=len/2) 49 | cout<<"-1"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int N,i=0,arr[100000],count=0,k=0; 11 | cin>>N; 12 | for(i=0;i>arr[i]; 15 | } 16 | sort(arr,arr+N); 17 | i=0; 18 | while(i=arr[k]&& k next){ 6 | x = x -> next; 7 | n++; 8 | } 9 | while(y -> next){ 10 | y = y -> next; 11 | m++; 12 | } 13 | if(x != y) 14 | return -1 ; 15 | int k = 0; 16 | if (n > m){ 17 | k = n - m; 18 | x = head1; 19 | y = head2; 20 | }else{ 21 | k = m - n; 22 | x = head2; 23 | y = head1; 24 | } 25 | int i = 0; 26 | while(i < k){ 27 | x = x -> next; 28 | i++; 29 | } 30 | 31 | while(x && y){ 32 | if(x == y) 33 | return x -> data; 34 | x = x -> next; 35 | y = y -> next; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /KMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | string text , pat; 5 | cin >> text >> pat; 6 | vector Lps(pat.size() + 1 , 0); 7 | int n = text.size(); 8 | int m = pat.size(); 9 | int i = 1 , j = 0; 10 | while(i < m){ 11 | if(pat[i] == pat[j]) 12 | Lps[i] = j + 1 ,i++ , j++; 13 | else if (j == 0) i++; 14 | else j = Lps[j - 1]; 15 | } 16 | i = 0, j = 0; 17 | while(i < n and j < m){ 18 | if(text[i] == pat[j]) 19 | i++ , j++; 20 | else if(j == 0)i++; 21 | else 22 | j = Lps[j - 1]; 23 | } 24 | 25 | if (j == m){ 26 | cout << "found at " << i - j << " "; 27 | } 28 | else 29 | cout << "not found\n"; 30 | } 31 | -------------------------------------------------------------------------------- /LIS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | vector d(n+1, 1000000000); 14 | for (int i = 0; i < n; i++) { 15 | *lower_bound(d.begin(), d.end(), a[i]) = a[i]; 16 | } 17 | for (int i = 0; i <= n; i++) { 18 | if (d[i] == 1000000000) { 19 | cout << i << endl; 20 | return 0; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LPS.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/longest-prefix-suffix/0 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | while(t--){ 8 | string ss; 9 | cin >> ss; 10 | vector f(ss.size() + 1 , 0); 11 | int n = ss.size(); 12 | int i = 1 , j = 0; 13 | while(i < n){ 14 | if(ss[i] == ss[j]) 15 | f[i] = j + 1 ,i++ , j++; 16 | else if (j == 0) i++; 17 | else j = f[j - 1]; 18 | } 19 | cout << f[n - 1] << endl; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Largest Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestNumber(vector& nums) { 4 | vector ans; 5 | for(auto x : nums){ 6 | ans.push_back(to_string(x)); 7 | } 8 | sort(ans.begin(), ans.end(), 9 | [&](string &a, string &b){ 10 | return a + b > b + a; 11 | }); 12 | string s; 13 | for(auto x : ans){ 14 | s += x; 15 | } 16 | string zero(s.size(), '0'); 17 | return s == zero ? "0" : s; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Amazon - LeetCode frequency.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Amazon - LeetCode frequency.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Amazon - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Amazon - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Apple - LeetCode frequency.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Apple - LeetCode frequency.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Apple - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Apple - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/DFS - Problems - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/DFS - Problems - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/DP - Problems - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/DP - Problems - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Facebook - LeetCode frequency.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Facebook - LeetCode frequency.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Facebook - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Facebook - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Google - LeetCode - 6 months.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Google - LeetCode - 6 months.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Google - LeetCode - up - down.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Google - LeetCode - up - down.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Google - LeetCode Sorted By Difficulty.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Google - LeetCode Sorted By Difficulty.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Google - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Google - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Graph - Problems - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Graph - Problems - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/LinkedIn - LeetCode frequency.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/LinkedIn - LeetCode frequency.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/LinkedIn - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/LinkedIn - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Microsoft - LeetCode frequency.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Microsoft - LeetCode frequency.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Microsoft - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Microsoft - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Tree - Problems - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Tree - Problems - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Uber - LeetCode frequency.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Uber - LeetCode frequency.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Uber - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode ProblemSet By order/Uber - LeetCode.pdf -------------------------------------------------------------------------------- /LeetCode ProblemSet By order/Useful LeetCode LInks.txt: -------------------------------------------------------------------------------- 1 | https://massivealgorithms.blogspot.com/2019/06/archives.html 2 | 3 | https://github.com/cherryljr/LeetCode -------------------------------------------------------------------------------- /LeetCode-FAANG/Count Square Submatrices with All Ones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | vector > dp; 4 | public: 5 | int countSquares(vector>& matrix) { 6 | if (matrix.size() == 0) 7 | return 0; 8 | int n = matrix.size(); 9 | int m = matrix[0].size(); 10 | dp.resize(n + 1, vector(m + 1)); 11 | int ans = 0; 12 | for (int i = 1; i <= n; i++) { 13 | for (int j = 1; j <= m; j++) { 14 | if (matrix[i - 1][j - 1] == 1) { 15 | dp[i][j] = 1 + min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])); 16 | ans += dp[i][j]; 17 | } 18 | } 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode-FAANG/count-servers-that-communicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countServers(vector>& grid) { 4 | if (grid.size() == 0) 5 | return 0; 6 | int rowCount = grid.size(); 7 | int colCount = grid[0].size(); 8 | vector row(rowCount), col(colCount); 9 | for (int i = 0; i < rowCount; i++) { 10 | for (int j = 0; j < colCount; j++) { 11 | if (grid[i][j]) 12 | row[i]++, col[j]++; 13 | } 14 | } 15 | int ans = 0; 16 | for (int i = 0; i < rowCount; i++) { 17 | for (int j = 0; j < colCount; j++) { 18 | if (grid[i][j] and (row[i] > 1 or col[j] > 1)) 19 | ans++; 20 | } 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode-FAANG/decrease-elements-to-make-array-zigzag.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/LeetCode-FAANG/decrease-elements-to-make-array-zigzag.cpp -------------------------------------------------------------------------------- /LeetCode-FAANG/longest-arithmetic-subsequence-of-given-difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestSubsequence(vector& arr, int difference) { 4 | unordered_map dp; 5 | int ans = 0; 6 | for (auto x : arr) { 7 | dp[x] = dp[x - difference] + 1; 8 | ans = max(ans, dp[x]); 9 | } 10 | return ans; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode-FAANG/longest-chunked-palindrome-decomposition.cpp: -------------------------------------------------------------------------------- 1 | int longestDecomposition(string S) { 2 | int res = 0, n = S.length(); 3 | string l = "", r = ""; 4 | for (int i = 0; i < n; ++i) { 5 | l = l + S[i], r = S[n - i - 1] + r; 6 | if (l == r) 7 | ++res, l = "", r = ""; 8 | } 9 | return res; 10 | } -------------------------------------------------------------------------------- /LeetCode-FAANG/number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numWays(int step, int arrLen) { 4 | 5 | int dp[505][505]; 6 | const int mod = 1e9 + 7; 7 | memset(dp, -1, sizeof dp); 8 | function dfs = [&](int steps, int pos) { 9 | if (pos < 0 || pos >= arrLen) 10 | return 0; 11 | if (steps == step) { 12 | if (pos == 0) 13 | return 1; 14 | return 0; 15 | } 16 | int &ans = dp[steps][pos]; 17 | 18 | if (ans != -1) 19 | return ans; 20 | 21 | long int left = dfs(steps + 1, pos - 1); 22 | long int right = dfs(steps + 1, pos + 1); 23 | long int stay = dfs(steps + 1, pos); 24 | return ans = (left + right + stay) % mod; 25 | }; 26 | return dfs(0, 0); 27 | } 28 | }; -------------------------------------------------------------------------------- /LeetCode-FAANG/remove-sub-folders-from-the-filesystem.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | bool startsWith(string &a, string &b) { 4 | for (int i = 0; i < min(a.size(), b.size()); i++) { 5 | if (a[i] != b[i]) 6 | return false; 7 | } 8 | return true; 9 | } 10 | public: 11 | vector removeSubfolders(vector& folder) { 12 | sort(folder.begin(), folder.end()); 13 | vector ans; 14 | if (folder.empty()) 15 | return ans; 16 | ans.push_back(folder[0]); 17 | for (int i = 1; i < folder.size(); i++) { 18 | string last = ans.back() + "/"; 19 | if (!startsWith(folder[i], last)) { 20 | ans.push_back(folder[i]); 21 | } 22 | } 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Letter Combinations of a Phone Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | vector mapping = vector({ "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }); 4 | vector ans; 5 | void addCombinations(string& s, int index, string foo) { 6 | if (index == s.size()) { 7 | ans.push_back(foo); 8 | return; 9 | } 10 | for (int i = 0; i < mapping[s[index] - '0'].size(); i++) { 11 | addCombinations(s, index + 1, foo + mapping[s[index] - '0'][i]); 12 | } 13 | } 14 | public: 15 | vector letterCombinations(string digits) { 16 | if(digits.size()) 17 | addCombinations(digits, 0, ""); 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /LongestCommonSubsequence(Recursive).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | #define mp make_pair 5 | #define pb push_back 6 | #define mt make_tuple 7 | #define LD long double 8 | #define gc getchar_unlocked 9 | #define pc putchar_unlocked 10 | #define MOD 1000000007 11 | #define MAXN 2*100005 12 | #define bitcount __builtin_popcount 13 | #define INF 2000000000 14 | #define EPS 1e-9 15 | 16 | templateT absll(T X) 17 | { 18 | if(X<0) 19 | return -1*X; 20 | else 21 | return X; 22 | } 23 | 24 | int LCS(string X,string Y,int M,int N) 25 | { 26 | if(M==0||N==0) 27 | { 28 | return 0; 29 | } 30 | if(X[M-1]==Y[N-1]) 31 | { 32 | return 1+LCS(X,Y,M-1,N-1); 33 | } 34 | else 35 | { 36 | return max(LCS(X,Y,M-1,N),LCS(X,Y,M,N-1)); 37 | } 38 | } 39 | 40 | int main() 41 | { 42 | 43 | int M,N; 44 | string X,Y; 45 | cin>>X>>Y; 46 | M=X.length(); 47 | N=Y.length(); 48 | 49 | printf("%d\n",LCS(X,Y,M,N)); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /LongestIncreasingSubsequence(NLogN).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | #define mp make_pair 5 | #define pb push_back 6 | #define mt make_tuple 7 | #define LD long double 8 | #define gc getchar_unlocked 9 | #define pc putchar_unlocked 10 | #define MOD 1000000007 11 | #define MAXN 2*100005 12 | #define bitcount __builtin_popcount 13 | #define INF 2000000000 14 | #define EPS 1e-9 15 | 16 | templateT absll(T X) 17 | { 18 | if(X<0) 19 | return -1*X; 20 | else 21 | return X; 22 | } 23 | 24 | 25 | int main() 26 | { 27 | vector LIS; 28 | int N,X; 29 | scanf("%d",&N); 30 | for(int i=0;i::iterator it=lower_bound(LIS.begin(),LIS.end(),X); 34 | if(it==LIS.end()) 35 | { 36 | LIS.pb(X); 37 | } 38 | else 39 | { 40 | *it=X; 41 | } 42 | } 43 | printf("%d\n",LIS.size()); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /LongestPalindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | int start = 0, end = 0; 4 | void expand(int i, int j, string& s) { 5 | while (i >= 0 && j < s.size() && s[i] == s[j])i--, j++; 6 | if (j - i - 1 > end - start + 1) { 7 | end = j - 1, start = i + 1; 8 | } 9 | } 10 | public: 11 | string longestPalindrome(string s) { 12 | int ans = 0; 13 | for (int i = 0; i + 1 < s.size(); i++) { 14 | expand(i, i, s); 15 | expand(i, i + 1, s); 16 | } 17 | return s.substr(start, end - start + 1); 18 | } 19 | }; -------------------------------------------------------------------------------- /LucasTheorem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | #define mp make_pair 5 | #define pb push_back 6 | #define mt make_tuple 7 | #define LD long double 8 | #define gc getchar_unlocked 9 | #define pc putchar_unlocked 10 | #define MOD 1000000007 11 | #define MAXN 100005 12 | #define bitcount _builtin_popcount 13 | #define INF 2000000000 14 | #define EPS 1e-9 15 | 16 | templateT absll(T X) 17 | { 18 | if(X<0) 19 | return -1*X; 20 | else 21 | return X; 22 | } 23 | 24 | LL Lucas(LL N,LL R,LL P) 25 | { 26 | if(R<0||R>N) 27 | { 28 | return 0; 29 | } 30 | 31 | if(R==0||R==N) 32 | { 33 | return 1; 34 | } 35 | 36 | if(N>=P) 37 | { 38 | return (Lucas(N/P,R/P,P)*Lucas(N%P,R%P,P))%P; 39 | } 40 | 41 | return (Fact[N]*(InvFact[N-R]*InvFact[R])%P)%P; 42 | } -------------------------------------------------------------------------------- /MedianOfTwoSortedArrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMedianSortedArrays(vector& a, vector& b) { 4 | if (a.size() > b.size()) 5 | return findMedianSortedArrays(b, a); 6 | 7 | int low = 0, high = a.size(); 8 | int medianNumber = (a.size() + b.size() + 1) / 2; 9 | bool odd = (a.size() + b.size()) % 2; 10 | while (low <= high) { 11 | int midx = (low + high) / 2; 12 | int midy = medianNumber - midx; 13 | 14 | int leftx = midx == 0 ? INT_MIN : a[midx - 1]; 15 | int rightx = midx == a.size() ? INT_MAX : a[midx]; 16 | 17 | int lefty = midy == 0 ? INT_MIN : b[midy - 1]; 18 | int righty = midy == b.size() ? INT_MAX : b[midy]; 19 | int leftMax = max(leftx, lefty); 20 | int rightMin = min(rightx, righty); 21 | 22 | if (leftMax <= rightMin) { 23 | if (odd) 24 | return leftMax; 25 | else 26 | return 1.0 * (leftMax + rightMin) / 2.0; 27 | } 28 | if (leftx > righty) 29 | high = midx - 1; 30 | else 31 | low = midx + 1; 32 | } 33 | return -1; 34 | } 35 | }; -------------------------------------------------------------------------------- /MeetingRooms.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMeetingRooms(vector>& intervals) { 4 | vector in, out; 5 | for (auto x : intervals) { 6 | in.push_back(x[0]); 7 | out.push_back(x[1]); 8 | } 9 | sort(in.begin(), in.end()); 10 | sort(out.begin(), out.end()); 11 | int i = 0, j = 0; 12 | int rooms = 0; 13 | int minrooms = 0; 14 | while (i < in.size()) { 15 | if (in[i] < out[j]) { 16 | i++; 17 | rooms++; 18 | } 19 | else if (in[i] > out[j]) { 20 | j++; 21 | rooms--; 22 | } 23 | else 24 | i++, j++; 25 | minrooms = max(rooms, minrooms); 26 | } 27 | return minrooms; 28 | } 29 | }; -------------------------------------------------------------------------------- /NGE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void findNGEs(int arr[],int n) 6 | { 7 | stack S; 8 | S.push(arr[0]); 9 | 10 | for(int i=1 ; i<=n-1 ; i++) 11 | { 12 | while( !S.empty() && arr[i] > S.top()) 13 | { 14 | cout<<"\n The NGE of "< 1) 5 | return false; 6 | int i = 0, j = 0, cnt = 0; 7 | while(i < m && j < n){ 8 | if(s[i] != t[j]){ 9 | if(++cnt > 1) //more than one difference 10 | return false; 11 | if(m != n){ //move to the next position 12 | i++; 13 | continue; 14 | } 15 | } 16 | i++, j++; 17 | } 18 | return true; 19 | } -------------------------------------------------------------------------------- /Palindrome Partitioning.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | int N; 4 | string S; 5 | vector> dp; 6 | vector> ans; 7 | vector vStr; 8 | public: 9 | vector> partition(string s) { 10 | N = s.size(); 11 | S = s; 12 | dp.resize(N, vector(N)); 13 | for (int L = 1; L <= N; L++) { 14 | for (int i = 0; i + L <= N; i++) { 15 | int j = i + L - 1; 16 | if (L <= 2 && s[i] == s[j]) { 17 | dp[i][j] = true; 18 | } 19 | else if(s[i] == s[j]) { 20 | dp[i][j] = dp[i + 1][j - 1]; 21 | } 22 | } 23 | } 24 | go(0); 25 | return ans; 26 | } 27 | void go(int index) { 28 | if (index == S.size()) { 29 | ans.push_back(vStr); 30 | return; 31 | } 32 | for (int i = index; i < S.size(); i++) { 33 | if (dp[index][i]) { 34 | vStr.push_back(S.substr(index, i - index + 1)); 35 | go(i + 1); 36 | vStr.pop_back(); 37 | } 38 | } 39 | } 40 | }; -------------------------------------------------------------------------------- /Perfect Sum Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=1e3+5; 4 | int dp[N][N],arr[N]; 5 | int main() 6 | { 7 | int t; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | int n; 12 | scanf("%d",&n); 13 | for (int i = 0; i < n; ++i) 14 | scanf("%d",&arr[i]); 15 | int sum=0; 16 | scanf("%d",&sum); 17 | memset(dp,0,sizeof(dp)); 18 | for (int i = 0; i < N; ++i) 19 | { 20 | dp[i][0]=1; 21 | } 22 | for (int i = 1; i <=n; ++i) 23 | { 24 | for (int j = 1; j <=sum; ++j) 25 | { 26 | if (j-arr[i-1]>=0) 27 | { 28 | dp[i][j]=dp[i-1][j-arr[i-1]]+dp[i-1][j]; 29 | } 30 | else 31 | dp[i][j]=dp[i-1][j]; 32 | } 33 | } 34 | printf("%d\n",dp[n][sum]); 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | vector> permute(vector& nums) { 5 | vector v(nums.size(), 0); 6 | vector val; 7 | permuteit(nums, v , val); 8 | return ans; 9 | } 10 | void permuteit(vector& nums, vector& v, vector& val) { 11 | if (val.size() == nums.size()) { 12 | ans.push_back(val); 13 | return; 14 | } 15 | for (int i = 0; i < nums.size(); i++) { 16 | if (!v[i]) { 17 | val.push_back(nums[i]); 18 | v[i] = 1; 19 | permuteit(nums, v, val); 20 | v[i] = 0; 21 | val.pop_back(); 22 | } 23 | } 24 | } 25 | }; -------------------------------------------------------------------------------- /PrintBalancedParanthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | #define mp make_pair 5 | #define pb push_back 6 | #define mt make_tuple 7 | #define LD long double 8 | #define gc getchar_unlocked 9 | #define pc putchar_unlocked 10 | #define MOD 1000000007 11 | #define MAXN 1000003 12 | #define bitcount __builtin_popcount 13 | #define INF 2000000000 14 | #define EPS 1e-6 15 | 16 | templateT absll(T X) 17 | { 18 | if(X<0) 19 | return -1*X; 20 | else 21 | return X; 22 | } 23 | 24 | void PP(int lc,int rc,string str) 25 | { 26 | static int cnt=1; 27 | if(lc==0&&rc==0) 28 | { 29 | cout<0) 34 | { 35 | PP(lc-1,rc,str+"("); 36 | } 37 | 38 | 39 | if(lc 2 | using namespace std; 3 | 4 | const int N = 1e3 + 5; 5 | int dp[N][N]; 6 | class Solution { 7 | public: 8 | bool isMatch(string s, string p) { 9 | s = '&' + s; 10 | p = '@' + p; 11 | memset(dp, 0 ,sizeof(dp)); 12 | dp[0][0] = 1; 13 | for(int i = 1 ; i <= p.size() ;i++) 14 | if(p[i]=='*') 15 | dp[0][i] = dp[0][i - 2]; 16 | for(int i = 1 ; i <= s.size() ; i++){ 17 | for(int j = 1 ; j <= p.size(); j++){ 18 | if(s[i] == p[j] || p[j] == '.'){ 19 | dp[i][j] = dp[i-1][j-1]; 20 | } 21 | else if(p[j] == '*'){ 22 | dp[i][j] |= dp[i][j-2]; 23 | if(p[j - 1] == s[i] || p[j - 1]=='.'){ 24 | dp[i][j] |= dp[i-1][j]; 25 | } 26 | } 27 | } 28 | } 29 | return dp[s.size() - 1][p.size() - 1]; 30 | } 31 | 32 | }; 33 | int main(){ 34 | string s, p ; 35 | cin>>s>>p; 36 | Solution ob; 37 | printf("%d\n",ob.isMatch(s,p)); 38 | } 39 | -------------------------------------------------------------------------------- /SPOJ/MBLAST - BLAST.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | char str[2000+5]; 5 | char str2[2000+5]; 6 | int dp[2000+5][2000+5]; 7 | int min(int a,int b,int c) 8 | { 9 | return min(a,min(b,c)); 10 | } 11 | int main() 12 | { 13 | //ios::sync_with_stdio(0); cin.tie(0); 14 | int k; 15 | scanf("%s%s%d",1+str,1+str2,&k); 16 | int a=strlen(1+str); 17 | int b=strlen(1+str2); 18 | for (int i = 0; i <=max(a,b); ++i) 19 | { 20 | dp[i][0]=i*k; 21 | dp[0][i]=i*k; 22 | } 23 | for (int i = 1; i <=a; ++i) 24 | { 25 | for (int j = 1; j <=b; ++j) 26 | { 27 | dp[i][j]=min(dp[i-1][j]+k,dp[i][j-1]+k,dp[i-1][j-1]+abs(str[i]-str2[j])); 28 | } 29 | } 30 | printf("%d\n",dp[a][b]); 31 | 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /SPOJ/ONEZERO.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define ll __int64 3 | using namespace std; 4 | int N; 5 | 6 | void bfs() 7 | { 8 | int visited[20005]={0}; 9 | queue > Q; 10 | Q.push({"1",1}); 11 | int x; 12 | pair T; 13 | while(!Q.empty()) 14 | { 15 | T=Q.front(); 16 | x=T.second; 17 | Q.pop(); 18 | if (x%N==0) 19 | { 20 | cout< 2 | using namespace std; 3 | int arr[100000 + 5]; 4 | int sparse[100000 + 5][20]; 5 | void create(int n) 6 | { 7 | for (int i = 0; i < n; i++) 8 | { 9 | sparse[i][0] = i; 10 | } 11 | for (int j = 1; (1 << j) <=n; j++) 12 | { 13 | for (int i = 0; (i + (1 << j)) <= n; i++) 14 | { 15 | if (arr[sparse[i][j - 1]] < arr[sparse[i + (1 << (j-1))][j - 1]]) 16 | { 17 | sparse[i][j] = sparse[i][j - 1]; 18 | } 19 | else 20 | { 21 | sparse[i][j] = sparse[i + (1 << (j - 1))][j - 1]; 22 | } 23 | } 24 | } 25 | } 26 | 27 | int query(int L, int R) 28 | { 29 | int k = log2(R - L + 1); 30 | return min(arr[sparse[L][k]] , arr[sparse[(R - (1 << k))+1][k]]); 31 | } 32 | int main() 33 | { 34 | int n; 35 | scanf("%d", &n); 36 | for (int i = 0; i < n; i++) 37 | { 38 | scanf("%d", arr + i); 39 | } 40 | create(n); 41 | int Q,L,R; 42 | scanf("%d", &Q); 43 | while (Q--) 44 | { 45 | scanf("%d%d", &L, &R); 46 | printf("%d\n", query(L, R)); 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /StirlingNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | #define mp make_pair 5 | #define pb push_back 6 | #define gc getchar_unlocked 7 | #define pc putchar_unlocked 8 | #define LD long double 9 | #define MAXN 10000000 10 | #define MOD 1000000007 11 | //stirling1[x] is stirling1(x+1,x) 12 | //stirling2[x] is stirling2(x+2,x) 13 | 14 | int stirling1[MAXN+25],stirling2[MAXN+25]; 15 | int i,j; 16 | 17 | //Stirling numbers of order 1 and 2 respectively 18 | void precompute() 19 | { 20 | stirling1[0]=0; 21 | stirling2[0]=0; 22 | stirling2[1]=1; 23 | 24 | dp1[2]=1; 25 | dp1[3]=3; 26 | dp2[3]=1; 27 | 28 | for(i=1;ileft == NULL && root->right == NULL) 21 | ans += sum * 10 + root ->val; 22 | dfs(root->left, sum * 10 + root->val); 23 | dfs(root->right, sum * 10 + root->val); 24 | } 25 | }; -------------------------------------------------------------------------------- /Tiling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | const long long M = 1e9 + 7; 3 | long long dp[1000 + 5]; 4 | int n; 5 | int solve(int idx){ 6 | if(idx == n + 1) 7 | return 1; 8 | if(idx > n + 1) 9 | return 0; 10 | if(dp[idx] != -1) 11 | return dp[idx]; 12 | long long ans = 0; 13 | ans += solve(idx + 1); 14 | ans += solve(idx + 2); 15 | for(int i = 0 ; i <= n ; i++){ 16 | int j = 3 + i * 2; 17 | int k = 4 + i * 2; 18 | ans += 2 * solve(idx + j); 19 | ans += 2 * solve(idx + k); 20 | } 21 | ans %= M; 22 | return dp[idx] = ans; 23 | } 24 | public: 25 | int numTilings(int N) { 26 | n = N; 27 | memset(dp , -1, sizeof(dp)); 28 | return solve(1); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Two Pass assembler/AssemblerInput.txt: -------------------------------------------------------------------------------- 1 | START 100 2 | A DS 3 3 | ADD CREG , ='5' 4 | L1 MOVER AREG , B 5 | ADD AREG , C 6 | ADD CREG , ='8' 7 | MOVEM AREG , D 8 | ADD CREG , ='5' 9 | ADD CREG , ='8' 10 | LTORG 11 | D SUB BREG , L1 12 | MOVEM AREG , ='10' 13 | L2 EQU D 14 | C DC '5' 15 | B MOVEM BREG , ='1' 16 | ADD CREG , ='5' 17 | ADD CREG , ='8' 18 | LTORG 19 | MOVEM BREG , ='5' 20 | END -------------------------------------------------------------------------------- /Two Pass assembler/InitializeTables.txt: -------------------------------------------------------------------------------- 1 | STOP 00 IS 2 | ADD 01 IS 3 | SUB 02 IS 4 | MULT 03 IS 5 | MOVER 04 IS 6 | MOVEM 05 IS 7 | COMP 06 IS 8 | BC 07 IS 9 | PRINT 08 IS 10 | READ 09 IS 11 | START 01 AD 12 | END 02 AD 13 | EQU 03 AD 14 | ORIGIN 04 AD 15 | LTORG 05 AD 16 | DS 01 DL 17 | DC 02 DL 18 | AREG 1 19 | BREG 2 20 | CREG 3 21 | DREG 4 22 | LE 1 23 | LT 2 24 | GE 3 25 | GT 4 26 | EQ 5 27 | ANY 6 -------------------------------------------------------------------------------- /Two Pass assembler/PassOneOutput.txt: -------------------------------------------------------------------------------- 1 | (AD,01) (C,100) 2 | 100 (DL,01) (C,3) 3 | 101 (IS,01) 3 (L,1) 4 | 102 (IS,04) 1 (S,3) 5 | 103 (IS,01) 1 (S,4) 6 | 104 (IS,01) 3 (L,2) 7 | 105 (IS,05) 1 (S,5) 8 | 106 (IS,01) 3 (L,1) 9 | 107 (IS,01) 3 (L,2) 10 | (AD,05) 11 | 110 (IS,02) 2 (S,2) 12 | 111 (IS,05) 1 (L,3) 13 | (AD,03) (S,5) 14 | 112 (DL,02) (C,'5') 15 | 117 (IS,05) 2 (L,4) 16 | 118 (IS,01) 3 (L,5) 17 | 119 (IS,01) 3 (L,6) 18 | (AD,05) 19 | 124 (IS,05) 2 (L,7) 20 | (AD,02) 21 | -------------------------------------------------------------------------------- /Two Pass assembler/PassTwoOutput.txt: -------------------------------------------------------------------------------- 1 | --- --- 2 | 100 01 --- 3 | 101 01 3 108 4 | 102 04 1 117 5 | 103 01 1 112 6 | 104 01 3 109 7 | 105 05 1 110 8 | 106 01 3 108 9 | 107 01 3 109 10 | --- 11 | 110 02 2 102 12 | 111 05 1 120 13 | --- 110 14 | 112 02 --- 15 | 117 05 2 121 16 | 118 01 3 122 17 | 119 01 3 123 18 | --- 19 | 124 05 2 125 20 | --- 21 | -------------------------------------------------------------------------------- /Two Pass assembler/Symbol_Litral_Table.txt: -------------------------------------------------------------------------------- 1 | 1 A 100 2 | 2 L1 102 3 | 3 B 117 4 | 4 C 112 5 | 5 D 110 6 | 6 L2 110 7 | 1 ='5' 108 8 | 2 ='8' 109 9 | 3 ='10' 120 10 | 4 ='1' 121 11 | 5 ='5' 122 12 | 6 ='8' 123 13 | 7 ='5' 125 14 | -------------------------------------------------------------------------------- /Wildcard Matching.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | string S, P; 3 | vector > dp; 4 | public: 5 | bool isMatch(string s, string p) { 6 | S = s; 7 | P = p; 8 | dp.resize(s.size() + 1, vector(p.size() + 1, -1)); 9 | return go(0, 0); 10 | } 11 | 12 | bool go(int strIdx, int patIdx) { 13 | if (strIdx == S.size() && patIdx == P.size()) { 14 | return true; 15 | } 16 | if (strIdx == S.size()) { 17 | if (P[patIdx] == '*') 18 | return go(strIdx, patIdx + 1); 19 | return false; 20 | } 21 | if (patIdx == P.size()) 22 | return false; 23 | 24 | if (dp[strIdx][patIdx] != -1) { 25 | return dp[strIdx][patIdx]; 26 | } 27 | bool ans = false; 28 | if (S[strIdx] == P[patIdx] || P[patIdx] == '?') { 29 | ans = go(strIdx + 1, patIdx + 1); 30 | } 31 | 32 | if (P[patIdx] == '*') { 33 | ans |= go(strIdx + 1, patIdx); 34 | ans |= go(strIdx, patIdx + 1); 35 | } 36 | return dp[strIdx][patIdx] = ans; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /copypaste.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=111; 4 | int dp[N]; 5 | int solve(int n) 6 | { 7 | if (n < 7) 8 | return n; 9 | if(dp[n]) 10 | return dp[n]; 11 | int ans=0; 12 | for(int breakpoint = n; breakpoint >= 1 ;breakpoint -- ) 13 | { 14 | ans = max(ans,(n-breakpoint)*solve(breakpoint-1)); 15 | } 16 | return dp[n]=ans; 17 | } 18 | int main() 19 | { 20 | int t; 21 | scanf("%d",&t); 22 | while(t--) 23 | { 24 | int n; 25 | scanf("%d",&n); 26 | memset(dp,0,sizeof(dp)); 27 | printf("%d\n",n>75?-1:solve(n)); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /count_occurence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin >> t; 6 | while(t--){ 7 | int n , key; 8 | cin >> n >> key; 9 | vector v(n); 10 | for(auto &temp : v) 11 | cin >> temp; 12 | int low = 0 , high = n - 1; 13 | while(low <= high){ 14 | int mid = low + high >> 1; 15 | if(key <= v[mid]) 16 | high = mid - 1; 17 | else 18 | low = mid + 1; 19 | } 20 | 21 | int start = low ; 22 | low = 0 , high = n - 1; 23 | while(low <= high){ 24 | int mid = low + high >> 1; 25 | if(key >= v[mid]) 26 | low = mid + 1; 27 | else 28 | high = mid - 1; 29 | } 30 | int end = low; 31 | if(end - start) 32 | cout << end - start << endl; 33 | else 34 | cout << - 1 << endl; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /eggdropping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=1e3; 4 | int dp[N][N]; 5 | int solve(int floor,int eggs) 6 | { 7 | if(eggs==1) 8 | return floor; 9 | if(floor<=1) 10 | return floor; 11 | 12 | if(dp[floor][eggs]) 13 | return dp[floor][eggs]; 14 | dp[floor][eggs]=INT_MAX; 15 | for (int i = 1; i <=floor; ++i) 16 | dp[floor][eggs] = min(dp[floor][eggs],1+max(solve(i-1,eggs-1),solve(floor - i,eggs))); 17 | 18 | return dp[floor][eggs]; 19 | } 20 | int main() 21 | { 22 | int floor,eggs; 23 | cin>>floor>>eggs; 24 | memset(dp,0,sizeof(dp)); 25 | solve(floor,eggs); 26 | printf("%d\n",dp[floor][eggs]); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /extended_gcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | long long xGCD(long long a, long long b, long long &x, long long &y) { 3 | if(b == 0) { 4 | x = 1; 5 | y = 0; 6 | return a; 7 | } 8 | 9 | long long x1, y1, gcd = xGCD(b, a % b, x1, y1); 10 | x = y1; 11 | y = x1 - (a / b) * y1; 12 | //printf("%d %d %d %d %d %d\n",a,b,x,y,x1,y1); 13 | return gcd; 14 | } 15 | 16 | 17 | int main() 18 | { 19 | long long x,a,b,y; 20 | long long t; 21 | scanf("%lld",&t); 22 | while(t--) 23 | { 24 | long long gcd; 25 | scanf("%lld%lld",&a,&b); 26 | gcd=xGCD(a,b,x,y); 27 | if (gcd!=1) 28 | { 29 | printf("-1\n"); 30 | } 31 | else 32 | if(x<=0) 33 | printf("%lld\n",x+b); 34 | else 35 | printf("%lld\n",x); 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /first-non-repeating-character-in-a-stream.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/first-non-repeating-character-in-a-stream/0/?ref=self 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | while(t--){ 8 | vector::iterator> has(26); 9 | int Hash[26] = { 0 }; 10 | list L; 11 | char ch; 12 | int n ; 13 | cin >> n; 14 | while(n--){ // read until end of file . 15 | cin >> ch; 16 | if(Hash[ch - 'a']){ 17 | if(has[ch - 'a'] != L.end()){ 18 | L.erase(has[ch - 'a']); 19 | } 20 | has[ch - 'a'] = L.end(); 21 | }else{ 22 | Hash[ch - 'a'] = 1; 23 | has[ch - 'a'] = L.insert(L.end(), ch); 24 | } 25 | if(L.begin() == L.end()){ 26 | cout << -1 << " "; 27 | } 28 | else{ 29 | cout << *L.begin() << " "; 30 | } 31 | } 32 | cout << endl; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /floyd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=1e3+5; 4 | int dp[N][N]; 5 | int main() 6 | { 7 | int t; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | int n; 12 | scanf("%d",&n); 13 | for(int i = 0; i < n; ++i) 14 | for(int j = 0; j < n; ++j) 15 | scanf("%d",&dp[i][j]); 16 | 17 | for(int k=0;k h; 3 | public: 4 | int lengthOfLongestSubstringKDistinct(string s, int K) { 5 | int ans = 0, unique = 0; 6 | int low = 0; 7 | for (int i = 0; i < s.size(); i++) { 8 | int idx = s[i] - 'a'; 9 | if (h[idx]++ == 0)unique++; 10 | while (unique > K) { 11 | idx = s[low++] - 'a'; 12 | if (--h[idx] == 0)unique--; 13 | } 14 | ans = max(ans, i - low + 1); 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /medianrunningSTL.cpp: -------------------------------------------------------------------------------- 1 | //Bitch Please ! :P 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | using namespace __gnu_pbds; 7 | typedef tree< 8 | int, 9 | null_type, 10 | less_equal, //less for set less_equal for multiset 11 | rb_tree_tag, 12 | tree_order_statistics_node_update> 13 | ordered_set; 14 | ordered_set X; 15 | int get(int n){ 16 | return *X.find_by_order(n); 17 | } 18 | int main() 19 | { 20 | int n,temp; 21 | scanf("%d",&n); 22 | for (int i = 0; i < n; ++i) 23 | { 24 | scanf("%d",&temp); 25 | X.insert(temp); 26 | printf("%.1f\n",i%2==0 ?get(i/2)*1.0:(get(i/2)+get(i/2+1))/2.0); 27 | 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /minimum-platform.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=1e4; 4 | int arr[N],dep[N]; 5 | int main() 6 | { 7 | int t; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | int n,l,r; 12 | scanf("%d",&n); 13 | memset(arr,0,sizeof(arr)); 14 | for (int i = 0; i < n; ++i) 15 | { 16 | scanf("%d",&arr[i]); 17 | } 18 | for (int i = 0; i < n; ++i) 19 | { 20 | scanf("%d",&dep[i]); 21 | } 22 | sort(arr,arr + n); 23 | sort(dep,dep + n); 24 | int ans=1,platform=0; 25 | int i=0,j=0; 26 | while(i 3 | using namespace std; 4 | int main() 5 | { 6 | int t; 7 | scanf("%d",&t); 8 | while(t--) 9 | { 10 | int dp[101]; 11 | fill(dp,dp+101,INT_MAX-100000); 12 | int n,del,copy,insert; 13 | scanf("%d",&n); 14 | scanf("%d%d%d",&insert,&del,©); 15 | dp[0]=0; 16 | for (int i = 1; i <=n; ++i) 17 | { 18 | if (i%2==0) 19 | { 20 | dp[i]=min(dp[i-1]+insert,dp[i/2]+copy); 21 | } 22 | else 23 | dp[i]=min(dp[i-1]+insert,dp[(i+1)/2]+copy+del); 24 | } 25 | printf("%d\n",dp[n]); 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /minimumcoinchange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=1e5+5; 4 | int dp[2][N]; 5 | int cost[N],sum,n; 6 | int main() 7 | { 8 | int t; 9 | scanf("%d",&t); 10 | while(t--) 11 | { 12 | memset(dp,0,sizeof(dp)); 13 | scanf("%d %d",&n,&sum); 14 | for (int i = 0; i < n; ++i) 15 | scanf("%d",&cost[i]); 16 | for (int i = 1; i < N; ++i) 17 | dp[0][i]=INT_MAX-500; 18 | 19 | for(int i=1;i<=n;i++) 20 | { 21 | for (int j = 1; j <=sum; ++j) 22 | { 23 | if (j-cost[i-1]>=0) 24 | { 25 | dp[i%2][j] = min(dp[(i+1)%2][j],1+dp[i%2][j-cost[i-1]]); 26 | } 27 | else 28 | dp[i%2][j] = dp[(i+1)%2][j]; 29 | } 30 | } 31 | printf("%d\n",dp[n%2][sum] > sum ? -1 :dp[n%2][sum]); 32 | } 33 | return 0; 34 | } 35 | /* 36 | 0 1 2 3 37 | 1 1 2 38 | 2 39 | 3 40 | */ 41 | -------------------------------------------------------------------------------- /missing-ranges.cpp: -------------------------------------------------------------------------------- 1 | // Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. 2 | 3 | // Example: 4 | 5 | // Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99, 6 | // Output: ["2", "4->49", "51->74", "76->99"] 7 | 8 | class Solution { 9 | public: 10 | string get_range(int start, int end) 11 | { 12 | return start==end? to_string(start) : to_string(start)+"->"+to_string(end); 13 | } 14 | vector findMissingRanges(vector& nums, int lower, int upper) { 15 | vector result; 16 | int pre = lower-1; 17 | for(int i =0; i <= nums.size(); i++) 18 | { 19 | int cur = (i==nums.size()? upper+1:nums[i]); 20 | if(cur-pre>=2) 21 | result.push_back(get_range(pre+1,cur-1)); 22 | pre = cur; 23 | } 24 | return result; 25 | } 26 | }; -------------------------------------------------------------------------------- /n%S!.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector factor(long long x) 5 | { 6 | vector res; 7 | long long i=2; 8 | while(i*i<=x) 9 | { 10 | while(x%i==0) 11 | { 12 | res.push_back(i); 13 | x=x/i; 14 | } 15 | i++; 16 | } 17 | if (x!=1) 18 | res.push_back(x); 19 | return res; 20 | } 21 | int main() 22 | { 23 | 24 | long long t; 25 | scanf("%lld",&t); 26 | while(t--) 27 | { 28 | long long n; 29 | scanf("%lld",&n); 30 | if(n==1) 31 | {puts("1");continue;} 32 | vector v=factor(n); 33 | sort(v.begin(),v.end()); 34 | long long ans=0; 35 | for (long long i = 0; i < v.size(); ++i) 36 | { 37 | auto hi=upper_bound(v.begin(), v.end(),v[i]); 38 | auto low=lower_bound(v.begin(), v.end(),v[i]); 39 | long long hehe=(hi-low); 40 | ans=max(ans,(long long)v[i]*hehe); 41 | } 42 | printf("%lld\n",ans); 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /ncr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | #define mod 1000000000 5 | 6 | ll arr[1010][1010] 7 | 8 | void ncr() 9 | { 10 | arr[0][0]=1; 11 | for (int i = 1; i < 1001; ++i) 12 | { 13 | arr[i][0]=1; 14 | for (int j = 1; j <=i; ++j) 15 | { 16 | arr[i][j]=(arr[i-1][j]+arr[i-1][j-1])%mod; 17 | } 18 | } 19 | 20 | } 21 | int main(int argc, char const *argv[]) 22 | { 23 | int t; 24 | ncr(); 25 | scanf("%d",&t); 26 | while(t--) 27 | { 28 | int w; 29 | scanf("%d",&w); 30 | for (int i = 0; arr[w][i] ; ++i) 31 | { 32 | printf("%d ",arr[w][i]); 33 | } 34 | 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /nextPermutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | int i = nums.size() - 1; 5 | if (nums.size() < 2) 6 | return; 7 | while (i > 0 && nums[i] <= nums[i - 1]) i--; 8 | if (i <= 0) { 9 | reverse(nums.begin(), nums.end()); 10 | return; 11 | } 12 | int j = i; 13 | int index = -1; 14 | int prev = -1; 15 | while (j < nums.size()) { 16 | if (nums[i - 1] >= nums[j]) break; 17 | prev = j; 18 | j++; 19 | } 20 | swap(nums[i - 1], nums[prev]); 21 | reverse(nums.begin() + i, nums.end()); 22 | } 23 | }; -------------------------------------------------------------------------------- /next_right_ptr.cpp: -------------------------------------------------------------------------------- 1 | Node * findNext(Node *r){ 2 | if(!r) return NULL; 3 | Node * s = r->nextRight; 4 | while(s){ 5 | if(s->left) return s->left; 6 | if(s->right) return s->right; 7 | s=s->nextRight; 8 | } 9 | return NULL; 10 | } 11 | void connect(Node *p) 12 | { 13 | if(!p) return; 14 | p->nextRight = NULL; 15 | 16 | while(p){ 17 | 18 | Node *q = p; 19 | 20 | while(q){ 21 | 22 | if(q->left){ 23 | if(q->right) {q->left->nextRight = q->right;} 24 | else q->left->nextRight = findNext(q); 25 | } 26 | if(q->right){ 27 | q->right->nextRight = findNext(q); 28 | } 29 | q = q->nextRight; 30 | } 31 | 32 | if(p->left) p = p->left; 33 | else if(p->right) p = p->right; 34 | else p = findNext(p); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /nqueen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int Queen[11]; 4 | int place(int row2,int col2) 5 | { 6 | int i; 7 | for(i=0;i=0) 20 | { 21 | Queen[k]++; 22 | if (Queen[k]==n) 23 | { 24 | Queen[k]=-1; 25 | k--; 26 | } 27 | else 28 | if (place(k,Queen[k])) 29 | { 30 | if (k==n-1) 31 | { 32 | printf("["); 33 | for (int i = 0; i < n; ++i) 34 | { 35 | printf("%d ",Queen[i]+1); 36 | } 37 | printf("] "); 38 | 39 | } 40 | else 41 | k++; 42 | } 43 | 44 | } 45 | } 46 | int main() 47 | { 48 | int t; 49 | scanf("%d",&t); 50 | while(t--) 51 | { 52 | int n; 53 | scanf("%d",&n); 54 | memset(Queen,-1,sizeof(Queen)); 55 | if(n==2||n==3) 56 | printf("-1"); 57 | else 58 | nqueen(n); 59 | printf("\n"); 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /partition-to-k-equal-sum-subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector visited; 3 | public: 4 | bool canPartitionKSubsets(vector& nums, int k) { 5 | visited.resize(nums.size() + 1, 0); 6 | long long sum = accumulate(nums.begin(), nums.end(), 0); 7 | sort(nums.rbegin(), nums.rend()); 8 | if (sum % k != 0) 9 | return false; 10 | int newSum = sum / k; 11 | function find = [&](int idx, int sum, int k) { 12 | if (k == 1) 13 | return sum == newSum; 14 | if (sum == 0) 15 | return find(0, newSum, k - 1); 16 | for (int i = idx; i < nums.size(); i++) { 17 | if (!visited[i] && sum >= nums[i]) { 18 | visited[i] = 1; 19 | if (find(idx + 1, sum - nums[i], k)) 20 | return true; 21 | visited[i] = 0; 22 | } 23 | } 24 | return false; 25 | }; 26 | return find(0, newSum, k); 27 | } 28 | }; -------------------------------------------------------------------------------- /rightViewofBT.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector ans; 3 | public: 4 | vector rightSideView(TreeNode* root) { 5 | dfs(root, 0); 6 | return ans; 7 | } 8 | void dfs(TreeNode* root, int level){ 9 | if(root == NULL){ 10 | return; 11 | } 12 | if(ans.size() == level){ 13 | ans.push_back(root -> val); 14 | } 15 | dfs(root -> right, level + 1); 16 | dfs(root -> left , level + 1); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /sortColors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class Solution { 6 | public: 7 | void sortColors(vector& nums) { 8 | int high = nums.size() - 1, low = 0, mid = 0; 9 | while (mid <= high) { 10 | switch (nums[mid]) 11 | { 12 | case 0: 13 | swap(nums[mid], nums[low]); 14 | mid++, low++; 15 | break; 16 | case 1: 17 | mid++; 18 | break; 19 | case 2: 20 | swap(nums[mid], nums[high]); 21 | high--; 22 | break; 23 | } 24 | } 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /sorted_and_rotated_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n, key; 5 | cin >> n >> key; 6 | vector arr(n); 7 | for(auto &temp : arr) 8 | cin >> temp; 9 | int low = 0 , high = n - 1; 10 | bool ok = false; 11 | while(low <= high){ 12 | int mid = low + high >> 1; 13 | if(arr[mid] == key){ 14 | cout << "Found at " << mid; 15 | ok = true; 16 | break; 17 | } 18 | if(arr[low] <= arr[mid]){ 19 | if(key >= arr[low] and key < arr[mid]) 20 | high = mid - 1; 21 | else 22 | low = mid + 1; 23 | } 24 | if(arr[mid] <= arr[high]){ 25 | if(key <= arr[high] and key > arr[mid]) 26 | low = mid + 1; 27 | else 28 | high = mid - 1; 29 | } 30 | } 31 | if(!ok) 32 | cout << "Not Found\n"; 33 | } 34 | -------------------------------------------------------------------------------- /strobogrammatic-number-ii.cpp: -------------------------------------------------------------------------------- 1 | // A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). 2 | 3 | // Find all strobogrammatic numbers that are of length = n. 4 | 5 | // Example: 6 | 7 | // Input: n = 2 8 | // Output: ["11","69","88","96"] 9 | 10 | class Solution { 11 | public: 12 | vector findStrobogrammatic(int n) { 13 | return helper(n , n); 14 | } 15 | vector helper(int m, int n){ 16 | if(m == 0) return vector({""}); 17 | if(m == 1) return vector({"0", "1", "8"}); 18 | vector tmp = helper(m - 2, n), res; 19 | for(int i = 0; i < tmp.size(); i++){ 20 | if(m != n) res.push_back("0" + tmp[i] + "0"); 21 | res.push_back("1" + tmp[i] + "1"); 22 | res.push_back("6" + tmp[i] + "9"); 23 | res.push_back("8" + tmp[i] + "8"); 24 | res.push_back("9" + tmp[i] + "6"); 25 | } 26 | return res; 27 | } 28 | }; -------------------------------------------------------------------------------- /strobogrammatic-number.cpp: -------------------------------------------------------------------------------- 1 | // A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). 2 | 3 | // Write a function to determine if a number is strobogrammatic. The number is represented as a string. 4 | 5 | // Example 1: 6 | 7 | // Input: "69" 8 | // Output: true 9 | // Example 2: 10 | 11 | // Input: "88" 12 | // Output: true 13 | // Example 3: 14 | 15 | // Input: "962" 16 | // Output: false 17 | 18 | public boolean isStrobogrammatic(String num) { 19 | for (int i=0, j=num.length()-1; i <= j; i++, j--) 20 | if (!"00 11 88 696".contains(num.charAt(i) + "" + num.charAt(j))) 21 | return false; 22 | return true; 23 | } -------------------------------------------------------------------------------- /sum-of-distances-in-tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sumOfDistancesInTree(int N, vector>& edges) { 4 | vector down(N), count(N + 1); // do we need it ?! [Check] 5 | vector graph[N]; 6 | for (int i = 0; i < edges.size(); i++) { 7 | graph[edges[i][0]].push_back(edges[i][1]); 8 | graph[edges[i][1]].push_back(edges[i][0]); 9 | } 10 | function dfsdown = [&](int u, int p) { 11 | count[u] = 1; 12 | for (auto v : graph[u]) { 13 | if (v != p) { 14 | dfsdown(v, u); 15 | count[u] += count[v]; 16 | down[u] += count[v] + down[v]; 17 | } 18 | } 19 | }; 20 | function dfsup = [&](int u, int p) { 21 | for (auto v : graph[u]) { 22 | if (v != p) { 23 | down[v] = down[u] - count[v] + N - count[v]; 24 | dfsup(v, u); 25 | } 26 | } 27 | }; 28 | dfsdown(0, -1); 29 | dfsup(0, -1); 30 | return down; 31 | } 32 | }; -------------------------------------------------------------------------------- /triplet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=1e3+5; 4 | int arr[N]; 5 | int main() 6 | { 7 | int t; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | int sum,n; 12 | scanf("%d %d",&n,&sum); 13 | for (int i = 0; i < n; ++i) 14 | { 15 | scanf("%d",&arr[i]); 16 | } 17 | sort(arr,arr+n); 18 | int count=0; 19 | for (int i = 0; i < n; ++i) 20 | { 21 | int req = arr[i]; 22 | int l=i+1,r=n-1; 23 | 24 | while(l sum - req) 29 | r--; 30 | else 31 | printf("%d %d %d\n",arr[i],arr[l],arr[r]), 32 | count++,l++,r--; 33 | } 34 | } 35 | printf("%d\n",count>0); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /upperBoundLowerBound.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PawanAIT/AlgorithmsLibrary/b5687782ddfbbcbe77462ef9ee058e846354570b/upperBoundLowerBound.cpp -------------------------------------------------------------------------------- /wiggle-sort.cpp: -------------------------------------------------------------------------------- 1 | // Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... 2 | 3 | // Example: 4 | 5 | // Input: nums = [3,5,2,1,6,4] 6 | // Output: One possible answer is [3,5,1,6,2,4] 7 | 8 | The final sorted nums needs to satisfy two conditions: 9 | 10 | If i is odd, then nums[i] >= nums[i - 1]; 11 | If i is even, then nums[i] <= nums[i - 1]. 12 | The code is just to fix the orderings of nums that do not satisfy 1 and 2. 13 | 14 | class Solution { 15 | public: 16 | void wiggleSort(vector& nums) { 17 | int n = nums.size(); 18 | for (int i = 1; i < n; i++) 19 | if (((i & 1) && nums[i] < nums[i - 1]) || (!(i & 1) && nums[i] > nums[i - 1])) 20 | swap(nums[i], nums[i - 1]); 21 | } 22 | }; 23 | --------------------------------------------------------------------------------