├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── assets ├── cpp.png ├── java.png └── python.png ├── cpp ├── armyGame.cpp ├── bdayGift.cpp ├── bestDivisor.cpp ├── busStation.cpp ├── connectingTowns.cpp ├── diwaliLights.cpp ├── evenOddQuery.cpp ├── fillingJars.cpp ├── findThePoint.cpp ├── halloweenParty.cpp ├── handshake.cpp ├── isFibo.cpp ├── matrixTracing.cpp ├── maximumDraws.cpp ├── minHeightTriangle.cpp ├── mostDistant.cpp ├── mutualRecurrences.cpp ├── points_on_a_line.cpp ├── possiblePath.cpp ├── pythagoreanTriplet.cpp ├── restaurant.cpp ├── reverseGame.cpp ├── specialMultiple.cpp └── verticalSticks.cpp ├── java └── src │ ├── ArmyGame.java │ ├── BestDivisor.java │ ├── ConnectingTowns.java │ ├── CuttingPaperSquares.java │ ├── EasySum.java │ ├── FillingJars.java │ ├── FindThePoint.java │ ├── HalloweenParty.java │ ├── Handshake.java │ ├── JimAndTheJokes.java │ ├── LeonardosPrimeFactors.java │ ├── MaximumDraws.java │ ├── MinimumHeightTriangle.java │ ├── MostDistant.java │ ├── PossiblePath.java │ ├── Restaurant.java │ ├── ReverseGame.java │ ├── SherlockAndDivisors.java │ ├── SherlockAndMovingTiles.java │ ├── StrangeGridAgain.java │ └── SumarAndTheFloatingRocks.java ├── programming-language-logos.md └── python └── probability └── random_number_generator.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | venv 3 | out 4 | dist 5 | *.iml 6 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | This project aims to create a common source from all Mathematics questions on HackerRank and values every single 4 | contribution made by the community. 5 | 6 | We aim to solve all questions in the HackerRank Mathematics domain in as many languages as possible to show the 7 | different code styles and approaches that can be used to solve the same problem. 8 | 9 | Contributing is very simple, simply identify a question from the [README](README.md) file that has yet to be solved in 10 | a programming language of your choice. 11 | 12 | Solve that question and make sure that all tests pass on HackerRank and then create a pull request with the 13 | added solution and updated readme. 14 | 15 | ## Pull Request Process 16 | 17 | 1. Fork this repository to your github account and then clone your forked repository to your machine. 18 | 2. Say you solve some problem called __cars-and-trucks__ in using your favorite programming language, 19 | say Python then simple add a new file called 20 | `cars-and-trucks.py` in the `python` directory. 21 | 3. In the [README](README.md) file, do not make any modification. The README file will be modified 22 | by __@anishLearnsToCode__. 23 | 24 | 25 | ## Git Workflow 26 | 1. Fork this repo 27 | 28 | 2. Add this repo as the remote: 29 | ````bash 30 | git remote add anish https://github.com/anishLearnsToCode/hackerrank-math.git 31 | git remote 32 | ```` 33 | 34 | 3. Always pull main from this repo and program in new branches: 35 | ````bash 36 | git checkout main 37 | git pull anish main 38 | git push origin main 39 | git checkout -b new-feature 40 | ```` 41 | 42 | 4. Push your new branch onto your repo and create a Pull Request from GitHub 43 | ````bash 44 | git push -u origin new-feature 45 | ```` 46 | and create a pull request 47 | 48 | 5. Update __main__ branch once the PR is approved and rebase any other feature branches on top of it. 49 | ````bash 50 | git checkout main 51 | git pull anish main 52 | git checkout feature-2 53 | git rebase main 54 | ```` 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2021 anishLearnsToCode 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 4 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 5 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 6 | persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 9 | Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 12 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 13 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 14 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank Mathematics 2 | 3 | ![problems-solved](https://img.shields.io/badge/Problems%20Solved-27/284-1f425f.svg) 4 | ![problems-solved-java](https://img.shields.io/badge/Java-9/284-008000.svg) 5 | ![problems-solved-python](https://img.shields.io/badge/Python-1/284-008000.svg) 6 | ![problems-solved-cpp](https://img.shields.io/badge/C++-25/284-008000.svg) 7 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md) 8 | [![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming) 9 | [![profile](https://img.shields.io/badge/also%20see-My%20Hackerrank%20Profile-1f72ff.svg)](https://www.hackerrank.com/anishviewer) 10 | 11 | ## Problems 12 | 13 | ### Fundamentals ![problems-solved](https://img.shields.io/badge/Solved-23/32-00ffff.svg) 14 | | Name | Difficulty | Solution | 15 | |-----------------------------------------------------------------------------------------------------------|:----------:|:-------------------------------------------------------------------------------------------------------------------:| 16 | | [Find The Point](https://www.hackerrank.com/challenges/find-point/problem) | Easy | [![Java](assets/java.png)](java/src/FindThePoint.java) [![cpp](assets/cpp.png)](cpp/findThePoint.cpp) | 17 | | [Maximum Draws](https://www.hackerrank.com/challenges/maximum-draws) | Easy | [![Java](assets/java.png)](java/src/MaximumDraws.java) [![cpp](assets/cpp.png)](cpp/maximumDraws.cpp) | 18 | | [Handshake](https://www.hackerrank.com/challenges/handshake) | Easy | [![Java](assets/java.png)](java/src/Handshake.java) [![cpp](assets/cpp.png)](cpp/handshake.cpp) | 19 | | [Minimum Height Triangle](https://www.hackerrank.com/challenges/lowest-triangle) | Easy | [![Java](assets/java.png)](java/src/MinimumHeightTriangle.java) [![cpp](assets/cpp.png)](cpp/minHeightTriangle.cpp) | 20 | | [Army Game](https://www.hackerrank.com/challenges/game-with-cells) | Easy | [![Java](assets/java.png)](java/src/ArmyGame.java) [![cpp](assets/cpp.png)](cpp/armyGame.cpp) | 21 | | [Leonardo's Prime Factors](https://www.hackerrank.com/challenges/leonardo-and-prime) | Easy | [![Java](assets/java.png)](java/src/LeonardosPrimeFactors.java) | 22 | | [Connecting Towns](https://www.hackerrank.com/challenges/connecting-towns) | Easy | [![Java](assets/java.png)](java/src/ConnectingTowns.java) [![cpp](assets/cpp.png)](cpp/connectingTowns.cpp) | 23 | | [Cutting Paper Squares](https://www.hackerrank.com/challenges/p1-paper-cutting) | Easy | [![Java](assets/java.png)](java/src/CuttingPaperSquares.java) | 24 | | [Sherlock and Moving Tiles](https://www.hackerrank.com/challenges/sherlock-and-moving-tiles) | Easy | [![Java](assets/java.png)](java/src/SherlockAndMovingTiles.java) | 25 | | [Best Divisor](https://www.hackerrank.com/challenges/best-divisor) | Easy | [![Java](assets/java.png)](java/src/BestDivisor.java) [![cpp](assets/cpp.png)](cpp/bestDivisor.cpp) | 26 | | [Restaurant](https://www.hackerrank.com/challenges/restaurant) | Easy | [![Java](assets/java.png)](java/src/Restaurant.java) [![cpp](assets/cpp.png)](cpp/restaurant.cpp) | 27 | | [Reverse Game](https://www.hackerrank.com/challenges/reverse-game) | Easy | [![Java](assets/java.png)](java/src/ReverseGame.java) [![cpp](assets/cpp.png)](cpp/reverseGame.cpp) | 28 | | [Strange Grid Again](https://www.hackerrank.com/challenges/strange-grid) | Easy | [![Java](assets/java.png)](java/src/StrangeGridAgain.java) | 29 | | [Sherlock and Divisor](https://www.hackerrank.com/challenges/sherlock-and-divisors) | Easy | [![Java](assets/java.png)](java/src/SherlockAndDivisors.java) | 30 | | [Halloween Party](https://www.hackerrank.com/challenges/halloween-party) | Easy | [![Java](assets/java.png)](java/src/HalloweenParty.java) [![cpp](assets/cpp.png)](cpp/halloweenParty.cpp) | 31 | | [Filling Jars](https://www.hackerrank.com/challenges/filling-jars) | Easy | [![Java](assets/java.png)](java/src/FillingJars.java) [![cpp](assets/cpp.png)](cpp/fillingJars.cpp) | 32 | | [Sumar and The Floating Rocks](https://www.hackerrank.com/challenges/harry-potter-and-the-floating-rocks) | Easy | [![Java](assets/java.png)](java/src/SumarAndTheFloatingRocks.java) | 33 | | [Russian Peasant Exponent](https://www.hackerrank.com/challenges/russian-peasant-exponentiation) | Easy | | 34 | | [Most Distant](https://www.hackerrank.com/challenges/most-distant) | Easy | [![Java](assets/java.png)](java/src/MostDistant.java) [![cpp](assets/cpp.png)](cpp/mostDistant.cpp) | 35 | | [Possible Path](https://www.hackerrank.com/challenges/possible-path) | Easy | [![Java](assets/java.png)](java/src/PossiblePath.java) [![cpp](assets/cpp.png)](cpp/possiblePath.cpp) | 36 | | [Jim and The Jokes](https://www.hackerrank.com/challenges/jim-and-the-jokes) | Medium | | 37 | | [Bus Station](https://www.hackerrank.com/challenges/bus-station) | Medium | [![cpp](assets/cpp.png)](cpp/busStation.cpp) | 38 | | [Is Fibo](https://www.hackerrank.com/challenges/is-fibo) | Medium | [![cpp](assets/cpp.png)](cpp/isFibo.cpp) | 39 | | [K Candy Score](https://www.hackerrank.com/challenges/k-candy-store) | Medium | | 40 | | [Diwali Lights](https://www.hackerrank.com/challenges/diwali-lights) | Medium | [![cpp](assets/cpp.png)](cpp/diwaliLights.cpp) | 41 | | [Mutual Recurrences](https://www.hackerrank.com/challenges/mutual-recurrences) | Medium | [![cpp](assets/cpp.png)](cpp/mutualRecurrences.cpp) | 42 | | [Summing The N Series](https://www.hackerrank.com/challenges/summing-the-n-series) | Medium | | 43 | | [Special Multiple](https://www.hackerrank.com/challenges/special-multiple) | Medium | [![cpp](assets/cpp.png)](cpp/specialMultiple.cpp) | 44 | | [Die Hard 3](https://www.hackerrank.com/challenges/die-hard-3) | Medium | | 45 | | [Even Odd Query](https://www.hackerrank.com/challenges/even-odd-query) | Hard | [![cpp](assets/cpp.png)](cpp/evenOddQuery.cpp) | 46 | | [Sherlock and Permutation](https://www.hackerrank.com/challenges/sherlock-and-permutations) | Hard | | 47 | | [Matrix Tracing](https://www.hackerrank.com/challenges/matrix-tracing) | Hard | [![cpp](assets/cpp.png)](cpp/matrixTracing.cpp) | 48 | 49 | 50 | ### Number Theory ![problems-solved](https://img.shields.io/badge/Solved-0/77-00ffff.svg) 51 | 52 | | Name | Difficulty | Solution | 53 | |------------------------------------------------------------------------------------------------------|:----------:|:--------:| 54 | | [Constructing a Number](https://www.hackerrank.com/challenges/constructing-a-number) | Easy | | 55 | | [Sherlock and GCD](https://www.hackerrank.com/challenges/sherlock-and-gcd) | Easy | | 56 | | [Primitive Problem](https://www.hackerrank.com/challenges/primitive-problem) | Easy | | 57 | | [Identify Smith Numbers](https://www.hackerrank.com/challenges/identify-smith-numbers) | Easy | | 58 | | [Euler's Criterion](https://www.hackerrank.com/challenges/eulers-criterion) | Easy | | 59 | | [Fibonacci Finding (Easy)](https://www.hackerrank.com/challenges/fibonacci-finding-easy) | Easy | | 60 | | [Cheese and Random Toppings](https://www.hackerrank.com/challenges/cheese-and-random-toppings) | Easy | | 61 | | [John and GCD List](https://www.hackerrank.com/challenges/john-and-gcd-list) | Easy | | 62 | | [Closest Number](https://www.hackerrank.com/challenges/closest-number) | Medium | | 63 | | [Twins](https://www.hackerrank.com/challenges/twins) | Medium | | 64 | | [The Chosen One](https://www.hackerrank.com/challenges/the-chosen-one) | Medium | | 65 | | [Power of Large Numbers](https://www.hackerrank.com/challenges/power-of-large-numbers) | Medium | | 66 | | [Salary Blues](https://www.hackerrank.com/challenges/salary-blues) | Medium | | 67 | | [Number of Zero-xor subsets](https://www.hackerrank.com/challenges/number-of-subsets) | Medium | | 68 | | [Breaking Sticks](https://www.hackerrank.com/challenges/breaking-sticks) | Medium | | 69 | | [Easy GCD](https://www.hackerrank.com/challenges/easy-gcd-1) | Medium | | 70 | | [Manasa Loves Maths](https://www.hackerrank.com/challenges/manasa-loves-maths) | Medium | | 71 | | [Easy Maths](https://www.hackerrank.com/challenges/easy-math) | Medium | | 72 | | [Equations](https://www.hackerrank.com/challenges/equations) | Medium | | 73 | | [Binomial Coefficients](https://www.hackerrank.com/challenges/binomial-coefficients) | Medium | | 74 | | [Lucy and Flowers](https://www.hackerrank.com/challenges/lucy-and-flowers) | Medium | | 75 | | [Divisibility of Power](https://www.hackerrank.com/challenges/divisibility-of-power) | Medium | | 76 | | [A Weird Function](https://www.hackerrank.com/challenges/a-weird-function) | Medium | | 77 | | [Coprime Conundrum](https://www.hackerrank.com/challenges/arthur-and-coprimes) | Medium | | 78 | | [Megaprime Numbers](https://www.hackerrank.com/challenges/megaprime-numbers) | Medium | | 79 | | [Ants](https://www.hackerrank.com/challenges/ants) | Medium | | 80 | | [Ichigo and Cubes](https://www.hackerrank.com/challenges/ichigo-and-cubes) | Medium | | 81 | | [Prime Sum](https://www.hackerrank.com/challenges/prime-sum) | Medium | | 82 | | [Hyperrectangle GCD](https://www.hackerrank.com/challenges/hyperrectangle-gcd) | Medium | | 83 | | [Ichigo and Rooms](https://www.hackerrank.com/challenges/ichigo-and-rooms) | Medium | | 84 | | [Ajob Sequence](https://www.hackerrank.com/challenges/ajob-subsequence) | Medium | | 85 | | [Eugene and Big Number](https://www.hackerrank.com/challenges/eugene-and-big-number) | Medium | | 86 | | [Matrix Tree](https://www.hackerrank.com/challenges/matrix-tree) | Medium | | 87 | | [Nice Clique](https://www.hackerrank.com/challenges/niceclique) | Medium | | 88 | | [Down The Rabbit Hole](https://www.hackerrank.com/challenges/down-the-rabbit-hole) | Medium | | 89 | | [Little Panda Power](https://www.hackerrank.com/challenges/littlepandapower) | Hard | | 90 | | [Mehta and His Laziness](https://www.hackerrank.com/challenges/mehta-and-his-laziness) | Hard | | 91 | | [Minimal Distance to Pi](https://www.hackerrank.com/challenges/minimal-distance-to-pi) | Hard | | 92 | | [Help Mike](https://www.hackerrank.com/challenges/help-mike) | Hard | | 93 | | [Dancing in Pairs](https://www.hackerrank.com/challenges/dance-class) | Hard | | 94 | | [Akhil and GF](https://www.hackerrank.com/challenges/akhil-and-gf) | Hard | | 95 | | [Little Ashish's Huge Donation](https://www.hackerrank.com/challenges/little-chammys-huge-donation) | Hard | | 96 | | [Manasa and Factorials](https://www.hackerrank.com/challenges/manasa-and-factorials) | Hard | | 97 | | [Satisfactory Pair](https://www.hackerrank.com/challenges/pairs-again) | Hard | | 98 | | [Largest Non Co-Prime SubMatrix](https://www.hackerrank.com/challenges/largest-coprime-submatrix) | Hard | | 99 | | [Divisor Exploration II](https://www.hackerrank.com/challenges/divisor-exploration-2) | Hard | | 100 | | [Strange Numbers](https://www.hackerrank.com/challenges/strange-numbers) | Hard | | 101 | | [Divisor Exploration 3](https://www.hackerrank.com/challenges/divisor-exploration-3) | Hard | | 102 | | [Superpowers of 2](https://www.hackerrank.com/challenges/superpowers) | Hard | | 103 | | [Sherlock Puzzle](https://www.hackerrank.com/challenges/sherlock-puzzle) | Hard | | 104 | | [Devu Vs Police](https://www.hackerrank.com/challenges/devu-police) | Hard | | 105 | | [Long Permutation](https://www.hackerrank.com/challenges/long-permutation) | Hard | | 106 | | [Divisor Exploration](https://www.hackerrank.com/challenges/divisor-exploration) | Hard | | 107 | | [Scalar Products](https://www.hackerrank.com/challenges/scalar-products) | Hard | | 108 | | [Number of M-Coprime Arrays](https://www.hackerrank.com/challenges/number-of-m-coprime-arrays) | Hard | | 109 | | [nCr](https://www.hackerrank.com/challenges/ncr) | Expert | | 110 | | [Chandrima and XOR](https://www.hackerrank.com/challenges/chandrima-and-xor) | Hard | | 111 | | [Find The Operations](https://www.hackerrank.com/challenges/flip) | Hard | | 112 | | [Fibonacci GCD](https://www.hackerrank.com/challenges/fibonacci-gcd) | Hard | | 113 | | [A Very Special Multiple](https://www.hackerrank.com/challenges/a-very-special-multiple) | Hard | | 114 | | [Fibonacci LCM](https://www.hackerrank.com/challenges/fibonacci-lcm) | Hard | | 115 | | [Unfriendly Numbers](https://www.hackerrank.com/challenges/unfriendly-numbers) | Hard | | 116 | | [HackerRank Number](https://www.hackerrank.com/challenges/hackerrank-number) | Expert | | 117 | | [GCD Product](https://www.hackerrank.com/challenges/gcd-product) | Hard | | 118 | | [The Bouncing Flying Ball](https://www.hackerrank.com/challenges/the-bouncing-flying-ball) | Hard | | 119 | | [Period](https://www.hackerrank.com/challenges/period) | Hard | | 120 | | [Order of Prime Factorial](https://www.hackerrank.com/challenges/order-of-prime-in-factorial) | Hard | | 121 | | [Degree of an Algebraic Number](https://www.hackerrank.com/challenges/degree-of-an-algebraic-number) | Advanced | | 122 | | [Computer Virus](https://www.hackerrank.com/challenges/demidenko-computer-virus) | Advanced | | 123 | | [GCD Sequence](https://www.hackerrank.com/challenges/gcd-sequence) | Hard | | 124 | | [Coprime Power Sum](https://www.hackerrank.com/challenges/coprime-power-sum) | Advanced | | 125 | | [Fun With 1010](https://www.hackerrank.com/challenges/fun-with-1010) | Expert | | 126 | | [GCD Mocktail](https://www.hackerrank.com/challenges/gcd-mocktail) | Advanced | | 127 | | [Laser Beam](https://www.hackerrank.com/challenges/laser-beam) | Expert | | 128 | | [Minion of The Year](https://www.hackerrank.com/challenges/minion-of-the-year) | Expert | | 129 | | [Bear and Cryptography](https://www.hackerrank.com/challenges/bear-and-cryptography) | Advanced | | 130 | 131 | ### Combinatorics ![problems-solved](https://img.shields.io/badge/Solved-0/57-00ffff.svg) 132 | 133 | | Name | Difficulty | Solution | 134 | |----------------------------------------------------------------------------------------------------------------------|:----------:|:--------:| 135 | | [A Chocolate Fiesta](https://www.hackerrank.com/challenges/a-chocolate-fiesta) | Easy | | 136 | | [Picking Cards](https://www.hackerrank.com/challenges/picking-cards) | Easy | | 137 | | [nCr Table](https://www.hackerrank.com/challenges/ncr-table) | Medium | | 138 | | [Coinage](https://www.hackerrank.com/challenges/coinage) | Medium | | 139 | | [Building a List](https://www.hackerrank.com/challenges/building-a-list) | Medium | | 140 | | [Merge List](https://www.hackerrank.com/challenges/merge-list) | Medium | | 141 | | [Choose and Calculate](https://www.hackerrank.com/challenges/choose-and-calculate) | Medium | | 142 | | [Sherlock and Pairs](https://www.hackerrank.com/challenges/sherlock-and-pairs) | Medium | | 143 | | [Consecutive Subsequences](https://www.hackerrank.com/challenges/consecutive-subsequences) | Medium | | 144 | | [Anti-Palindromic Strings](https://www.hackerrank.com/challenges/antipalindromic-strings) | Medium | | 145 | | [Lexicographic Paths](https://www.hackerrank.com/challenges/lexicographic-steps) | Medium | | 146 | | [Number List](https://www.hackerrank.com/challenges/number-list) | Medium | | 147 | | [Journey To Mars](https://www.hackerrank.com/challenges/ajourney) | Medium | | 148 | | [Super Humble Matrix](https://www.hackerrank.com/challenges/super-humble-matrix) | Medium | | 149 | | [Volleyball Match](https://www.hackerrank.com/challenges/volleyball-match) | Medium | | 150 | | [Cyclic Quadruples](https://www.hackerrank.com/challenges/cyclicquadruples) | Medium | | 151 | | [Longest Increasing Subsequence Arrays](https://www.hackerrank.com/challenges/longest-increasing-subsequence-arrays) | Medium | | 152 | | [Digit Products](https://www.hackerrank.com/challenges/digit-products) | Medium | | 153 | | [Tower 3-coloring](https://www.hackerrank.com/challenges/tower-3-coloring) | Medium | | 154 | | [Count Fox Sequences](https://www.hackerrank.com/challenges/count-fox-sequences) | Medium | | 155 | | [Manasa and Combinatorics](https://www.hackerrank.com/challenges/manasa-and-combinatorics) | Medium | | 156 | | [Coloring Grid](https://www.hackerrank.com/challenges/coloring-grid) | Medium | | 157 | | [Div and Span](https://www.hackerrank.com/challenges/div-and-span) | Medium | | 158 | | [Ichigo and Revenge](https://www.hackerrank.com/challenges/ichigo-and-revenge) | Medium | | 159 | | [Binomial Coefficients Revenge](https://www.hackerrank.com/challenges/bincoefrevenge) | Medium | | 160 | | [Circles Math](https://www.hackerrank.com/challenges/circles-math) | Medium | | 161 | | [Costly Graphs](https://www.hackerrank.com/challenges/costly-graphs) | Medium | | 162 | | [Bridges and Harbors](https://www.hackerrank.com/challenges/bridges-and-harbors) | Medium | | 163 | | [Mehta and the Typical Supermarket](https://www.hackerrank.com/challenges/mehta-and-the-typical-supermarket) | Hard | | 164 | | [Game of Thrones - II](https://www.hackerrank.com/challenges/game-of-throne-ii) | Hard | | 165 | | [Permutation Problem](https://www.hackerrank.com/challenges/permutation-problem) | Hard | | 166 | | [Towers](https://www.hackerrank.com/challenges/towers) | Hard | | 167 | | [Introduction to Representation Theory](https://www.hackerrank.com/challenges/introduction-to-representation-theory) | Hard | | 168 | | [Tile Painting: Revisited!](https://www.hackerrank.com/challenges/tile-painting-revisited) | Hard | | 169 | | [Beautiful Sets](https://www.hackerrank.com/challenges/polita-sets) | Hard | | 170 | | [Alien Flowers](https://www.hackerrank.com/challenges/alien-flowers) | Hard | | 171 | | [Grid Lines](https://www.hackerrank.com/challenges/grid-lines) | Hard | | 172 | | [Highway Construction](https://www.hackerrank.com/challenges/highway-construction) | Hard | | 173 | | [Value of all Permutations](https://www.hackerrank.com/challenges/value-of-all-permutations) | Hard | | 174 | | [Farmer](https://www.hackerrank.com/challenges/demidenko-farmer) | Advanced | | 175 | | [Irresponsible Numbers](https://www.hackerrank.com/challenges/irresponsible-numbers) | Hard | | 176 | | [Academy Surveillance](https://www.hackerrank.com/challenges/surveillance) | Advanced | | 177 | | [Parity Party](https://www.hackerrank.com/challenges/parity-party) | Expert | | 178 | | [String Modification](https://www.hackerrank.com/challenges/string-modification) | Expert | | 179 | | [To Heap or Not to Heap](https://www.hackerrank.com/challenges/to-heap-or-not-to-heap) | Advanced | | 180 | | [Sasha and Swaps](https://www.hackerrank.com/challenges/sasha-and-swaps) | Hard | | 181 | | [k-Balance Number](https://www.hackerrank.com/challenges/k-balance) | Hard | | 182 | | [Powercode](https://www.hackerrank.com/challenges/powercode) | Hard | | 183 | | [Insane DFS](https://www.hackerrank.com/challenges/insane-dfs) | Expert | | 184 | | [How Many Trees?](https://www.hackerrank.com/challenges/how-many-trees) | Advanced | | 185 | | [Strongly Connected Digraphs](https://www.hackerrank.com/challenges/strongly-connected-digraphs) | Hard | | 186 | | [Robanukkah Tree](https://www.hackerrank.com/challenges/r-tree-decoration) | Hard | | 187 | | [Teleporters](https://www.hackerrank.com/challenges/teleporters) | Expert | | 188 | | [Count Palindromes](https://www.hackerrank.com/challenges/count-palindromes) | Hard | | 189 | | [Cycle Representation](https://www.hackerrank.com/challenges/cycle-representation) | Expert | | 190 | | [To Infinity and Beyond](https://www.hackerrank.com/challenges/to-infinity-and-beyond) | Expert | | 191 | | [Byteland Itinerary](https://www.hackerrank.com/challenges/byteland-itinerary) | Advanced | | 192 | 193 | ### Algebra ![problems-solved](https://img.shields.io/badge/Solved-1/43-00ffff.svg) 194 | 195 | | Name | Difficulty | Solution | 196 | |--------------------------------------------------------------------------------------------------|:----------:|:----------------------------------------------------:| 197 | | [Combo Meals](https://www.hackerrank.com/challenges/combo-meal) | Easy | | 198 | | [Difference and Product](https://www.hackerrank.com/challenges/difference-and-product) | Easy | | 199 | | [Pythagorean Triplet](https://www.hackerrank.com/challenges/pythagorean-triple) | Easy | [![cpp](assets/cpp.png)](cpp/pythagoreanTriplet.cpp) | 200 | | [Number Groups](https://www.hackerrank.com/challenges/number-groups) | Easy | | 201 | | [Wet Shark and 42](https://www.hackerrank.com/challenges/wet-shark-and-42) | Easy | | 202 | | [Simple One](https://www.hackerrank.com/challenges/simple-one) | Easy | | 203 | | [Stepping Stones Game](https://www.hackerrank.com/challenges/stepping-stones-game) | Medium | | 204 | | [Shashank and List](https://www.hackerrank.com/challenges/shashank-and-list) | Medium | | 205 | | [Triangle Numbers](https://www.hackerrank.com/challenges/triangle-numbers) | Medium | | 206 | | [Little Gaurav and Sequences](https://www.hackerrank.com/challenges/little-gaurav-and-sequence) | Medium | | 207 | | [Tell the Average](https://www.hackerrank.com/challenges/tell-the-average) | Medium | | 208 | | [Game of Rotation](https://www.hackerrank.com/challenges/game-of-rotation) | Medium | | 209 | | [Hackonacci Matrix Rotations](https://www.hackerrank.com/challenges/hackonacci-matrix-rotations) | Medium | | 210 | | [Birthday Triplets](https://www.hackerrank.com/challenges/the-triplets) | Medium | | 211 | | [Manasa and Subsequences](https://www.hackerrank.com/challenges/manasa-and-sub-sequences) | Medium | | 212 | | [Count Solutions](https://www.hackerrank.com/challenges/count-solutions) | Medium | | 213 | | [Manasa and Calculations](https://www.hackerrank.com/challenges/manasa-and-calculations) | Medium | | 214 | | [The Simplest Sum](https://www.hackerrank.com/challenges/the-simplest-sum) | Medium | | 215 | | [Manasa and Pizza](https://www.hackerrank.com/challenges/manasa-and-pizza) | Medium | | 216 | | [Permutation Equations](https://www.hackerrank.com/challenges/permutation-equations) | Medium | | 217 | | [Introduction To Algebra](https://www.hackerrank.com/challenges/introduction-to-algebra) | Medium | | 218 | | [Cross Matrix](https://www.hackerrank.com/challenges/cross-matrix) | Medium | | 219 | | [Easy Sum](https://www.hackerrank.com/challenges/easy-sum) | Hard | | 220 | | [Pairwise Sum and Divide](https://www.hackerrank.com/challenges/pairwise-sum-and-divide) | Hard | | 221 | | [Power Calculation](https://www.hackerrank.com/challenges/power-calculation) | Advanced | | 222 | | [Sherlock and Square](https://www.hackerrank.com/challenges/sherlock-and-square) | Hard | | 223 | | [Sherlock and Queries](https://www.hackerrank.com/challenges/sherlock-and-queries) | Advanced | | 224 | | [Summing the K-N Series](https://www.hackerrank.com/challenges/summing-the-k-n-series) | Hard | | 225 | | [XOR Love](https://www.hackerrank.com/challenges/xor-love) | Hard | | 226 | | [Circle Summation](https://www.hackerrank.com/challenges/circle-summation) | Advanced | | 227 | | [Girlfriend and Necklace](https://www.hackerrank.com/challenges/gneck) | Hard | | 228 | | [Signal Tower](https://www.hackerrank.com/challenges/signal-tower) | Advanced | | 229 | | [Summing the K-N-R Series](https://www.hackerrank.com/challenges/summing-the-k-n-r-series) | Advanced | | 230 | | [The Axis of Awesome](https://www.hackerrank.com/challenges/the-axis-of-awesome) | Expert | | 231 | | [Emma and Sum of Products](https://www.hackerrank.com/challenges/emma-and-sum-of-products) | Hard | | 232 | | [Fun with Series](https://www.hackerrank.com/challenges/fun-with-series) | Advanced | | 233 | | [Counting Equations](https://www.hackerrank.com/challenges/counting-equations) | Expert | | 234 | | [Calculate It](https://www.hackerrank.com/challenges/calculate-it) | Expert | | 235 | | [Rational Sums](https://www.hackerrank.com/challenges/rational-sums) | Expert | | 236 | | [Introduction To Algebra 2](https://www.hackerrank.com/challenges/introduction-to-algebra-2) | Advanced | | 237 | | [The Black Box](https://www.hackerrank.com/challenges/black-box-1) | Advanced | | 238 | | [Black Hole](https://www.hackerrank.com/challenges/demidenko-black-hole) | Hard | | 239 | | [Minimum Cyclic Shift](https://www.hackerrank.com/challenges/minimal-cyclic-shift) | Expert | | 240 | 241 | ### Geometry ![problems-solved](https://img.shields.io/badge/Solved-1/30-00ffff.svg) 242 | 243 | | Name | Difficulty | Solution | 244 | |--------------------------------------------------------------------------------------|:----------:|:--------------------------------------------------:| 245 | | [Points On A Line](https://www.hackerrank.com/challenges/points-on-a-line) | Easy | [![cpp](assets/cpp.png)](cpp/points_on_a_line.cpp) | 246 | | [Rectangular Game](https://www.hackerrank.com/challenges/rectangular-game) | Easy | | 247 | | [Sherlock and Counting](https://www.hackerrank.com/challenges/sherlock-and-counting) | Easy | | 248 | | [Sherlock and Planes](https://www.hackerrank.com/challenges/sherlock-and-planes) | Easy | | 249 | | [Xrange's Pancakes](https://www.hackerrank.com/challenges/xrange-and-pizza) | Easy | | 250 | | [Points on a Rectangle](https://www.hackerrank.com/challenges/points-on-rectangle) | Easy | | 251 | | [Circle City](https://www.hackerrank.com/challenges/circle-city) | Medium | | 252 | | [Spheres](https://www.hackerrank.com/challenges/spheres) | Medium | | 253 | | [Baby Step, Giant Step](https://www.hackerrank.com/challenges/baby-step-giant-step) | Medium | | 254 | | [A Circle and Square](https://www.hackerrank.com/challenges/a-circle-and-a-square) | Medium | | 255 | | [Hyperspace Travel](https://www.hackerrank.com/challenges/hyperspace-travel) | Medium | | 256 | | [Solve Equations](https://www.hackerrank.com/challenges/solve-equations) | Medium | | 257 | | [Stars](https://www.hackerrank.com/challenges/stars) | Medium | | 258 | | [Polar Angels](https://www.hackerrank.com/challenges/polar-angles) | Medium | | 259 | | [Jim and The Challenge](https://www.hackerrank.com/challenges/jim-and-the-challenge) | Medium | | 260 | | [Jim Beam](https://www.hackerrank.com/challenges/jim-beam) | Hard | | 261 | | [Sherlock and Geometry](https://www.hackerrank.com/challenges/sherlock-and-geometry) | Advanced | | 262 | | [Good Point](https://www.hackerrank.com/challenges/good-point) | Hard | | 263 | | [Hard Homework](https://www.hackerrank.com/challenges/hard-homework) | Expert | | 264 | | [Polygons](https://www.hackerrank.com/challenges/polygons) | Hard | | 265 | | [Polygon](https://www.hackerrank.com/challenges/polygon) | Hard | | 266 | | [Meeting Point](https://www.hackerrank.com/challenges/meeting-point) | Hard | | 267 | | [House Location](https://www.hackerrank.com/challenges/house-location) | Hard | | 268 | | [The Letter N](https://www.hackerrank.com/challenges/n-letter) | Advanced | | 269 | | [Best Sum](https://www.hackerrank.com/challenges/best-sum) | Advanced | | 270 | | [Geometry Queries](https://www.hackerrank.com/challenges/geometry-queries) | Advanced | | 271 | | [Count Triangles](https://www.hackerrank.com/challenges/count-triangles) | Advanced | | 272 | | [Isosceles Triangle](https://www.hackerrank.com/challenges/isosceles-triangles) | Hard | | 273 | | [Elastic Rope](https://www.hackerrank.com/challenges/elastic-rope) | Expert | | 274 | | [Painting Figures](https://www.hackerrank.com/challenges/painting-figures) | Expert | | 275 | 276 | ### Probability ![problems-solved](https://img.shields.io/badge/Solved-3/28-00ffff.svg) 277 | 278 | | Name | Difficulty | Solution | 279 | |--------------------------------------------------------------------------------------------------------------------|:----------:|:---------------------------------------------------------------------------------------------------------:| 280 | | [Random Number Generator](https://www.hackerrank.com/challenges/random-number-generator) | Easy | [![py](https://img.icons8.com/color/35/000000/python.png)](python/probability/random_number_generator.py) | 281 | | [B'Day Gift](https://www.hackerrank.com/challenges/bday-gift) | Easy | [![cpp](assets/cpp.png)](cpp/bdayGift.cpp) | 282 | | [Day 4: Normal Distribution #1](https://www.hackerrank.com/challenges/normal-distribution-1) | Medium | | 283 | | [Day 4: Normal Distribution #2](https://www.hackerrank.com/challenges/normal-distribution-2) | Medium | | 284 | | [Vertical Sticks](https://www.hackerrank.com/challenges/vertical-sticks) | Medium | [![cpp](assets/cpp.png)](cpp/verticalSticks.cpp) | 285 | | [Extremely Dangerous Virus](https://www.hackerrank.com/challenges/extremely-dangerous-virus) | Medium | | 286 | | [The Matchstick Experiment](https://www.hackerrank.com/challenges/matchstick-experiment) | Medium | | 287 | | [Palindromes](https://www.hackerrank.com/challenges/palindromes) | Medium | | 288 | | [Lazy Sorting](https://www.hackerrank.com/challenges/lazy-sorting) | Medium | | 289 | | [Binomial Distribution #1](https://www.hackerrank.com/challenges/binomial-distribution-1) | Medium | | 290 | | [Kevin and Expected Values](https://www.hackerrank.com/challenges/kevin-and-expected-value) | Medium | | 291 | | [The White Lotus and Caterpillar Game](https://www.hackerrank.com/challenges/the-white-lotus-and-caterpillar-game) | Medium | | 292 | | [Dice Stats](https://www.hackerrank.com/challenges/dice-stats) | Medium | | 293 | | [Magic Number Tree](https://www.hackerrank.com/challenges/james-tree) | Medium | | 294 | | [Colorful Polygon](https://www.hackerrank.com/challenges/colorful-polygon) | Medium | | 295 | | [Sherlock and Probability](https://www.hackerrank.com/challenges/sherlock-and-probability) | Hard | | 296 | | [Normal Distribution #3](https://www.hackerrank.com/challenges/normal-distribution-3) | Hard | | 297 | | [Binomial Distribution #2](https://www.hackerrank.com/challenges/binomial-distribution-2) | Hard | | 298 | | [Binomial Distribution #3](https://www.hackerrank.com/challenges/binomial-distribution-3) | Hard | | 299 | | [Assignment Problem](https://www.hackerrank.com/challenges/assignment) | Expert | | 300 | | [Connect The Country](https://www.hackerrank.com/challenges/connect-the-country) | Hard | | 301 | | [Random Integers Random Bits](https://www.hackerrank.com/challenges/rirb) | Hard | | 302 | | [Random Number Generator](https://www.hackerrank.com/challenges/random-number-generator-1) | Hard | | 303 | | [Zombie March***](https://www.hackerrank.com/challenges/zombie-march) | Hard | | 304 | | [Bear and Dancing](https://www.hackerrank.com/challenges/bear-and-dancing) | Hard | | 305 | | [Randomness](https://www.hackerrank.com/challenges/randomness) | Advanced | | 306 | | [Random](https://www.hackerrank.com/challenges/random) | Hard | | 307 | | [Mathematical Expectation](https://www.hackerrank.com/challenges/mathematical-expectation) | Hard | | 308 | 309 | ### Linear Algebra Foundations ![problems-solved](https://img.shields.io/badge/Solved-0/17-00ffff.svg) 310 | 311 | | Name | Difficulty | Solution | 312 | |----------------------------------------------------------------------------------------------------------------------------------------------------------------------|:----------:|:--------:| 313 | | [Linear Algebra Foundations #1 - Matrix Addition](https://www.hackerrank.com/challenges/linear-algebra-foundations-1) | Easy | | 314 | | [Linear Algebra Foundations #2 - Matrix Subtraction](https://www.hackerrank.com/challenges/linear-algebra-foundations-2) | Easy | | 315 | | [Linear Algebra Foundations #3 - Matrix Multiplication](https://www.hackerrank.com/challenges/linear-algebra-foundations-3) | Easy | | 316 | | [Linear Algebra Foundations #4 - Matrix Multiplication](https://www.hackerrank.com/challenges/linear-algebra-foundations-4) | Easy | | 317 | | [Linear Algebra Foundations #5 - The 100th Power of a Matrix](https://www.hackerrank.com/challenges/linear-algebra-foundations-5-the-100th-power-of-a-matrix) | Medium | | 318 | | [Linear Algebra Foundations #6 - An Equation Involving Matrices](https://www.hackerrank.com/challenges/linear-algebra-foundations-6-the-nsupthsup-power-of-a-matrix) | Medium | | 319 | | [Linear Algebra Foundations #10 - Eigenvectors](https://www.hackerrank.com/challenges/linear-algebra-fundamentals-10-eigenvectors) | Medium | | 320 | | [Determinant of the Matrix #1](https://www.hackerrank.com/challenges/determinant-of-the-matrix-1) | Medium | | 321 | | [Determinant of the Matrix #3](https://www.hackerrank.com/challenges/determinant-of-the-matrix-3) | Medium | | 322 | | [Linear Algebra Foundations #7 - The 1000th Power of a Matrix](https://www.hackerrank.com/challenges/linear-algebra-foundations-7-the-1000th-power-of-a-matrix) | Hard | | 323 | | [Linear Algebra Foundations #8 - Systems of Equations](https://www.hackerrank.com/challenges/linear-algebra-fundamentals-8-systems-of-equations) | Hard | | 324 | | [Linear Algebra Foundations #9 - Eigenvalues](https://www.hackerrank.com/challenges/linear-algebra-fundamentals-9-eigenvalues) | Hard | | 325 | | [Determinant of the Matrix #2](https://www.hackerrank.com/challenges/determinant-of-the-matrix-2) | Hard | | 326 | | [Eigenvalue of Matrix I](https://www.hackerrank.com/challenges/eigenvalue-of-matrix-1) | Hard | | 327 | | [Eigenvalue of Matrix #2](https://www.hackerrank.com/challenges/eigenvalue-of-matrix-2) | Hard | | 328 | | [Eigenvalue of Matrix #3](https://www.hackerrank.com/challenges/eigenvalue-of-matrix-3) | Hard | | 329 | | [Eigenvalue of Matrix #4](https://www.hackerrank.com/challenges/eigenvalue-of-matrix-4) | Hard | | 330 | -------------------------------------------------------------------------------- /assets/cpp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/hackerrank-math/d07f06be997572d3082ad87dc5ebcfa45bd59342/assets/cpp.png -------------------------------------------------------------------------------- /assets/java.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/hackerrank-math/d07f06be997572d3082ad87dc5ebcfa45bd59342/assets/java.png -------------------------------------------------------------------------------- /assets/python.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/hackerrank-math/d07f06be997572d3082ad87dc5ebcfa45bd59342/assets/python.png -------------------------------------------------------------------------------- /cpp/armyGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | int m; 7 | cin >> n >> m; 8 | if (n <= 2 && m <= 2) 9 | cout << "1"; 10 | else { 11 | cout << ((n % 2) + (n / 2)) * ((m / 2) + (m % 2)) << endl; 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /cpp/bdayGift.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int N; 11 | cin >> N; 12 | 13 | cout << fixed; 14 | cout << setprecision(1); 15 | double total = 0; 16 | for (int i = 0; i < N; i++) { 17 | int cur; 18 | cin >> cur; 19 | total += 0.5 * cur; 20 | } 21 | 22 | cout << total << endl; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /cpp/bestDivisor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int n; 10 | cin >> n; 11 | int best = -1, ans = 1; 12 | for (int i = 1; i <= n; i++) { 13 | if (n % i != 0) { 14 | continue; 15 | } 16 | int s = 0; 17 | int x = i; 18 | while (x > 0) { 19 | s += x % 10; 20 | x /= 10; 21 | } 22 | if (s > best) { 23 | best = s; 24 | ans = i; 25 | } 26 | } 27 | cout << ans << endl; 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /cpp/busStation.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/bus-station 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | bool check(const set &mm, int val, int sum) { 11 | if (sum / val > mm.size()) { 12 | return false; 13 | } 14 | for (int i = val; i < sum; i += val) { 15 | if (mm.find(i) == mm.end()) { 16 | return false; 17 | } 18 | } 19 | return true; 20 | } 21 | 22 | int main() { 23 | int n; 24 | cin >> n; 25 | vector a(n); 26 | int sum = 0; 27 | set mm; 28 | for (int i = 0; i < n; i++) { 29 | cin >> a[i]; 30 | sum += a[i]; 31 | mm.insert(sum); 32 | } 33 | vector res; 34 | for (int i = 1; i * i <= sum; i++) { 35 | if (sum % i == 0) { 36 | if (check(mm, sum / i, sum)) { 37 | res.push_back(sum / i); 38 | } 39 | if (i * i != sum && check(mm, i, sum)) { 40 | res.push_back(i); 41 | } 42 | } 43 | } 44 | sort(res.begin(), res.end()); 45 | for (int i = 0; i < res.size(); i++) { 46 | cout << res[i] << " "; 47 | } 48 | cout << endl; 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /cpp/connectingTowns.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | while (n--) { 8 | int t; 9 | long long sum = 1; 10 | cin >> t; 11 | for (int j = 0; j < t - 1; j++) { 12 | long long x; 13 | cin >> x; 14 | sum = ((sum % 1234567) * (x % 1234567)) % 1234567; 15 | } 16 | cout << sum << endl; 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /cpp/diwaliLights.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/diwali-lights 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | cin >> t; 9 | for (int i = 0; i < t; i++) { 10 | int n, ans = 1; 11 | cin >> n; 12 | for (int i = 0; i < n; i++) { 13 | ans = ans * 2; 14 | ans = ans % 100000; 15 | } 16 | cout << ans - 1 << endl; 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /cpp/evenOddQuery.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/even-odd-query 2 | 3 | #include 4 | using namespace std; 5 | 6 | int A[100008], N; 7 | 8 | int find(int x, int y) { 9 | if (A[x] % 2) 10 | return 1; 11 | if (x == y) 12 | return 0; 13 | if (A[x + 1] == 0) 14 | return 1; 15 | return 0; 16 | } 17 | 18 | int main() { 19 | int x, y, Q; 20 | cin >> N; 21 | for (int i = 1; i <= N; i++) 22 | cin >> A[i]; 23 | cin >> Q; 24 | while (Q--) { 25 | cin >> x >> y; 26 | if (find(x, y)) 27 | cout << "Odd" << endl; 28 | else 29 | cout << "Even" << endl; 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /cpp/fillingJars.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/filling-jars 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int N, M; 12 | long long tot = 0; 13 | cin >> N >> M; 14 | for (int i = 0; i < M; i++) { 15 | int a, b; 16 | long long k; 17 | cin >> a >> b >> k; 18 | tot += (b - a + 1) * k; 19 | } 20 | 21 | cout << tot / N << endl; 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /cpp/findThePoint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin >> T; 11 | if ((1 <= T) && (T <= 15)) { 12 | 13 | int x1, y1, x2, y2; 14 | for (int i = 0; i < T; i++) { 15 | cin >> x1 >> y1 >> x2 >> y2; 16 | if ((x1 == x2) && (y1 == y2)) { 17 | cout << x1 << " " << y1 << endl; 18 | continue; 19 | } 20 | int x3 = x2 + (x2 - x1); 21 | cout << x3 << " " << ((y2 - y1) * x3 + (x2 * y1) - (y2 * x1)) / (x2 - x1) << endl; 22 | } 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /cpp/halloweenParty.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/halloween-party 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | int T; 13 | cin >> T; 14 | assert(1 <= T); 15 | assert(T <= 10); 16 | while (T--) { 17 | long long int N; 18 | cin >> N; 19 | assert(1 <= N); 20 | assert(N <= 10000000); 21 | if (N % 2 == 0) { 22 | cout << (N / 2) * (N / 2) << endl; 23 | } else { 24 | cout << (N / 2) * (N / 2 + 1) << endl; 25 | } 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /cpp/handshake.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | 8 | while (t--) { 9 | int n; 10 | cin >> n; 11 | 12 | cout << ((n - 1) * n) / 2 << endl; 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /cpp/isFibo.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/is-fibo 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define ll long long 8 | 9 | const ll MAXV = 10000000000LL; 10 | set fibs; 11 | void init() { 12 | ll a = 0, b = 1; 13 | while (a < MAXV) { 14 | fibs.insert(a); 15 | b += a; 16 | a = b - a; 17 | } 18 | } 19 | 20 | int main() { 21 | init(); 22 | int t; 23 | cin >> t; 24 | while (t--) { 25 | ll n; 26 | cin >> n; 27 | if (fibs.count(n)) 28 | cout << "IsFibo\n"; 29 | else 30 | cout << "IsNotFibo\n"; 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /cpp/matrixTracing.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/matrix-tracing 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | const int md = 1000000007; 11 | const int co = 2000010; 12 | 13 | long long fact[co + 10], inv[co + 10]; 14 | 15 | int main() { 16 | fact[0] = 1; 17 | for (int i = 1; i <= co; i++) 18 | fact[i] = fact[i - 1] * i % md; 19 | for (int i = 0; i <= co; i++) { 20 | int x = 1, step = 1 << 30; 21 | while (step > 0) { 22 | x = (long long)x * x % md; 23 | if (step & (md - 2)) 24 | x = (long long)x * fact[i] % md; 25 | step >>= 1; 26 | } 27 | inv[i] = x; 28 | } 29 | int tt; 30 | cin >> tt; 31 | while (tt--) { 32 | int n, m; 33 | cin >> n >> m; 34 | long long ans = fact[n + m - 2]; 35 | ans = ans * inv[n - 1] % md; 36 | ans = ans * inv[m - 1] % md; 37 | cout << ans << endl; 38 | } 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /cpp/maximumDraws.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | 8 | while (t--) { 9 | int n; 10 | cin >> n; 11 | cout << n + 1 << " \n"; 12 | } 13 | 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /cpp/minHeightTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int base, area; 6 | 7 | cin >> base >> area; 8 | 9 | cout << (area * 2 + (base - 1)) / base << endl; 10 | 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /cpp/mostDistant.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | int main() { 7 | int n; 8 | cin >> n; 9 | long long int x[n], y[n]; 10 | int i; 11 | for (i = 0; i < n; i++) { 12 | cin >> x[i] >> y[i]; 13 | } 14 | sort(x, x + n); 15 | sort(y, y + n); 16 | double d1, d2, d3, d4, d5, d6; 17 | d1 = abs(x[n - 1] - x[0]); 18 | d2 = abs(y[n - 1] - y[0]); 19 | d3 = abs(sqrt((x[0] * x[0]) + (y[0] * y[0]))); 20 | d4 = abs(sqrt((x[0] * x[0]) + (y[n - 1] * y[n - 1]))); 21 | d5 = abs(sqrt((x[n - 1] * x[n - 1]) + (y[0] * y[0]))); 22 | d6 = abs(sqrt((x[n - 1] * x[n - 1]) + (y[n - 1] * y[n - 1]))); 23 | double a[6]; 24 | a[0] = d1; 25 | a[1] = d2; 26 | a[2] = d3; 27 | a[3] = d4; 28 | a[4] = d5; 29 | a[5] = d6; 30 | sort(a, a + 6); 31 | printf("%.6lf\n", a[5]); 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /cpp/mutualRecurrences.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/mutual-recurrences 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int M = 30; 8 | const int mod = 1000000000; 9 | 10 | int sub(int x, int y) { 11 | x -= y; 12 | if (x < 0) 13 | x += mod; 14 | return x; 15 | } 16 | 17 | int add(int x, int y) { 18 | x += y; 19 | if (x >= mod) 20 | x -= mod; 21 | return x; 22 | } 23 | 24 | int mul(int x, int y) { 25 | return (1ll * x * y) % mod; 26 | } 27 | 28 | struct Matrix { 29 | int a[M][M]; 30 | 31 | Matrix() { 32 | for (int i = 0; i < M; ++i) 33 | fill(a[i], a[i] + M, 0); 34 | } 35 | 36 | int *operator[](int x) { 37 | return a[x]; 38 | } 39 | 40 | Matrix operator*(Matrix &to) { 41 | Matrix ans; 42 | for (int i = 0; i < M; ++i) 43 | for (int j = 0; j < M; ++j) 44 | for (int k = 0; k < M; ++k) 45 | ans[i][j] = add(ans[i][j], mul(a[i][k], to[k][j])); 46 | return ans; 47 | } 48 | 49 | vector operator*(vector to) { 50 | vector ans(24); 51 | for (int i = 0; i < 24; ++i) 52 | for (int j = 0; j < 24; ++j) 53 | ans[i] = add(ans[i], mul(a[i][j], to[j])); 54 | return ans; 55 | } 56 | 57 | } ONE; 58 | 59 | Matrix bin(Matrix A, long long n) { 60 | Matrix B = ONE; 61 | while (n) { 62 | if (n & 1) 63 | B = B * A; 64 | A = A * A; 65 | n >>= 1; 66 | } 67 | return B; 68 | } 69 | 70 | void kill() { 71 | int a, b, c, d, e, f, g, h; 72 | cin >> a >> b >> c >> d >> e >> f >> g >> h; 73 | 74 | Matrix A; 75 | A[0][a - 1]++; 76 | A[0][10 + b - 1]++; 77 | A[0][10 + c - 1]++; 78 | A[0][22]++; 79 | 80 | A[10][10 + e - 1]++; 81 | A[10][f - 1]++; 82 | A[10][g - 1]++; 83 | A[10][20]++; 84 | 85 | for (int i = 1; i <= 9; ++i) { 86 | A[i][i - 1]++; 87 | A[i + 10][10 + i - 1]++; 88 | } 89 | 90 | A[20][20] = h; 91 | A[20][21] = h; 92 | A[21][21] = h; 93 | 94 | A[22][22] = d; 95 | A[22][23] = d; 96 | A[23][23] = d; 97 | 98 | vector r(24, 1); 99 | r[20] = 0; 100 | r[21] = 1; 101 | r[22] = 0; 102 | r[23] = 1; 103 | 104 | long long n; 105 | cin >> n; 106 | 107 | A = bin(A, n + 1); 108 | 109 | r = A * r; 110 | cout << r[0] << " " << r[10] << endl; 111 | } 112 | 113 | int main() { 114 | for (int i = 0; i < M; ++i) 115 | ONE[i][i] = 1; 116 | 117 | int t; 118 | cin >> t; 119 | while (t--) { 120 | kill(); 121 | } 122 | return 0; 123 | } 124 | 125 | -------------------------------------------------------------------------------- /cpp/points_on_a_line.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int n; 5 | int x, y; 6 | map m1; 7 | map m2; 8 | int main() 9 | { 10 | m1.clear(); 11 | m2.clear(); 12 | cin >> n; 13 | while (n--) 14 | { 15 | cin >> x >> y; 16 | m1[x]; 17 | m2[y]; 18 | } 19 | if (m1.size() <= 1 || m2.size() <= 1) 20 | { 21 | cout << "YES\n"; 22 | } 23 | else 24 | { 25 | cout << "NO\n"; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /cpp/possiblePath.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/possible-path/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | long long gcd(long long a, long long b) { 7 | return b == 0 ? a : gcd(b, a % b); 8 | } 9 | 10 | int main() { 11 | int t; 12 | long long a, b, x, y; 13 | cin >> t; 14 | while (t--) { 15 | cin >> a >> b >> x >> y; 16 | if (gcd(a, b) == gcd(x, y)) 17 | cout << "YES\n"; 18 | else 19 | cout << "NO\n"; 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /cpp/pythagoreanTriplet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long int a, b, c; 6 | cin >> a; 7 | 8 | if (a % 2 == 0) { 9 | b = ((a / 2) * (a / 2)) - 1; 10 | c = b + 2; 11 | } else { 12 | b = ((a * a) - 1) / 2; 13 | c = b + 1; 14 | } 15 | 16 | cout << a << " " << b << " " << c; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /cpp/restaurant.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd(int a, int b) { 5 | if (a == 0) { 6 | return b; 7 | } else if (b == 0) { 8 | return a; 9 | } else { 10 | return gcd(b, a % b); 11 | } 12 | } 13 | 14 | int main() { 15 | int n; 16 | cin >> n; 17 | 18 | while (n--) { 19 | int a, b; 20 | cin >> a >> b; 21 | 22 | cout << (a * b) / (gcd(a, b) * gcd(a, b)) << "\n"; 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /cpp/reverseGame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t, n, k; 6 | cin >> t; 7 | while (t--) { 8 | cin >> n >> k; 9 | if (k < n / 2) { 10 | cout << (2 * k + 1) << "\n"; 11 | } else { 12 | cout << (n - 1 - k) * 2 << "\n"; 13 | } 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /cpp/specialMultiple.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/special-multiple 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | const int N = 5555; 11 | 12 | bool was[N]; 13 | int x[N], pr[N], pd[N]; 14 | 15 | int main() 16 | { 17 | int tt; 18 | cin >> tt; 19 | while (tt--) 20 | { 21 | int n; 22 | cin >> n; 23 | for (int i = 0; i < n; i++) 24 | was[i] = false; 25 | int b = 1, e = 1; 26 | x[1] = 9 % n; 27 | was[x[1]] = true; 28 | pr[x[1]] = -1; 29 | pd[x[1]] = -1; 30 | while (b <= e) 31 | { 32 | int nx = (x[b] * 10 + 0) % n; 33 | if (!was[nx]) 34 | { 35 | e++; 36 | x[e] = nx; 37 | was[nx] = true; 38 | pr[nx] = x[b]; 39 | pd[nx] = 0; 40 | } 41 | nx = (x[b] * 10 + 9) % n; 42 | if (!was[nx]) 43 | { 44 | e++; 45 | x[e] = nx; 46 | was[nx] = true; 47 | pr[nx] = x[b]; 48 | pd[nx] = 9; 49 | } 50 | b++; 51 | } 52 | int p = 0; 53 | string res = ""; 54 | while (pr[p] != -1) 55 | { 56 | res += (char)(pd[p] + 48); 57 | p = pr[p]; 58 | } 59 | res += "9"; 60 | reverse(res.begin(), res.end()); 61 | cout << res << endl; 62 | } 63 | return 0; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /cpp/verticalSticks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 50; 8 | int t, n, arr[N]; 9 | double dp[N + 1]; 10 | 11 | int main() { 12 | for (cin >> t; t--;) { 13 | cin >> n; 14 | for (int i = 0; i < n; i++) { 15 | cin >> arr[i]; 16 | } 17 | double result = 0; 18 | for (int i = 0; i < n; i++) { 19 | int gte = 0; 20 | for (int j = 0; j < n; j++) 21 | if (arr[i] <= arr[j]) 22 | gte++; 23 | gte--; 24 | for (int j = 1; j <= n; j++) { 25 | dp[0] = j; 26 | for (int k = 1; k < j; k++) { 27 | double p = (double)gte / (double)(n - j + k); 28 | dp[k] = p * (j - k) + (1 - p) * dp[k - 1]; 29 | } 30 | result += dp[j - 1] / n; 31 | } 32 | } 33 | cout.precision(2); 34 | cout.setf(ios::fixed | ios::showpoint); 35 | cout << result << endl; 36 | } 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /java/src/ArmyGame.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/game-with-cells 2 | // T: O(1) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class ArmyGame { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | final int n = SCANNER.nextInt(); 12 | final int m = SCANNER.nextInt(); 13 | System.out.println(minimumPackages(m, n)); 14 | } 15 | 16 | private static int minimumPackages(double m, double n) { 17 | return (int) (Math.ceil(m / 2) * Math.ceil(n / 2)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/BestDivisor.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/best-divisor 2 | // T: O(N log(N)) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class BestDivisor { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | private static final class Number implements Comparable { 11 | private final int value; 12 | private final int sumOfDigits; 13 | 14 | Number(int value) { 15 | this.value = value; 16 | this.sumOfDigits = sumOfDigits(value); 17 | } 18 | 19 | private int sumOfDigits(int x) { 20 | int result = 0; 21 | while (x > 0) { 22 | result += x % 10; 23 | x /= 10; 24 | } 25 | return result; 26 | } 27 | 28 | @Override 29 | public int compareTo(Number o) { 30 | if (this.sumOfDigits == o.sumOfDigits) return Integer.compare(o.value, this.value); 31 | return Integer.compare(this.sumOfDigits, o.sumOfDigits); 32 | } 33 | } 34 | 35 | public static void main(String[] args) { 36 | final int n = SCANNER.nextInt(); 37 | final Number input = new Number(n); 38 | Number result = input; 39 | for (int i = 1 ; i <= n / 2 ; i++) { 40 | if (n % i == 0) { 41 | final Number number = new Number(i); 42 | if (number.compareTo(input) > 0 && number.compareTo(result) > 0) { 43 | result = number; 44 | } 45 | } 46 | } 47 | System.out.println(result.value); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /java/src/ConnectingTowns.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/connecting-towns 2 | // t: number of test cases 3 | // n: max number of cities in any test cases 4 | // T: O(t * n) 5 | // S: O(n) 6 | 7 | import java.util.Scanner; 8 | 9 | public class ConnectingTowns { 10 | private static final Scanner SCANNER = new Scanner(System.in); 11 | 12 | private static final int MODULO = 1_234_567; 13 | 14 | public static void main(String[] args) { 15 | int testCases = SCANNER.nextInt(); 16 | while (testCases-- > 0) { 17 | final int towns = SCANNER.nextInt(); 18 | final int[] distances = getArray(towns - 1); 19 | final int paths = totalPaths(distances); 20 | System.out.println(paths); 21 | } 22 | } 23 | 24 | private static int totalPaths(int[] array) { 25 | int result = 1; 26 | for (int element : array) { 27 | result = (result * element) % MODULO; 28 | } 29 | return result; 30 | } 31 | 32 | private static int[] getArray(int length) { 33 | final int[] array = new int[length]; 34 | for (int i = 0 ; i < length ; i++) { 35 | array[i] = SCANNER.nextInt(); 36 | } 37 | return array; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /java/src/CuttingPaperSquares.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/p1-paper-cutting 2 | // T: O(1) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class CuttingPaperSquares { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | final int m = SCANNER.nextInt(); 12 | final int n = SCANNER.nextInt(); 13 | System.out.println(minimumCuts(m, n)); 14 | } 15 | 16 | private static long minimumCuts(long m, long n) { 17 | return m * n - 1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/EasySum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class EasySum { 4 | private static final Scanner SCANNER = new Scanner(System.in); 5 | 6 | public static void main(String[] args) { 7 | int testCases = SCANNER.nextInt(); 8 | while (testCases-- > 0) { 9 | final int n = SCANNER.nextInt(); 10 | final int m = SCANNER.nextInt(); 11 | final int rows = ceil((double) (n - m + 1) / m) + 1; 12 | final int k = m - ((n - m + 1) % m); 13 | System.out.println(summation(m - 1) * rows - (summation(m - 1) - summation(m - k - 1))); 14 | } 15 | } 16 | 17 | private static int ceil(double x) { 18 | return (int) Math.ceil(x); 19 | } 20 | 21 | private static long summation(long x) { 22 | return (x * (x + 1)) / 2; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/FillingJars.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/filling-jars 2 | // m: operations 3 | // T: O(m) 4 | // S: O(1) 5 | 6 | import java.util.Scanner; 7 | 8 | public class FillingJars { 9 | private static final Scanner SCANNER = new Scanner(System.in); 10 | 11 | public static void main(String[] args) { 12 | final int jars = SCANNER.nextInt(); 13 | final int operations = SCANNER.nextInt(); 14 | long candies = 0; 15 | for (int i = 0 ; i < operations ; i++) { 16 | final long a = SCANNER.nextLong(); 17 | final long b = SCANNER.nextLong(); 18 | final long k = SCANNER.nextLong(); 19 | candies += k * (b - a + 1); 20 | } 21 | System.out.println(candies / jars); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/FindThePoint.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/find-point 2 | // T: O(t) t = number of test cases, for each test case O(1) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class FindThePoint { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | int testCases = SCANNER.nextInt(); 12 | while (testCases-- > 0) { 13 | final int px = SCANNER.nextInt(); 14 | final int py = SCANNER.nextInt(); 15 | final int qx = SCANNER.nextInt(); 16 | final int qy = SCANNER.nextInt(); 17 | final int resultX = 2 * qx - px; 18 | final int resultY = 2 * qy - py; 19 | System.out.println(resultX + " " + resultY); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/HalloweenParty.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/halloween-party 2 | // t: number of test cases 3 | // T: O(t) 4 | // S: O(1) 5 | 6 | import java.util.Scanner; 7 | 8 | public class HalloweenParty { 9 | private static final Scanner SCANNER = new Scanner(System.in); 10 | 11 | public static void main(String[] args) { 12 | int testCases = SCANNER.nextInt(); 13 | while (testCases-- > 0) { 14 | final long cuts = SCANNER.nextLong(); 15 | System.out.println(maxBlocks(cuts)); 16 | } 17 | } 18 | 19 | private static long maxBlocks(final long n) { 20 | return (n / 2) * (n - n / 2); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/Handshake.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/handshake 2 | // T: O(t) t = number of test cases, for each test case O(1) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class Handshake { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | int testCases = SCANNER.nextInt(); 12 | while (testCases-- > 0) { 13 | final int people = SCANNER.nextInt(); 14 | System.out.println(nC2(people)); 15 | } 16 | } 17 | 18 | private static int nC2(int x) { 19 | return (x * (x - 1)) / 2; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/JimAndTheJokes.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.stream.LongStream; 3 | 4 | public class JimAndTheJokes { 5 | private static final Scanner SCANNNER = new Scanner(System.in); 6 | 7 | public static void main(String[] args) { 8 | final long[] t = new long[38]; 9 | for (int n = SCANNNER.nextInt(); n > 0; n--) { 10 | final int m = SCANNNER.nextInt(); 11 | final int d = SCANNNER.nextInt(); 12 | final String s = Integer.toString(d); 13 | if (m > 1 && s.chars().noneMatch(c -> c >= '0' + m)) { 14 | t[Integer.parseInt(s, m)]++; 15 | } 16 | } 17 | System.out.println(LongStream.of(t).map(k -> k * (k - 1) / 2).sum()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/LeonardosPrimeFactors.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/leonardo-and-prime 2 | // t: test cases 3 | // n: max value of n in nay test case 4 | // T: O(t * log(n)) 5 | // T: O(1) 6 | 7 | import java.util.Scanner; 8 | 9 | public class LeonardosPrimeFactors { 10 | private static final Scanner SCANNER = new Scanner(System.in); 11 | 12 | private static final long[] SMALLEST_PRIME_FACTOR_PRODUCTS = { 13 | 2, 6, 30, 210, 2310, 30030, 510510, 9699690, 223092870, 6469693230L, 200560490130L, 7420738134810L, 14 | 304250263527210L, 13082761331670030L, 614889782588491410L 15 | }; 16 | 17 | public static void main(String[] args) { 18 | int testCases = SCANNER.nextInt(); 19 | while (testCases-- > 0) { 20 | final long n = SCANNER.nextLong(); 21 | final int result = maxDistinctPrimeFactorsBetween1And(n); 22 | System.out.println(result); 23 | } 24 | } 25 | 26 | private static int maxDistinctPrimeFactorsBetween1And(long x) { 27 | int index = binarySearch(x); 28 | if (index >= SMALLEST_PRIME_FACTOR_PRODUCTS.length) return index; 29 | return SMALLEST_PRIME_FACTOR_PRODUCTS[index] == x 30 | ? index + 1 31 | : index; 32 | } 33 | 34 | private static int binarySearch(long x) { 35 | final int length = SMALLEST_PRIME_FACTOR_PRODUCTS.length; 36 | int left = 0, right = length - 1, middle; 37 | while (left <= right) { 38 | middle = left + (right - left) / 2; 39 | if (SMALLEST_PRIME_FACTOR_PRODUCTS[middle] == x) return middle; 40 | else if (SMALLEST_PRIME_FACTOR_PRODUCTS[middle] > x) right = middle - 1; 41 | else left = middle + 1; 42 | } 43 | return left; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /java/src/MaximumDraws.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/maximum-draws 2 | // T: O(t) t = number of testcases, for each testcase O(1) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class MaximumDraws { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | int testCases = SCANNER.nextInt(); 12 | while (testCases-- > 0) { 13 | final int colors = SCANNER.nextInt(); 14 | System.out.println(colors + 1); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/MinimumHeightTriangle.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/lowest-triangle 2 | // T: O(1) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class MinimumHeightTriangle { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | final int base = SCANNER.nextInt(); 12 | final int area = SCANNER.nextInt(); 13 | final int minHeight = minHeight(area, base); 14 | System.out.println(minHeight); 15 | } 16 | 17 | private static int minHeight(double area, double base) { 18 | return (int) Math.ceil(2 * area / base); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/MostDistant.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/most-distant 2 | // T: O(N) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class MostDistant { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | int N = SCANNER.nextInt(); 12 | int xMin = 0, xMax = 0, yMin = 0, yMax = 0; 13 | 14 | xMin = xMax = SCANNER.nextInt(); 15 | yMin = yMax = SCANNER.nextInt(); 16 | 17 | for (int i = 1 ; i < N ; i++) { 18 | final int x = SCANNER.nextInt(); 19 | final int y = SCANNER.nextInt(); 20 | yMin = Math.min(yMin, y); 21 | yMax = Math.max(yMax, y); 22 | xMin = Math.min(xMin, x); 23 | xMax = Math.max(xMax, x); 24 | } 25 | 26 | double distance = max( 27 | xMax - xMin, 28 | yMax - yMin, 29 | distance(xMin, yMin), 30 | distance(xMin, yMax), 31 | distance(yMin, xMax), 32 | distance(yMax, xMax) 33 | ); 34 | 35 | System.out.println(distance); 36 | } 37 | 38 | private static double distance(int a, int b) { 39 | return Math.sqrt(a * a + b * b); 40 | } 41 | 42 | private static double max(double... numbers) { 43 | double result = Double.MIN_VALUE; 44 | for (double number : numbers) { 45 | result = Math.max(result, number); 46 | } 47 | return result; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /java/src/PossiblePath.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/possible-path 2 | // t: number of test cases 3 | // n: max(a, b, x, y) 4 | // T: O(t * log(n)) 5 | // S: O(log(n)) 6 | 7 | import java.util.Scanner; 8 | 9 | public class PossiblePath { 10 | private static final Scanner SCANNER = new Scanner(System.in); 11 | 12 | public static void main(String[] args) { 13 | int testCases = SCANNER.nextInt(); 14 | while (testCases-- > 0) { 15 | final long a = SCANNER.nextLong(); 16 | final long b = SCANNER.nextLong(); 17 | final long x = SCANNER.nextLong(); 18 | final long y = SCANNER.nextLong(); 19 | System.out.println(canReach(a, b, x, y) ? "YES" : "NO"); 20 | } 21 | } 22 | 23 | private static boolean canReach(long a, long b, long x, long y) { 24 | return gcd(a, b) == gcd(x, y); 25 | } 26 | 27 | private static long gcd(long a, long b) { 28 | return b == 0 ? a : gcd(b, a % b); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /java/src/Restaurant.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/restaurant 2 | // t: number of test cases 3 | // n: max of l and b in each entry 4 | // T: O(t * log(n)) 5 | // S: O(1) 6 | 7 | import java.util.Scanner; 8 | 9 | public class Restaurant { 10 | private static final Scanner SCANNER = new Scanner(System.in); 11 | 12 | public static void main(String[] args) { 13 | int testCases = SCANNER.nextInt(); 14 | while (testCases-- > 0) { 15 | final int l = SCANNER.nextInt(); 16 | final int b = SCANNER.nextInt(); 17 | System.out.println(maximumNumberOfSquares(l, b)); 18 | } 19 | } 20 | 21 | private static int maximumNumberOfSquares(int l, int b) { 22 | final int sideLength = gcd(l, b); 23 | return (l * b) / (sideLength * sideLength); 24 | } 25 | 26 | private static int gcd(int a, int b) { 27 | return b == 0 ? a : gcd(b, a % b); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java/src/ReverseGame.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/reverse-game 2 | // t: number of test cases 3 | // T: O(t) 4 | // S: O(1) 5 | 6 | import java.util.Scanner; 7 | 8 | public class ReverseGame { 9 | private static final Scanner SCANNER = new Scanner(System.in); 10 | 11 | public static void main(String[] args) { 12 | int testCases = SCANNER.nextInt(); 13 | while (testCases-- > 0) { 14 | final int n = SCANNER.nextInt(); 15 | final int k = SCANNER.nextInt(); 16 | System.out.println(finalIndex(n, k)); 17 | } 18 | } 19 | 20 | private static int finalIndex(int n, int k) { 21 | if (k < n / 2) return 2 * k + 1; 22 | return (n - 1 - k) * 2; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/SherlockAndDivisors.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/sherlock-and-divisors 2 | // t: number of test cases 3 | // T: O(t * sqrt(n)) 4 | // S: O(1) 5 | 6 | import java.util.Scanner; 7 | 8 | public class SherlockAndDivisors { 9 | private static final Scanner SCANNER = new Scanner(System.in); 10 | 11 | public static void main(String[] args) { 12 | int testCases = SCANNER.nextInt(); 13 | while (testCases-- > 0) { 14 | final int n = SCANNER.nextInt(); 15 | System.out.println(evenDivisors(n)); 16 | } 17 | } 18 | 19 | private static int evenDivisors(final int n) { 20 | if (n % 2 == 1) return 0; 21 | int count = 1; 22 | for(int divisor = 2 ; divisor * divisor <= n ; divisor++){ 23 | if(n % divisor == 0) { 24 | if (divisor % 2 == 0) count++; 25 | 26 | int dividend = n / divisor; 27 | if(dividend != divisor && dividend % 2 == 0) count++; 28 | } 29 | } 30 | return count; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /java/src/SherlockAndMovingTiles.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/sherlock-and-moving-tiles 2 | // t: test cases 3 | // T: O(t) 4 | // S: O(1) 5 | 6 | import java.util.Scanner; 7 | 8 | public class SherlockAndMovingTiles { 9 | private static final Scanner SCANNER = new Scanner(System.in); 10 | 11 | private static final double SQRT_2 = Math.sqrt(2); 12 | 13 | public static void main(String[] args) { 14 | final int length = SCANNER.nextInt(); 15 | final int v1 = SCANNER.nextInt(); 16 | final int v2 = SCANNER.nextInt(); 17 | final int velocityDiff = Math.abs(v1 - v2); 18 | int testCases = SCANNER.nextInt(); 19 | while (testCases-- > 0) { 20 | final double area = SCANNER.nextDouble(); 21 | System.out.println(time(area, length, velocityDiff)); 22 | } 23 | } 24 | 25 | private static double time(double area, int length, int velocityDiff) { 26 | return SQRT_2 * (length - Math.sqrt(area)) / velocityDiff; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java/src/StrangeGridAgain.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/strange-grid 2 | // T: O(1) 3 | // S: O(1) 4 | 5 | import java.util.Scanner; 6 | 7 | public class StrangeGridAgain { 8 | private static final Scanner SCANNER = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | final int r = SCANNER.nextInt(); 12 | final int c = SCANNER.nextInt(); 13 | System.out.println(integerValue(r, c)); 14 | } 15 | 16 | private static long integerValue(final long row, final long column) { 17 | return 10 * ((row - 1) / 2) + (row - 1) % 2 + 2 * (column - 1); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/SumarAndTheFloatingRocks.java: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/harry-potter-and-the-floating-rocks 2 | // t: test cases 3 | // n: max(x, y) 4 | // T: O(t * log(n)) 5 | // S: O(log(n)) 6 | 7 | import java.util.Objects; 8 | import java.util.Scanner; 9 | 10 | public class SumarAndTheFloatingRocks { 11 | private static final Scanner SCANNER = new Scanner(System.in); 12 | 13 | private static final class Point { 14 | private final int x; 15 | private final int y; 16 | 17 | private Point(int x, int y) { 18 | this.x = x; 19 | this.y = y; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object obj) { 24 | if (obj == this) return true; 25 | if (obj == null || obj.getClass() != this.getClass()) return false; 26 | var that = (Point) obj; 27 | return this.x == that.x && 28 | this.y == that.y; 29 | } 30 | 31 | @Override 32 | public int hashCode() { 33 | return Objects.hash(x, y); 34 | } 35 | } 36 | 37 | public static void main(String[] args) { 38 | int testCases = SCANNER.nextInt(); 39 | while (testCases-- > 0) { 40 | final int x1 = SCANNER.nextInt(); 41 | final int y1 = SCANNER.nextInt(); 42 | final int x2 = SCANNER.nextInt(); 43 | final int y2 = SCANNER.nextInt(); 44 | System.out.println(integralPointsBetween(new Point(x1, y1), new Point(x2, y2))); 45 | } 46 | } 47 | 48 | private static int integralPointsBetween(Point p1, Point p2) { 49 | if (p1.equals(p2)) return 0; 50 | if (p1.x == p2.x) return Math.abs(p1.y - p2.y) - 1; 51 | if (p1.y == p2.y) return Math.abs(p1.x - p2.x) - 1; 52 | final int diffX = Math.abs(p1.x - p2.x); 53 | final int diffY = Math.abs(p1.y - p2.y); 54 | final int slopeGcd = gcd(diffX, diffY); 55 | final int factor = diffX / slopeGcd; 56 | return (diffX - 1) / factor; 57 | } 58 | 59 | private static int gcd(int a, int b) { 60 | return b == 0 ? a : gcd(b, a % b); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /programming-language-logos.md: -------------------------------------------------------------------------------- 1 | ## Programming Language Logos 2 | To add a logo in the [README](README.md) table simply add a link with an embedded picture as 3 | ```markdown 4 | [![Python](icon-link)](this link will be filled by the author if the pull request is accepted) 5 | ``` 6 | 7 | For the icon link refer to the table below to obtain the link for a specific programming language: 8 | 9 | | Programming Language | Icon | Icon Link | 10 | |:--------------------:|:------------------------------------------------------------------------------:|:---------------------------------------------------------------------:| 11 | | Java | ![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png) | https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png | 12 | | Python | ![Python](https://img.icons8.com/color/35/000000/python.png) | https://img.icons8.com/color/35/000000/python.png | 13 | | C | ![C](https://img.icons8.com/color/35/000000/c-programming.png) | https://img.icons8.com/color/35/000000/c-programming.png | 14 | | C++ | ![C++](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png) | https://img.icons8.com/color/35/000000/c-plus-plus-logo.png | 15 | | C# | ![C#](https://img.icons8.com/color/35/000000/c-sharp-logo.png) | https://img.icons8.com/color/35/000000/c-sharp-logo.png | 16 | | JavaScript | ![JavaScript](https://img.icons8.com/color/40/000000/javascript.png) | https://img.icons8.com/color/40/000000/javascript.png | 17 | | TypeScript | ![TypeScript](https://img.icons8.com/color/40/000000/typescript.png) | https://img.icons8.com/color/40/000000/typescript.png | 18 | | Ruby | ![Ruby](https://img.icons8.com/office/35/000000/ruby-programming-language.png) | https://img.icons8.com/office/35/000000/ruby-programming-language.png | 19 | | Scala | ![Scala](https://img.icons8.com/dusk/35/000000/scala.png) | https://img.icons8.com/office/35/000000/ruby-programming-language.png | 20 | | PGH | ![PHP](https://img.icons8.com/officel/40/000000/php-logo.png) | https://img.icons8.com/officel/40/000000/php-logo.png | 21 | | Swift | ![Swift](https://img.icons8.com/fluent/40/000000/swift.png) | https://img.icons8.com/fluent/40/000000/swift.png | 22 | -------------------------------------------------------------------------------- /python/probability/random_number_generator.py: -------------------------------------------------------------------------------- 1 | from math import gcd 2 | 3 | 4 | def probability(a: int, b: int, c: int) -> str: 5 | if a + b <= c: 6 | return '1/1' 7 | if a >= c and b >= c: 8 | x = c ** 2 9 | y = 2 * a * b 10 | hcf = gcd(x, y) 11 | return f'{x // hcf}/{y // hcf}' 12 | 13 | if a <= c and b <= c: 14 | x = 2 * a * b - (a - c + b) ** 2 15 | y = 2 * a * b 16 | hcf = gcd(x, y) 17 | return f'{x // hcf}/{y // hcf}' 18 | 19 | a, b = max(a, b), min(a, b) 20 | x = c ** 2 - (c - b) ** 2 21 | y = 2 * a * b 22 | hcf = gcd(x, y) 23 | return f'{x // hcf}/{y // hcf}' 24 | 25 | 26 | test_cases = int(input()) 27 | for _ in range(test_cases): 28 | a, b, c = map(int, input().split()) 29 | print(probability(a, b, c)) 30 | --------------------------------------------------------------------------------