├── .gitignore ├── A2OJ ├── A - Watermelon.cpp ├── Given length and sum of digits.cpp └── Number of Ways.cpp ├── Codechef ├── APRIL2020 - LONG │ ├── CARSELL.cpp │ ├── COVIDLQ.cpp │ ├── STRNO.cpp │ └── UNITGCD.cpp ├── APRIL2020-LONG │ ├── April 20A.cpp │ ├── April 20B.cpp │ └── April 20C.cpp ├── AUG-Long1.cpp ├── AUG-Long2.cpp ├── AUG-Long3.cpp ├── AUG-Long4.cpp ├── AUG-Long5.cpp ├── AUG-Lunch Time │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── Anagram Substring Search.cpp ├── Biteration1.cpp ├── Biteration2.cpp ├── Biteration3.cpp ├── COC battle.cpp ├── Chef and Mean.cpp ├── Competitive-Programming - Shortcut.lnk ├── EID.cpp ├── FEB2020-LONG │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ ├── 4.cpp │ └── 5.cpp ├── GOOGOL05 STL.cpp ├── June 19A.cpp ├── June 19B.cpp ├── June 19C.cpp ├── June-cookoff1.c ├── June-cookoff1.cpp ├── June-cookoff2.cpp ├── KMP Algoithm.cpp ├── LTIME74B.cpp ├── LTIME74B_SWAPPING.cpp ├── LTIME74B_SWAPPING1.cpp ├── OCT-Long1.cpp ├── OCT-Long2.cpp ├── OCT-Long3.cpp ├── OCT-Long4.cpp ├── OCT-Long6.cpp ├── Paying up.cpp ├── Rat Maze.cpp ├── SEPT Lunch Time │ ├── q1.cpp │ └── q2.cpp ├── SEPT-LONG2 ├── SEPT-LONG2.cpp ├── SEPT-Long1 ├── SEPT-Long1.cpp ├── SEPT2020-LUNCHTIME │ ├── GCD operations.cpp │ ├── Root the Tree.cpp │ └── Watermelon.cpp ├── Sorting Tool.cpp ├── String matching.cpp ├── Take Turn STL.cpp ├── Untitled4.cpp ├── cook off1.cpp └── cook off2.cpp ├── Coding Blocks ├── Binary Strings without consecutive 1.cpp ├── Divide & Conquer │ ├── Agressive Cows.cpp │ ├── Alpha Score.cpp │ ├── Binary Search in Sorted Rotated Array.cpp │ ├── Inversion_ Count.cpp │ ├── Maximum Subarray Sum.cpp │ ├── Painter Partition Problem.cpp │ ├── Pair of Shoses.cpp │ ├── Read the pages.cpp │ ├── Simple Enough.cpp │ └── Square Root.cpp ├── Favorite Dice.cpp ├── Graph │ ├── Dijkastra Algorithm.cpp │ ├── Krukshal Algorithm.cpp │ ├── Prim's Algorithm.c │ └── Prim's Algorithm.cpp ├── Hostel Visit.cpp ├── Mathematics │ ├── Birthday Parradox problem.cpp │ ├── Boston numbers.cpp │ ├── Catalan number.cpp │ ├── Divisible subarray.cpp │ ├── Evaluating Function.py │ ├── Factorial Problem.cpp │ ├── Fast Fibonacci.cpp │ ├── Favorite Dice.cpp │ ├── Fibosum.cpp │ ├── Gray similar code.cpp │ ├── Mables.py │ ├── Marbles Star & Bar theorem.cpp │ ├── Math day.py │ ├── Matrix exponentiation.cpp │ ├── Modular fast exponentiation.cpp │ ├── NK ladder problem.cpp │ ├── Random query.cpp │ ├── Recursive Sequence 2.cpp │ ├── Recursive Sequence.cpp │ └── Summing Sum.cpp ├── Matrix exponentiation.cpp ├── Median of Sorted Array.cpp ├── Modular fast exponentiation.cpp ├── NK ladder problem.cpp ├── Not so easy math.cpp ├── Number Theory │ ├── Alice and Totient │ ├── Alice and Totient.cpp │ ├── Boring Factorial.cpp │ ├── Deepak and Prime.cpp │ ├── Extended Euclids algorithm.cpp │ ├── Fast Modular Exponentiation using Bitmasking.cpp │ ├── Find It.cpp │ ├── Light's new car.cpp │ ├── Multiplicative Modular inverse.cpp │ ├── Nth prime.cpp │ ├── Number of Divisors.cpp │ ├── Playing with divisors is fun.cpp │ ├── Prime Factor │ ├── Prime Factor.cpp │ ├── Segmented Sieve.cpp │ ├── Sieve OF Eranthesesnes.cpp │ ├── T factor.cpp │ └── Virat and nCr.cpp ├── Number of BST with N nodes.cpp ├── Playing with bits.cpp ├── Recursion and Backtrackking │ ├── 2048 problem.cpp │ ├── Absolute the Sum.cpp │ ├── Binary String.cpp │ ├── Class Assignement.cpp │ ├── Codes of String.cpp │ ├── Expand the Pond.cpp │ ├── Friends Pairing Problem.cpp │ ├── Generate Parentheses.cpp │ ├── N queen using backtracking.cpp │ ├── No same Permutations.cpp │ ├── Permuatations of String.cpp │ ├── Phone Keypad.cpp │ ├── Quick Sort.cpp │ ├── Rat in Maze Variation │ │ ├── Rat in Maze.cpp │ │ └── desktop.ini │ ├── Recursion sequences.cpp │ ├── Smart Keypad Advanced.cpp │ ├── Smart Keypad-1.cpp │ ├── Subset Sum Easy.cpp │ ├── Sudoku Solver.cpp │ ├── SumITUp.cpp │ ├── Tiling Problem.cpp │ ├── Tower of Hanoi.cpp │ ├── Tricky Permutations.cpp │ ├── increasing_decresing no.cpp │ ├── issorted.cpp │ └── random_number_generator.cpp ├── Segment Tree │ ├── Array Queries1.cpp │ ├── KQuery.cpp │ ├── KQueryO.cpp │ ├── MAX Query1.cpp │ ├── MIN QUERY1.cpp │ ├── Matrix Queries1.cpp │ ├── Maximum Sum Query.cpp │ ├── Range Minimum Query.cpp │ ├── Sum of given range.cpp │ └── number of distinct elements in range.cpp ├── Setbits.cpp ├── String Sort.cpp ├── String Window.cpp ├── Subarraty with distinct elements.cpp ├── Subset sum.cpp ├── Tavas and saddas.cpp ├── Ultra-Fast-Mathematicians.cpp ├── Unique number 2.cpp ├── Unique number 3.cpp ├── Unlock.cpp ├── Untitled4.cpp ├── bitmasking.h └── test.cpp ├── GFG ├── Array │ ├── Biotonic Array.cpp │ ├── Count Distinct element in every window of size K.cpp │ ├── Equal To Product.cpp │ ├── Equilibrium Point.cpp │ ├── Find duplicates in Array.cpp │ ├── Four number Sum.cpp │ ├── Increasing Triplet Subsequence.cpp │ ├── K frequent Elements.cpp │ ├── K largest elements.cpp │ ├── Kadanes Algorithm.cpp │ ├── Kth Smallest.cpp │ ├── Largest subarray with equal number of 0s and 1s.cpp │ ├── Largest sum subarray of size atleast K.cpp │ ├── Last digit of A^B for large numbers.cpp │ ├── Leaders in an array.cpp │ ├── Longest subarray with sum K.cpp │ ├── Maximum Product Subarray.cpp │ ├── Maximum Product of Three elements.cpp │ ├── Maximum Subarray.cpp │ ├── Maximum index Difference.cpp │ ├── Maximum of all Subarray of Size K.cpp │ ├── Median of two sorted array of equal size.cpp │ ├── Minimum Indexed.cpp │ ├── Minimum Platform.cpp │ ├── Move zero to end of Array.cpp │ ├── Pythagorean Triplet.cpp │ ├── Quick Select.cpp │ ├── Rain Water Trapping.cpp │ ├── Reverse array in Group.cpp │ ├── Smallest subarray with Sum greater than X.cpp │ ├── Sort an array of 0s, 1s and 2s │ ├── Sort an array of 0s, 1s and 2s.cpp │ ├── Sorted subsequence of size 3.cpp │ ├── Stickler Thief.cpp │ ├── Stock Buy and Sell.cpp │ ├── Subarray with equal number of 0s and 1s.cpp │ ├── Subarray with given sum.cpp │ ├── Triplet Sum in Array.cpp │ ├── Triplets with zero sum.cpp │ └── Zero Sum Subarray.cpp ├── Bit-Manipulation │ ├── Equal Sum and XOR.cpp │ ├── Finding Number.cpp │ ├── Flip Bits.cpp │ ├── Game of XOR.cpp │ ├── Josephous Problem.cpp │ ├── Magic Triplets.cpp │ ├── Maximum AND value.cpp │ ├── Maximum Subarray XOR.cpp │ ├── Maximum XOR Pair.cpp │ ├── Minimum XOR Pair.cpp │ ├── Multiple of 3.cpp │ ├── Replace N by M.cpp │ ├── Reverse Bits.cpp │ ├── Sum of Bit Difference.cpp │ ├── Sum of XOR of all pair.cpp │ ├── Swap Odd bits with Even bits.cpp │ └── bitmasking.h ├── Graph │ ├── BFS of directed graph.cpp │ ├── DFS of undirected graph.cpp │ ├── Detect Cycle in Directed graph.cpp │ └── Detect cycle in an undirected graph.cpp ├── Matrix │ └── Rotate a matrix by 90 degree clockwise.cpp ├── Stack-Queue │ ├── Balanced Paranthesis.cpp │ ├── Bracket Number.cpp │ ├── Celebrity Problem.cpp │ ├── Count Reversals.cpp │ ├── Decode the String.cpp │ ├── First Negative of Window of Size K.cpp │ ├── Generate Binary Numbers.cpp │ ├── Immediate Smaller Element.cpp │ ├── Implement two stacks in Array.cpp │ ├── Jumping Numbers.cpp │ ├── Longest Valid Parenthesis.cpp │ ├── Maximum Difference.cpp │ ├── Maximum Reactangular Area in Histogram.cpp │ ├── Maximum area Rectangle in Binary Matrix.cpp │ ├── Maximum of Minimum of every window of Size K.cpp │ ├── Minimum Element in Stack.cpp │ ├── Next Greater Element.cpp │ ├── Next Smaller Element.cpp │ ├── Queue using Two Stack.cpp │ ├── Reverse words in given string.cpp │ ├── Sort a Stack.cpp │ ├── Stack using two queue.cpp │ ├── Stock Span Problem.cpp │ ├── String Manipulation.cpp │ └── tempCodeRunnerFile.cpp ├── Strings │ └── Count substring with same starting and ending character.cpp └── Tree │ ├── BInaryTree_Operations.h │ ├── Balanced Tree.cpp │ ├── Bottom View.cpp │ ├── Boundary Traversal.cpp │ ├── Check if Subtree.cpp │ ├── Connect nodes at Same Level.cpp │ ├── Diagonal Sum.cpp │ ├── Diameter of Binary Tree.cpp │ ├── Duplicate Subtree in Tree.cpp │ ├── Find the Closest Element in Binary Search Tree BST.cpp │ ├── Isomorphic Tree.cpp │ ├── LCA.cpp │ ├── Left Right View of Binary Tree.cpp │ ├── Level Order Traversal Line By Line.cpp │ ├── Level Order Traversal in Spiral Form.cpp │ ├── Maximum Node Level.cpp │ ├── Maximum Root to Leaf Path Sum.cpp │ ├── Maximum Sum between leaves.cpp │ ├── Maximum Width of Binary Tree.cpp │ ├── Minimum Depth.cpp │ ├── Minimum distance between two Nodes.cpp │ ├── N-ary Tree │ ├── Check mirror in n-ary tree.cpp │ └── is Valid BST(Binary Search Tree).cpp │ ├── Node at a distance from Leaf.cpp │ ├── Print nodes that not have sibling.cpp │ ├── Remove half nodes.cpp │ ├── Root to Leaf Path.cpp │ ├── Root to Leaf path Sum.cpp │ ├── Serialize and Deserialize.cpp │ ├── Sum root to Leaf Numbers.cpp │ ├── SumTree.cpp │ ├── Symmetric Tree foldable Tree.cpp │ ├── Ternary Expression to Binary Tree.cpp │ ├── Top View.cpp │ ├── Transform to Sum Tree.cpp │ └── Vertical Order Traversal.cpp ├── Hackerearth ├── .cpp ├── 1.cpp ├── ATM QUEUE HEIGHT.cpp ├── Algorithms │ ├── Little Monk and Good string.cpp │ ├── Sorting │ │ ├── Easy Going.cpp │ │ ├── Maximum building speed.cpp │ │ └── Puchhi and Luggage.cpp │ └── String Algorithm │ │ ├── Hamming.cpp │ │ ├── Humming Distance.cpp │ │ ├── Message Transfer2.cpp │ │ └── Playful string.cpp ├── Array Rotation.cpp ├── Beautiful Segments.cpp ├── Binary Search Modified.cpp ├── Binary Search.cpp ├── Bit flipping game.cpp ├── Bitset.cpp ├── BooBoo and Upsolving(Binary Search).cpp ├── Building Height in street.cpp ├── Chandan and Balanced strings.cpp ├── Code Arena 2.cpp ├── Compare strings using binary search.cpp ├── Continious Sequence Coaches.cpp ├── Count subarrays having sum greater than K.cpp ├── Deque STL.cpp ├── Distinct elements in subarray.cpp ├── Eqilibrium Index of an array.cpp ├── First Non Repeating character.cpp ├── Flight Takeoff.ccp.c ├── Flight Takeoff.cpp ├── Game of OXA.cpp ├── Graph Theory │ ├── Bishu and his girlfiend.cpp │ ├── El Nino │ ├── El Nino.cpp │ ├── Guddi Trapped in Room │ ├── Guddi Trapped in Room.cpp │ ├── Happy Vertices │ ├── Happy Vertices.cpp │ ├── Kingdom of Monkeys.cpp │ ├── Monk in real state.cpp │ ├── So NP.cpp │ ├── test │ └── test.cpp ├── Greater than right.c ├── Greater than right.cpp ├── Hashing.cpp ├── Hourstorm#14 │ └── HS1.cpp ├── Implement Queue using Stack.cpp ├── Lko1.cpp ├── Longest subsequence with common digit.cpp ├── Lower bound and upper Bound.cpp ├── MAximum Sum subarray and no of such subarrays.cpp ├── Maximum Sum subarray greater than K.cpp ├── Maximum even subarray.cpp ├── Merge Sorted Liinked List.cpp ├── Micro and Binary strings.cpp ├── Minimum Sum formed by two digits.cpp ├── Minimum length of subarray having sum greater than or equal to K.cpp ├── Minimum length of subarray using Binary Search.cpp ├── Minimus sum formed by digits.cpp ├── Missing Numbers.cpp ├── Next Greater number.cpp ├── Nth prime.cpp ├── Number Theory │ └── tutorial.cpp ├── Police And Thief Grid.cpp ├── Prime factors.cpp ├── Quadratic Equation.cpp ├── Queue using Linked list.cpp ├── Roud table.cpp ├── Shubham and binary strings.cpp ├── Stack using Queue.cpp ├── Stack using node.cpp ├── Stack.c ├── Stack.cpp ├── Strange Game.cpp ├── Subarray with 0 sum.cpp ├── Subset AND.cpp ├── Subtract 1 from array and make them zero.cpp ├── Sum of Cubes of three prime number.cpp ├── Sum of numbers.cpp ├── Untitled1.cpp ├── Untitled2.cpp ├── Untitled3.cpp ├── Untitled4.cpp ├── Untitled5.c ├── Untitled5.cpp ├── long2.cpp ├── lunch.cpp ├── lunch1.cpp ├── max frequency.cpp ├── maximum in subarray.cpp ├── maximum sum subarray.cpp ├── minimum time to update array.cpp ├── repeated K times.cpp ├── string check.cpp ├── target.cpp ├── template.cpp └── test.cpp ├── Hackerrank + Geeks for Geeks + DS Courseware ├── ACM ICPC team.cpp ├── AP in array.c ├── ASSIGN1.cpp ├── ASSIGN2.cpp ├── ASSIGN3.cpp ├── Absolute Permutation.cpp ├── Absolute Permutation1.cpp ├── Array Frequency.cpp ├── Array Manipulation.cpp ├── Array Rotation.c ├── BST Lowest Common Ancestor.cpp ├── Binary Number using Queue.cpp ├── Binary Search Tree PKS.cpp ├── Binary Search Tree.cpp ├── Bubble Sort.c ├── Bubble Sort.cpp ├── Chase Problem.cpp ├── Circular Linked List.cpp ├── Counting Sort.c ├── Counting Sort.cpp ├── DOUBLE LL EVALUATION.cpp ├── Delete Duplicate Linked List.c ├── Delete N node after M node.cpp ├── Dequeue Array.cpp ├── Dequeue.cpp ├── Dequeue2.cpp ├── Determinant of Matrix.cpp ├── Double Ended Priority Queue.cpp ├── Double Linkec List.cpp ├── Double Linked List PKS.cpp ├── Factorial big number.cpp ├── Final1.cpp ├── Find Merge Point of Two Lists.cpp ├── Find x so that sum become zero.cpp ├── GCD.c ├── Get Min at Top in Stack.cpp ├── Graph Representation.cpp ├── Heap Sort.cpp ├── Infix to Postfix 2.cpp ├── Infix to Postfix.cpp ├── Insertion Sort.cpp ├── Inverse of a Matrix.cpp ├── Kadane's Algorithm.c ├── Kadane's Algorithm.cpp ├── LAB2.cpp ├── LAB_EVAL.cpp ├── LL Practice.c ├── Lab1.cpp ├── Linked List Implementation.cpp ├── Linked List using Array.cpp ├── Linked List.cpp ├── Linked list 2.cpp ├── Linked list3.cpp ├── Loop In Linked List.cpp ├── Matrix Multiplication.cpp ├── Maximum sum subarray using DC ├── Maximum sum subarray using DC.cpp ├── Mege Sort.cpp ├── Merge K sorted arrays.cpp ├── Modified Kaprekar Number.c ├── Next Permutation.cpp ├── Optimal Sequence for AVL insertion.cpp ├── Pallindrome Linked List.c ├── Pallindrome Linked List.cpp ├── Postfix Evaluation.cpp ├── Priority Queue.cpp ├── Pythagorean Triplet.c ├── Quadratic Probing.cpp ├── Queue Array.cpp ├── Queue Implementation using Array.cpp ├── Queue Linked_List.cpp ├── Quick Sort.cpp ├── Range sum.cpp ├── Reverse Linked list using stack.cpp ├── Rotate Matrix.c ├── Selection Sort.c ├── Selection Sort.cpp ├── Selection with Recursion.c ├── Sieve of Eratosthenes.c ├── Sieve of Eratosthenes.cpp ├── Smallest Subarray.c ├── Sort Stack.cpp ├── Sorted Array to BST.cpp ├── Spiral Matrix.c ├── Stack Array.cpp ├── Stack Implementation using Array.c ├── Stack Implementation using Array.cpp ├── Stack using Linked List.cpp ├── Stock Span Problem.cpp ├── Strassens Algorithm.c ├── String.cpp ├── Sub matrix with ALL 1.c ├── Sum of all subarrays.cpp ├── Ternary search.cpp ├── Untitled1.c ├── Untitled1.c.save-failed ├── Untitled1.cpp ├── Untitled2 - Copy.c ├── Untitled2.c ├── Untitled2.cpp ├── Untitled3.cpp ├── Untitled4.c ├── Untitled4.cpp ├── Untitled5 - Copy.c ├── Untitled5.c ├── Untitled5.cpp ├── Untitled6.c ├── Untitled7.c ├── Username and factorial.c ├── Vowel in Sub string.cpp ├── XOR of all subarrays.cpp ├── battle1.cpp ├── binary.c ├── check.c ├── codechef robo.c ├── convert string to integer.c ├── cp2.cpp ├── delete from array.c ├── demo.txt ├── demo.txt.txt ├── demo1.txt ├── duplicate element.c ├── hackerrank encryption.c ├── hck.txt ├── hexa.c ├── inversion array.c ├── iterator.cpp ├── just check.c ├── lcm.c ├── max contigious sum.c ├── max with least comparison.c ├── min max pallindrome 1.c ├── min max pallindrome.c ├── nand.c ├── nand1.c ├── output.txt ├── pair.cpp ├── pattern1.c ├── pattern2.c ├── pattern3.c ├── pc1.cpp ├── prime.c ├── py.jpg ├── reverse linked list.cpp ├── reverse string.c ├── roman_yr.c ├── second max in array.c ├── shifting container.cpp ├── shreya.c ├── sin_series.c ├── snackdown.c ├── sort name.c ├── sort on frequency.c ├── split Array.c ├── squareinsquare.c ├── string to int.c ├── stringMultiplication.cpp ├── test.cpp ├── untitled3.c └── untitled3.c.save-failed ├── Leetcode ├── Array │ ├── Add Binary.cpp │ ├── Add Operators.cpp │ ├── Add Strings.cpp │ ├── Best Time To Buy And Sell Stock 3 & 4.cpp │ ├── Best time to buy and sell stock 1.cpp │ ├── Car Pooling.cpp │ ├── Container With Maximum Water.cpp │ ├── Continious subarray sum.cpp │ ├── Decode Ways.cpp │ ├── Employee Free Time.cpp │ ├── Exam Rooms.cpp │ ├── Exclusive time of function.cpp │ ├── Find K closest elements.cpp │ ├── Find Median from Data Stream.cpp │ ├── Find Pivot.cpp │ ├── Find all dissappeared number in an array.cpp │ ├── Find duplicate number in array.cpp │ ├── Find first and last occurence of element.cpp │ ├── First Bad Version.cpp │ ├── Friends of Appropriate Ages.cpp │ ├── Insert Interval.cpp │ ├── Intersection of Three 3 sorted arrays.cpp │ ├── Intersection of Two Arrays.cpp │ ├── Intersection of two arrays II.cpp │ ├── Interval List Intersection.cpp │ ├── K Closest Points.cpp │ ├── Koko Eating Bananas.cpp │ ├── Kth Largest Element in Array.cpp │ ├── Longest Continious Increasing Subsequence(Subarray).cpp │ ├── Majority Element.cpp │ ├── Majority Element2.cpp │ ├── Maximum Consecutive 1s.cpp │ ├── Maximum sum in circular subarray.cpp │ ├── Maximum sum of 3 non overlapping subarray.cpp │ ├── Median of Two Sorted Arrays.cpp │ ├── Meeting Rooms.cpp │ ├── Meeting Rooms2.cpp │ ├── Merge Intervals.cpp │ ├── Merge Sorted Array.cpp │ ├── Merge Three 3 Sorted Arrays.cpp │ ├── Minimum Arrows to Burst Balloons.cpp │ ├── Minimum in rotated sorted array.cpp │ ├── Missing Element in Sorted Array.cpp │ ├── Monotonic Array.cpp │ ├── Move Zeroes.cpp │ ├── Next Permutation.cpp │ ├── Non Overlapping Intervals.cpp │ ├── Pair with given sum in Sorted and rotated array.cpp │ ├── Pancake Sorting.cpp │ ├── Partition Equal Subset Sum.cpp │ ├── Peak Element.cpp │ ├── Plus One.cpp │ ├── Product of array except itself.cpp │ ├── Range Sum Query 2D Immutable.cpp │ ├── Read N characters given Read4.cpp │ ├── Remove duplicates element.cpp │ ├── Reverse to Make Equal.cpp │ ├── Rotate Arrays.cpp │ ├── Search in rotated sorted array.cpp │ ├── Single Element in Sorted Array.cpp │ ├── Sort Colors.cpp │ ├── Sorted Iterator.cpp │ ├── Split Array With Equal Sum.cpp │ ├── Target Sum.cpp │ ├── Task Scheduler.cpp │ ├── Teemo Attacking.cpp │ ├── Three 3 Sum Closest.cpp │ ├── Three Sum.cpp │ ├── Top K Frequent Words.cpp │ ├── Top K frequent Elements.cpp │ ├── Two Sum sorted.cpp │ ├── Two Sum.cpp │ ├── Valid Numbers.cpp │ ├── Valid Sudoku.cpp │ ├── Verifying an Alien Dictionary.cpp │ ├── Wiggle Sort Wave Array.cpp │ └── Wood Cut or Cut Wood.cpp ├── Backtracking │ ├── Combination Sum.cpp │ ├── Increasing Subsequence.cpp │ ├── Permutation.cpp │ └── Subset.cpp ├── Binary Indexed Tree │ └── Queue reconstruction by height.cpp ├── Bit Manipulation │ └── Counting Setbits.cpp ├── DP │ ├── Climbing Stairs.cpp │ ├── Coin Change.cpp │ ├── Dungeon Game.cpp │ ├── House Thief Robber.cpp │ ├── Jump Game.cpp │ ├── Longest Increasing Subsequence.cpp │ ├── Longest string chain.cpp │ ├── Maximal Square.cpp │ ├── Minimum Cost For Tickets.cpp │ ├── Minimum Falling Path Sum.cpp │ ├── Minimum Path Sum.cpp │ ├── Perfect Squares.cpp │ └── Unique Paths.cpp ├── Graph │ ├── 01 Matrix.cpp │ ├── Accounts Merge.cpp │ ├── Alien Dictionary.cpp │ ├── Cheapest flight within K stops.cpp │ ├── Clone Graph.cpp │ ├── Course Schedule.cpp │ ├── Cutoff Trees for Golf Event.cpp │ ├── Employee Importance.cpp │ ├── Escape a large Maze.cpp │ ├── Finding Bridge in a graph.cpp │ ├── Flood Fill.cpp │ ├── Friend Circles.cpp │ ├── Is Graph Bipartite.cpp │ ├── Island Perimeter.cpp │ ├── Longest Increasing Path in a Matrix.cpp │ ├── Maximum Area of Island.cpp │ ├── Minesweeper.cpp │ ├── Minimum Knight Moves.cpp │ ├── Minimum Time to Collect all the Apples.cpp │ ├── Network Delay Time.cpp │ ├── Number of Battle Ships.cpp │ ├── Number of Connected Components in an Undirected Graph.cpp │ ├── Number of Islands.cpp │ ├── Pacific Atlantic Water Flow.cpp │ ├── Reachable Nodes in Sub DIvided Graph.cpp │ ├── Reconstruct Iternary.cpp │ ├── Restore IP Address.cpp │ ├── Rotting Oranges.cpp │ ├── Shortest Bridge.cpp │ ├── Shortest Minimum Distance from All Buildings.cpp │ ├── Shortest Path in Binary Matrix.cpp │ ├── Similar String Group.cpp │ ├── Sliding Puzzle.cpp │ ├── Surrounded Region.cpp │ ├── Swim in Rising Water.cpp │ ├── Topological Sort.cpp │ ├── Tree Diameter Unordered Graph Diameter.cpp │ ├── Walls and Gates Guards.cpp │ ├── Word Ladder 2.cpp │ ├── Word Ladder.cpp │ ├── Word Search 2.cpp │ └── Word Search.cpp ├── Linked List │ ├── Add Two2 Numbers.cpp │ ├── Convert BST to Doubly Linked List.cpp │ ├── Convert Binary Number to Linked List.cpp │ ├── Convert Sorted List to Binary Search Tree.cpp │ ├── Copy List With Random Pointer.cpp │ ├── Delete N nodes after every M nodes.cpp │ ├── Delete Remove Nth node from end of Linked List.cpp │ ├── Delete node in Linked List.cpp │ ├── Flatten Binary Tree to Linked List.cpp │ ├── Flatten Nested List Iterator.cpp │ ├── Flatten a Multilevel Doubly Linked List.cpp │ ├── Insert into cyclic sorted Linked List.cpp │ ├── Insertion Sort Linked List.cpp │ ├── Intersection of two linked list.cpp │ ├── LRU Cache.cpp │ ├── Linked List Cycle.cpp │ ├── Linked List in Binary Tree.cpp │ ├── Merge 2 Linked List.cpp │ ├── Merge K sorted linked list.cpp │ ├── Merge in between Linked List.cpp │ ├── Middle of Linked List.cpp │ ├── Nested List Weight Sum.cpp │ ├── Odd Even Linked List.cpp │ ├── Pallindrome Linked List.cpp │ ├── Partition List.cpp │ ├── Print Reverse Linked List.cpp │ ├── Remove 0 sum consecutive node.cpp │ ├── Remove Duplicates from Sorted Linked List.cpp │ ├── Remove Linked List Elements.cpp │ ├── Reorder List.cpp │ ├── Reverse Linked List.cpp │ ├── Reverse Nodes in K Group.cpp │ ├── Rotate List.cpp │ ├── Split Linked List in Parts.cpp │ ├── Swap Nodes in Pair.cpp │ └── Swapping Node in a Linked List.cpp ├── Misclenaous │ ├── Data Stream as Disjoint Intervals.cpp │ ├── Design Circular Queue.cpp │ ├── Diagonal Matrix Traversal.cpp │ ├── Divide two integers.cpp │ ├── Dot Product of Sparse Vectors.cpp │ ├── Fast Exponentiation Pow.cpp │ ├── Find K Pairs with Smallest Sum.cpp │ ├── Find most Frequent Element in All Intervals.cpp │ ├── Find the Kth Smallest Sum of a Matrix with Sorted Rows.cpp │ ├── Fraction to Recurring Decimal.cpp │ ├── Index of Maximum Ocurring Element with Equal Probability.cpp │ ├── Insert Delete Get Random in O(1).cpp │ ├── Integer to Roman.cpp │ ├── Kth Smallest Element in Sorted Matrix.cpp │ ├── Left Most Column with Atleast a one.cpp │ ├── Letter Combination or Phone Keypad.cpp │ ├── Logger Rate Limiter.cpp │ ├── Maximum Swap.cpp │ ├── Minimum Area Rectangle.cpp │ ├── Nth Digit.cpp │ ├── Number of Days between two dates.cpp │ ├── Number of Ways to Stay in Same Position after some steps.cpp │ ├── Peeking Iterator.cpp │ ├── Random Pick Index.cpp │ ├── Random Pick with Weight.cpp │ ├── Randomly generated mines on Grid.cpp │ ├── Rotate Image.cpp │ ├── Schedule of Taks interval.cpp │ ├── Search in 2D Matrix.cpp │ ├── Set Matrix Zeroes.cpp │ ├── Single Number.cpp │ ├── Sparse Matrix Multiplication.cpp │ ├── Strobogrammatic Nmber2.cpp │ ├── Strobogrammatic Number.cpp │ └── Toeplitz Matrix.cpp ├── Monthly Challenge │ ├── October 2020 │ │ ├── Combination Sum.cpp │ │ └── Number of Recent Calls.cpp │ └── September 2020 │ │ ├── Find the Difference.cpp │ │ └── Largest Number.cpp ├── Stack │ ├── Basic Calculator 2.cpp │ ├── Basic Calculator.cpp │ ├── Decode String.cpp │ └── Minimum Add to make Parenthesis Valid.cpp ├── Strings │ ├── Add Bold Tag in String.cpp │ ├── All anagrams of string.cpp │ ├── Backspace String Compare.cpp │ ├── Before and After Puzzle.cpp │ ├── Can Make Palindrome from Substring.cpp │ ├── Convert Number to Hexadecimal.cpp │ ├── Custom Sort String.cpp │ ├── Duplicate File in System.cpp │ ├── Expression Add Operators.cpp │ ├── Generate Parenthesis.cpp │ ├── Goat Latin.cpp │ ├── Group Anagrams.cpp │ ├── Group Shifted Strings Cipher.cpp │ ├── Integer to english words.cpp │ ├── Longest Common Prefix.cpp │ ├── Longest Pallindromic Substring.cpp │ ├── Longest Repeating Substring.cpp │ ├── Longest Substring With Atleast K repeating characters.cpp │ ├── Longest Substring without repeating characters.cpp │ ├── Longest substring with atmost 2 distinct characters.cpp │ ├── Longest substring without repeated characters.cpp │ ├── Minimum Swaps to make String Equal.cpp │ ├── Minimum Time Difference.cpp │ ├── Minimum Window Substring.cpp │ ├── Minimum remove to make valid Parenthesis.cpp │ ├── Multiply Strings.cpp │ ├── One Edit distance.cpp │ ├── Palindromic Pallindromic Substring.cpp │ ├── Pallindrome Pairs.cpp │ ├── Pallindromic Substrings.cpp │ ├── Remove Invalid Parenthesis.cpp │ ├── Reorder data in log files.cpp │ ├── Reorganize String.cpp │ ├── Repeated Substring Pattern.cpp │ ├── Reverse Words in a String.cpp │ ├── Score of Parantheses.cpp │ ├── Simplify Path.cpp │ ├── String Compression.cpp │ ├── Valid Palindrome 2.cpp │ ├── Valid Palindrome.cpp │ ├── Valid Parentheses.cpp │ ├── Valid Parenthesis String.cpp │ ├── Validate IP Addresses.cpp │ ├── Word Break 2.cpp │ ├── Word Break.cpp │ ├── ZigZag conversion.cpp │ ├── sort character by frequency.cpp │ └── stoi atoi.cpp ├── Tree │ ├── All Nodes Distance K in a Binary Tree.cpp │ ├── Binary Search Tree Iterator.cpp │ ├── Binary Search Tree to greater sum Tree.cpp │ ├── Binary Tree Longest Consecutive Sequence.cpp │ ├── Binary Tree Maximum Path Sum.cpp │ ├── Binary Tree Paths.cpp │ ├── Binary Tree.h │ ├── Check Completeness of Binary Tree.cpp │ ├── Closest Value in Binary Search Tree BST 2.cpp │ ├── Construct BST from Preorder.cpp │ ├── Construct Binary Tree from Inorder & preorder.cpp │ ├── Construct Binary Tree from Preorder and Postorder.cpp │ ├── Convert Sorted Array to BST.cpp │ ├── Delete node in BST.cpp │ ├── Distribute Coins in Binary Tree.cpp │ ├── Find elements in a contaminated BST.cpp │ ├── K th Smallest Element in BST.cpp │ ├── Largest BST Subtree.cpp │ ├── Largest Value in Each Level.cpp │ ├── Maximum Binary Tree.cpp │ ├── Maximum Difference between Node and Ancestor.cpp │ ├── Merge Binary Tree.cpp │ ├── Merge Two 2 BST.cpp │ ├── Modes of in a BST.cpp │ ├── Morris Traversal.cpp │ ├── Path Sum.cpp │ ├── Path in a zig-zag labelled binary tree.cpp │ ├── Populating Next Right Pointer 2 Connect Nodes at Same Level.cpp │ ├── Populating Next Right Pointer or Connect nodes at Same Level.cpp │ ├── Range Sum of BST.cpp │ ├── Recover binary search tree.cpp │ ├── Serialize and Deserialize BST.cpp │ ├── Serialize and Deserialize NAry n-ary tree.cpp │ ├── Smalest Subtree with Deepest Node.cpp │ ├── Smallest String starting from leaf.cpp │ ├── Split BST.cpp │ ├── Subtree of Another Tree.cpp │ ├── Sum of Distance in Tree.cpp │ ├── Unique BST.cpp │ └── Valid Binary Tree or Validate Binary Tree.cpp └── Trie │ ├── Add and Search Word DataStructure design.cpp │ ├── Implement Magic Dictionary.cpp │ ├── Implement Trie.cpp │ ├── Search Suggestion System.cpp │ └── Stream of Characters.cpp ├── OAs ├── Palantir_test.cpp ├── PayPal │ └── HCK1.cpp ├── Samsung │ ├── Largest BST Subtree.cpp │ ├── bad pair.cpp │ ├── minimum add to make pallindrome.cpp │ ├── test.cpp │ └── total_tc.cpp ├── Stripe │ ├── 1.cpp │ ├── stripe-deisgn-challenge (1).cpp │ ├── tempCodeRunnerFile.cpp │ ├── yuarel.c │ └── yuarel.h └── Waart │ ├── countTriplets.cpp │ ├── maxdiff.cpp │ ├── mex.cpp │ ├── minMoves.cpp │ └── splitgroup.cpp ├── README.md ├── SPOJ ├── CRZYSMKR.cpp ├── EDIT.cpp ├── POUR1.cpp └── SPOJ_LIST │ ├── ABSP1.cpp │ ├── DOL.cpp │ ├── ESYRCRTN.cpp │ ├── GIRLSNBS.cpp │ ├── MOHIB.cpp │ ├── PHT.cpp │ ├── QUADAREA.cpp │ ├── SPCQ.cpp │ ├── SPOJQUESTIONSLIST.pdf │ ├── acpc11b.cpp │ ├── army.cpp │ ├── codchess.cpp │ ├── crds.cpp │ ├── ec_conb.cpp │ ├── fashion.cpp │ ├── fctrl.cpp │ ├── fctrl2(c++17).cpp │ ├── fctrl2(using int array).cpp │ ├── ieeeeebgam.cpp │ └── infix2postfix.cpp ├── library ├── Big Integer.cpp ├── Catalan Number.pdf ├── Competitive Programming Roadmap.txt ├── Cpp_STL_ReferenceManual.pdf ├── Mathematical Expectation.pdf ├── Number Theory GeeksforGeeks.pdf ├── Template.cpp └── all stl functions.pdf └── test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all 2 | * 3 | 4 | # Unignore all with extensions 5 | !*.c* 6 | 7 | !*.cpp* 8 | 9 | !*.pdf* 10 | 11 | !*.h* 12 | 13 | # Unignore all dirs 14 | !*/ -------------------------------------------------------------------------------- /A2OJ/A - Watermelon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<>w; 12 | if(w&1 || w==2) 13 | cout<<"NO"; 14 | else 15 | cout<<"YES"; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Codechef/APRIL2020-LONG/April 20A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define mod 1000000007 5 | 6 | int main() 7 | { 8 | int t,n,i,j; 9 | cin>>t; 10 | while (t--) 11 | { 12 | cin>>n; 13 | ll a[n]; 14 | for(i=0;i>a[i]; 16 | sort(a,a+n,greater()); 17 | for(i=0;i0?a[i]-i:0; 19 | 20 | ll ans=0; 21 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while (t--) 9 | { 10 | int i,n; 11 | cin>>n; 12 | int a[n], prev=-1; 13 | bool followed=true; 14 | for(i=0;i>a[i]; 17 | if(a[i]==1) 18 | { 19 | if((prev!=-1) && (i-prev)<6) 20 | { 21 | followed=false; 22 | } 23 | prev=i; 24 | } 25 | } 26 | followed?cout<<"YES\n":cout<<"NO\n"; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Codechef/APRIL2020-LONG/April 20C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | 5 | bool isPossible(ll n, ll k) 6 | { 7 | vector prime; 8 | while(n%2==0) 9 | { 10 | prime.emplace_back(2); 11 | n/=2; 12 | } 13 | 14 | for(ll i=3;i*i<=n;i+=2) 15 | { 16 | while(n%i==0) 17 | { 18 | prime.emplace_back(i); 19 | n/=i; 20 | } 21 | } 22 | 23 | if(n>2) 24 | prime.emplace_back(n); 25 | 26 | return ((prime.size()>t; 37 | while(t--) 38 | { 39 | ll x,k; 40 | cin>>x>>k; 41 | if(isPossible(x,k)) 42 | cout<<"1\n"; 43 | else 44 | cout<<"0\n"; 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Codechef/AUG-Long1.cpp: -------------------------------------------------------------------------------- 1 | /*August Long Challenge Div2: https://www.codechef.com/AUG19B/problems/MSNSADM1*/ 2 | 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define deb(x) cout<<#x<<" "<>t; 15 | while(t--) 16 | { 17 | int n; 18 | cin>>n; 19 | int a[n],b[n]; 20 | int max_score=0; 21 | for(i=0;i>a[i]; 23 | for(i=0;i>b[i]; 25 | 26 | for(i=0;i 4 | using namespace std; 5 | #define ll long long 6 | #define deb(x) cout<<#x<<" "<>t; 15 | while(t--) 16 | { 17 | ll n,k; 18 | cin>>n>>k; 19 | 20 | ll t1=n/k; 21 | ll t2=__gcd(n, k); 22 | if(k==1) 23 | cout<<"NO\n"; 24 | 25 | else if(t1%t2==0) 26 | cout<<"NO\n"; 27 | else 28 | cout<<"YES\n"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Codechef/AUG-Long3.cpp: -------------------------------------------------------------------------------- 1 | /* Another solution may also be possible, but it's not working https://ideone.com/zdB4Py */ 2 | 3 | #include 4 | using namespace std; 5 | main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | int t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | string s; 14 | cin>>s; 15 | int c=0; 16 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<>t; 12 | while(t--) 13 | { 14 | cin>>n; 15 | int a[n]; 16 | for(i=0;i>a[i]; 18 | for(i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<>t; 39 | fac(); 40 | solve(); 41 | while(t--) 42 | { 43 | cin>>n; 44 | 45 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<>t; 12 | while(t--) 13 | { 14 | cin>>n; 15 | ll a[n]; 16 | double sum=0.0; 17 | for(i=0;i>a[i]; 20 | sum+=a[i]; 21 | } 22 | double mean=sum/n; 23 | int flag=0; 24 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int t; 6 | cin>>t; 7 | long long int n,i,min_diff,diff; 8 | 9 | while(t--) 10 | { 11 | cin>>n; 12 | vector v(n); 13 | for(i=0;i 11 | #define ll long long int 12 | using namespace std; 13 | 14 | int main() { 15 | // your code goes here 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | cout.tie(NULL); 19 | int t; 20 | cin >> t; 21 | while(t--){ 22 | ll n, k, s = 0, i; 23 | cin >> n >> k; 24 | ll a[n]; 25 | for(i = 0 ; i < n ;i++){ 26 | cin >> a[i]; 27 | s += a[i]; 28 | } 29 | cout << (s % k) << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Codechef/GOOGOL05 STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | int n,i; 8 | cin>>n; 9 | map lab; 10 | string dept; 11 | 12 | for(i=0;i>dept; 14 | lab[dept]++; 15 | } 16 | for(auto it:lab) 17 | cout< 2 | using namespace std; 3 | #define ll long long 4 | main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(0); 8 | ll t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | ll n,sum=0,x; 13 | cin>>n; 14 | for (ll int x = n; x > 0; x = x / 10) 15 | sum = sum + x % 10; 16 | ll int z=sum; 17 | while(z%10!=0) 18 | z++; 19 | cout< 2 | #define ll unsigned long long 3 | #define mod 1000000009 4 | using namespace std; 5 | 6 | ll exponentiation(ll A, ll B,mod) 7 | { 8 | // Base cases 9 | if (A == 0) 10 | return 0; 11 | if (B == 0) 12 | return 1; 13 | 14 | // If B is even 15 | ll y; 16 | if (B % 2 == 0) { 17 | y = exponentiation(A, B / 2, mod); 18 | y = (y * y) % C; 19 | } 20 | 21 | // If B is odd 22 | else { 23 | y = A % mod; 24 | y = (y * exponentiation(A, B - 1, mod) % mod) % mod; 25 | } 26 | 27 | return (ll)((y + mod) % mod); 28 | } 29 | 30 | main() 31 | { 32 | ios_base::sync_with_stdio(false); 33 | cin.tie(0); 34 | ll t,k,i; 35 | cin>>t; 36 | 37 | while(t--) 38 | { 39 | cin>>k; 40 | ll pre_ans=exponentiation(2,k-1,mod); 41 | ll ans=(10*pre_ans)%mod; 42 | cout< 2 | using namespace std; 3 | #define ll long long 4 | main() 5 | { 6 | int t; 7 | ll n,i=0,j; 8 | cin>>t; 9 | while(t--) 10 | { 11 | cin>>n; 12 | string a,b; 13 | cin>>a>>b; 14 | ll c=0; 15 | 16 | if(a[0]==b[0]) 17 | c++; 18 | if(a[1]==b[1] &&(a[0]==b[0] || b[0]=='N')) 19 | c++; 20 | 21 | for(i=2;i 2 | using namespace std; 3 | #define ll long long 4 | main() 5 | { 6 | int t; 7 | ll n,i,m; 8 | cin>>t; 9 | while(t--) 10 | { 11 | cin>>n>>m; 12 | ll taste[m+1]={0}; 13 | ll d,v; 14 | while(n--) 15 | { 16 | cin>>d>>v; 17 | if(taste[d]==0) 18 | taste[d]=v; 19 | else 20 | taste[d]=max(taste[d],v); 21 | } 22 | sort(taste,taste+m+1,greater()); 23 | 24 | ll ans=taste[0]+taste[1]; 25 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<0) 10 | { 11 | sum+=(n%10); 12 | n/=10; 13 | } 14 | return sum; 15 | } 16 | 17 | main() 18 | { 19 | ios_base::sync_with_stdio(false); 20 | cin.tie(0); 21 | int t; 22 | cin>>t; 23 | while(t--) 24 | { 25 | int n,i,j; 26 | cin>>n; 27 | ll a[n]; 28 | for(i=0;i>a[i]; 30 | 31 | ll max_pro=0; 32 | 33 | for(i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<>t; 20 | while(t--) 21 | { 22 | int ans=1; 23 | cin>>n; 24 | int a[n]; 25 | for(int i=0;i>a[i]; 27 | 28 | for(int i=1;i 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "< 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<>t 12 | while(t--) 13 | { 14 | cin>>n>>q; 15 | int b[n-1]; 16 | int sum[n-1]; 17 | 18 | for(i=0;i>b[i]; 21 | if(i==0) 22 | sum[i]=b[i]; 23 | else 24 | sum[i]=sum[i-1]+b[i]; 25 | } 26 | 27 | int p,q; 28 | while(q--) 29 | { 30 | cin>>p>>q; 31 | int a=min(p,q); 32 | int b=max(p,q); 33 | if(b-a==1) 34 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(0); 7 | cout.tie(0); 8 | 9 | int t,i,j,n,k; 10 | cin>>t; 11 | while(t--) 12 | { 13 | cin>>n>>k; 14 | int a[n]; 15 | for(i=0;i>a[i]; 17 | 18 | vector v; 19 | int sum; 20 | 21 | for(i=0;i 2 | using namespace std; 3 | typedef long long int ull; 4 | main() 5 | { 6 | 7 | ull t; 8 | cin>>t; 9 | 10 | int fib[60]; 11 | fib[0]=0; 12 | fib[1]=1; 13 | for(int i=2;i<60;i++) 14 | fib[i]=(fib[i-1]+fib[i-2])%10; 15 | 16 | while(t--) 17 | { 18 | long double n; 19 | cin>>n; 20 | ull x=log2(n); 21 | ull c=pow(2,x); 22 | c=(c-1)%60; 23 | 24 | cout< 2 | #define ll long long int 3 | using namespace std; 4 | int gcd(int a, int b) 5 | { 6 | if (b == 0) 7 | return a; 8 | return gcd(b, a % b); 9 | } 10 | int main() 11 | { 12 | ios_base::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | int tc,n,flag,v; 15 | cin>>tc; 16 | while(tc--) 17 | { 18 | flag=1; 19 | cin>>n; 20 | vectora(n); 21 | vectorb(n); 22 | for(int i=0;i>b[i]; 26 | } 27 | for(int i=0;i 2 | #define ll long long int 3 | using namespace std; 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int tc,n,sum,x; 9 | cin>>tc; 10 | while(tc--) 11 | { 12 | sum=0; 13 | cin>>n; 14 | vectora(n); 15 | for(int i=0;i>a[i]; 18 | sum+=a[i]; 19 | } 20 | if(sum>=0) 21 | { 22 | cout<<"YES"< 2 | using namespace std; 3 | main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | string txt="ABABABCABABABCABABABC"; 8 | string pat="ABABAC"; 9 | int l=txt.length(); 10 | int m=pat.length(); 11 | string kmp; 12 | int i,j; 13 | for(i=0;i<=l-2*m;i++) 14 | { 15 | kmp=""; 16 | for(j=0;j 2 | using namespace std; 3 | main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | int n,i,t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | cin>>n; 12 | int a[n],b[n]; 13 | for(i=0;i>a[i]; 15 | 16 | for(i=0;i>b[i]; 18 | sort(a,a+n); 19 | sort(b,b+n); 20 | 21 | int c=0,d=0; 22 | for(i=0;i<(2*n);i++) 23 | { 24 | if(i%2==0&&c 2 | using namespace std; 3 | #define ll long long 4 | main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(0); 8 | int t,n; 9 | cin>>t; 10 | while(t--) 11 | { 12 | 13 | cin>>n; 14 | string s,r; 15 | cin>>s>>r; 16 | int a1=0,a2=0,b1=0,b2=0; 17 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(0); 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | string s; 13 | int i; 14 | cin>>s; 15 | int n=s.length(); 16 | int c[n+1]; 17 | for(i=1;i<=n;i++) 18 | c[i]=c[i-1]+s[i-1]-'0'; 19 | 20 | int ans=0; 21 | for(i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "< v; 7 | void solve(ll n) 8 | { 9 | // if(n<=1) 10 | // { 11 | // v.emplace_back(n); 12 | // return; 13 | // } 14 | if(n==0) 15 | return; 16 | if(n==1) 17 | { 18 | v.emplace_back(1); 19 | return; 20 | } 21 | solve(n/2); 22 | solve(n%2); 23 | solve(n/2);kk 24 | } 25 | 26 | main() 27 | { 28 | ios_base::sync_with_stdio(false); 29 | cin.tie(NULL); 30 | 31 | ll n,l,r; 32 | cin>>n>>l>>r; 33 | solve(n); 34 | 35 | ll c=0; 36 | for(;l<=r;l++) 37 | if(v[l-1]==1) 38 | c++; 39 | cout< 9 | using namespace std; 10 | main() 11 | { 12 | ios_base::sync_with_stdio(false); 13 | cin.tie(0); 14 | 15 | int t; 16 | cin>>t; 17 | while(t--) 18 | { 19 | double n; 20 | cin>>n; 21 | double temp=0,i=0; 22 | while(i 2 | using namespace std; 3 | main() 4 | { 5 | 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int q,k; 9 | cin>>q>>k; 10 | long long int t,x,y; 11 | priority_queue,greater> dist; 12 | long long int distance; 13 | while(q--) 14 | { 15 | cin>>t; 16 | if(dist.size()==k+1) 17 | dist.pop_back(); 18 | if(t==1) 19 | { 20 | cin>>x>>y; 21 | distance=x*x + y*y; 22 | dist.push(distance); 23 | } 24 | 25 | else 26 | cout< 6 | using namespace std; 7 | int main() 8 | { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(0); 11 | 12 | double in,p; 13 | cin>>in; 14 | double num=365,den=365; 15 | p=1.0; 16 | num--; 17 | int n=1; 18 | // p is the probability of all persons having birthday on different dates 19 | while(p>(1.0-in)) 20 | { 21 | p*=(num/den); 22 | num--; 23 | n++; 24 | } 25 | cout< 2 | #define MAX 500 3 | using namespace std; 4 | 5 | int catalan(int n) 6 | { 7 | cpp_int ans=1; 8 | for(int i=2;i<=n;i++) 9 | ans=(ans*(n+i))/i; 10 | return ans; 11 | } 12 | 13 | /* 14 | void catalan(int n) 15 | { 16 | long long int res[MAX],carry=0,l=1; 17 | res[0]=1; 18 | 19 | for(int i=2;i<=n;i++) 20 | { 21 | for(int j=0;j0) 29 | { 30 | res[l++]=carry%10; 31 | carry/=10; 32 | } 33 | } 34 | for(int i=l-1;i>=0;i--) 35 | cout<>n; 44 | catalan(n); 45 | } 46 | -------------------------------------------------------------------------------- /Coding Blocks/Mathematics/Evaluating Function.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | ans =4*pow(n,3)+5*pow(n,2)-6*n+14 3 | print(ans) -------------------------------------------------------------------------------- /Coding Blocks/Mathematics/Favorite Dice.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Similar to Coupon Collector Problem (Mathematical Expectation) 3 | 4 | Spoj: https://www.spoj.com/problems/FAVDICE/ 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | main() 10 | { 11 | ios_base::sync_with_stdio(false); 12 | cin.tie(0); 13 | 14 | int t; 15 | cin>>t; 16 | while(t--) 17 | { 18 | double n; 19 | cin>>n; 20 | double temp=0,i=0; 21 | while(i0: 12 | [N,K]=sys.stdin.readline().rstrip().split(' ') 13 | N = int(N) 14 | K = int(K) 15 | 16 | n=N-1 17 | k=K-1 18 | print(star_bar(n,k)) 19 | t=t-1 -------------------------------------------------------------------------------- /Coding Blocks/Mathematics/Marbles Star & Bar theorem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Marbles Problem using Star and Bar principle 3 | Spoj: https://www.spoj.com/problems/MARBLES/ 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | #define ll long long 9 | 10 | ll bar_star(ll n,ll k) 11 | { 12 | ll ans=1ll; 13 | if(n==k) 14 | return ans; 15 | k=min(n-k,k); 16 | for(ll i=0;i>t; 26 | while(t--) 27 | { 28 | ll n,k; 29 | cin>>n>>k; 30 | cout<0: 6 | [a,n,p]=sys.stdin.readline().rstrip().split(' ') 7 | a=int(a) 8 | n=int(n) 9 | p=int(p) 10 | n=math.factorial(n) 11 | ans=pow(a,n,p) 12 | print(ans) 13 | t=t-1 -------------------------------------------------------------------------------- /Coding Blocks/Mathematics/Modular fast exponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | ll power(ll a,ll b, ll c) 6 | { 7 | if(b==0) 8 | return 1; 9 | 10 | // If b is even then b/2 else a(b/2) 11 | 12 | ll pre_ans=power(a,b/2,c); 13 | pre_ans=(pre_ans*pre_ans)%c; 14 | if(b&1) 15 | pre_ans=(pre_ans*a)%c; 16 | return pre_ans; 17 | } 18 | main() 19 | { 20 | ll a=2; 21 | ll b=100; 22 | cout< 2 | using namespace std; 3 | int main() { 4 | int n,i; 5 | cin>>n; 6 | int size=2*n; 7 | int a[size]; 8 | for(i=0;i>a[i]; 10 | sort(a,a+size); 11 | cout< 2 | using namespace std; 3 | #define ll long long 4 | 5 | ll power(ll a,ll b, ll c) 6 | { 7 | if(b==0) 8 | return 1; 9 | 10 | // If b is even then b/2 else a(b/2) 11 | 12 | ll pre_ans=power(a,b/2,c); 13 | pre_ans=(pre_ans*pre_ans)%c; 14 | if(b&1) 15 | pre_ans=(pre_ans*a)%c; 16 | return pre_ans; 17 | } 18 | main() 19 | { 20 | ll a=2; 21 | ll b=100; 22 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define max 100000000 5 | bool prime[max]={false}; 6 | ll find_prime(int n) 7 | { 8 | ll i,j,c=0; 9 | 10 | prime[0]=false; 11 | prime[1]=false; 12 | prime[2]=true; 13 | 14 | for(i=3;i>n; 41 | cout< 2 | using namespace std; 3 | 4 | int x,y,gcd; 5 | 6 | //bottom up approach 7 | void extended_euclid(int a,int b) 8 | { 9 | //base case 10 | if(b==0) 11 | { 12 | x=1; 13 | y=0; 14 | gcd=a; 15 | return; 16 | } 17 | //recursive case 18 | extended_euclid(b,a%b); 19 | int cx=y; 20 | int cy=x-(a/b)*y; 21 | 22 | x=cx; 23 | y=cy; 24 | } 25 | main() 26 | { 27 | // Solve 18x+30y=6 28 | int a=8,b=7; 29 | extended_euclid(a,b); 30 | cout<<(x+b)%b; 31 | } 32 | -------------------------------------------------------------------------------- /Coding Blocks/Number Theory/Fast Modular Exponentiation using Bitmasking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | ll fastexpo(ll a, ll b, ll m) 6 | { 7 | ll ans=1; 8 | while(b>0) 9 | { 10 | if(b&1) 11 | ans=(ans*a)%m; 12 | a=(a*a)%m; 13 | b=b>>1; 14 | } 15 | return ans; 16 | } 17 | main() 18 | { 19 | ll a, b, m; 20 | cin>>a>>b>>m; 21 | cout< 2 | using namespace std; 3 | #define ll long long 4 | 5 | main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(0); 9 | 10 | ll n,q,i,j,ele; 11 | cin>>n; 12 | ll a[n]; 13 | for(i=0;i>a[i]; 15 | ll MAX=*max_element(a,a+n); 16 | 17 | ll cnt[MAX+1]={0}; 18 | ll ans[100005]={0}; 19 | 20 | for(i=0;i>q; 32 | while(q--) 33 | { 34 | cin>>ele; 35 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define mod 1000000007 5 | 6 | ll stringToInt(string a, ll p) 7 | { 8 | ll ans=0; 9 | for(ll i=0;i>t; 30 | string a,b; 31 | while(t--) 32 | { 33 | cin>>a>>b; 34 | ll A=stringToInt(a,mod); 35 | ll B=stringToInt(b,mod-1); 36 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define mod 1000000007 5 | 6 | ll x,y; 7 | void multiplicative_inverse(ll a, ll m) 8 | { 9 | if(m==0) 10 | { 11 | x=1; 12 | y=0; 13 | return; 14 | } 15 | else 16 | { 17 | multiplicative_inverse(m,a%m); 18 | ll Cx=y; 19 | ll Cy=x-(a/m)*y; 20 | x=Cx; 21 | y=Cy; 22 | } 23 | } 24 | main() 25 | { 26 | ll n; 27 | cin>>n; 28 | multiplicative_inverse(n,mod); 29 | cout<<(x+mod)%mod; 30 | } 31 | -------------------------------------------------------------------------------- /Coding Blocks/Number Theory/Prime Factor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sastava007/Tech-Interview-Preparation/31e54258b6caddcee4f594da59341761bfd08964/Coding Blocks/Number Theory/Prime Factor -------------------------------------------------------------------------------- /Coding Blocks/Playing with bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | ll setbits=0; 6 | 7 | ll set_bits(ll n) 8 | { 9 | if(n>0) 10 | { 11 | setbits++; 12 | n=n&(n-1); 13 | set_bits(n); 14 | 15 | } 16 | else 17 | return setbits; 18 | 19 | } 20 | 21 | main() 22 | { 23 | ll q,a,b,i; 24 | cin>>q; 25 | while(q--) 26 | { 27 | cin>>a>>b; 28 | ll ans=0; 29 | for(i=a;i<=b;i++) 30 | { 31 | ans+=set_bits(i); 32 | setbits=0; 33 | } 34 | cout< 2 | using namespace std; 3 | #define ll long long 4 | void compute(unordered_map &m,int n) 5 | { 6 | if(n==0) 7 | return; 8 | int res=n%10; 9 | compute(m,n/10); 10 | cout<m; 19 | m[0]="zero",m[1]="one",m[2]="two",m[3]="three",m[4]="four",m[5]="five",m[6]="six",m[7]="seven",m[8]="eight",m[9]="nine"; 20 | compute(m,n); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Coding Blocks/Recursion and Backtrackking/Absolute the Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<>t; 26 | while(t--) 27 | { 28 | cin>>n; 29 | int sum=0; 30 | int a[n]; 31 | for(i=0;i>a[i]; 34 | if(i&1) 35 | a[i]=1; 36 | } 37 | for(i=1;i 8 | using namespace std; 9 | #define ll long long 10 | 11 | ll compute(int n) 12 | { 13 | ll a[n+1],b[n+1]; 14 | a[1]=b[1]=1; 15 | for(ll i=2;i<=n;i++) 16 | { 17 | a[i]=a[i-1]+b[i-1]; 18 | b[i]=a[i-1]; 19 | } 20 | return a[n]+b[n]; 21 | } 22 | main() 23 | { 24 | ios_base::sync_with_stdio(false); 25 | cin.tie(0); 26 | int t; 27 | cin>>t; 28 | for(int i=1;i<=t;i++) 29 | { 30 | int n; 31 | cin>>n; 32 | cout<<"#"< 2 | using namespace std; 3 | 4 | void generateParentheses(int open, int close, int n, string s) 5 | { 6 | if(open==n && close==n) 7 | { 8 | cout<>n; 21 | generateParentheses(0,0,n,""); 22 | } 23 | -------------------------------------------------------------------------------- /Coding Blocks/Recursion and Backtrackking/Permuatations of String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | void permute(char *in, int i) 6 | { 7 | if(in[i]=='\0') 8 | { 9 | cout<>in; 30 | permute(in,0); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Coding Blocks/Recursion and Backtrackking/Phone Keypad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | char keypad[][10]={"","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"}; 6 | void phone_keypad(char *in, char *out, int i, int j) 7 | { 8 | if(in[i]=='\0') 9 | { 10 | out[j]='\0'; 11 | cout<>in; 34 | phone_keypad(in,out,0,0); 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Coding Blocks/Recursion and Backtrackking/Rat in Maze Variation/desktop.ini: -------------------------------------------------------------------------------- 1 | [LocalizedFileNames] 2 | Rat in Maze.o=@Rat in Maze.o,0 3 | Rat in Maze.cpp=@Rat in Maze.cpp,0 4 | Rat in Maze.exe=@Rat in Maze.exe,0 5 | -------------------------------------------------------------------------------- /Coding Blocks/Recursion and Backtrackking/Recursion sequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | void recursion_sequences(char *s, char *temp, int i, int j) 6 | { 7 | //base case 8 | if(s[i]=='\0') 9 | { 10 | temp[j]='\0'; 11 | cout< 2 | using namespace std; 3 | 4 | char keypad[][10]={ " ", ".+@$", "abc", "def", "ghi", "jkl" , "mno", "pqrs" , "tuv", "wxyz" }; 5 | void solveKeypad(string num, char *out, int i, int j) 6 | { 7 | //base case 8 | if(num[i]=='\0') 9 | { 10 | out[j]='\0'; 11 | cout<>num; 29 | char out[1000]; 30 | 31 | solveKeypad(num,out,0,0); 32 | } 33 | -------------------------------------------------------------------------------- /Coding Blocks/Recursion and Backtrackking/Tricky Permutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | void permute(char *in, int i) 6 | { 7 | if(in[i]=='\0') 8 | { 9 | cout<>in; 36 | sort(in,in+n); 37 | permute(in,0); 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /Coding Blocks/Recursion and Backtrackking/issorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool issorted(int *a,int n) 5 | { 6 | if(n==1) 7 | return true; 8 | if(a[0] 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(0); 8 | srand(time(NULL)); 9 | for(int i=0;i<20;i++) 10 | cout< 2 | using namespace std; 3 | 4 | // Time complexicity O(no of bits) 5 | /* 6 | int set_bits(int n) 7 | { 8 | int setbits=0; 9 | while(n=n>>1) 10 | setbits++; 11 | return setbits; 12 | } 13 | */ 14 | // Time complexicity O(no of set bits). This approach will keep on removing the last setbits 15 | void filterchar(char *a,int n) 16 | { 17 | int i=0; 18 | while(n>0) 19 | { 20 | n&1==1?cout<>1; 23 | } 24 | cout<<"\n"; 25 | } 26 | void generate_subs(char *a) 27 | { 28 | int n=strlen(a); 29 | int range=(1< 2 | using namespace std; 3 | 4 | main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | 9 | long long int n,i,j=0; 10 | cin>>n; 11 | long long int a[n]; 12 | for(i=0;i>a[i]; 14 | long long int l=0,mod =1000000007; 15 | 16 | unordered_set s; 17 | for(i=0;i 2 | using namespace std; 3 | #define ll long long 4 | main() 5 | { 6 | char a[20]; 7 | cin>>a; 8 | 9 | int n=strlen(a),i; 10 | ll ans=0; 11 | ans=(1<=0;i--,pos++) 16 | { 17 | 18 | if(a[i]=='7') 19 | ans+=(1< 3 | using namespace std; 4 | int main() { 5 | 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(0); 8 | 9 | int t,i; 10 | string a,b; 11 | cin>>t; 12 | while(t--) 13 | { 14 | cin>>a>>b; 15 | int l=a.length(); 16 | for(i=0;i 2 | using namespace std; 3 | 4 | static int cnt[64]={0}; 5 | 6 | void extract_bits(int n) 7 | { 8 | int i=0; 9 | while(n>0) 10 | { 11 | if(n&1==1) 12 | cnt[i]++; 13 | n=n>>1; 14 | i++; 15 | } 16 | return; 17 | } 18 | 19 | main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(0); 23 | int n,i; 24 | cin>>n; 25 | int a[n]; 26 | for(i=0;i>a[i]; 29 | extract_bits(a[i]); 30 | } 31 | int ans=0; 32 | for(i=0;i<64;i++) 33 | { 34 | cnt[i]%=3; 35 | ans+=cnt[i]*pow(2,i); 36 | } 37 | cout< 2 | using namespace std; 3 | int main() { 4 | 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | 8 | int n,k,i,swape=0; 9 | cin>>n>>k; 10 | long long int v[n],pos[n]; 11 | 12 | for(i=0;i>v[i]; 15 | pos[v[i]]=i; 16 | } 17 | 18 | i=0; 19 | 20 | while(swape 2 | using namespace std; 3 | #define ll long long 4 | void count_digits(ll n,unordered_set s) 5 | { 6 | while(n>0 && s.size()<=2) 7 | { 8 | s.insert(n%10); 9 | n/=110; 10 | } 11 | 12 | } 13 | main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(0); 17 | 18 | int t,k; 19 | cin>>t; 20 | while(t--) 21 | { 22 | cin>>k; 23 | ll c=0; 24 | ll n=pow(10,k)-1,i; 25 | 26 | for(i=0;i<=n;i++) 27 | { 28 | unordered_set s; 29 | count_digits(i,s); 30 | count_digits(n-i,s); 31 | if((s.size())<=2) 32 | { 33 | cout< 4 | #define ll long long 5 | using namespace std; 6 | 7 | int equilibriumPoint(long long a[], int n) { 8 | 9 | ll sum = 0; 10 | for(int i=0; i m; 11 | 12 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n, i, j, k; 11 | cin>>n>>k; 12 | 13 | int a[n]; 14 | 15 | for(i=0; i>a[i]; 17 | 18 | int sum = 0, maxlen = 0; 19 | unordered_map m; 20 | 21 | for(i=0; i 4 | using namespace std; 5 | 6 | int main() { 7 | //code 8 | int t,i; 9 | cin>>t; 10 | while(t--) 11 | { 12 | string a,b; 13 | char ans ='$'; 14 | cin>>a>>b; 15 | int min= INT_MAX; 16 | unordered_map m; 17 | for(i=0;i 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,i,zero=0,x; 12 | cin>>n; 13 | vector a; 14 | for(i=0;i>x; 17 | if(x!=0) 18 | a.emplace_back(x); 19 | else 20 | zero++; 21 | } 22 | for(i=0;i 6 | #define ll long long 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int t; 12 | cin>>t; 13 | while(t--) 14 | { 15 | int n,k,i; 16 | cin>>n>>k; 17 | ll a[n]; 18 | for(i=0;i>a[i]; 20 | 21 | for(i=0;i 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | int n, k, i, j; 13 | cin>>n>>k; 14 | int a[n]; 15 | 16 | for(i=0;i>a[i]; 18 | 19 | i = 0, j = 0; 20 | 21 | int sum = 0; 22 | int minlen = INT_MAX; 23 | 24 | while(ik && i 2^2 6 | i.e the number of unset bits in 'n' 7 | */ 8 | 9 | int numOfBits(int n) 10 | { 11 | int x=log2(n)+1; 12 | return x; 13 | } 14 | 15 | int countValues (int n) 16 | { 17 | return (1<<(numOfBits(n)-__builtin_popcount(n))); 18 | } -------------------------------------------------------------------------------- /GFG/Bit-Manipulation/Game of XOR.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Simplest approach for this solution 3 | Here each element will occur (n-1)*(i+1) times in all subarrays. So the element which occur odd number of times will be counted in final XOR else ignore it. 4 | */ 5 | 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int t; 13 | cin>>t; 14 | while(t--) 15 | { 16 | int n,i,ans=0; 17 | cin>>n; 18 | int a[n]; 19 | for(i=0;i>a[i]; 22 | if( ((n-i)*(i+1))&1 ) 23 | ans ^= a[i]; 24 | } 25 | cout< 8 | using namespace std; 9 | 10 | int josephus(int n, int k) 11 | { 12 | if(n==1) 13 | return 0; 14 | else 15 | return ((josephus(n-1,k)+k)%n); 16 | } 17 | int main() 18 | { 19 | int t; 20 | cin>>t; 21 | while(t--) 22 | { 23 | int n,k; 24 | cin>>n>>k; 25 | cout< 6 | using namespace std; 7 | 8 | int smallerThan(int a[], int val, int i) 9 | { 10 | int c=0; 11 | for(int j=0;jval) 22 | c++; 23 | return c; 24 | } 25 | 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | int n,i,ans=0; 33 | cin>>n; 34 | int a[n]; 35 | for(i=0;i>a[i]; 37 | for(i=1;i 6 | using namespace std; 7 | #define ll long long 8 | #define mod 1000000007 9 | 10 | #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); 11 | 12 | int main() 13 | { 14 | IOS; 15 | 16 | int t; 17 | cin>>t; 18 | while (t--) 19 | { 20 | int n,i; 21 | cin>>n; 22 | int a[n]; 23 | for(i=0;i>a[i]; 25 | sort(a,a+n); 26 | int max_xor=INT_MAX; 27 | for(i=0;i 8 | using namespace std; 9 | int main() 10 | { 11 | int t; 12 | cin>>t; 13 | while (t--) 14 | { 15 | int n; 16 | cin>>n; 17 | int even_bits = n&(0xAAAAAAAA); 18 | int odd_bits = n&(0x55555555); 19 | 20 | // right shift all even set bits 21 | even_bits = even_bits>>1; 22 | // left shift all odd set bits 23 | odd_bits = odd_bits<<1; 24 | 25 | n = even_bits|odd_bits; 26 | cout< bfs(vector g[], int N) 7 | { 8 | vector result; 9 | unordered_set visited; 10 | queue q; 11 | 12 | q.push(0); 13 | visited.insert(0); 14 | 15 | while(!q.empty()) 16 | { 17 | int temp = q.front(); 18 | q.pop(); 19 | result.push_back(temp); 20 | 21 | for(int neigh: g[temp]) 22 | if(visited.find(neigh)==visited.end()) 23 | { 24 | q.push(neigh); 25 | visited.insert(neigh); 26 | } 27 | } 28 | 29 | return result; 30 | } -------------------------------------------------------------------------------- /GFG/Graph/DFS of undirected graph.cpp: -------------------------------------------------------------------------------- 1 | /* Given undirected graph, and it doesn't contain any back edges and selfloops */ 2 | 3 | void util(int v, vector g[], unordered_set &visited, vector &result) 4 | { 5 | result.push_back(v); 6 | visited.insert(v); 7 | 8 | for(int neigh: g[v]) 9 | if(visited.find(neigh)==visited.end()) 10 | util(neigh, g, visited, result); 11 | 12 | } 13 | 14 | vector dfs(vector g[], int N) 15 | { 16 | unordered_set visited; 17 | vector result; 18 | 19 | for(int i=0; i 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int t; 12 | cin>>t; 13 | while(t--) 14 | { 15 | int n, i, j; 16 | cin>>n; 17 | 18 | int a[n][n]; 19 | 20 | for(i=0;i>a[i][j]; 23 | 24 | for(i=0;ileft); 12 | int right=isBalancedUtil(root->right); 13 | 14 | if(abs(left-right)>1) 15 | flag=false; 16 | 17 | return max(left,right)+1; 18 | } 19 | 20 | bool isBalanced(Node *root) 21 | { 22 | flag=true; 23 | int x = isBalancedUtil(root); 24 | return flag; 25 | } 26 | -------------------------------------------------------------------------------- /GFG/Tree/Diagonal Sum.cpp: -------------------------------------------------------------------------------- 1 | void diagonalSum(Node* root, int d, map &m) 2 | { 3 | if(root==NULL) 4 | return; 5 | 6 | m[d]+=root->data; 7 | diagonalSum(root->left, d+1, m); 8 | diagonalSum(root->right, d, m); 9 | 10 | } 11 | void diagonalSum(Node* root) 12 | { 13 | if(root==NULL) 14 | return; 15 | 16 | map m; 17 | diagonalSum(root, 0, m); 18 | 19 | for(auto it: m) 20 | cout<left, root2->left) && isIsomorphic(root1->right, root2->right); 14 | bool isMirror = isIsomorphic(root1->left, root2->right) && isIsomorphic(root1->right, root2->left); 15 | 16 | if(root1->data == root2->data) 17 | { 18 | return (isIdentical || isMirror); 19 | } 20 | 21 | return false; 22 | } -------------------------------------------------------------------------------- /GFG/Tree/Node at a distance from Leaf.cpp: -------------------------------------------------------------------------------- 1 | void util(Node* root, vector v, unordered_set &s, int k) 2 | { 3 | if(root==NULL) 4 | return; 5 | 6 | v.emplace_back(root); 7 | 8 | if(!root->left && !root->right && v.size()>k) 9 | s.insert(v[v.size()-k-1]); 10 | 11 | //Here we've made 2 copies of vector, one for left and one for right subtree. Each copy of vector 12 | //will contain all ancestors will we reach the leaf node. 13 | 14 | util(root->left, v, s, k); 15 | util(root->right, v, s, k); 16 | } 17 | 18 | int printKDistantfromLeaf(Node* root, int k) 19 | { 20 | if(root==NULL) 21 | return 0; 22 | 23 | vector v; 24 | unordered_set s; 25 | 26 | util(root, v, s, k); 27 | 28 | return s.size(); 29 | } -------------------------------------------------------------------------------- /GFG/Tree/Print nodes that not have sibling.cpp: -------------------------------------------------------------------------------- 1 | set s; 2 | void printSiblingUtil(Node* node) 3 | { 4 | if(node==NULL) 5 | return; 6 | 7 | if(node->left && !node->right) 8 | s.insert(node->left->data); 9 | if(!node->left && node->right) 10 | s.insert(node->right->data); 11 | 12 | printSiblingUtil(node->left); 13 | printSiblingUtil(node->right); 14 | } 15 | 16 | void printSibling(Node* node) 17 | { 18 | s.clear(); 19 | printSiblingUtil(node); 20 | if(!s.size()) 21 | cout<<"-1"; 22 | else 23 | { 24 | for(auto it: s) 25 | cout<left && !root->right) 11 | return root; 12 | 13 | root->left = RemoveHalfNodes(root->left); 14 | root->right = RemoveHalfNodes(root->right); 15 | 16 | if(root->left && !root->right) 17 | return root->left; 18 | if(!root->left && root->right) 19 | return root->right; 20 | 21 | return root; 22 | } -------------------------------------------------------------------------------- /GFG/Tree/Root to Leaf path Sum.cpp: -------------------------------------------------------------------------------- 1 | /* https://practice.geeksforgeeks.org/problems/root-to-leaf-path-sum/1 */ 2 | 3 | bool hasPathSum(Node *node, int sum) 4 | { 5 | if(node==NULL) 6 | return (sum==0); 7 | 8 | if(node->left==NULL && node->right==NULL) 9 | return (node->data==sum); 10 | 11 | bool leftPathSum = hasPathSum(node->left, sum-node->data); 12 | bool rightPathSum = hasPathSum(node->right, sum-node->data); 13 | 14 | return (leftPathSum || rightPathSum); 15 | } -------------------------------------------------------------------------------- /GFG/Tree/Ternary Expression to Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | Node *convertExpression(string str, int i) 2 | { 3 | if(str.length()==0) 4 | return NULL; 5 | 6 | Node* root = new Node(str[0]); 7 | 8 | stack s; 9 | s.push(root); 10 | 11 | for(i=1; ileft=node; 17 | s.push(node); 18 | } 19 | else if(str[i]==':') 20 | { 21 | Node *node = new Node(str[i+1]); 22 | s.pop(); //to remove the sibling node 23 | while(!s.top()->right==NULL) //find the ancestor which don't have a right child yet 24 | s.pop(); 25 | s.top()->right=node; 26 | s.push(node); 27 | } 28 | } 29 | 30 | return root; 31 | } -------------------------------------------------------------------------------- /GFG/Tree/Transform to Sum Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree, we've to tarnsform it to the Sum Tree. 3 | A sum tree is one which has its value equal to sum of left & right subtree. 4 | 5 | Leaf node has 0 sum 6 | */ 7 | 8 | int toSumTreeUtil(Node *node) 9 | { 10 | if(node==NULL) 11 | return 0; 12 | 13 | int left = toSumTreeUtil(node->left); 14 | int right = toSumTreeUtil(node->right); 15 | 16 | int subtreeSum = left + right + node->data; 17 | 18 | node->data = left + right; 19 | return subtreeSum; 20 | } 21 | 22 | void toSumTree(Node *node) 23 | { 24 | if(node==NULL) 25 | return ; 26 | 27 | int dump = toSumTreeUtil(node); 28 | } -------------------------------------------------------------------------------- /Hackerearth/.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | 5 | main() 6 | { 7 | int t,a,b,c,k,res; 8 | cin>>t; 9 | while(t--) 10 | { 11 | cin>>a>>b>>c>>k; 12 | int low=0,high=100000,res=-1,mid; 13 | while(low<=high) 14 | { 15 | mid=(low+high)/2; 16 | ll val=a*(mid*mid) + b*mid +c; 17 | if(val >=k) 18 | { 19 | res=mid; 20 | high=mid-1; 21 | } 22 | else 23 | low=mid+1; 24 | } 25 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | long long int n,q,r,c,s; 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | cin>>n; 11 | q=n,c=0,r=0,s=0; 12 | while(q!=0) 13 | { 14 | r=q%10; 15 | s+=r; 16 | q/=10; 17 | c++; 18 | } 19 | if(s==(c*(c+1)/2)) 20 | cout<<"YES"< 2 | using namespace std; 3 | main() 4 | { 5 | long long int n,i,c=1; 6 | cin>>n; 7 | long long int a[n]; 8 | for(i=0;i>a[i]; 10 | for(i=0;ia[i+1]) 13 | { 14 | c++; 15 | continue; 16 | } 17 | } 18 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n,m,i,s1=0,s2=0; 6 | cin>>n>>m; 7 | int a[n],max_sum[n],min_sum[n]; 8 | for(i=0;i>a[i]; 11 | max_sum[i]=a[i]; 12 | min_sum[i]=a[i]; 13 | } 14 | sort(min_sum,min_sum+n); 15 | sort(max_sum,max_sum+n,greater()); 16 | for(i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | 6 | int n,i; 7 | cin>>n; 8 | int size=2*n; 9 | int total=0; 10 | int a[size]; 11 | for(i=0;i>a[i]; 13 | 14 | sort(a,a+size); 15 | 16 | for(i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | scanf("%d",&t); 7 | 8 | int n,i; 9 | 10 | while(t--) 11 | { 12 | scanf("%d",&n); 13 | vector v(n); 14 | int a[n]; 15 | 16 | for(i=0;i 2 | using namespace std; 3 | 4 | main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | long int n,m,i,j; 9 | cin>>n>>m; 10 | vector s(n); 11 | for(i=0;i>s[i]; 13 | 14 | sort(s.begin(),s.end()); 15 | 16 | string ans=""; 17 | 18 | for(i=0;i temp; 22 | for(j=0;jmax_fr) 32 | { 33 | max_fr=it.second; 34 | ch=it.first; 35 | } 36 | } 37 | ans+=ch; 38 | } 39 | cout< 2 | using namespace std; 3 | int humming_distance(string a,string b,int m) 4 | { 5 | int i,haming_dist=0; 6 | for(i=0;i 6 | using namespace std; 7 | main() 8 | { 9 | int t,n,i,k; 10 | cin>>t; 11 | while(t--) 12 | { 13 | cin>>n>>k; 14 | int a[n],b[n]; 15 | for(i=0;i>a[i]; 18 | b[(i+k)%n]=a[i]; 19 | } 20 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int t,n,i,max_ele,ele,j; 6 | cin>>t; 7 | while(t--) 8 | { 9 | cin>>n; 10 | max_ele=0; 11 | vector a,b; 12 | for(i=0;i>ele; 15 | a.push_back(ele); 16 | } 17 | 18 | for(i=0;i>ele; 21 | b.push_back(ele); 22 | } 23 | for(i=0;i 2 | using namespace std; 3 | int binary_sch(long long int a[],int ele,int low,int high) 4 | { 5 | int res; 6 | while(low<=high) 7 | { 8 | res=high; 9 | int mid=(low+high)/2; 10 | if(a[mid]==ele) 11 | { 12 | res=mid; 13 | low=mid+1; 14 | } 15 | else if(ele>a[mid]) 16 | { 17 | high=mid-1; 18 | res=high; 19 | } 20 | else if(ele>n; 31 | // int a[n]; 32 | // for(i=0;i>a[i]; 34 | // sort(a,a+n); 35 | // cin>>q; 36 | // while(q--) 37 | // { 38 | // cin>>ele; 39 | // cout< 2 | using namespace std; 3 | #define ll long long 4 | #define pb(x) push_back(x) 5 | main() 6 | { 7 | int t,i,n; 8 | string s; 9 | cin>>n; 10 | map m; 11 | while(n--) 12 | { 13 | cin>>s; 14 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int n,i; 6 | cin>>n; 7 | int a[n]; 8 | for(i=0;i>a[i]; 11 | bitset<100> b(a[i]); 12 | cout< 2 | using namespace std; 3 | int main() { 4 | int n,m,i,ques,c; 5 | cin>>n>>m; 6 | long long int a[n],sum,max_ele=0; 7 | for(i=0;i>a[i]; 10 | max_ele=max(max_ele,a[i]); 11 | } 12 | while(1) 13 | { 14 | sum=0,c=0; 15 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int s,n,r,c,i,h,max; 6 | cin>>s; 7 | while(s--) 8 | { 9 | c=1; 10 | cin>>n>>r; 11 | long long int h[n]; 12 | for(i=0;i>h[i]; 14 | max=h[0]; 15 | for(i=1;ih[i-1]&&h[i]>max) 17 | { 18 | c++; 19 | max=h[i]; 20 | } 21 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | int n,i; 6 | cin>>n; 7 | int a[n]; 8 | vector s; 9 | for(i=0;i>a[i]; 12 | s.push_back(a[i]); 13 | } 14 | for(int i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | long long int n,q,r,c,s; 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | cin>>n; 11 | q=n,c=0,r=0,s=0; 12 | while(q!=0) 13 | { 14 | r=q%10; 15 | s+=r; 16 | q/=10; 17 | c++; 18 | } 19 | if(s==(c*(c+1)/2)) 20 | cout<<"YES"< 2 | using namespace std; 3 | main() 4 | { 5 | int t,n,i,start,k,curr_sum,c; 6 | cin>>t; 7 | while(t--) 8 | { 9 | cin>>n>>k; 10 | long long int a[n]; 11 | for(i=0;i>a[i]; 13 | start=0,curr_sum=0,c=0; 14 | for(i=0;i=k) 18 | { 19 | curr_sum-=a[start++]; 20 | c++; 21 | } 22 | } 23 | cout< 2 | #define ll long long unsigned 3 | using namespace std; 4 | main() 5 | { 6 | int n,i,j,c=0; 7 | cin>>n; 8 | long long int a[n]; 9 | unordered_set s; 10 | for(i=0;i>a[i]; 13 | s.insert(a[i]); 14 | } 15 | int dist=s.size(); 16 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,sum=0,left_sum=0; 6 | cin>>n; 7 | int a[n]; 8 | for(i=0;i>a[i]; 11 | sum+=a[i]; 12 | 13 | } 14 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | string s; 6 | cin>>s; 7 | int i,n=s.length(); 8 | int a[n]; 9 | memset(a,-1,n); 10 | for(i=0;i 2 | using namespace std; 3 | int find(int a,int b,int n) 4 | { 5 | int c=0; 6 | if(a%b==0||b%a==0) 7 | c=a%b==0?a/b:b/a; 8 | else if(a*b<=n) 9 | c++; 10 | return c; 11 | } 12 | main() 13 | { 14 | int t,c,n,p,q,r; 15 | cin>>t; 16 | while(t--) 17 | { 18 | 19 | cin>>n>>p>>q>>r; 20 | c=0; 21 | if(p==q||q==r||r==p) 22 | { 23 | cout<<"0"< 3 | using namespace std; 4 | #define ll long long 5 | 6 | void dfs(vector G[], bool vis[], int s) 7 | { 8 | vis[s]=true; 9 | for(auto it:G[s]) 10 | if(!vis[it]) 11 | dfs(G,vis,it); 12 | } 13 | 14 | int main() 15 | { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | cout.tie(NULL); 19 | 20 | int t; 21 | cin>>t; 22 | while(t--) 23 | { 24 | int n; 25 | cin>>n; 26 | set s; 27 | for(int i=0;i>a>>b; 31 | s.insert(a); 32 | s.insert(b); 33 | } 34 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | long long int n,i; 6 | cin>>n; 7 | long long int a[n]; 8 | for(i=0;i>a[i]; 10 | for(i=0;i=*max_element(a+i+1,a+n)) 12 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | long long int n,i,max; 6 | cin>>n; 7 | long long int a[n]; 8 | vectorv; 9 | for(i=0;i>a[n-1-i]; 11 | max=a[0]; 12 | for(i=0;i=max) 14 | { 15 | max=a[i]; 16 | v.push_back(a[i]); 17 | } 18 | for(auto it=v.rbegin();it!=v.rend();it++) 19 | cout<<*it<<" "; 20 | } 21 | -------------------------------------------------------------------------------- /Hackerearth/Hourstorm#14/HS1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<n) 17 | break; 18 | x++; 19 | } 20 | printf("%lld\n",c); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Hackerearth/Lko1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int n,m,i,j,temp,flag; 6 | cin>>n; 7 | set a; 8 | for(i=0;i>temp; 11 | a.insert(temp); 12 | } 13 | cin>>m; 14 | set c; 15 | for(j=0;j>temp; 18 | c.insert(temp); 19 | } 20 | set b; 21 | for(auto it=a.begin();it!=a.end();it++) 22 | for(auto jt=c.begin();jt!=c.end();jt++) 23 | { 24 | flag=0; 25 | temp=*jt-*it; 26 | for(auto kt=a.begin();kt!=a.end();kt++) 27 | { 28 | if(c.find(temp+ *kt)==c.end()) 29 | { 30 | flag=1; 31 | break; 32 | } 33 | } 34 | if(flag==0) 35 | b.insert(temp); 36 | } 37 | for(auto it=b.begin();it!=b.end();it++) 38 | cout<<*it<<" "; 39 | } 40 | -------------------------------------------------------------------------------- /Hackerearth/Longest subsequence with common digit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int n,i; 6 | cin>>n; 7 | int a[n],q,r,max=0; 8 | map f; 9 | bool visited[10]; 10 | for(i=0;i>a[i]; 13 | q=a[i]; 14 | memset(visited,false,10); 15 | while(q>0) 16 | { 17 | r=q%10; 18 | if(visited[r]==false&&r!=0) 19 | { 20 | f[r]++; 21 | visited[r]=true; 22 | if(f[r]>max) 23 | max=f[r]; 24 | } 25 | q/=10; 26 | } 27 | } 28 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | int q,n,i,j,x,y; 6 | cin>>n; 7 | int a[n]; 8 | for(i=0;i>a[i]; 10 | sort(a,a+n); 11 | 12 | cin>>q; 13 | while(q--) 14 | { 15 | cin>>x>>y; 16 | if(x==0) 17 | { 18 | j=lower_bound(a,a+n,y)-a; 19 | if(j!=n) 20 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,k=1; 6 | long long int sum_so_far=0,max_sum=0; 7 | cin>>n; 8 | long long int a[n]; 9 | for(i=0;i>a[i]; 11 | for(i=0;imax_sum) 17 | { 18 | k=1; 19 | max_sum=sum_so_far; 20 | } 21 | else if(sum_so_far==max_sum) 22 | k++; 23 | } 24 | if(max_sum>0) 25 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | int t,n,k,i,res; 6 | cin>>t; 7 | while(t--) 8 | { 9 | cin>>n>>k; 10 | k++; 11 | int a[n+1]; 12 | a[0]=0; 13 | for(i=1;i>a[i]; 16 | a[i]+=a[i-1]; 17 | } 18 | sort(a,a+(n+1)); 19 | res=(n+1)*(n+1); 20 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int t,n,i,c=0,max=0; 6 | cin>>t; 7 | while(t--) 8 | { 9 | cin>>n; 10 | max=0,c=0; 11 | int a[n]; 12 | for(i=0;i>a[i]; 14 | if(a[0]%2==0) 15 | c++; 16 | for(i=1;imax) 26 | max=c; 27 | } 28 | } 29 | cout< 2 | using namespace std; 3 | #define ll long long 4 | main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int t; 9 | cin>>t; 10 | 11 | while(t--) 12 | { 13 | ll n; 14 | cin>>n; 15 | string s; 16 | cin>>s; 17 | ll l=stoll(s,nullptr,2); 18 | cout<<__builtin_popcountll(l)<<"\n"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Hackerearth/Minimum length of subarray having sum greater than or equal to K.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | BUT THIS ALGO ONLY WORK FOR ALL POSITIVE NUMBERS OR ALL NEGATIVE NUMBERS 3 | Complexicity: O(n(log n)) 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | main() 9 | { 10 | int a[10]={1,1,2,1,2,2,3,1,2,2}; 11 | int k=7,diff=10,start=0,curr_sum=0; 12 | for(int i=0;i<10;i++) 13 | { 14 | curr_sum+=a[i]; 15 | while(curr_sum>=k) 16 | { 17 | diff=min(diff,i-start+1); 18 | curr_sum-=a[start++]; 19 | } 20 | 21 | } 22 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | int n,i; 6 | cin>>n; 7 | int s[n]; 8 | for(i=0;i>s[i]; 10 | sort(s,s+n); 11 | string a,b; 12 | for(i=0;i 2 | using namespace std; 3 | void swap(int *a,int *b) 4 | { 5 | int t=*a; 6 | *a=*b; 7 | *b=t; 8 | } 9 | void sort(int i,int n,int *a) 10 | { 11 | for(int i=i;ia[j+1]) 14 | swap(&a[j],&a[j+1]); 15 | } 16 | main() 17 | { 18 | int n,i; 19 | cin>>n; 20 | int a[n]; 21 | for(i=0;i>a[i]; 23 | for(i=n-1;i>0;i--) 24 | if(a[i]>a[i-1]) 25 | break; 26 | if(i==0) 27 | { 28 | cout<<"NOT POssible"; 29 | exit(1); 30 | } 31 | if(i==n-1) 32 | swap(&a[n-1],&a[n-2]); 33 | else 34 | { 35 | swap(&a[i-1],&a[n-1]); 36 | sort(a+i,a+n); 37 | } 38 | for(i=0;i 2 | using namespace std; 3 | #define ll long long 4 | 5 | ll power(ll a, ll b, ll m) 6 | { 7 | if(b==0) 8 | return 1; 9 | ll ans=power(a,b/2,m); 10 | ans=(ans*ans)%m; 11 | if(b&1) 12 | ans=(ans*a)%m; 13 | return ans; 14 | } 15 | 16 | ll x,y; 17 | void extended_euclid(ll a,ll m) 18 | { 19 | //base case 20 | if(m==0) 21 | { 22 | x=1; 23 | y=0; 24 | return; 25 | } 26 | //recursive case 27 | extended_euclid(m,a%m); 28 | ll cx=y; 29 | ll cy=x-(a/m)*y; 30 | 31 | x=cx; 32 | y=cy; 33 | } 34 | main() 35 | { 36 | ll a,b,c,m; 37 | cin>>a>>b>>c>>m; 38 | ll ans1=power(a,b,m); 39 | 40 | extended_euclid(c,m); 41 | ll ans2=(x+m)%m; 42 | 43 | cout<<(ans1*ans2)%m; 44 | } 45 | -------------------------------------------------------------------------------- /Hackerearth/Police And Thief Grid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int t,n,k,i,j,c; 6 | char ele,first; 7 | cin>>t; 8 | while(t--) 9 | { 10 | cin>>n>>k; 11 | c=0; 12 | for(i=0;i > s; 15 | for(j=0;j>ele; 18 | if(j==0) 19 | first=ele; 20 | if(ele==first) //to store the first encountered element of every row in the stack 21 | s.push(make_pair(ele,j)); 22 | else if((j-s.top().second)<=k) 23 | { 24 | s.pop(); 25 | c++; 26 | } 27 | } 28 | } 29 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | cout.tie(NULL); 8 | 9 | int t,l,n; 10 | 11 | cin>>t; 12 | while(t--) 13 | { 14 | cin>>l; 15 | char s[l+1]; 16 | cin>>s; 17 | n=atoi(s); 18 | cout<<"\n"< 2 | using namespace std; 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *top=NULL; 9 | void push(int ele) 10 | { 11 | struct node *new_node=(struct node *)malloc(sizeof(struct node)); 12 | new_node->data=ele; 13 | new_node->next=NULL; 14 | if(top==NULL) 15 | top=new_node; 16 | else 17 | { 18 | new_node->next=top; 19 | top=new_node; 20 | } 21 | } 22 | void pop() 23 | { 24 | if(top==NULL) 25 | cout<<"Underflow"<data<next; 31 | free(r); 32 | } 33 | } 34 | main() 35 | { 36 | push(10); 37 | push(20); 38 | push(30); 39 | push(40); 40 | pop(); 41 | pop(); 42 | } 43 | -------------------------------------------------------------------------------- /Hackerearth/Strange Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int t,n,k,max,dif,i; 6 | cin>>t; 7 | while(t--) 8 | { 9 | cin>>n>>k; 10 | long long int a[n],b[n],s=0; 11 | max=0; 12 | for(i=0;i>a[i]; 14 | for(i=0;i>b[i]; 16 | for(i=0;imax) 20 | max=b[i]; 21 | } 22 | max=max+1; 23 | for(i=0;i 5 | using namespace std; 6 | main() 7 | { 8 | int n=5,i,sum=0; 9 | int a[]={4, 2, -3, 1, 6}; 10 | unordered_set sumset; 11 | for(i=0;i 2 | using namespace std; 3 | #define ll long long 4 | 5 | main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(0); 9 | cout.tie(0); 10 | 11 | int t,i; 12 | cin>>t; 13 | int z,n; 14 | while(t--) 15 | { 16 | cin>>z>>n; 17 | ll a[n]; 18 | 19 | for(i=0;i>a[i]; 22 | z&=a[i]; 23 | } 24 | if(z) 25 | cout<<"No\n"; 26 | else 27 | cout<<"Yes\n"; 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Hackerearth/Subtract 1 from array and make them zero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | long long int n,i,flag=0; 6 | cin>>n; 7 | long long int a[n],s; 8 | for(i=0;i>a[i]; 10 | s=a[0]+a[n-1]; 11 | for(i=1;i 2 | using namespace std; 3 | main() 4 | { 5 | int n,q,k,j; 6 | cin>>n>>q; 7 | int a[n]; 8 | for(int i=0;i>a[i]; 11 | if(i!=0) 12 | a[i]+=a[i-1]; 13 | 14 | } 15 | while(q--) 16 | { 17 | cin>>k; 18 | j=binary_search(a,a+n,k); 19 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | long long int n,q,i,j; 6 | cin>>n; 7 | long long int a[n],avg[n],k; 8 | for(i=0;i>a[i]; 11 | if(i!=0) 12 | a[i]+=a[i-1]; 13 | } 14 | cin>>q; 15 | 16 | for(i=0;i>k; 22 | j=lower_bound(avg,avg+n,k)-avg; 23 | cout< 2 | main() 3 | { 4 | long long int t,n,k,v,i; 5 | scanf("%lld",&t); 6 | while(t--) 7 | { 8 | scanf("%lld%lld%lld",&n,&k,&v); 9 | long long int a[n],s=0; 10 | for(i=0;i0) 16 | printf("%d\n",(v*(n+k)-s)/k); 17 | else 18 | printf("-1\n"); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Hackerearth/lunch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int t,n,k,v,i; 5 | scanf("%d",&t); 6 | while(t--) 7 | { 8 | scanf("%d%d%d",&n,&k,&v); 9 | long long int a[n],s=0; 10 | for(i=0;i 2 | main() 3 | { 4 | int t,n,k,v,i; 5 | scanf("%d",&t); 6 | while(t--) 7 | { 8 | scanf("%d%d%d",&n,&k,&v); 9 | long long int a[n],s=0; 10 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,c=1,max=0,max_ele; 6 | cin>>n; 7 | int a[n]; 8 | for(i=0;i>a[i]; 10 | sort(a,a+n); 11 | for(i=0;imax) 21 | { 22 | max=c; 23 | max_ele=a[i]; 24 | } 25 | } 26 | } 27 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | int n,k,i; 6 | cin>>n>>k; 7 | int a[n]; 8 | for(i=0;i>a[i]; 10 | for(i=0;i<=n-k;i++) 11 | cout<<*max_element(a+i,a+i+k)<<" "; 12 | } 13 | -------------------------------------------------------------------------------- /Hackerearth/maximum sum subarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,c=1; 6 | cin>>n; 7 | long long int a[n]; 8 | for(i=0;i>a[i]; 10 | sort(a,a+n,greater()); 11 | long long int s=a[0],max_sum=a[0]; 12 | for(i=1;imax_sum) 16 | { 17 | max_sum=s; 18 | c++; 19 | } 20 | else 21 | break; 22 | } 23 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | long long int n,k,i; 8 | while(t--) 9 | { 10 | cin>>n>>k; 11 | long long int a[n],min=1e6+1; 12 | for(i=0;i>a[i]; 15 | if(a[i] 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,k,c=0; 6 | cin>>n; 7 | int a[n]; 8 | for(i=0;i>a[i]; 10 | sort(a,a+n); 11 | cin>>k; 12 | for(i=0;i 2 | using namespace std; 3 | main(){ 4 | int n,i,j,c=0; 5 | cin>>n; 6 | string s; 7 | cin>>s; 8 | vector v; 9 | for(i=0;i=v[i]) 23 | c++; 24 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | int n,q,i,target,s; 6 | cin>>n; 7 | int a[n]; 8 | for(i=0;i>a[i]; 10 | cin>>q; 11 | while(q--) 12 | { 13 | s=0; 14 | cin>>target; 15 | for(i=0;i=target) 19 | { 20 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "< 2 | int main(void) 3 | { 4 | int i=0,j=0,fact[20000],k=0,l=0,n=0,temp=0; 5 | 6 | fact[0]=1; 7 | l=1; 8 | printf("enter value: "); 9 | scanf("%d",&n); 10 | for(i=2;i<=n;i++) 11 | { 12 | for(j=0;j0) 18 | { 19 | fact[l++]=temp%10; 20 | temp=temp/10; 21 | } 22 | } 23 | for(i=l-1;i>-1;i--) 24 | printf("%d",fact[i]); 25 | 26 | printf("\npress any key to exit.... "); 27 | getchar(); //On windows change this line to getch(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/ACM ICPC team.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n,m,i,j,c,count=0; 6 | cin>>n>>m; 7 | string s[n]; 8 | for(i=0;i>s[i]; 10 | 11 | vector v; 12 | for(i=0;i(s[i]).to_ullong(); 16 | unsigned long long b = std::bitset<64>(s[j]).to_ullong(); 17 | unsigned long long c=a|b; 18 | v.push_back( __builtin_popcount(c)); 19 | } 20 | int max_ele=*max_element(v.begin(), v.end()); 21 | cout< 2 | main() 3 | { 4 | int l=0,d=1,c=0; 5 | int a[5]; 6 | for(int i=0;i<5;i++) 7 | scanf("%d",&a[i]); 8 | for(int i=2;i<=5;i++) 9 | { 10 | l=a[0]+ (i-1)*d; 11 | if(l!=a[i-1]) 12 | c++; 13 | } 14 | printf("%d",c); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/ASSIGN1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,j,key; 6 | cout<<"Enter size of array"<>n; //to take input size of array from the user 8 | int arr[n]; 9 | cout<<"Enter elements of array"<>arr[i]; 12 | cout<<"Enter element to delete"<>key; 14 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,j,k; 6 | cout<<"Enter size of array"<>n; //input size of array from user 8 | int arr[n]; 9 | cout<<"Enter elements of array"<>arr[i]; 12 | for(i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int t,n,k,i,c=0,flag=0; 6 | cin>>t; 7 | while(t--) 8 | { 9 | cin>>n>>k; 10 | int arr[n]; 11 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,j; 6 | cin>>n; 7 | int a[n],f[n]; 8 | for(i=0;i>a[i]; 11 | f[i]=0; 12 | } 13 | for(i=0;i 2 | void main() 3 | { 4 | int d,n,i; 5 | scanf("%d%d",&n,&d); 6 | int a[n],t[n-d]; 7 | for(i=0;idata || v2==temp->data) 7 | { 8 | return temp; 9 | } 10 | else if(v1data && v2data) 11 | { 12 | temp=temp->left; 13 | } 14 | else if(v1>temp->data && v2>temp->data) 15 | { 16 | temp=temp->right; 17 | } 18 | else 19 | { 20 | return temp; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Bubble Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | register int t; 5 | int a[5]={8,5,4,2,1}; 6 | for(int i=0;i<5;i++) 7 | for(int j=0;j<4;j++) 8 | if(a[j]>a[j+1]) 9 | { 10 | t=a[j]; 11 | a[j]=a[j+1]; 12 | a[j+1]=t; 13 | } 14 | for(int k=0;k<5;k++) 15 | printf("%d ",a[k]); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Chase Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int p1,p2,s1,s2; 6 | cout<<"Enter P1 and S1"<>p1>>s1; 8 | cout<<"Enter P2 and S2"<>p2>>s2; 10 | if(p2>p1&&s2>s1) 11 | cout<<"NEVER Meet"< 2 | main() 3 | { 4 | int n; 5 | int i; 6 | scanf("%d",&n); 7 | int a[n],f[100]={0}; 8 | for(i=0;i 2 | #define max 100 3 | using namespace std; 4 | struct dequeue 5 | { 6 | int array[max]; 7 | int rear; 8 | int fron; 9 | }; 10 | bool isempty(struct dequeue *q) 11 | { 12 | if(q->fron==-1 && q->rear==-1) 13 | return true; 14 | } 15 | bool isfull(struct dequeue *q) 16 | { 17 | if(q->fron==0&&q->rear==max-1 || q->fron=q->rear+1) 18 | return true; 19 | } 20 | void insert_rear(struct dequeue *q,int ele) 21 | { 22 | if(isfull(q)) 23 | cout<<"overflow"<rear=0; 29 | q->array[q->rear]=ele; 30 | q->fron=0; 31 | } 32 | else if(q->rear==max-1) 33 | q->rear=0; 34 | else 35 | q->rear++; 36 | } 37 | } 38 | main() 39 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Factorial big number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 500 3 | using namespace std; 4 | 5 | void factorial(int n) 6 | { 7 | int res[MAX],carry=0,l=1; 8 | res[0]=1; 9 | 10 | for(int i=2;i<=n;i++) 11 | { 12 | for(int j=0;j0) 20 | { 21 | res[l++]=carry%10; 22 | carry/=10; 23 | } 24 | } 25 | for(int i=l-1;i>=0;i--) 26 | cout<>n; 35 | factorial(n); 36 | } 37 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Find Merge Point of Two Lists.cpp: -------------------------------------------------------------------------------- 1 | int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) 2 | { 3 | SinglyLinkedListNode* tmp1=head1; 4 | SinglyLinkedListNode* tmp2=head2; 5 | while(tmp1!=tmp2) 6 | { 7 | tmp1=tmp1->next; 8 | tmp2=tmp2->next; 9 | if(tmp1==NULL) 10 | { 11 | tmp1=head2; 12 | } 13 | if(tmp2==NULL) 14 | { 15 | tmp2=head1; 16 | } 17 | } 18 | return tmp1->data; 19 | } -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Find x so that sum become zero.cpp: -------------------------------------------------------------------------------- 1 | /*Given an array of n integers, The task is to find another integer x such 2 | that, if all the elements of the array are subtracted individually from the number 3 | x, then the sum of all the differences should add to 0. If any such integer 4 | exists, print the value of x, else print -1.*/ 5 | #include 6 | using namespace std; 7 | main() 8 | { 9 | int n,i,s=0; 10 | cin>>n; 11 | int a[n]; 12 | for(i=0;i>a[i]; 15 | s+=a[i]; 16 | } 17 | if(s%n==0) 18 | cout< 2 | 3 | int gcd(int a,int b) 4 | { 5 | if(b!=0) 6 | return gcd(b,a%b); 7 | else 8 | return a; 9 | } 10 | int main() 11 | { 12 | int a[5]={-2,4,0,8,16},result=a[0]; 13 | for(int i=0;i<4;i++) 14 | result=gcd(result,a[i+1]); 15 | printf("%d",result); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Graph Representation.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Insertion Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int n,i,value,hole; 6 | cin>>n; 7 | int a[n]; 8 | for(i=0;i>a[i]; 10 | for(i=1;i0 && a[hole-1]>value) 15 | { 16 | a[hole]=a[hole-1]; 17 | hole=hole-1; 18 | } 19 | a[hole]=value; 20 | } 21 | for(i=0;i 4 | using namespace std; 5 | main() 6 | { 7 | int a[]={-2, -3, 4, -1, -2, 1, 5, -3}; 8 | int max_so_far=0,max_upto_here=0; 9 | for(int i=0;i<8;i++) 10 | { 11 | max_upto_here+=a[i]; 12 | if(max_upto_here<0) 13 | max_upto_here=0; 14 | if(max_upto_here>max_so_far) 15 | max_so_far=max_upto_here; 16 | } 17 | if(max_so_far!=0) 18 | cout< 4 | using namespace std; 5 | main() 6 | { 7 | int a[]={-10,1,3,-2,4}; 8 | int max_so_far=a[0],max_upto_here=a[0]; 9 | for(int i=1;i<5;i++) 10 | { 11 | max_upto_here=max(a[i],max_upto_here+a[i]); 12 | if(max_upto_here>max_so_far) 13 | max_so_far=max_upto_here; 14 | } 15 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n,i,j; 7 | cin>>n; 8 | vector a(n); 9 | int s[n]; 10 | for(i=0;i>a[i]; 12 | sort(a.begin(),a.end()); 13 | for(i=0;i 2 | using namespace std; 3 | 4 | int sum=INT_MIN; 5 | int max_sum_subarray(int low, int high, int *a) 6 | { 7 | 8 | if(low==high) 9 | { 10 | return a[low]; 11 | 12 | } 13 | 14 | int mid=(low+high)/2; 15 | int left=max_sum_subarray(low,mid,a); 16 | int right=max_sum_subarray(mid+1,high,a); 17 | 18 | if((left+right)>sum) 19 | sum=left+right; 20 | return sum; 21 | } 22 | 23 | main() 24 | { 25 | int a[]={3,-1,-1,10,-3,-2,4}; 26 | cout< 2 | #include 3 | int length(int a) 4 | { 5 | int q=a,c=0; 6 | while(q!=0) 7 | { 8 | q/=10; 9 | c++; 10 | } 11 | return c; 12 | } 13 | int rnum(int a,int n,int d) 14 | { 15 | int b=pow(10,d); 16 | int r=a%b; 17 | return r; 18 | } 19 | int lnum(int a,int n,int d) 20 | { 21 | int l=a/pow(10,d); 22 | return l; 23 | } 24 | main() 25 | { 26 | int p,q,l=0,r=0,n,s,d,c=0; 27 | scanf("%d%d",&p,&q); 28 | for(int i=p;i<=q;i++) 29 | { 30 | s=i*i; 31 | d=length(i); 32 | n=length(s); 33 | r=rnum(s,n,d); 34 | l=lnum(s,n,d); 35 | if(r+l==i) 36 | { 37 | printf("%d ",i); 38 | c++; 39 | } 40 | } 41 | if(c==0) 42 | printf("INVALID RANGE"); 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Next Permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | main() 5 | { 6 | int c=0,j=0,n,i; 7 | cin>>n; 8 | string s[n],r[n],t; 9 | for(i=0;i>s[i]; 11 | while(j0) 17 | { 18 | r[j]=s[j]; 19 | c++; 20 | break; 21 | } 22 | } while(next_permutation(s[j].begin(),s[j].end())); 23 | if(c==0) 24 | r[j]="no answer"; 25 | j++; 26 | } 27 | for (i = 0; i < n; i++) 28 | cout< 2 | using namespace std; 3 | void solve(int a[],int low,int high) 4 | { 5 | if(low<=high) 6 | { 7 | int mid=(low+high)/2; 8 | cout< 2 | main() 3 | { 4 | int a[8]={1,2,3,4,5,6,7,8}; 5 | int c=0; 6 | for(int i=0;i<8;i++) 7 | for(int j=i+1;j<8;j++) 8 | for(int k=j+1;k<8;k++) 9 | if(a[i]*a[i]==a[j]*a[j]+a[k]*a[k]||a[j]*a[j]==a[i]*a[i]+a[k]*a[k]||a[k]*a[k]==a[j]*a[j]+a[i]*a[i]) 10 | { 11 | c++; 12 | /*a[k]=0; 13 | a[i]=0; 14 | a[j]=0;*/ 15 | printf("%d%d%d",a[i],a[j],a[k]); 16 | } 17 | printf("\n%d",c); 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Quick Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void quick_sort(int a[],int first,int last) 4 | { 5 | int low=first; 6 | int high=last; 7 | int pi=a[(first+last)/2]; 8 | do 9 | { 10 | while(a[low]pi) //change sign for descending 13 | high--; 14 | if(low<=high) 15 | { 16 | int temp=a[low]; 17 | a[low++]=a[high]; 18 | a[high--]=temp; 19 | } 20 | }while(low<=high); 21 | if(first 2 | using namespace std; 3 | #define ll long long 4 | main() 5 | { 6 | ll t,n,i; 7 | cin>>n; 8 | vector v(n),sum(n); 9 | 10 | for(i=0;i>v[i]; 13 | if(i!=0) 14 | sum[i]=sum[i-1]+v[i]; 15 | else 16 | sum[i]=v[i]; 17 | } 18 | cin>>t; 19 | ll l,r,x,op,c=0; 20 | while(t--) 21 | { 22 | cin>>op; 23 | if(op==1) 24 | { 25 | cin>>l>>r; 26 | if(l>=2) 27 | cout<>x; 34 | c++; 35 | sum.insert(sum.begin(),x); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Rotate Matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | void rotate(int a[4][4],int m,int n) 3 | { 4 | int i,k=0,l=0,p,c; 5 | while(k=l;i--) 27 | { 28 | c=a[m-1][i]; 29 | a[m-1][i]=p; 30 | p=c; 31 | } 32 | m--; 33 | } 34 | if(l=k;i--) 37 | { 38 | c=a[i][l]; 39 | a[i][l]=p; 40 | p=c; 41 | } 42 | l++; 43 | } 44 | 45 | } 46 | for(int x=0;x<4;x++) 47 | for(int y=0;y<4;y++) 48 | printf("%d ",a[x][y]); 49 | 50 | } 51 | main() 52 | { 53 | int a[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},m=4,n=4; 54 | rotate(a,m,n); 55 | } 56 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Selection Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | register int min,t,l; 5 | int a[5]={64,25,22,12,11},j; 6 | for(int i=0;i<5;i++) 7 | { 8 | min=a[i]; 9 | for(j=i;j<5;j++) 10 | if(a[j]<=min) 11 | { 12 | min=a[j]; 13 | l=j; 14 | } 15 | 16 | t=a[i]; 17 | a[i]=min; 18 | a[l]=t; 19 | } 20 | for(int k=0;k<5;k++) 21 | printf("%d ",a[k]); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Selection Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int a[]={10, 7, 8, 9, 1, 5}; 6 | int i,j,min,n=6,min_index,temp; 7 | for(i=0;i 2 | int i=0,j=0,l,t,min; 3 | void selection(int *a,int n) 4 | { 5 | if(i 2 | main() 3 | { 4 | int a[100],i,j; 5 | for(i=0;i<100;i++) 6 | a[i]=i+1; 7 | 8 | for(i=1;i<100;i++) 9 | { 10 | for(j=0;j<100;j++) 11 | if(a[i]!=0&&a[j]%a[i]==0) 12 | a[j]=0; 13 | } 14 | for(i=0;i<100;i++) 15 | if(a[i]!=0) 16 | printf("%d ",a[i]); 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Sieve of Eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void check_prime(int *prime,int b) 4 | { 5 | for(int i=2;i*i<=b;i++) 6 | { 7 | if(prime[i-2]!=-1) 8 | { 9 | for(int j=i*i;j<=b;j+=i) 10 | prime[j]=-1; 11 | } 12 | } 13 | } 14 | 15 | int main() 16 | { 17 | int a,b,i,j,q,c=0,rem=0,s; 18 | cin>>a>>b; 19 | int prime[b+1]; 20 | for(i=0;i 2 | main() 3 | { 4 | int arr[5] = {1, 10, 5, 2, 7},min=2580,t; 5 | for(int i=0;i<5;i++) 6 | { 7 | int sum=0; 8 | for(int j=i;j<5;j++) 9 | { 10 | sum+=arr[i]+arr[j]; 11 | if(sum>10) 12 | { 13 | t=j-i+1; 14 | if(t 3 | using namespace std; 4 | void solve(int a[],int low,int high) 5 | { 6 | if(low<=high) 7 | { 8 | int mid=(low+high)/2; 9 | cout< 2 | void spiral(int a[4][4],int m,int n) 3 | { 4 | int i,k=0,l=0; 5 | while(k=l;i--) 16 | printf("%d ",a[m-1][i]); 17 | m--; 18 | } 19 | if(l=k;i--) 22 | printf("%d ",a[i][l]); 23 | l++; 24 | } 25 | 26 | 27 | } 28 | 29 | 30 | } 31 | main() 32 | { 33 | int a[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},m=4,n=4; 34 | spiral(a,m,n); 35 | } 36 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Stack Implementation using Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int mian() 4 | { 5 | } 6 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Stack using Linked List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *top=NULL; 9 | void push(int ele) 10 | { 11 | struct node *new_node=(struct node *)malloc(sizeof(struct node)); 12 | new_node->data=ele; 13 | new_node->next=NULL; 14 | if(top==NULL) 15 | top=new_node; 16 | else 17 | { 18 | new_node->next=top; 19 | top=new_node; 20 | } 21 | } 22 | void pop() 23 | { 24 | if(top==NULL) 25 | cout<<"Underflow"<data<next; 31 | free(r); 32 | } 33 | } 34 | main() 35 | { 36 | push(10); 37 | push(20); 38 | push(30); 39 | push(40); 40 | pop(); 41 | pop(); 42 | } 43 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | main() 5 | { 6 | 7 | string s="Hello",s2; 8 | s2=s.substr(0,s.length()); 9 | cout< 2 | main() 3 | { 4 | int a[5][5]={{0, 1, 1, 0, 1}, 5 | {1, 1, 0, 1, 0}, 6 | {0, 1, 1, 1, 0}, 7 | {1, 1, 1, 1, 0}, 8 | {1, 1, 1, 1, 1}}; 9 | int s[6][5],k=0,l=0,max=0,i,j; 10 | while(k<5&&l<5) 11 | { 12 | int c=0; 13 | for(int i=0;i<=k;i++) 14 | for(int j=0;j<=l;j++) 15 | if(a[i][j]!=1) 16 | c++; 17 | if(c==0) 18 | s[i][j]=k; 19 | else 20 | s[i][j]=0; 21 | k++; 22 | l++; 23 | } 24 | for(int x=0;x<6;x++) 25 | for(int y=0;y<5;y++) 26 | if(s[x][y]>max) 27 | max=s[x][y]; 28 | printf("\n%d",max); 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Sum of all subarrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Find sum of all subarrays 3 | Note: Subarrays means contigious subarrays and there are n(n+1)/2 subarrays having array of size n 4 | Geeks for Geeks: https://www.geeksforgeeks.org/sum-of-all-subarrays/ 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | main() 10 | { 11 | int n,i; 12 | cin>>n; 13 | int a[n]; 14 | int ans=0; 15 | for(i=0;i>a[i]; 18 | ans+=a[i]*(n-i)*(i+1); 19 | } 20 | cout< 3 | int fun(int a){ 4 | if(a==1) 5 | return a; 6 | s=a+fun(a-1); 7 | return s; 8 | } 9 | 10 | main() 11 | { 12 | int a=3; 13 | printf("%d",fun(a)); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Untitled1.c.save-failed: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | main() 4 | { 5 | char s[]={1,0,1,0}; 6 | char t[]={0,1,0,1}; 7 | int a=atoi(s); 8 | int b=atoi(t); 9 | a=a|b; 10 | printf("%d",a); 11 | } 12 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Untitled2 - Copy.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int x=4,y=0,z; 5 | while(x>=0) 6 | { 7 | x--; 8 | y++; 9 | if(x==y) 10 | continue; 11 | else 12 | printf("\n%d%d",x,y); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Untitled2.c: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int a[5]={'2','4','4','5','2'}; 6 | int s=0; 7 | for(int i=0;i<5;i++) 8 | s^=a[i]; 9 | cout< 2 | using namespace std; 3 | void solve(string s,int n) 4 | { 5 | int i,no_of_replace=0,no_of_a=0; 6 | if(s[0]=='a') 7 | no_of_a++; 8 | 9 | for(i=1;i>t; 39 | while(t--) 40 | { 41 | cin>>n>>s; 42 | solve(s,n); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Untitled4.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | char a[]="1239"; 5 | int b=1239; 6 | if(a==b) 7 | printf("dhjj"); 8 | } 9 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Untitled4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | main() 7 | { 8 | vector v{1,2,3,4}; 9 | vector::iterator it=v.begin(),it1=v.end(); 10 | do 11 | { 12 | cout< 2 | main() 3 | { 4 | int n; 5 | char arr[10002]; 6 | scanf("%d",&n); 7 | for(int i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int a[]={8 8 7 7 5 5 4 3 2}; 6 | cout< 2 | using namespace std; 3 | main() 4 | { 5 | int a[]={8, 8, 7, 7, 5, 5, 4, 3, 2}; 6 | cout< 2 | main() 3 | { 4 | int n,s; 5 | scanf("%d",&n); 6 | for(int i=0;i 2 | main() 3 | { 4 | int n; 5 | scanf("%d",&n); 6 | for(int i=0;i 2 | #include 3 | int factorial(int n) 4 | { 5 | int p=1; 6 | for(int i=1;i<=n;i++) 7 | p*=i; 8 | return p; 9 | } 10 | 11 | main() 12 | { 13 | int n,i; 14 | char a[10][20]={"Mady","hash_include","sastava007","shakshi119","shikha001","pallo","the_unknown","sudo_bug","newton003","harshal"}; 15 | char *s; 16 | printf("Enter Your Username\n"); 17 | scanf("%s",s); 18 | for(i=0;i<10;i++) 19 | { 20 | if(strcmp(s,a[i])==0) 21 | { 22 | printf("Enter a number to find factorial\n"); 23 | scanf("%d",&n); 24 | printf("%d\n",factorial(n)); 25 | break; 26 | } 27 | } 28 | if(i==10) 29 | printf("Access Denied\n"); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/Vowel in Sub string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | string s; 10 | int l; 11 | long long int ans=0; 12 | cin>>s; 13 | l=s.length(); 14 | for(int i=1;i<=s.length();i++) 15 | { 16 | if(s[i-1]=='a'||s[i-1]=='e'||s[i-1]=='i'||s[i-1]=='o'||s[i-1]=='u'||s[i-1]=='A'||s[i-1]=='E'||s[i-1]=='I'||s[i-1]=='O'||s[i-1]=='U') 17 | { 18 | ans+=l*i; 19 | } 20 | l--; 21 | } 22 | cout< 8 | using namespace std; 9 | main() 10 | { 11 | ios_base::sync_with_stdio(false); 12 | cin.tie(0); 13 | int n,i; 14 | cin>>n; 15 | int a[n]; 16 | for(i=0;i>a[i]; 18 | int xore=0; 19 | for(i=0;i 2 | using namespace std; 3 | main() 4 | { 5 | int n,q,i,flag=0; 6 | int a,x,b=-1; 7 | cin>>n>>q; 8 | int s[n]; 9 | for(i=0;i>s[i]; 11 | while(q--) 12 | { 13 | flag=0; 14 | cin>>a>>x; 15 | if(a==0) 16 | cin>>b; 17 | 18 | if(a==0) 19 | s[x-1]=b; 20 | else if(a==1) 21 | { 22 | for(i=0;i=x) 24 | { 25 | cout< 2 | main() 3 | { 4 | for(int i=0;i<3;++i) 5 | { 6 | int c=1; 7 | for(int j=0;j<3;j++) 8 | { 9 | if(i+j>=2) 10 | { 11 | printf("%d",c); 12 | c++; 13 | } 14 | else 15 | printf(" "); 16 | } 17 | for(int k=0;k 2 | #include 3 | main() 4 | { long long int a,b; 5 | int t,j=0,c=0; 6 | scanf("%d",&t); 7 | int d[t]; 8 | while(j 2 | main() 3 | { 4 | int t,j=0,n,i,k=0; 5 | char *s; 6 | scanf("%d",&t); 7 | int r[t]; 8 | while(k 2 | #include 3 | #include 4 | main() 5 | { 6 | char *s="2576"; 7 | int n=atoi(s); 8 | printf("%d",n); 9 | } 10 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/cp2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main() 4 | { 5 | int n,q,i; 6 | cin>>n; 7 | int a[n]; 8 | for(i=0;i>a[i]; 10 | int nq,c=0; 11 | cin>>nq; 12 | sort(a,a+n); 13 | while(nq--) 14 | { 15 | cin>>q; 16 | c=0; 17 | if(q>a[n-1]) 18 | { 19 | 20 | cout<q) 31 | { 32 | m=(n-m)/2; 33 | } 34 | else 35 | { 36 | m=(n+m)/2; 37 | } 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/delete from array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int delete(int *a,int i,int n) 3 | { 4 | for(int j=n-1-i;j 2 | main() 3 | { 4 | int n,i,j; 5 | scanf("%d",&n); 6 | int a[n]; 7 | for(i=0;i 2 | main() 3 | { 4 | int n; 5 | char arr[10002]; 6 | scanf("%d",&n); 7 | for(int i=0;i 2 | main() 3 | { 4 | int n,q,c=0,hexa=0; 5 | scanf("%d",&n); 6 | q=n; 7 | while(q!=0) 8 | { 9 | q=q/10; 10 | c++; 11 | } 12 | int temp,rem; 13 | char arr[c]; 14 | for(int i=0;i=0;x--) 34 | printf("%s",arr[x]); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/inversion array.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int n[5],i,j,c=0; 5 | for(i=0;i<5;i++) 6 | scanf("%d",&n[i]); 7 | for(i=0;i<5;i++) 8 | for(j=0;j<5;j++) 9 | if(n[i]>n[j]&&i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | main() 7 | { 8 | 9 | vector v1(6); 10 | v1={111,24,13,45,53,62}; 11 | int a[]={2,4,67,7,3,1,0,56}; 12 | // vector::iterator x=*min_element(v1.begin(),v1.end())-v1.begin(); 13 | 14 | sort(a,a+8,greater()); 15 | for(int i=0;i<8;i++) 16 | cout< 2 | main() 3 | { 4 | int a[10]={1,3,5,3,7,1,5,1,5,1},f[10]={-1},res[10],max=0; 5 | for(int i=0;i<10;i++) 6 | { 7 | int c=1; 8 | for(int j=i+1;j<10;j++) 9 | if(a[i]==a[j]) 10 | { 11 | c++; 12 | a[j]=0; 13 | } 14 | if(a[i]!=0) 15 | f[i]=c; 16 | } 17 | /*for(int k=0;k<10;k++) 18 | if(a[k]!=0) 19 | printf("Frequency of %d is %d\n",a[k],f[k]); 20 | */ 21 | for(int k=0;k<10;k++) 22 | if(f[k]!=-1) 23 | { 24 | if(f[k]>max) 25 | max=f[k]; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/lcm.c: -------------------------------------------------------------------------------- 1 | #include 2 | int lcm(int n,int *a) 3 | { 4 | int i=1,c=0; 5 | while(1) 6 | { 7 | for(int j=0;j 2 | #include 3 | main() 4 | { 5 | int i,a[8],max=0,j,sub[8]; 6 | for(i =0;i<8;i++) 7 | { 8 | scanf("%d",&a[i]); 9 | sub[i]=-1; 10 | } 11 | 12 | for(i =0;i<8;i++) 13 | { 14 | int s=0; 15 | for(j=i;j<8;j++) 16 | { 17 | s+=a[j]; 18 | if(s>max) 19 | { 20 | max=s; 21 | sub[j]=j; 22 | } 23 | } 24 | } 25 | for(i =0;i<8;i++) 26 | if(sub[i]!=-1) 27 | printf("%d",sub[i]); 28 | printf("\n%d",max); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/max with least comparison.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | main() 4 | { 5 | clock_t start,end; 6 | start=clock(); 7 | int a[5],i,max=0; 8 | for(i=0;i<5;i++) 9 | scanf("%d",&a[i]); 10 | for(i=0;i<4;i++) 11 | { 12 | int t=a[i]>a[i+1]?a[i]:a[i+1]; 13 | if(t>max) 14 | max=t; 15 | } 16 | printf("%d",max); 17 | end=clock(); 18 | double time_used = ((double) (end - start)) / CLOCKS_PER_SEC; 19 | printf("%lf",time_used); 20 | } 21 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/min max pallindrome 1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int check(char *a) 4 | { 5 | char *b=strrev(a); 6 | if(strcmp(a,b)==0) 7 | return 1; 8 | else 9 | return 0; 10 | } 11 | 12 | main() 13 | { 14 | int min=100,max=0; 15 | char s[100]; 16 | gets(s); 17 | int i,j; 18 | for(i=0;s[i]!='\0';i++) 19 | { 20 | char t[20]; 21 | for(j=0;s[j]!=' '&&s[j]!='\0';j++) 22 | { 23 | t[j]=s[i]; 24 | i++; 25 | } 26 | if(check(t)==1) 27 | { 28 | if(strlen(t)<=min) 29 | min=strlen(t); 30 | if(strlen(t)>=max) 31 | max=strlen(t); 32 | } 33 | } 34 | printf("min=%d max=%d",min,max); 35 | } 36 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/min max pallindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int pallindrome(char *s) 4 | { 5 | char *t=strrev(s); 6 | if(strcmp(s,t)==0) 7 | return strlen(s); 8 | else 9 | return 0; 10 | } 11 | 12 | main() 13 | { 14 | char *s="wow I speak abccba",t[10]; 15 | int n=strlen(s),i=0,j=0,min=1,max=1; 16 | while(s[i]!='\0') 17 | { 18 | while(s[j]!=' '&& j=max) 27 | max=pallindrome(t); 28 | i++; 29 | j=i; 30 | } 31 | 32 | printf("min=%d max=%d",min,max); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/nand.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int t; 5 | scanf("%d",&t); 6 | long long int n[t],rem,q,c; 7 | 8 | int d[t],j=0; 9 | while(j 2 | main() 3 | { 4 | int c=1,d=3; 5 | for(int i=0;i<7;i++) 6 | { 7 | for(int j=0;j<7;j++) 8 | { 9 | c=c-1; 10 | if(i==j||i+j==6) 11 | { 12 | 13 | if(i<=3) 14 | { 15 | printf("%d",c); 16 | c++; 17 | } 18 | else if(i>3) 19 | { 20 | printf("%d",d); 21 | d--; 22 | } 23 | } 24 | else 25 | printf(" "); 26 | } 27 | printf("\n"); 28 | 29 | } 30 | 31 | } 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/output.txt: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | void demo(); 5 | void (*fun)(); 6 | fun = demo; 7 | (*fun)(); 8 | fun(); 9 | return 0; 10 | } 11 | 12 | void demo() 13 | { 14 | printf("GeeksQuiz "); 15 | } 16 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/pair.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | main() 6 | { 7 | 8 | pair> P; 9 | P.first=2; 10 | P.second.first=3; 11 | P.second.second=4; 12 | cout< 2 | main() 3 | { 4 | for(int i=0;i<3;++i) 5 | { 6 | int c=1; 7 | for(int j=0;j<3;j++) 8 | { 9 | if(i+j>=2) 10 | { 11 | printf("%d",c); 12 | c++; 13 | } 14 | else 15 | printf(" "); 16 | } 17 | for(int k=0;k 2 | main() 3 | { 4 | for(int i=0;i<5;i++) 5 | { 6 | int a=65; 7 | for(int j=0;j<5;j++) 8 | { 9 | if(j>=i) 10 | { 11 | printf("%c",a); 12 | a++; 13 | } 14 | else 15 | printf(" "); 16 | } 17 | a=a-2; 18 | for(int k=5;k>i+1;k--) 19 | { 20 | printf("%c",a); 21 | a--; 22 | } 23 | printf("\n"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/pattern3.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int n; 5 | scanf("%d",&n); 6 | for(int i=0;i=n) 13 | printf(" "); 14 | else 15 | { 16 | printf("%c",a); 17 | a++;} 18 | } 19 | if(i==0) 20 | a=a-2; 21 | else 22 | a=a-1; 23 | int t=i; 24 | while(t!=0){printf(" ");t--;} 25 | for(int k=0;k 2 | using namespace std; 3 | 4 | 5 | 6 | 7 | int main() 8 | { 9 | int n,i,j; 10 | cin>>n; 11 | int a[n],s[n],c=0; 12 | for(i=0;i>a[i]; 14 | s[0]=1; 15 | sort(a,a+n); 16 | 17 | for(i=1;ia[i-1]) 20 | s[i]=(s[i-1]+1); 21 | else if(a[i]==a[i-1]&&(s[i-1]-1)!=0) 22 | s[i]=s[i-1]-1; 23 | else if(a[i]==a[i-1]) 24 | s[i]=s[i-1]; 25 | } 26 | for(i=0;i 2 | int i=2,j=2; 3 | void prime(int n) 4 | { 5 | int c=0; 6 | if(i 2 | main() 3 | { 4 | int c=0; 5 | char s[100]; 6 | gets(s); 7 | for(int i=0;s[i]!='\0';i++) 8 | c++; 9 | for(int i=c-1;i>=0;i--) 10 | printf("%c",s[i]); 11 | 12 | 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/roman_yr.c: -------------------------------------------------------------------------------- 1 | #include 2 | void roman(int n) 3 | { 4 | int q=n,m,d,c,l,x,v,ile,j; 5 | m=q/1000; 6 | q=q%1000; 7 | d=q/500; 8 | q=q%500; 9 | c=q/100; 10 | q=q%100; 11 | l=q/50; 12 | q=q%50; 13 | x=q/10; 14 | q=q%10; 15 | v=q/5; 16 | q=q%5; 17 | ile=q; 18 | for(j=0;j 2 | main() 3 | { 4 | int a[5],i,max=0,max2=0; 5 | for(i=0;i<5;i++) 6 | { 7 | scanf("%d",&a[i]); 8 | if(a[i]>max) 9 | max=a[i]; 10 | } 11 | 12 | for(i=0;i<5;i++) 13 | { 14 | if(a[i]!=max) 15 | { 16 | if(a[i]>max2) 17 | max2=a[i]; 18 | } 19 | } 20 | printf("%d",max2); 21 | } 22 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/shifting container.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t,n,i,j,flag; 6 | cin>>t; 7 | while(t--) 8 | { 9 | cin>>n; 10 | flag=0; 11 | int a[n][n]; 12 | for(i=0;i>a[i][j]; 15 | 16 | for(i=0;i 2 | #include 3 | main() 4 | { 5 | char *s="abcc"; 6 | char *t=strrev(s); 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/sin_series.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int i=1,p=1; 4 | float s=0; 5 | int fact(int n) 6 | { 7 | if(n>0) 8 | return n*fact(n-1); 9 | else 10 | return 1; 11 | } 12 | 13 | float series(float x,int n) 14 | { 15 | if(i<=n) 16 | { 17 | s=s+ pow(-1,i-1)*pow(x,2*i-1)/fact(2*i-1); 18 | i++; 19 | series(x,n); 20 | } 21 | return s; 22 | } 23 | main() 24 | { 25 | float x; 26 | int n; 27 | scanf("%f%d",&x,&n); 28 | printf("%f",series(x,n)); 29 | } 30 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/snackdown.c: -------------------------------------------------------------------------------- 1 | #include 2 | int prime(int n) //func to check whether a given no is prime or not 3 | { 4 | int c=0,i; 5 | for(i=2;i 2 | main() 3 | { 4 | char a[5][20]={"Rohit","Avanish","Shikhar","Shikha","Aditya"}; 5 | char t[15]; 6 | int i,j; 7 | for(int k=0;k<4;k++) 8 | for(i=0;i<4;i++) 9 | { 10 | int n=strlen(a[i]); 11 | int m=strlen(a[i+1]); 12 | for(j=0;ja[i+1][j]) 14 | { 15 | strcpy(t,a[i]); 16 | strcpy(a[i],a[i+1]); 17 | strcpy(a[i+1],t); 18 | } 19 | } 20 | for(i=0;i<5;i++) 21 | printf("%s ",a[i]); 22 | } 23 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/sort on frequency.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int a[8],i,f[8],j; 5 | for(i=0;i<8;i++) 6 | scanf("%d",&a[i]); 7 | 8 | for(i=0;i<8;i++) 9 | { 10 | int c=1; 11 | for(j=i+1;j<8;j++) 12 | if(a[i]==a[j]) 13 | { 14 | c++; 15 | f[j]=0; 16 | } 17 | if(f[i]!=0) 18 | f[i]=c; 19 | } 20 | for(i=0;i<8;i++) 21 | if(f[i]!=0) 22 | printf("Frequency of %d is %d\n",a[i],f[i]); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/split Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int a[6]={1,2,3,4,5,6}; 5 | for(int i=0;i<6/2;i++) 6 | { 7 | int t=a[i]; 8 | a[i]=a[6/2+i]; 9 | a[6/2+i]=t; 10 | } 11 | for(int i=0;i<6;i++) 12 | printf("%d",a[i]); 13 | } 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/squareinsquare.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int c=1,n,i,j,x,s=0,k; 5 | scanf("%d",&n); 6 | k=n-1; 7 | 8 | 9 | while(1) 10 | { 11 | for(i=0;i=k||j<=s||j>=k) 16 | printf("+"); 17 | else 18 | printf(" "); 19 | } 20 | printf("\n"); 21 | } 22 | printf("\n1.Continue\n0.Exit\n"); 23 | scanf("%d",&c); 24 | if(c==0) 25 | break; 26 | s++; 27 | k--; 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/string to int.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | main() 5 | { 6 | char s[100]; 7 | scanf("%s",s); 8 | int n=strlen(s),a=0; 9 | for(int i=0;i 2 | using namespace std; 3 | int main() { 4 | vector v{-4,-5},t{3}; 5 | vector ans(3); 6 | merge(v.begin(),v.end(),t.begin(),t.end(),ans.begin()); 7 | for(auto it:ans) 8 | cout< 2 | int main() 3 | { 4 | int t,r,l; 5 | scanf("%d",&t); 6 | while(t--) 7 | { 8 | scanf("%d%d",&l,&r); 9 | int s1=r*r; 10 | int s2=(l-1)*(l-1); 11 | printf("%d",s1-s2); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Hackerrank + Geeks for Geeks + DS Courseware/untitled3.c.save-failed: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int length(int a) 4 | { 5 | int q=a,c=0; 6 | while(q!=0) 7 | { 8 | q/=10; 9 | c++; 10 | } 11 | return c; 12 | } 13 | int rnum(int a,int n,int d) 14 | { 15 | int b=pow(10,d); 16 | int r=a%b; 17 | return r; 18 | } 19 | int lnum(int a,int n,int d) 20 | { 21 | int l=a/pow(10,n-d); 22 | return l; 23 | } 24 | 25 | main() 26 | { 27 | int a=2500; 28 | printf("right %d",rnum(a,4,2)); 29 | printf("left %d",lnum(a,4,2)); 30 | } 31 | -------------------------------------------------------------------------------- /Leetcode/Array/Add Binary.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string addBinary(string a, string b) 5 | { 6 | string result = ""; 7 | int apos = a.size() - 1; 8 | int bpos = b.size() - 1; 9 | int adigit, bdigit, carry = 0; 10 | 11 | while (apos >= 0 || bpos >= 0 || carry == 1) 12 | { 13 | adigit = bdigit = 0; 14 | 15 | if (apos >= 0) 16 | adigit = a[apos--] == '1'; 17 | if (bpos >= 0) 18 | bdigit = b[bpos--] == '1'; 19 | 20 | // Another way: the digit is 1 if adigit + bdigit + carry == 1 or == 3, but I noticed 21 | // XOR is more concise: 22 | result = static_cast(adigit ^ bdigit ^ carry + '0') + result; 23 | carry = (adigit + bdigit + carry) >= 2; 24 | } 25 | 26 | return result; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/Array/Container With Maximum Water.cpp: -------------------------------------------------------------------------------- 1 | /* A variation of finding rain water trapping problem, where instead of finding total water trapped we've to find the maximum amount of water b/w two end or maximum capacity of a container */ 2 | 3 | class Solution { 4 | public: 5 | int maxArea(vector& height) 6 | { 7 | int maxWater = 0; 8 | int left = 0, right = height.size()-1; 9 | 10 | while(left& nums) 10 | { 11 | int totalSum = accumulate(nums.begin(), nums.end(), 0); 12 | int leftSum = 0; 13 | 14 | for(int i=0; i false 5 | call isBadVersion(5) -> true 6 | call isBadVersion(4) -> true 7 | 8 | Then 4 is the first bad version 9 | TC: O(logN) and PSpace: O(1) 10 | */ 11 | 12 | class Solution { 13 | public: 14 | int firstBadVersion(int n) 15 | { 16 | int low =1, high = n; 17 | while(low<=high) 18 | { 19 | if(low==high) 20 | return low; 21 | int mid = low + (high-low)/2; 22 | if(isBadVersion(mid)) 23 | high = mid; 24 | else 25 | low = mid+1; 26 | } 27 | 28 | return low; 29 | } 30 | }; -------------------------------------------------------------------------------- /Leetcode/Array/Median of Two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define deb(x) cout<<#x<<" "<& nums1, vector& nums2) { 9 | 10 | vector temp(nums1.size()+nums2.size()); 11 | merge(nums1.begin(),nums1.end(),nums2.begin(),nums2.end(),temp.begin()); 12 | 13 | int n=temp.size(); 14 | if(n&1) 15 | { 16 | double ans=(double)(temp[n/2]*1.0); 17 | return ans; 18 | } 19 | else 20 | { 21 | double ans=(double)(temp[(n-1)/2]+temp[(n+1)/2])/2; 22 | return ans; 23 | } 24 | 25 | } 26 | }; 27 | 28 | main() 29 | { 30 | ios_base::sync_with_stdio(false); 31 | cin.tie(NULL); 32 | 33 | Solution obj; 34 | vector a{1,2},b{3}; 35 | cout<& nums1, int m, vector& nums2, int n) 12 | { 13 | int i = m-1; 14 | int j = n-1; 15 | 16 | int e = m+n-1; 17 | 18 | while(i>=0 && j>=0) 19 | { 20 | if(nums1[i]>nums2[j]) 21 | nums1[e--] = nums1[i--]; 22 | else 23 | nums1[e--] = nums2[j--]; 24 | } 25 | 26 | while(j>=0) 27 | nums1[e--] = nums2[j--]; 28 | 29 | //here we aren't doing for remaining nums1 bcoz it's already in final array 30 | } 31 | }; -------------------------------------------------------------------------------- /Leetcode/Array/Minimum in rotated sorted array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& nums) 4 | { 5 | int low = 0, high = nums.size()-1; 6 | 7 | while(low nums[high]) //if mid lies on first line 12 | low = mid+1; 13 | else if(nums[mid] < nums[high]) //if mid lies on second line 14 | high = mid; 15 | } 16 | 17 | return nums[low]; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode/Array/Monotonic Array.cpp: -------------------------------------------------------------------------------- 1 | /* Simple one pass solution to check monotonically increasing or decreasing array */ 2 | 3 | class Solution 4 | { 5 | public: 6 | bool isMonotonic(vector &A) { 7 | bool increasing = true; 8 | bool decreasing = true; 9 | 10 | for (int i = 0; i < A.size() - 1; ++i) { 11 | if (A[i] > A[i+1]) 12 | increasing = false; 13 | if (A[i] < A[i+1]) 14 | decreasing = false; 15 | } 16 | 17 | return increasing || decreasing; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode/Array/Plus One.cpp: -------------------------------------------------------------------------------- 1 | /* */ 2 | 3 | vector plusOne(vector& digits) 4 | { 5 | vector res; 6 | int carry = 1; 7 | for (int i = digits.size() - 1; i >= 0; i--) 8 | { 9 | int sum = carry + digits[i]; 10 | carry = sum / 10; 11 | res.push_back(sum % 10); 12 | } 13 | if (carry) res.push_back(1); 14 | reverse(res.begin(), res.end()); 15 | return res; 16 | } -------------------------------------------------------------------------------- /Leetcode/Array/Remove duplicates element.cpp: -------------------------------------------------------------------------------- 1 | /* Idea is to find count/repitition of duplicate elements in sorted array and then change the values inplace */ 2 | 3 | class Solution { 4 | public: 5 | int removeDuplicates(vector& nums) 6 | { 7 | int count = 0; 8 | for(int i=1; i& a, vector& b) 9 | { 10 | if(s.size()!=b.size()) 11 | return false; 12 | 13 | unordered_mapmymap; 14 | 15 | for(int x:a)mymap[x]++; 16 | for(int x:b)mymap[x]--; 17 | 18 | for(auto it=mymap.begin();it!=mymap.end();++it) 19 | if(it->second != 0)return false; 20 | 21 | return true; 22 | } -------------------------------------------------------------------------------- /Leetcode/Array/Sort Colors.cpp: -------------------------------------------------------------------------------- 1 | /* Sort an array of 0s, 1s, and 2s */ 2 | 3 | void sortColors(vector& nums) 4 | { 5 | int n = nums.size(); 6 | if(n==0) 7 | return; 8 | 9 | int low = 0, mid = 0, high = n-1; 10 | 11 | while(mid <= high) 12 | { 13 | if(nums[mid]==0) 14 | swap(nums[low++], nums[mid++]); 15 | else if(nums[mid]==2) 16 | swap(nums[mid], nums[high--]); 17 | else 18 | mid++; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Array/Teemo Attacking.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPoisonedDuration(vector& timeSeries, int duration) 4 | { 5 | int ans=0; 6 | if(timeSeries.size()==0) 7 | { 8 | return ans; 9 | } 10 | ans+=duration; 11 | for(int i=1;i=duration) 14 | { 15 | ans+=duration; 16 | } 17 | else 18 | { 19 | ans+=timeSeries[i]-timeSeries[i-1]; 20 | } 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /Leetcode/Array/Wiggle Sort Wave Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]... 3 | Example: nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4] 4 | 5 | Here we can clearly observe that A[even] <= A[odd] and A[odd] >= A[even]. So we can just iterate over the array, and whenever we find any of these coditions not following then simmply swap 6 | the adjacent values. 7 | 8 | */ 9 | 10 | void wiggleSort(vector &nums) 11 | { 12 | int n = nums.size(); 13 | if(n==0 || n<=1) 14 | return; 15 | 16 | for(int i=0; inums[i+1]) 21 | swap(nums[i], nums[i+1]); 22 | } 23 | else 24 | { 25 | if(nums[i]>1 would basically loose one number so we have to add one extra 1 wheras there is no such case for even number 4 | */ 5 | 6 | class Solution { 7 | public: 8 | vector countBits(int num) 9 | { 10 | vector ans(num+1, 0); 11 | for(int i=0; i<=num; i++) 12 | { 13 | ans[i] = ans[i/2] + (i&1); 14 | } 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leetcode/DP/Perfect Squares.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | leastPerfectSquare[i] = the least number of perfect square numbers which sum to i. Note that leastPerfectSquare[0] is 0. 3 | For each no. "i" it can be represented as a sum of some perfect square number + some remaining number 4 | 5 | leastPerfectSquare[i] = min(leastPerfectSquare[i], leastPerfectSquare[i-j*j]+1) 6 | We're adding +1 for the current perfect square number 'j*j' 7 | */ 8 | 9 | class Solution { 10 | public: 11 | int numSquares(int n) { 12 | 13 | vector leastPerfectSquare(n+1, INT_MAX); 14 | leastPerfectSquare[0] = 0; 15 | 16 | for(int i=1; i<=n; i++) 17 | { 18 | for(int j=1; j*j<=i; j++) 19 | { 20 | leastPerfectSquare[i] = min(leastPerfectSquare[i], leastPerfectSquare[i-j*j]+1); 21 | } 22 | } 23 | return leastPerfectSquare[n]; 24 | } 25 | }; -------------------------------------------------------------------------------- /Leetcode/Graph/Employee Importance.cpp: -------------------------------------------------------------------------------- 1 | /* Find the total importace of employee with id */ 2 | 3 | /* 4 | // Definition for Employee. 5 | class Employee { 6 | public: 7 | int id; 8 | int importance; 9 | vector subordinates; 10 | }; 11 | */ 12 | 13 | class Solution { 14 | public: 15 | int getImportance(vector employees, int id) 16 | { 17 | unordered_map m; 18 | for(auto emp: employees) 19 | m[emp->id] = emp; 20 | 21 | stack todo; 22 | todo.push(m[id]); 23 | 24 | int importance = 0; 25 | 26 | while(!todo.empty()) 27 | { 28 | auto emp = todo.top(); 29 | todo.pop();; 30 | 31 | importance += emp->importance; 32 | 33 | for(int e: emp->subordinates) 34 | todo.push(m[e]); 35 | } 36 | return importance; 37 | } 38 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/Delete node in Linked List.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Here we don't have have head* instead we've to delete the given node itself and as we know that before deleting a node we've to modify the next pointer which isn't possible 3 | in this case coz we don't have access to previous node. So simple hack is to swap the values of node and node->next pointer and delete the next node. 4 | 5 | TC & Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | void deleteNode(ListNode* node) 11 | { 12 | 13 | int temp = node->val; 14 | node->val = node->next->val; 15 | node->next->val = temp; 16 | 17 | ListNode *toBeDeleted = node->next; 18 | node->next = node->next->next; 19 | 20 | 21 | delete(toBeDeleted); 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/Merge 2 Linked List.cpp: -------------------------------------------------------------------------------- 1 | /* Merge 2 sorted linked list */ 2 | 3 | class Solution { 4 | public: 5 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) 6 | { 7 | if(!l1) 8 | return l2; 9 | if(!l2) 10 | return l1; 11 | 12 | ListNode dummy(0); 13 | ListNode *p = &dummy; 14 | 15 | while(l1 && l2) 16 | { 17 | if(l1->val < l2->val) 18 | { 19 | p->next = l1; 20 | l1 = l1->next; 21 | } 22 | else 23 | { 24 | p->next = l2; 25 | l2 = l2->next; 26 | } 27 | p = p->next; 28 | } 29 | 30 | if(l1) 31 | p->next = l1; 32 | if(l2) 33 | p->next = l2; 34 | 35 | return dummy.next; 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/Middle of Linked List.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Concept of Slow and Fast pointers to find the middle of linked lists 3 | Each time, slow go 1 steps while fast go 2 steps. When fast arrives at the end, slow will arrive right in the middle. 4 | */ 5 | 6 | class Solution { 7 | public: 8 | ListNode* middleNode(ListNode* head) 9 | { 10 | ListNode *slow = head, *fast = head; 11 | 12 | while(fast && fast->next) 13 | { 14 | slow = slow->next; 15 | fast = fast->next->next; 16 | } 17 | 18 | return slow; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leetcode/Linked List/Swapping Node in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | The learning that we can make from this ques is how to solve problems requiring 2 pointers in a single traversal by making use of the search performed so far 3 | and without starting the search from head again. 4 | 5 | Idea is to just perform a single traversal and as soon as we get first pointer, start looking for second pointer right after using fast = p1->next 6 | */ 7 | 8 | ListNode *swapNodes(ListNode *head, int k) 9 | { 10 | ListNode *p1 = head; 11 | for (int i = 1; i < k; i++) 12 | p1 = p1->next; 13 | 14 | ListNode *slow = head, *fast = p1->next; 15 | 16 | while (fast) 17 | { 18 | fast = fast->next; 19 | slow = slow->next; 20 | } 21 | 22 | swap(slow->val, p1->val); 23 | return head; 24 | } -------------------------------------------------------------------------------- /Leetcode/Misclenaous/Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Clockwise 3 | - First find the transpose of matrix 4 | - Then reverse each row 5 | */ 6 | class Solution { 7 | public: 8 | void rotate(vector>& matrix) 9 | { 10 | for(int i=0; ib+a) 8 | return true; 9 | return false; 10 | } 11 | string largestNumber(vector& nums) 12 | { 13 | string x,ans=""; 14 | int n=nums.size(); 15 | if(n==0) 16 | return ans; 17 | sort(nums.begin(),nums.begin()+n,compare); 18 | if(nums[0]==0) 19 | return "0"; 20 | for(int i=0;i> 4 is till -1. the count is used to avoid infinite loop. 4 | */ 5 | 6 | const string HEX = "0123456789abcdef"; 7 | class Solution { 8 | public: 9 | string toHex(int num) { 10 | if (num == 0) return "0"; 11 | string result; 12 | int count = 0; 13 | while (num && count++ < 8) { 14 | result = HEX[(num & 0xf)] + result; 15 | num >>= 4; 16 | } 17 | return result; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode/Strings/Valid Palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isPalindrome(string s) 5 | { 6 | int n = s.size(), l = 0, r = n - 1; 7 | while (l <= r) 8 | { 9 | while (l <= r && !isalnum(s[l])) 10 | { 11 | l++; 12 | } 13 | while (l <= r && !isalnum(s[r])) 14 | { 15 | r--; 16 | } 17 | if (l<=r && tolower(s[l++]) != tolower(s[r--])) 18 | { 19 | return false; 20 | } 21 | } 22 | return true; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode/Tree/Convert Sorted Array to BST.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Convert a sorted array to a height balanced BST, such that diff of heiht of left & right subtree should always be less than 1. 3 | TC: O(N) & Space: O(logN) 4 | */ 5 | 6 | class Solution { 7 | public: 8 | TreeNode* sortedArrayToBST(vector& nums) 9 | { 10 | return util(nums, 0, nums.size()-1); 11 | } 12 | TreeNode* util(vector &nums, int left, int right) 13 | { 14 | if(left > right) 15 | return NULL; 16 | 17 | int mid = left +(right-left)/2; 18 | TreeNode *root = new TreeNode(nums[mid]); 19 | root->left = util(nums, left, mid-1); 20 | root->right = util(nums, mid+1, right); 21 | 22 | return root; 23 | } 24 | }; -------------------------------------------------------------------------------- /Leetcode/Tree/Merge Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /* Given 2 binary trees, merge them. 2 | Merge them in a way such that if both nodes exists then add values, otherwise keep the other node 3 | 4 | TC & Space: O(M) where M is the minimum number of nodes in both the trees 5 | 6 | Note: Ask the interviewer, whether we've to create a new tree or update the earlier one. 7 | */ 8 | 9 | class Solution { 10 | public: 11 | TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) 12 | { 13 | 14 | if(t1==NULL) 15 | return t2; 16 | if(t2==NULL) 17 | return t1; 18 | 19 | t1->val += t2->val; 20 | t1->left = mergeTrees(t1->left, t2->left); 21 | t1->right = mergeTrees(t1->right, t2->right); 22 | 23 | return t1; 24 | } 25 | }; -------------------------------------------------------------------------------- /OAs/Samsung/minimum add to make pallindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPallindrome(char *str) 5 | { 6 | if(strlen(str) == 1) 7 | return true; 8 | 9 | int n = strlen(str); 10 | 11 | char *i = str, *j = str+n-1; 12 | while(j>i) 13 | { 14 | if(*j != *i) 15 | return false; 16 | i++; 17 | j--; 18 | } 19 | return true; 20 | } 21 | 22 | int minAppends(char *str) 23 | { 24 | if(isPallindrome(str)) 25 | return 0; 26 | str++; 27 | return minAppends(str)+1; 28 | } 29 | 30 | char* makePallindrome(char *str) 31 | { 32 | int n = minAppends(str); 33 | char *res = new char[n]; 34 | 35 | for(int i=n-1; i>=0; i--) 36 | res[n-i-1] = str[i]; 37 | 38 | return res; 39 | } 40 | 41 | int main() 42 | { 43 | char s[]="abcdc"; 44 | cout< 2 | using namespace std; 3 | 4 | 5 | 6 | 7 | 8 | 9 | int main() 10 | { 11 | 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /OAs/Stripe/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | action -------------------------------------------------------------------------------- /OAs/Waart/splitgroup.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | 5 | ll power(ll x, ll y, ll p) 6 | { 7 | ll res = 1; 8 | x = x % p; 9 | 10 | while (y > 0) 11 | { 12 | if (y & 1) 13 | res = (res * x) % p; 14 | y = y >> 1; 15 | x = (x * x) % p; 16 | } 17 | return res; 18 | } 19 | int main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int t; 24 | 25 | cin >> t; 26 | while (t--) 27 | { 28 | ll n, mod = 1e9 + 7; 29 | cin >> n; 30 | cout << (power(2, n, 1e9 + 7) - 2 + mod)%mod << endl; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /SPOJ/CRZYSMKR.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* Note: (34^n)%11 always yields 1*/ 3 | 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define deb(x) cout<<#x<<" "<>t; 16 | while(t--) 17 | { 18 | cin>>n; 19 | ll temp=(30*n)%11; 20 | if(temp==0) 21 | cout<<"0\n"; 22 | else 23 | cout<<11-temp<<"\n"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /SPOJ/SPOJ_LIST/DOL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | for(int i=0;i>n; 23 | while(n%2==0) 24 | { 25 | n/=2; 26 | } 27 | cout<<"Case "<<(i+1)<<": "< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll x,a; 22 | cin>>x>>a; 23 | ll n=a-x; 24 | ll sum=n*(a+1); 25 | sum=sum-n*(n-1)/2; 26 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n;cin>>n; 22 | 23 | if(n%2) 24 | cout<<0<<"\n"; 25 | else 26 | cout<<1<<"\n"; 27 | 28 | } 29 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /SPOJ/SPOJ_LIST/ec_conb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long int 3 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 4 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 5 | #define MOD 1000000007 6 | #define pb push_back 7 | #define fo(i,n) for(ll i=0;i>t; 16 | while(t--) 17 | { 18 | tem = 0;; 19 | cin >> x; 20 | if (x % 2 != 0) 21 | { 22 | cout << x << endl; 23 | continue; 24 | } 25 | 26 | while (x != 0) 27 | { 28 | tem = tem << 1; 29 | tem = tem + (x & 1); 30 | x = x >> 1; 31 | 32 | } 33 | cout << tem << endl; 34 | } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /SPOJ/SPOJ_LIST/fctrl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n,ans=0; 22 | cin>>n; 23 | for(ll i=5;n/i>=1;i*=5) 24 | { 25 | ans+=n/i; 26 | } 27 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | 22 | } 23 | 24 | 25 | return 0; 26 | } 27 | 28 | 29 | // TO DO -------------------------------------------------------------------------------- /SPOJ/SPOJ_LIST/ieeeeebgam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | int n; 22 | cin>>n; 23 | 24 | double m=n; 25 | double ans=m/(m+1); 26 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define mod 1000000007 5 | #define mp make_pair 6 | #define pb push_back 7 | #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); 8 | 9 | int main() 10 | { 11 | 12 | // IOS; 13 | int t,i; 14 | cin>>t; 15 | while (t--) 16 | { 17 | 18 | } 19 | 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /library/all stl functions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sastava007/Tech-Interview-Preparation/31e54258b6caddcee4f594da59341761bfd08964/library/all stl functions.pdf --------------------------------------------------------------------------------