├── .gitattributes ├── .gitignore ├── Codeforces ├── Codeforces Round 280 (Div 2) │ ├── Vanya and Computer Game.cpp │ ├── Vanya and Cubes.cpp │ ├── Vanya and Exams.cpp │ ├── Vanya and Field.cpp │ └── Vanya and Lanterns.cpp ├── Codeforces Round 281 (Div 2) │ ├── Vasya and Basketball.cpp │ ├── Vasya and Chess.cpp │ ├── Vasya and Football.cpp │ └── Vasya and Wrestling.cpp ├── Codeforces Round 282 (Div 2) │ ├── 01 Digital Counter.cpp │ └── 02 Modular Equations.cpp ├── Codeforces Round 283 (Div 2) │ ├── Distributing Parts.cpp │ ├── Minimum Difficulty.cpp │ ├── Removing Columns.cpp │ ├── Secret Combination.cpp │ └── Tennis Game.cpp ├── Codeforces Round 320 (Div 2) │ ├── A Problem About Polyline.cpp │ ├── Finding Team Member.cpp │ ├── Or Game.cpp │ └── Raising Bacteria.cpp ├── Codeforces Round 321 (Div 2) │ ├── Kefa And Company.cpp │ ├── Kefa And First Steps.cpp │ ├── Kefa And Park.cpp │ └── Kefa and Watch.cpp ├── Codeforces Round 324 (Div 2) │ ├── Dima And Lisa.cpp │ ├── Kolya And Tanya.cpp │ └── Olesya and Rodion.cpp ├── Codeforces Round 325 (Div 2) │ ├── Alena's Schedule.cpp │ ├── Gennady the Dentist.cpp │ └── Laurenty and Shop.cpp └── Codeforces Round 674 (Div 3) │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ └── e.cpp ├── Cracking the Coding Interview └── Chapter 01 │ ├── Question01.cpp │ ├── Question02.cpp │ ├── Question03a.cpp │ ├── Question03b.cpp │ ├── Question04.cpp │ └── Question05.cpp ├── Edge Verve Coding Round ├── Question 1 - Shortest Path along the sides of 3D box.cpp ├── Question 2 - Zebra Alternations.cpp ├── Question 3 - Factorial of Large Numbers.cpp └── Question 3 - Factorial of Large Numbers.py ├── GeeksForGeeks ├── Finding the numbers.py ├── Maximum Index.py └── Subarray with given sum.py ├── Google APAC ├── 2015 APAC Test Round A │ ├── Problem A Seven-Segment Display │ │ ├── A-large-practice.in │ │ ├── A-large-practice.out │ │ ├── A-small-practice.in │ │ ├── A-small-practice.out │ │ └── Seven-Segment Display.cpp │ ├── Problem B Super 2048 │ │ ├── B-large-practice.in │ │ ├── B-large-practice.out │ │ ├── B-small-practice.in │ │ ├── B-small-practice.out │ │ └── Super 2048.cpp │ ├── Problem C Addition │ │ ├── Addition.cpp │ │ ├── C-small-practice.in │ │ └── C-small-practice.out │ └── Problem D Cut Tiles │ │ ├── Cut Tiles.cpp │ │ ├── D-large-practice.in │ │ ├── D-large-practice.out │ │ ├── D-small-practice.in │ │ └── D-small-practice.out ├── 2016 APAC Test Practice Round │ ├── Problem A Bad Horse │ │ ├── A-small-practice-1.in │ │ ├── A-small-practice-1.out │ │ ├── A-small-practice-2.in │ │ ├── A-small-practice-2.out │ │ └── Bad Horse.cpp │ ├── Problem B Captain Hammer │ │ ├── B-small-practice.in │ │ ├── B-small-practice.out │ │ └── Captain Hammer.cpp │ └── Problem C Moist │ │ ├── C-small-practice-1.in │ │ ├── C-small-practice-1.out │ │ ├── C-small-practice-2.in │ │ ├── C-small-practice-2.out │ │ └── Moist.cpp └── 2016 APAC Test Round A │ ├── Problem A Googol String │ ├── A-large-practice.in │ ├── A-large-practice.out │ ├── A-small-practice.in │ ├── A-small-practice.out │ └── Googol String.cpp │ ├── Problem B g Cube │ ├── B-large-practice.in │ ├── B-large-practice.out │ ├── B-small-practice.in │ ├── B-small-practice.out │ └── g Cube.cpp │ ├── Problem C g Campus │ ├── C-large-practice.in │ ├── C-large-practice.out │ ├── C-small-practice.in │ ├── C-small-practice.out │ └── g Campus.cpp │ └── Problem D g Snake │ ├── D-large-practice.in │ ├── D-large-practice.out │ ├── D-small-practice.in │ ├── D-small-practice.out │ └── g Snake.cpp ├── HackerEarth Solutions ├── CIRCLE1.cpp └── MINIMIZE_COST.cpp ├── HackerRank Solutions ├── Algorithms │ ├── Arrays And Sorting │ │ ├── Almost Sorted(Not Fully Solved).cpp │ │ ├── Closest Numbers.cpp │ │ ├── Correctness and the Loop Invariant.cpp │ │ ├── Counting Sort 1.cpp │ │ ├── Counting Sort 2.cpp │ │ ├── Counting Sort 3.cpp │ │ ├── Find the Median.cpp │ │ ├── Insertion Sort 1.cpp │ │ ├── Insertion Sort 2.cpp │ │ ├── Insertion Sort Advanced Analysis.cpp │ │ ├── Quick Sort 1 - Partition.cpp │ │ ├── QuickSort 2 - Sorting.cpp │ │ ├── Quicksort In-Place.cpp │ │ ├── Running Time of Algorithms.cpp │ │ ├── Running Time of Quicksort.cpp │ │ ├── Sherlock And Pairs.cpp │ │ ├── Sherlock and Watson.cpp │ │ ├── The Full Counting Sort.cpp │ │ └── Tutorial Info.cpp │ ├── Bit Manipulation │ │ ├── Counter Game.cpp │ │ └── Sansa and XOR.cpp │ ├── Combinatorics │ │ ├── Building a List.cpp │ │ ├── Connecting Towns.cpp │ │ ├── Diwali Lights.cpp │ │ ├── Diwali Lights.py │ │ ├── Handshake.cpp │ │ ├── Minimum Draws.cpp │ │ └── Picking Cards.cpp │ ├── Cryptography │ │ └── PRNG Sequence Guessing.java │ ├── Dynamic Programming │ │ ├── Equal.cpp │ │ ├── Fibonacci Modified.java │ │ ├── Stock Maximize.cpp │ │ ├── The Longest Common Subsequence.cpp │ │ ├── The Longest Increasing Subsequence.cpp │ │ ├── The Maximum Subarray.cpp │ │ ├── Unbounded Knap Sack.cpp │ │ └── Unbounded KnapSack.java │ ├── Geometry │ │ ├── Find Point.cpp │ │ ├── Rectangular Game.cpp │ │ ├── Sherlock And Planes.cpp │ │ └── Spheres.cpp │ ├── Graph Theory │ │ ├── A Journey To The Moon.cpp │ │ ├── Even Tree.cpp │ │ └── Favourite Sequence.cpp │ ├── Greedy │ │ ├── Algorithmic Crush.cpp │ │ ├── Chief Hopper.cpp │ │ ├── Cutting Boards.cpp │ │ ├── Flowers.cpp │ │ ├── Grid Challenge.cpp │ │ ├── Jim and the Orders.cpp │ │ ├── Largest Permutation.cpp │ │ ├── Priyanka and Toys.cpp │ │ └── Two arrays.cpp │ ├── Implementation │ │ ├── Angry Professor.py │ │ ├── Between Two Sets - Brute Force.py │ │ ├── Between Two Sets.java │ │ ├── Between Two Sets.py │ │ ├── Birthday Chocolate - Brute Force.py │ │ ├── Birthday Chocolate.java │ │ ├── Birthday Chocolate.py │ │ ├── Cats and a Mouse.py │ │ ├── Cavity Map.cpp │ │ ├── Chocolate Feast.py │ │ ├── Cut The Sticks.cpp │ │ ├── Designer PDF Viewer.cpp │ │ ├── Designer PDF Viewer.java │ │ ├── Designer PDF Viewer.py │ │ ├── Divisible Sum Pairs.py │ │ ├── Find Digits.py │ │ ├── Flatland Space Stations - O(n).py │ │ ├── Flatland Space Stations.py │ │ ├── Halloween Sale.py │ │ ├── Jumping on the Clouds - Revisited.py │ │ ├── Migratory Birds.java │ │ ├── Migratory Birds.py │ │ ├── Picking Numbers.java │ │ ├── Picking Numbers.py │ │ ├── Sequence Equation.py │ │ ├── Service Lane.cpp │ │ ├── Sherlock And Array.cpp │ │ ├── Sherlock And Queries.cpp │ │ ├── Sherlock and Squares.py │ │ ├── Utopian Tree.py │ │ └── Viral Advertising.py │ ├── Multiple Choice │ │ ├── A Basic Quiz on Algorithms #1.txt │ │ └── A Basic Quiz on Algorithms #2.txt │ ├── NP Complete │ │ └── N Queens.cpp │ ├── Number Theory │ │ └── Equations.cpp │ ├── Regex │ │ ├── Detect HTML Tags.py │ │ ├── Detect the Domain Name.java │ │ ├── Detect the Domain Name.py │ │ ├── Detecting Valid Latitude and Longitude Pairs.py │ │ ├── Find HackerRank.cpp │ │ ├── HackerRank Tweets.cpp │ │ ├── IP Address Validation.py │ │ ├── Saying Hi.cpp │ │ ├── Split the Phone Numbers.cpp │ │ ├── The British and American Style of Spelling.cpp │ │ ├── UK and US - Part 2.py │ │ ├── Utopian Identification Number.py │ │ └── Valid PAN format.cpp │ ├── Search │ │ ├── Circle City.cpp │ │ ├── Coin On The Table.cpp │ │ ├── Cut The Tree.cpp │ │ ├── Encryption.cpp │ │ ├── Ice Cream Parlor.cpp │ │ ├── Lonely Integer.cpp │ │ └── Pairs.cpp │ ├── Sorting │ │ └── Insertion Sort - Part 2.py │ ├── Strings │ │ ├── Anagram.cpp │ │ ├── Bigger Is Greater.cpp │ │ ├── Common Child.cpp │ │ ├── Gem Stones.cpp │ │ ├── Palindrome Index.cpp │ │ ├── Pangrams.cpp │ │ ├── Strong Password.cpp │ │ ├── Strong Password.java │ │ └── Strong Password.py │ ├── Summation And Algebra │ │ ├── Summing the N series.cpp │ │ └── Triangle Numbers.cpp │ └── Warm Up │ │ ├── ACM ICPC Team.cpp │ │ ├── Alternating Characters.cpp │ │ ├── Angry Children.cpp │ │ ├── Angry Professor.cpp │ │ ├── Chocolate Feast.cpp │ │ ├── Count Luck.cpp │ │ ├── Filling Jars.cpp │ │ ├── Find Digits.cpp │ │ ├── Flipping Bits.cpp │ │ ├── Game of Thrones - I.cpp │ │ ├── Halloween Party.cpp │ │ ├── Is Fibo.cpp │ │ ├── Manasa And Stones.cpp │ │ ├── Maximizing XOR.cpp │ │ ├── Sherlock And GCD.cpp │ │ ├── Sherlock And The Beast.cpp │ │ ├── Sherlock and Squares.cpp │ │ ├── Taum and B'day.cpp │ │ ├── The Love-Letter Mystery.cpp │ │ └── Utopian Tree.cpp ├── Artificial Intelligence │ └── Multiple_Linear_Regression_Predicting_Housing_Prices.m ├── C++ │ ├── Inheritance │ │ ├── Accessing Inherited Functions.cpp │ │ ├── Inheritance Introduction.cpp │ │ ├── Multi Level Inheritance.cpp │ │ └── Rectangle Area.cpp │ ├── Introduction │ │ ├── Arrays Introduction.cpp │ │ ├── Basic Data Types.cpp │ │ ├── Conditional Statements.cpp │ │ ├── For Loop.cpp │ │ ├── Functions.cpp │ │ ├── Hello, World!.cpp │ │ ├── Input and Output.cpp │ │ ├── Operator Overloading.cpp │ │ ├── Pointer.cpp │ │ └── Variable Sized Arrays.cpp │ ├── Other Concepts │ │ └── C++ Class Template Specialization.cpp │ ├── STL │ │ ├── Deque-STL.cpp │ │ ├── Lower Bound-STL.cpp │ │ ├── Map-STL.cpp │ │ ├── Sets-STL.cpp │ │ ├── Vector-Erase.cpp │ │ └── Vector-Sort.cpp │ └── Strings │ │ ├── StringStream.cpp │ │ └── Strings.cpp ├── C │ ├── Arrays and Strings │ │ └── Digit Frequency.c │ ├── Functions │ │ └── Calculate the Nth term.c │ └── Introduction │ │ ├── Functions in C.c │ │ ├── Hello World! in C.c │ │ ├── Playing With Characters.c │ │ ├── Pointers in C.c │ │ └── Sum and Difference of Two Numbers.c ├── Data Structures │ ├── Arrays │ │ ├── 2D Array.py │ │ ├── Arrays.py │ │ ├── Dynamic Array.java │ │ ├── Dynamic Array.py │ │ └── Left Rotation.py │ ├── Disjoint Set │ │ ├── Components is a graph.py │ │ ├── Kundu and Tree.py │ │ ├── Maximum Cost Queries.py │ │ └── Merging Communities.py │ ├── Heap │ │ ├── Jesse and Cookies.py │ │ ├── QHEAP1.cpp │ │ ├── QHEAP1.java │ │ └── QHEAP1.py │ ├── Linked Lists │ │ ├── Compare two linked lists.cpp │ │ ├── Compare two linked lists.java │ │ ├── Compare two linked lists.py │ │ ├── Cycle Detection.cpp │ │ ├── Cycle Detection.java │ │ ├── Cycle Detection.py │ │ ├── Delete duplicate value nodes from a sorted linked list.cpp │ │ ├── Delete duplicate value nodes from a sorted linked list.java │ │ ├── Delete duplicate value nodes from a sorted linked list.py │ │ ├── Reverse a doubly linked list.cpp │ │ ├── Reverse a doubly linked list.java │ │ └── Reverse a doubly linked list.py │ └── Trie │ │ ├── Contacts.java │ │ ├── Contacts.py │ │ └── No Prefix Set.py ├── Interview Preparation Kit │ ├── Dictionaries and Hashmaps │ │ ├── Count Triplets.java │ │ ├── Count Triplets.py │ │ ├── Frequency Queries.py │ │ ├── Hash Tables - Ransom Note.java │ │ ├── Hash Tables - Ransom Note.py │ │ ├── Sherlock and Anagrams.java │ │ ├── Sherlock and Anagrams.py │ │ ├── Two Strings.java │ │ └── Two Strings.py │ ├── Dynamic Programming │ │ ├── Candies.cpp │ │ ├── Candies.java │ │ ├── Candies.py │ │ ├── Max Array Sum.java │ │ └── Max Array Sum.py │ ├── Greedy Algorithms │ │ ├── Greedy Florist.java │ │ ├── Greedy Florist.py │ │ ├── Luck Balance.java │ │ ├── Luck Balance.py │ │ ├── Max Min.java │ │ ├── Max Min.py │ │ ├── Minimum Absolute Difference in an Array.java │ │ └── Minimum Absolute Difference in an Array.py │ ├── Search │ │ ├── Hash Tables - Ice Cream Parlour.java │ │ ├── Hash Tables - Ice Cream Parlour.py │ │ ├── Minimum Time Required.py │ │ ├── Pairs.cpp │ │ ├── Pairs.java │ │ ├── Pairs.py │ │ ├── Triple Sum.java │ │ └── Triple Sum.py │ ├── Sorting │ │ ├── Mark and Toys.cpp │ │ ├── Mark and Toys.java │ │ ├── Mark and Toys.py │ │ ├── Sorting - Bubble Sort.java │ │ └── Sorting - Bubble Sort.py │ ├── Stacks and Queues │ │ ├── Balanced Bracket.java │ │ ├── Balanced Bracket.py │ │ ├── Castle on the Grid.py │ │ ├── Largest Rectangle.java │ │ ├── Largest Rectangle.py │ │ ├── Queues- A Tale of Two Stacks.java │ │ └── Queues- A Tale of Two Stacks.py │ ├── String Manipulation │ │ ├── Alternating Characters.java │ │ ├── Alternating Characters.py │ │ ├── Common Child.cpp │ │ ├── Common Child.java │ │ ├── Common Child.py │ │ ├── Sherlock and the Valid String.java │ │ ├── Sherlock and the Valid String.py │ │ ├── Strings-Making Anagrams.java │ │ └── Strings-Making Anagrams.py │ └── Trees │ │ ├── Tree - Height of a Binary Tree.cpp │ │ ├── Tree - Height of a Binary Tree.java │ │ └── Tree - Height of a Binary Tree.py ├── Java │ ├── B'day Gift.java │ ├── Breaking the Records.java │ ├── CamelCase.java │ ├── Can You Access.java │ ├── Cats and a Mouse.java │ ├── Compare the Triplets.java │ ├── Divisible Sum Pairs.java │ ├── Extra Long Factorials.java │ ├── Grading Students.java │ ├── Java Anagrams.java │ ├── Java Comparator.java │ ├── Java Currency Formatter.java │ ├── Java Date and Time.java │ ├── Java If-Else.java │ ├── Java List.java │ ├── Java Static Initializer.java │ ├── Java String Reverse.java │ ├── Java Subarray.java │ ├── Java Substring Comparisons.java │ ├── Pattern Syntax Checker.java │ ├── Plus Minus.java │ ├── Sequence Equation.java │ ├── Staircase.java │ ├── Utopian Tree.java │ ├── Valid Username Regular Expression.java │ ├── Viral Advertising.java │ └── Welcome to Java.java ├── Linux Shell │ ├── Bash │ │ ├── A Personalized Echo.bash │ │ ├── Arithmetic Operations.bash │ │ ├── Comparing Numbers.bash │ │ ├── Compute the Average.bash │ │ ├── Concatanate an array with itself.bash │ │ ├── Count the Number of Elements in an array.bash │ │ ├── Diaplay an Element of an array.bash │ │ ├── Filter an Array with Patterns.bash │ │ ├── Getting Started with Conditionals.bash │ │ ├── Let's Echo.bash │ │ ├── Looping and Skipping.bash │ │ ├── Looping with Numbers.bash │ │ ├── More on Conditionals.bash │ │ ├── Read in an Array.bash │ │ ├── Remove the first capital letter from each array element.bash │ │ ├── Slice an Array.bash │ │ └── The World of Numbers.bash │ └── Text Processing │ │ ├── 'Grep' #1.bash │ │ ├── 'Grep' #2.bash │ │ ├── 'Grep' #3.bash │ │ ├── 'Grep' #4.bash │ │ ├── 'Sed' command #1.bash │ │ ├── 'Sed' command #2.bash │ │ ├── 'Sed' command #3.bash │ │ ├── 'Tr' Command #1.bash │ │ ├── 'Tr' Command #2.bash │ │ ├── 'Tr' Command #3.bash │ │ ├── 'Uniq' command #1.bash │ │ ├── 'Uniq' command #2.bash │ │ ├── 'Uniq' command #3.bash │ │ ├── 'Uniq' command #4.bash │ │ ├── Cut #1.bash │ │ ├── Cut #2.bash │ │ ├── Cut #3.bash │ │ ├── Cut #4.bash │ │ ├── Cut #5.bash │ │ ├── Cut #6.bash │ │ ├── Cut #7.bash │ │ ├── Cut #8.bash │ │ ├── Cut #9.bash │ │ ├── Head of a Text File #1.bash │ │ ├── Head of a Text File #2.bash │ │ ├── Middle of a Text File.bash │ │ ├── Sort Command #1.bash │ │ ├── Sort Command #2.bash │ │ ├── Sort Command #3.bash │ │ ├── Sort Command #4.bash │ │ ├── Sort Command #5.bash │ │ ├── Sort Command #7.bash │ │ ├── Sort command #6.bash │ │ ├── Tail of a Text File #1.bash │ │ └── Tail of a Text File #2.bash ├── Mathematics │ ├── Fundamentals │ │ ├── Find the Point.cpp │ │ ├── Find the Point.java │ │ ├── Find the Point.js │ │ ├── Find the Point.py │ │ └── Summing the N series.py │ └── Number Theory │ │ └── Closest Number.py ├── Python │ ├── Algorithms │ │ ├── B'day Gift.py │ │ ├── Breaking the Records.py │ │ ├── Cats and a Mouse.py │ │ ├── Compare the Triplets.py │ │ ├── Divisible Sum Pair.py │ │ ├── Grading Students.py │ │ ├── Plus Minus.py │ │ ├── Sequence Equation.py │ │ ├── Staircase.py │ │ ├── Utopian Tree.py │ │ └── Viral Advertising.py │ ├── Basic Data Types │ │ ├── Lists.py │ │ └── Nested Lists.py │ ├── Built-Ins │ │ └── Any or All.py │ ├── Collections │ │ ├── Collections.OrderedDict.py │ │ ├── Collections.deque().py │ │ ├── Collections.namedtuple().py │ │ ├── Company Logo.py │ │ ├── Counter.py │ │ ├── DefaultDict Tutorial.py │ │ └── Word Order.py │ ├── Data Types │ │ ├── Find the Second Largest Number.txt │ │ ├── List Comprehensions.txt │ │ ├── Lists.txt │ │ ├── Nested Lists.txt │ │ ├── Sets - Symmetric Difference.txt │ │ └── Tuples.txt │ ├── Date and Time │ │ ├── Calendar Module.txt │ │ └── Time Delta.txt │ ├── Errors and Exceptions │ │ └── Incorrect Regex.py │ ├── Introduction │ │ ├── Arithmetic Operators.txt │ │ ├── Finding the percentage.txt │ │ ├── Hello World!.txt │ │ ├── Integers Come In All Sizes.txt │ │ ├── Interchange two numbers.txt │ │ ├── Loops.txt │ │ ├── Mod Divmod.txt │ │ ├── Power - Mod Power.txt │ │ ├── Print Function.txt │ │ ├── Python Division.txt │ │ ├── Raw Input.txt │ │ └── What's Your Name.txt │ ├── Itertools │ │ ├── itertools.combinations(iterable, r).py │ │ └── itertools.product().txt │ ├── Math │ │ ├── Find Angle MBC.txt │ │ ├── Polar Coordinates.txt │ │ ├── Triangle Quest 2.txt │ │ └── Triangle Quest.txt │ ├── Numpy │ │ └── Concatenate.py │ ├── Regex and Parsing │ │ └── Group(), Groups() and Groupdict().py │ ├── Sets │ │ ├── Check Strict Superset.txt │ │ ├── Check Subset.txt │ │ ├── Introduction to Sets.txt │ │ ├── No Idea!.txt │ │ ├── Set .discard(), .remove() & .pop().txt │ │ ├── Set Mutations.txt │ │ ├── Set.add().txt │ │ ├── Set.difference().txt │ │ ├── Set.intersection().txt │ │ ├── Set.symmetric_difference().txt │ │ ├── Set.union().txt │ │ └── The Captian's Room.txt │ └── Strings │ │ ├── Alphabet Rangoli.txt │ │ ├── Capitalize!.txt │ │ ├── Designer Door Mat.txt │ │ ├── Find a string.py │ │ ├── Find a string.txt │ │ ├── Mutations.txt │ │ ├── String Formatting.txt │ │ ├── String Split and Case.txt │ │ ├── String Validators.txt │ │ ├── Text Alignment.txt │ │ ├── Text Wrap.txt │ │ ├── The Minion Game.txt │ │ └── sWAP cASE.txt ├── SQL │ ├── Advanced Join │ │ └── Projects.txt │ ├── Advanced Select │ │ └── The PADS.sql │ ├── Aggregation │ │ ├── Average Population.txt │ │ ├── Japan Population.txt │ │ ├── Population Density Difference.txt │ │ ├── Revising Aggregations - Averages.txt │ │ ├── Revising Aggregations - The Count Function.txt │ │ ├── Revising Aggregations - The Sum Function.txt │ │ ├── The Blunder.txt │ │ ├── Weather Observation Station 13.txt │ │ ├── Weather Observation Station 14.txt │ │ ├── Weather Observation Station 15.txt │ │ ├── Weather Observation Station 16.txt │ │ ├── Weather Observation Station 17.txt │ │ ├── Weather Observation Station 18.txt │ │ ├── Weather Observation Station 19.txt │ │ ├── Weather Observation Station 2.txt │ │ └── Weather Observation Station 20.txt │ ├── Alternative Queries │ │ ├── Draw The Triangle 1.sql │ │ └── Draw The Triangle 2.sql │ ├── Basic Join │ │ ├── African Cities.txt │ │ ├── Asian Population.txt │ │ ├── Average Population of Each Continent.txt │ │ └── The Report.txt │ └── Basic Select │ │ ├── Higher Than 75 Marks.txt │ │ ├── Japanese Cities' Detail.txt │ │ ├── Japanese Cities' Names.txt │ │ ├── Revising the Select Query - 1.txt │ │ ├── Revising the Select Query - 2.txt │ │ ├── Select All.txt │ │ ├── Select by ID.txt │ │ ├── Weather Observation Station 1.txt │ │ ├── Weather Observation Station 10.txt │ │ ├── Weather Observation Station 11.txt │ │ ├── Weather Observation Station 12.txt │ │ ├── Weather Observation Station 3.txt │ │ ├── Weather Observation Station 4.txt │ │ ├── Weather Observation Station 5.txt │ │ ├── Weather Observation Station 6.txt │ │ ├── Weather Observation Station 7.txt │ │ ├── Weather Observation Station 8.txt │ │ └── Weather Observation Station 9.txt ├── Security │ ├── Functions │ │ ├── Security Bijective Functions.txt │ │ ├── Security Function Inverse.txt │ │ ├── Security Functions II.txt │ │ ├── Security Functions.txt │ │ ├── Security Involution.txt │ │ └── Security Permutations.txt │ └── Terminology and Concepts │ │ ├── Security - Message Space and Ciphertext Space.txt │ │ ├── Security Encryption Scheme.txt │ │ └── Security Key Spaces.txt └── Tutorials │ ├── 10 Days of Javascript │ ├── Day 0 - Data Types.js │ ├── Day 0 - Hello, World!.js │ ├── Day 1 - Arithmetic Operators.js │ ├── Day 1 - Functions.js │ ├── Day 1 - Let and Const.js │ ├── Day 2 - Conditional Statements - If-Else.js │ ├── Day 2 - Conditional Statements - Switch.js │ ├── Day 2 - Loops.js │ ├── Day 3 - Arrays.js │ ├── Day 3 - Throw.js │ ├── Day 3 - Try, Catch, and Finally.js │ ├── Day 4 - Classes.js │ ├── Day 4 - Count Objects.js │ ├── Day 4 - Create a Rectangle Object.js │ ├── Day 5 - Arrow Functions.js │ ├── Day 5 - Inheritance.js │ ├── Day 5 - Template Literals.js │ ├── Day 6 - Bitwise Operators.js │ ├── Day 6 - JavaScript Dates.js │ ├── Day 7 - Regular Expressions I.js │ ├── Day 7 - Regular Expressions II.js │ ├── Day 7 - Regular Expressions III.js │ ├── Day 8 - Buttons Container │ │ ├── css │ │ │ └── buttonsGrid.css │ │ ├── index.html │ │ └── js │ │ │ └── buttonsGrid.js │ ├── Day 8 - Create a Button │ │ ├── css │ │ │ └── button.css │ │ ├── index.html │ │ └── js │ │ │ └── button.js │ └── Day 9 - Binary Calculator │ │ ├── css │ │ └── binaryCalculator.css │ │ ├── index.html │ │ └── js │ │ └── binaryCalculator.js │ ├── 10 Days of Statistics │ ├── Day 0 Mean Median and Mode.py │ ├── Day 0 Weighted Mean.py │ ├── Day 1 Interquartile Range.py │ ├── Day 1 Quartiles.py │ ├── Day 1 Standard Deviation.py │ ├── Day 2 Basic Probability.txt │ ├── Day 2 Compound Event Probability.txt │ └── Day 2 More Dice.txt │ └── 30 Days of Code │ ├── Day 0 Hello World.py │ ├── Day 1 Data Types.py │ ├── Day 10 Binary Numbers.py │ ├── Day 11 2D-Arrays.py │ ├── Day 12 Inheritance.py │ ├── Day 13 Abstract Classes.py │ ├── Day 14 Scope.py │ ├── Day 15 Linked List.py │ ├── Day 16 Exceptions-String to Integer.py │ ├── Day 17 More Exceptions.py │ ├── Day 18 Queues and Stacks.py │ ├── Day 19 Interfaces.java │ ├── Day 2 Operators.py │ ├── Day 20 Sorting.py │ ├── Day 21 Generics.cpp │ ├── Day 22 Binary Search Tree.cpp │ ├── Day 23 BST Level-Order Traversal.cpp │ ├── Day 24 More Linked Lists.cpp │ ├── Day 25 Running Time and Complexity.py │ ├── Day 26 Nested Logic.py │ ├── Day 27 Testing.py │ ├── Day 28 RegEx, Patterns and Intro to Databases.py │ ├── Day 29 Bitwise AND.py │ ├── Day 3 Intro to Conditionals.py │ ├── Day 4 Class vs. Instance.py │ ├── Day 5 Loops.py │ ├── Day 6 Let's Review.py │ ├── Day 7 Arrays.py │ ├── Day 8 Dictionaries and Maps.cpp │ └── Day 9 Recursion.py ├── LICENSE ├── LeetCode └── README.md ├── Minimum Subsequence in Non-Increasing Order.py ├── Model Algorithms ├── Advanced Data Structure │ ├── AVL Tree.cpp │ ├── Splay Tree.cpp │ ├── Trie.cpp │ └── XOR Linked List.cpp ├── Arrays │ └── Given an array A[] and a number x, check for pair in A[] with sum as x │ │ ├── Method 1(Using Sorting).cpp │ │ └── Method 2(Using Hash Map).cpp ├── Backtracking │ ├── Graph Coloring.cpp │ ├── Hamiltonian Cycle.cpp │ ├── Knight's Tour Problem.cpp │ ├── N Queens.cpp │ ├── Print All Permutations Of A Given String.cpp │ ├── Rat In A Maze.cpp │ └── Sudoku.cpp ├── Binary Search Tree │ ├── Add greater values to every node of BST.cpp │ ├── Ceil from a BST.cpp │ ├── Check every node of BST has only one child.cpp │ ├── Compare if two BSTs are same.cpp │ ├── Construct BST from PreOrder Traversal.cpp │ ├── Correct BST having two interchanged node.cpp │ ├── Find Triplet in BST.cpp │ ├── Print BST keys in the given range.cpp │ ├── Remove nodes outside given range.cpp │ ├── Sorted Array to Balanced BST.cpp │ └── Sorted Linked List to BST.cpp ├── Binary Tree │ ├── Binary Tree Introduction, Traversals and Tree Size.cpp │ ├── Build Tree From Inorder and Preorder Traversal.cpp │ ├── Check Height Balanced.cpp │ ├── Check if Sum Tree.cpp │ ├── Children Sum Property.cpp │ ├── Connect Nodes at Same Level Iterative.cpp │ ├── Connect Nodes at Same Level.cpp │ ├── Convert to Children Sum Property.cpp │ ├── Convert to Sum tree.cpp │ ├── Count Leaf Nodes.cpp │ ├── Delete Tree.cpp │ ├── Diameter of Tree.cpp │ ├── Double Tree.cpp │ ├── Foldable Binary Trees 01.cpp │ ├── Get Level of Node.cpp │ ├── Identical Trees.cpp │ ├── Inorder Traversal Using Stack.cpp │ ├── Level Order Spiral Tree Traversal.cpp │ ├── Level Order Tree Traversal.cpp │ ├── Maximum Width Of Tree.cpp │ ├── Mirror Tree.cpp │ ├── Morris Traversal(Inoreder Traversal Without Using Recurrsion or Stack).cpp │ ├── Path Having a given sum of Nodes.cpp │ ├── Populate Next Pointers.cpp │ ├── Print Ancestors of Node.cpp │ ├── Print K Distant Nodes.cpp │ ├── Print Paths.cpp │ ├── Sub Tree of a Tree.cpp │ └── Tree Depth.cpp ├── Divide And Conquer │ ├── Matrix Multiplication │ │ └── Matrix Multiplication - Naive Method.cpp │ ├── Maximum Subarray │ │ ├── Maximum Subarray Problem - Brute Force.cpp │ │ └── Maximum Subarray Problem - Divide And Conquer.cpp │ └── Number of Strings of Matched Parenthesis │ │ ├── Count Matched Parenthesis - Brute Force.cpp │ │ └── Count Matched Parenthesis - Memoized Solution.cpp ├── Dynamic Programming │ ├── 0-1 Knapsack.cpp │ ├── Binomial Coefficient.cpp │ ├── Box Stacking Problem.cpp │ ├── Coin Change.cpp │ ├── Edit Distance.cpp │ ├── Edit Distance │ │ ├── Edit Distance - Bottom Up.cpp │ │ └── Edit Distance - Recursion.cpp │ ├── Egg Dropping Puzzle.cpp │ ├── Fibonacci Numbers Optimized Power of Matrix.cpp │ ├── Fibonacci Numbers Space Optimized.cpp │ ├── Fibonacci Numbers │ │ ├── Fibonacci Using Iteration.cpp │ │ └── Fibonacci Using Recursion.cpp │ ├── Floyd Warshall.cpp │ ├── Knapsack │ │ ├── 0-1 KnapSack.cpp │ │ └── Unbounded KnapSack.cpp │ ├── Longest Bitonic Subsequence.cpp │ ├── Longest Common Subsequence.cpp │ ├── Longest Increasing Subsequence.cpp │ ├── Longest Increasing Subsequence │ │ ├── Longest Increasing Subsequence O(nlog(n)).cpp │ │ └── The Longest Increasing Subsequence O(n^2).cpp │ ├── Longest Palindromic Subsequence.cpp │ ├── Longest Sum Increasing Subsequence.cpp │ ├── Matrix Chain Multiplication Order.cpp │ ├── Matrix Chain Multiplication Order │ │ └── Matrix Chain Multiplication Order - Bottom Up.cpp │ ├── Minimum Cost Path.cpp │ ├── Minimum Number Of Jumps To Reach End.cpp │ ├── Palindrome Partitioning.cpp │ ├── Rod Cutting.cpp │ ├── Rod Cutting │ │ └── Rod Cutting.cpp │ ├── Sequence Allignment │ │ ├── Sequence Allignment - Bottom Up.cpp │ │ └── Sequence Allignment - Recursion.cpp │ └── Set Partitioning Problem.cpp ├── Graphs │ ├── Articulation Points.cpp │ ├── Bellman Ford Shortest Path Algorithm (for negative weight edges).cpp │ ├── Biconnected Graphs.cpp │ ├── Bipartite Graph.cpp │ ├── Boruvka's Algorithm for Minimum Spanning Tree.cpp │ ├── Breadth First Search.cpp │ ├── Bridges In A Graph.cpp │ ├── Channel Assignment Problem.cpp │ ├── Check a given Graph is Tree or Not.cpp │ ├── Check whether a Path Exists between two vertices in a Directed Graph.cpp │ ├── Count All Possible Walks from a Source to a Destination with exactly k edges.cpp │ ├── Depth First Search.cpp │ ├── Detect Cycle in a Directed Graph.cpp │ ├── Detect Cycle in an Undirected Graph using DFS.cpp │ ├── Detect Cycle in an Undirected Graph using Union Find.cpp │ ├── Dijkshtra's Shortest Path Algorithm.cpp │ ├── Euler Circuit in Directed Graph.cpp │ ├── Eulerian Path and Circuit in Undirected Graph.cpp │ ├── Find Number Of Islands.cpp │ ├── Find Whether a Graph is Strongly Connected or Not.cpp │ ├── Ford Fulkerson Algorithm for Maximum Flow Problem.cpp │ ├── Longest Path In a Directed Graph.cpp │ ├── Maximum Bipartite Matching.cpp │ ├── Maximum Number of Edge Disjoint Paths Between Two Vertices.cpp │ ├── Minimum S-T cut in a Flow Network.cpp │ ├── Representing Graphs.cpp │ ├── Shortest Path from Source to Destination with exactly k edges.cpp │ ├── Single Source Shortest Path in Directed Acyclic Graphs.cpp │ ├── Snakes and Ladders Problem.cpp │ ├── Strongly Connected Components.cpp │ ├── Topological Sort.cpp │ ├── Topological Sorting.cpp │ └── Transitive Closure of a Graph.cpp ├── Greedy Algorithms │ ├── Activity Selection Problem.cpp │ ├── Dijkstra's Shortest Path Algorithm.cpp │ ├── Huffman Encoding.cpp │ ├── Job Sequencing.cpp │ ├── Kruskals Minimum Spanning Tree.cpp │ ├── Minimum Number of Coins.cpp │ ├── Prims Minimum Spanning Tree for Adjacency List.cpp │ └── Prims Minimum Spanning Tree for Adjacency Matrix.cpp ├── Heap │ └── Heap.cpp ├── Huffman Encoding │ └── Huffman Encoding.cpp ├── Mathematical Algorithms │ ├── Adding Two Numbers in Base 14.cpp │ ├── Babylonian Method for Square Root.cpp │ ├── Multiple Of Three.cpp │ ├── Multiplication Without Using Multiplication, Division Or Bitwise Operators and No Loops.cpp │ └── Multiply By Seven.cpp ├── Minimum Spanning Tree │ ├── Kruskals Minimum Spanning Tree Algorithm.cpp │ ├── Prims MST for Adjacency List Representation.cpp │ └── Prims MST for Adjacency Matrix Representation.cpp ├── Pattern Searching │ ├── KMP Pattern Searching.cpp │ ├── Naive Pattern Searching for Special Pattern.cpp │ ├── Naive Pattern Searching.cpp │ └── Rabin Karp Pattern Searching.cpp ├── Sorting │ ├── BubbleSort.cpp │ ├── BucketSort.cpp │ ├── InsertionSort.cpp │ ├── MergeSort.cpp │ ├── QuickSort.cpp │ └── SelectionSort.cpp └── Statistics │ └── Selection.cpp ├── OS Tutorial Practice Codes ├── DataFile.txt ├── DataFile2.txt ├── DataFile3.txt ├── Tut03Code-1(16).c ├── Tut03Code-2(28).c ├── Tut03Code-3(30).c ├── Tut03Code-4(34).c ├── Tut03Code-5(37).c ├── Tut04Codes-01(11).c ├── Tut06Codes-01(37).c ├── Tut07Codes-01(17).c ├── Tut07Codes-02(20).c ├── Tut07Codes-03(22).c ├── Tut08Codes-01(11).c └── Untitled1.c ├── README.md ├── SPOJ Solutions ├── 1025. Fashion Shows C Code.c ├── 1025. Fashion Shows C++ Code.cpp ├── 1112. Number Steps.c ├── 1401 Factorial Problem.c ├── 2123. Candy I C Code.c ├── 2123. Candy I C++ Code.cpp ├── 2148. Candy III C Code.c ├── 3442. The last digit.c ├── 346. Bytelandian gold coins C Code.c ├── 346. Bytelandian gold coins C++ Code.cpp ├── 400. To and Fro.c ├── 42. Adding Reversed Numbers.c ├── 4300. Rectangles..c ├── 54. Julka.c ├── 7974. What’s Next.c ├── ABCDEF.cpp ├── ABSP1_Solution.c ├── ACPC10D.c ├── ACTIV.cpp ├── AFS.c ├── AGGRCOW.c ├── AKVQLD03.cpp ├── AMSCO1.cpp ├── AMSCO2.cpp ├── AMZSEQ.c ├── APPROB.py ├── APS.c ├── ASCDFIB.cpp ├── BLOPER.cpp ├── BOGGLE.cpp ├── BYTEFOOD.cpp ├── BYTESE2.cpp ├── C1LJUTNJ.cpp ├── CADYDIST.cpp ├── CISTFILL.cpp ├── CMIYC.cpp ├── CPTTRN1.cpp ├── CPTTRN2.cpp ├── CPTTRN3.cpp ├── CPTTRN4.cpp ├── CPTTRN5.cpp ├── CRAN01.cpp ├── CRSCNTRY.cpp ├── CUBEROO2.cpp ├── Circle Problem.c ├── DCEPC11B.cpp ├── DCEPC701.cpp ├── DDATE.cpp ├── DEFKIN.cpp ├── DIVISION.cpp ├── FCTRL2.c ├── FERT21_0.cpp ├── FISHER.cpp ├── FPOLICE.cpp ├── FRND.cpp ├── GP1.cpp ├── GSS1.cpp ├── GSS2.cpp ├── GSS3.cpp ├── GSSQUNCE.cpp ├── HABLU.cpp ├── HG.cpp ├── HS12MBR.cpp ├── ICPC12C.cpp ├── IITKWPCF.cpp ├── IITKWPCO.cpp ├── IITWPC4D.cpp ├── INS14J.cpp ├── JOHNNY.cpp ├── JOKER1.cpp ├── KAMIL.c ├── KGSS.cpp ├── KOPC12A.cpp ├── LARSUBP.cpp ├── LOOPEXP.cpp ├── MFISH.cpp ├── NBIN.cpp ├── NDIV.cpp ├── PAML.cpp ├── PIE.c ├── PIVAL.c ├── POTIONS.py ├── PSPHERE.cpp ├── PT07Z.cpp ├── Prime Number Generator.c ├── QN01.cpp ├── RMID.cpp ├── RROOT.cpp ├── SICRANO.cpp ├── SIRNUMS.cpp ├── SIZECON.c ├── SMPCIRC.cpp ├── SMPCPH1.cpp ├── SMPDIV.cpp ├── SMPSEQ3.cpp ├── SMPSEQ4.cpp ├── SMPSEQ5.cpp ├── SMPSEQ6.cpp ├── SMPSUM.cpp ├── SMPWOW.cpp ├── SPCJ.cpp ├── STPAR.cpp ├── STRHH.cpp ├── SUBXOR.cpp ├── TESTINT.cpp ├── VENOM.cpp ├── WAYS.c ├── WPC5I.cpp └── mkbudget.cpp └── Stanford Competetive Programming ├── 01Intro01.cpp ├── 01Intro02.cpp ├── 04DP01.cpp ├── 04DP03(Longest Common Subsequence).cpp ├── 04DP04(Convert to Palindrome).cpp ├── 06BGA(BFS).cpp ├── 06BGA(DFS).cpp ├── 06BGA(Kruskals Minimum Spanning Tree Algorithm).cpp ├── 06BGA(Prims MST for Adjacency List Representation).cpp ├── 06BGA(Prims MST for Adjacency Matrix Representation).cpp ├── 06BGA(Representing Graphs).cpp ├── 06BGA(Strongly Connected Components).cpp ├── 06BGA(Topological Sort).cpp └── TDBFS.cpp /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must end with two \r 29 | Icon 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 280 (Div 2)/Vanya and Cubes.cpp: -------------------------------------------------------------------------------- 1 | /* Author: Utkarsh Pathrabe 2 | * Question can be found at http://codeforces.com/contest/492/problem/A 3 | * Algorithms: Implementation Based 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main(void) { 11 | int n, height = 0, i = 1; 12 | scanf("%d", &n); 13 | while(n >= ((i * (i + 1)) / 2)) { 14 | n -= ((i * (i + 1)) / 2); 15 | i += 1; 16 | height += 1; 17 | } 18 | printf("%d\n", height); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 281 (Div 2)/Vasya and Chess.cpp: -------------------------------------------------------------------------------- 1 | /* Author: Utkarsh Pathrabe 2 | * Question can be found at http://codeforces.com/contest/493/problem/D 3 | * Algorithms: Constructive Algorithms, Games 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main(void) { 11 | long long int n; 12 | cin >> n; 13 | if((n % 2) == 1) 14 | printf("black\n"); 15 | else{ 16 | printf("white\n1 2\n"); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 282 (Div 2)/02 Modular Equations.cpp: -------------------------------------------------------------------------------- 1 | /* Author: Utkarsh Pathrabe 2 | * Question can be found at http://codeforces.com/contest/495/problem/B 3 | * Algorithms: Math 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main (void) { 11 | long long int a, b, n, count = 0, i; 12 | cin >> a >> b; 13 | n = a - b; 14 | if(n == 0) { 15 | cout << "infinity" << endl; 16 | } else if (n < 0) { 17 | cout << "0" << endl; 18 | } else { 19 | for (i = 1; i < sqrt(n); i++) { 20 | if ((n % i) == 0) { 21 | if (i > b) 22 | count += 1; 23 | if ((n / i) > b) 24 | count += 1; 25 | } 26 | } 27 | if (((i * i) == n) && (i > b)) 28 | count += 1; 29 | cout << count << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 320 (Div 2)/A Problem About Polyline.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define LLI long long int 3 | 4 | using namespace std; 5 | 6 | int main (void) { 7 | LLI a, b; 8 | cin >> a >> b; 9 | if (b > a) { 10 | cout << "-1" << endl; 11 | } else if (b == a) { 12 | cout << a << endl; 13 | } else { 14 | double u, v; 15 | LLI x, y; 16 | u = (a + b) / 2.0; 17 | v = (a - b) / 2.0; 18 | x = (a + b) / (2 * b); 19 | y = (a - b) / (2 * b); 20 | u = u / (double)x; 21 | v = v / (double)y; 22 | printf ("%.12lf\n", min (u, v)); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 320 (Div 2)/Or Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long int LLI; 6 | 7 | LLI a[200005] = {0}, pre[200005] = {0}, suf[200005] = {0}; 8 | 9 | int main (void) { 10 | int n, k, x; 11 | cin >> n >> k >> x; 12 | LLI prod = 1, ans = 0; 13 | for (int i = 0; i < k; i++) { 14 | prod *= x; 15 | } 16 | for (int i = 1; i <= n; i++) { 17 | cin >> a[i]; 18 | pre[i] = pre[i-1] | a[i]; 19 | } 20 | for (int i = n; i > 0; i--) { 21 | suf[i] = suf[i+1] | a[i]; 22 | } 23 | for (int i = 1; i <= n; i++) { 24 | ans = max (ans, (pre[i-1] | (a[i] * prod) | suf[i+1])); 25 | } 26 | cout << ans << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 320 (Div 2)/Raising Bacteria.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define LLI long long int 3 | 4 | using namespace std; 5 | 6 | int main (void) { 7 | LLI x, count = 0; 8 | cin >> x; 9 | while (x) { 10 | if (x & 1) { 11 | count++; 12 | } 13 | x = x >> 1; 14 | } 15 | cout << count << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 321 (Div 2)/Kefa And First Steps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n; 7 | vector arr; 8 | cin >> n; 9 | for (int i = 0; i < n; i++) { 10 | long long int temp; 11 | cin >> temp; 12 | arr.push_back(temp); 13 | } 14 | int maxi = 1, temp = 1; 15 | for (int i = 1; i < n; i++) { 16 | if (arr[i] >= arr[i-1]) { 17 | temp++; 18 | } else { 19 | temp = 1; 20 | } 21 | maxi = max(temp, maxi); 22 | } 23 | cout << maxi << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 321 (Div 2)/Kefa and Watch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define maxn 100010 3 | 4 | using namespace std; 5 | 6 | int main (void) { 7 | int n, m, k; 8 | char s[maxn]; 9 | int op, l, r, val; 10 | memset(s, '\0', sizeof(s)); 11 | scanf("%d %d %d", &n, &m, &k); 12 | scanf("%s", &s); 13 | for(int i = 0; i < m + k; i++) { 14 | scanf("%d %d %d %d", &op, &l, &r, &val); 15 | --l; 16 | --r; 17 | if(op == 1) 18 | memset(s + l, val + '0', r - l + 1); 19 | else 20 | printf(!memcmp(s + l, s + l + val, r - l + 1 - val) ? "YES\n" : "NO\n"); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 324 (Div 2)/Kolya And Tanya.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const long long int mod = 1000000007; 6 | 7 | long long int pow (int x, int y) { 8 | if (y == 0) { 9 | return 1; 10 | } 11 | long long int s = 1, temp = 1; 12 | if (y % 2 == 1) { 13 | s = x; 14 | } 15 | temp = (pow(x, y/2))%mod; 16 | return (temp*temp*s)%mod; 17 | } 18 | 19 | int main (void) { 20 | int n; 21 | cin >> n; 22 | cout << (pow(3, 3*n) - pow(7, n)%mod + mod)%mod << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 324 (Div 2)/Olesya and Rodion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, t; 7 | cin >> n >> t; 8 | if ((n == 1) && (t == 10)) { 9 | cout << "-1" << endl; 10 | } else if ((n > 1) && (t == 10)) { 11 | for (int i = 1; i < n; i++) { 12 | cout << "1"; 13 | } 14 | cout << "0" << endl; 15 | } else { 16 | for (int i = 0; i < n; i++) { 17 | cout << t; 18 | } 19 | cout << endl; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Codeforces/Codeforces Round 325 (Div 2)/Laurenty and Shop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, a[2][50] = {0}, b[50] = {0}; 7 | vector sums; 8 | cin >> n; 9 | for (int i = 0; i < 2; i++) { 10 | for (int j = 0; j < n-1; j++) { 11 | cin >> a[i][j]; 12 | } 13 | } 14 | for (int i = 0; i < n; i++) { 15 | cin >> b[i]; 16 | } 17 | for (int i = 0; i < n; i++) { 18 | int sum = b[i]; 19 | for (int j = 0; j < n-1; j++) { 20 | if (j < i) { 21 | sum += a[0][j]; 22 | } else { 23 | sum += a[1][j]; 24 | } 25 | } 26 | sums.push_back(sum); 27 | } 28 | sort(sums.begin(), sums.end()); 29 | cout << sums[0] + sums[1] << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Cracking the Coding Interview/Chapter 01/Question02.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UtkarshPathrabe/Competitive-Coding/ae26d3c35d18312dd606cab6750e9e072c477cc1/Cracking the Coding Interview/Chapter 01/Question02.cpp -------------------------------------------------------------------------------- /Cracking the Coding Interview/Chapter 01/Question05.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UtkarshPathrabe/Competitive-Coding/ae26d3c35d18312dd606cab6750e9e072c477cc1/Cracking the Coding Interview/Chapter 01/Question05.cpp -------------------------------------------------------------------------------- /Edge Verve Coding Round/Question 3 - Factorial of Large Numbers.py: -------------------------------------------------------------------------------- 1 | def factorial(number): 2 | temp = 1 3 | for i in range(1, number + 1): 4 | temp = temp * i 5 | return temp 6 | 7 | number = raw_input("Enter a number:") 8 | 9 | fact = factorial(int(number)) 10 | 11 | print fact 12 | -------------------------------------------------------------------------------- /GeeksForGeeks/Finding the numbers.py: -------------------------------------------------------------------------------- 1 | T = int(input()) 2 | for _ in range(T): 3 | N = int(input()) 4 | A = list(map(int, input().strip().split())) 5 | XOR = A[0] 6 | for i in range(1, N*2 + 2): 7 | XOR = XOR ^ A[i] 8 | rightmostSetBit = XOR & ~(XOR-1) 9 | X, Y = 0, 0 10 | for i in range(2*N + 2): 11 | if (A[i] & rightmostSetBit): 12 | X = X ^ A[i] 13 | else: 14 | Y = Y ^ A[i] 15 | print(min(X, Y), max(X, Y)) -------------------------------------------------------------------------------- /GeeksForGeeks/Maximum Index.py: -------------------------------------------------------------------------------- 1 | T = int(input()) 2 | for _ in range(T): 3 | N = int(input()) 4 | A = list(map(int, input().strip().split())) 5 | minFromLeft = [0] * N 6 | maxFromRight = [0] * N 7 | minFromLeft[0] = A[0] 8 | for i in range(1, N): 9 | minFromLeft[i] = min(A[i], minFromLeft[i-1]) 10 | maxFromRight[N-1] = A[N-1] 11 | for i in range(N-2, -1, -1): 12 | maxFromRight[i] = max(A[i], maxFromRight[i+1]) 13 | diff = 0 14 | i, j = 0, 0 15 | while i < N and j < N: 16 | if minFromLeft[i] <= maxFromRight[j]: 17 | diff = max(diff, j - i) 18 | j += 1 19 | else: 20 | i += 1 21 | print(diff) -------------------------------------------------------------------------------- /GeeksForGeeks/Subarray with given sum.py: -------------------------------------------------------------------------------- 1 | T = int(input()) 2 | for _ in range(T): 3 | N, S = list(map(int, input().strip().split())) 4 | A = list(map(int, input().strip().split())) 5 | start, end = 0, 0 6 | currentSum = A[start] 7 | foundSum = False 8 | while end < N and start < N: 9 | if currentSum == S: 10 | foundSum = True 11 | break 12 | elif currentSum < S: 13 | end += 1 14 | if (end < N): 15 | currentSum += A[end] 16 | elif currentSum > S: 17 | if (start < N): 18 | currentSum -= A[start] 19 | start += 1 20 | if foundSum: 21 | print(start+1, end+1) 22 | else: 23 | print(-1) -------------------------------------------------------------------------------- /Google APAC/2016 APAC Test Practice Round/Problem B Captain Hammer/Captain Hammer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const double pi = atan (1.0) * 4; 6 | 7 | int main (void) { 8 | ifstream cin; 9 | cin.open("B-small-practice.in"); 10 | FILE *fp; 11 | fp = fopen ("B-small-practice.out", "w"); 12 | int T; 13 | cin >> T; 14 | for (int t = 1; t <= T; t++) { 15 | double V, D; 16 | cin >> V >> D; 17 | double tmp = D * 9.8 / (V * V); 18 | if (tmp > 1) { 19 | tmp = 1; 20 | } else if (tmp < 0) { 21 | tmp = 0; 22 | } 23 | double ans = asin (tmp) * 180.0 / pi / 2.0; 24 | fprintf(fp, "Case #%d: %.6f\n", t, ans); 25 | } 26 | cin.close(); 27 | fclose(fp); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Google APAC/2016 APAC Test Practice Round/Problem C Moist/Moist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | ofstream cout; 7 | ifstream cin; 8 | cout.open("C-small-practice-2.out"); 9 | cin.open("C-small-practice-2.in"); 10 | int T; 11 | cin >> T; 12 | for (int t = 1; t <= T; t++) { 13 | int N, cost = 0; 14 | string last = " ", buf; 15 | cin >> N; 16 | getline (cin, buf); 17 | for (int i = 0; i < N; i++) { 18 | string str; 19 | getline (cin, str); 20 | if (str < last) { 21 | cost++; 22 | } else { 23 | last = str; 24 | } 25 | } 26 | cout << "Case #" << t << ": " << cost << endl; 27 | } 28 | cout.close(); 29 | cin.close(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Google APAC/2016 APAC Test Round A/Problem C g Campus/C-small-practice.out: -------------------------------------------------------------------------------- 1 | Case #1: 2 | Case #2: 3 | 77 4 | Case #3: 5 | 7 6 | Case #4: 7 | 7 8 | Case #5: 9 | Case #6: 10 | Case #7: 11 | Case #8: 12 | Case #9: 13 | 45 14 | Case #10: 15 | 83 16 | -------------------------------------------------------------------------------- /Google APAC/2016 APAC Test Round A/Problem D g Snake/D-large-practice.out: -------------------------------------------------------------------------------- 1 | Case #1: 50112 2 | Case #2: 130920 3 | Case #3: 174321 4 | Case #4: 176432 5 | Case #5: 175902 6 | Case #6: 175383 7 | Case #7: 78005 8 | Case #8: 175089 9 | Case #9: 174408 10 | Case #10: 183251 11 | -------------------------------------------------------------------------------- /Google APAC/2016 APAC Test Round A/Problem D g Snake/D-small-practice.out: -------------------------------------------------------------------------------- 1 | Case #1: 207 2 | Case #2: 356 3 | Case #3: 295 4 | Case #4: 301 5 | Case #5: 335 6 | Case #6: 208 7 | Case #7: 212 8 | Case #8: 201 9 | Case #9: 250 10 | Case #10: 203 11 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Arrays And Sorting/Counting Sort 1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, Count[100] = {0}, temp; 7 | cin >> n; 8 | for (int i = 0; i < n; i++) { 9 | cin >> temp; 10 | Count[temp] += 1; 11 | } 12 | for (int i = 0; i < 100; i++) { 13 | cout << Count[i] << " "; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Arrays And Sorting/Counting Sort 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, Count[100] = {0}, temp; 7 | cin >> n; 8 | for (int i = 0; i < n; i++) { 9 | cin >> temp; 10 | Count[temp] += 1; 11 | } 12 | for (int i = 0; i < 100; i++) { 13 | while (Count[i]--) { 14 | cout << i << " "; 15 | } 16 | } 17 | cout << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Arrays And Sorting/Counting Sort 3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, Count[100] = {0}, Freq[100] = {0}, temp; 7 | char str[50]; 8 | cin >> n; 9 | for (int i = 0; i < n; i++) { 10 | cin >> temp >> str; 11 | Count[temp] += 1; 12 | } 13 | Freq[0] = Count[0]; 14 | for (int i = 1; i < 100; i++) { 15 | Freq[i] = Freq[i - 1] + Count[i]; 16 | } 17 | for (int i = 0; i < 100; i++) { 18 | cout << Freq[i] << " "; 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Arrays And Sorting/Running Time of Algorithms.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void InsertionSort (vector &Arr) { 6 | int len = Arr.size(), shift = 0; 7 | for (int i = 1; i < len; i++) { 8 | int Val = Arr[i], j = i - 1; 9 | while ((j >= 0) && (Arr[j] > Val)) { 10 | Arr[j + 1] = Arr[j]; 11 | j -= 1; 12 | shift += 1; 13 | } 14 | Arr[j + 1] = Val; 15 | } 16 | cout << shift << endl; 17 | } 18 | 19 | int main (void) { 20 | vector Arr; 21 | int ArrSize; 22 | cin >> ArrSize; 23 | for (int i = 0; i < ArrSize; i++) { 24 | int temp; 25 | cin >> temp; 26 | Arr.push_back(temp); 27 | } 28 | InsertionSort(Arr); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Arrays And Sorting/Sherlock And Pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long int Count[1000002] = {0}; 6 | 7 | int main (void) { 8 | int T, N; 9 | cin >> T; 10 | while (T--) { 11 | cin >> N; 12 | long long int A[N], count = 0; 13 | for (int i = 0; i < 1000002; i++) 14 | Count[i] = 0; 15 | for (int i = 0; i < N; i++) { 16 | cin >> A[i]; 17 | Count[A[i]] += 1; 18 | } 19 | for (int i = 0; i < 1000002; i++) { 20 | count += (Count[i] * (Count[i] - 1)); 21 | } 22 | cout << count << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Arrays And Sorting/Sherlock and Watson.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int N, K, Q, temp; 7 | cin >> N >> K >> Q; 8 | int A[N]; 9 | for (int i = 0; i < N; i++) { 10 | cin >> A[i]; 11 | } 12 | for (int i = 0; i < Q; i++) { 13 | cin >> temp; 14 | cout << A[(N - (K % N) + temp) % N] << endl; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Arrays And Sorting/Tutorial Info.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int BSearch (int ar[], int num, int start, int end) { 6 | if (start <= end) { 7 | int mid = (start + end) / 2; 8 | if (num == ar[mid]) { 9 | return mid; 10 | } else if (num > ar[mid]) { 11 | return BSearch (ar, num, mid + 1, end); 12 | } else { 13 | return BSearch (ar, num, start, mid - 1); 14 | } 15 | } 16 | return -1; 17 | } 18 | 19 | int main (void) { 20 | int V, n; 21 | cin >> V >> n; 22 | int ar[n]; 23 | for (int i = 0; i < n; i++) { 24 | cin >> ar[i]; 25 | } 26 | cout << BSearch (ar, V, 0, n - 1) << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Bit Manipulation/Counter Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, len, count; 7 | unsigned long long int N, factor, temp; 8 | cin >> T; 9 | while (T--) { 10 | cin >> N; 11 | count = 0; 12 | while (N != 1) { 13 | len = 0; 14 | temp = N; 15 | while (temp != 0) { 16 | temp /= 2; 17 | len++; 18 | } 19 | factor = (unsigned long long int) 1 << (len - 1); 20 | if (N == factor) { 21 | N /= 2; 22 | } else { 23 | N -= factor; 24 | } 25 | count++; 26 | } 27 | if (count % 2 == 0) { 28 | cout << "Richard" << endl; 29 | } else { 30 | cout << "Louise" << endl; 31 | } 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Bit Manipulation/Sansa and XOR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, N, Num, Ans; 7 | unsigned long long int times; 8 | cin >> T; 9 | while (T--) { 10 | cin >> N; 11 | Ans = 0; 12 | for (int i = 0; i < N; i++) { 13 | cin >> Num; 14 | times = (unsigned long long int) (i + 1) * (N - i); 15 | if ((times % 2) == 1) { 16 | Ans ^= Num; 17 | } 18 | } 19 | cout << Ans << endl; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Combinatorics/Building a List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector strVector; 6 | 7 | int main(void) { 8 | int T; 9 | cin >> T; 10 | while (T--) { 11 | int N; 12 | cin >> N; 13 | string str; 14 | cin >> str; 15 | for (int i = 1; i < (1 << N); i++) { 16 | string ans = ""; 17 | for (int j = 0; j < N; j++) 18 | if (i & (1 << j)) 19 | ans += str[j]; 20 | strVector.push_back(ans); 21 | } 22 | sort(strVector.begin(), strVector.end()); 23 | for (int i = 0; i < (1 << N) - 1; i++) 24 | cout << strVector[i] << endl; 25 | strVector.clear (); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Combinatorics/Connecting Towns.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, N; 7 | cin >> T; 8 | while (T--) { 9 | long long int temp, ans = 1; 10 | cin >> N; 11 | for (int i = 0; i < N - 1; i++) { 12 | cin >> temp; 13 | ans = (ans * temp) % 1234567; 14 | } 15 | cout << ans % 1234567 << endl; 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Combinatorics/Diwali Lights.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int expo100000 (int x, int y) { 6 | int res = 1, temp = x; 7 | while (y) { 8 | if (y & 1) { 9 | res = (res * temp) % 100000; 10 | } 11 | temp = (temp * temp) % 100000; 12 | y >>= 1; 13 | } 14 | return res % 100000; 15 | } 16 | 17 | int main (void) { 18 | int T, N, ans; 19 | cin >> T; 20 | while (T--) { 21 | cin >> N; 22 | ans = expo100000 (2, N); 23 | ans--; 24 | if (ans == -1) ans = 99999; 25 | cout << ans << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Combinatorics/Diwali Lights.py: -------------------------------------------------------------------------------- 1 | for i in range(0, int(input())): 2 | N = int(input()) 3 | print (pow(2, N, 10**5) - 1) 4 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Combinatorics/Handshake.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, N; 7 | cin >> T; 8 | while (T--) { 9 | cin >> N; 10 | cout << (N * (N - 1)) / 2 << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Combinatorics/Minimum Draws.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, N; 7 | cin >> T; 8 | while (T--) { 9 | cin >> N; 10 | cout << N + 1 << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Combinatorics/Picking Cards.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | int N, c; 10 | long long int ans = 1; 11 | cin >> N; 12 | int count[N]; 13 | for (int i = 0; i < N; i++) 14 | count[i] = 0; 15 | for (int i = 0; i < N; i++) { 16 | cin >> c; 17 | count[c] += 1; 18 | } 19 | for (int i = 1; i < N; i++) { 20 | count[i] += count[i-1]; 21 | } 22 | for (int i = 0; i < N; i++) { 23 | ans = ans * (count[i] - i) % 1000000007; 24 | if (count[i] < i) { 25 | break; 26 | } 27 | } 28 | cout << ans << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Dynamic Programming/Fibonacci Modified.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | public static void main(String[] args) { 9 | Scanner in = new Scanner(System.in); 10 | int A, B, N; 11 | BigInteger f1, f2, f3 = new BigInteger("0"); 12 | A = in.nextInt(); 13 | B = in.nextInt(); 14 | N = in.nextInt(); 15 | f1 = new BigInteger(Integer.toString(A)); 16 | f2 = new BigInteger(Integer.toString(B)); 17 | for (int i = 2; i < N; i++) { 18 | f3 = f1.add(f2.multiply(f2)); 19 | f1 = f2; 20 | f2 = f3; 21 | } 22 | System.out.println(f3); 23 | in.close(); 24 | } 25 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Geometry/Find Point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, Px, Py, Qx, Qy; 7 | cin >> T; 8 | while (T--) { 9 | cin >> Px >> Py >> Qx >> Qy; 10 | cout << (2 * Qx) - Px << " " << (2 * Qy) - Py << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Geometry/Rectangular Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | unsigned long long int total = 0, N, a, b, x = INT_MAX, y = INT_MAX; 7 | freopen ("Input.txt", "r", stdin); 8 | freopen ("Output.txt", "w", stdout); 9 | cin >> N; 10 | while (N--) { 11 | cin >> a >> b; 12 | x = min (x, a); 13 | y = min (y, b); 14 | } 15 | total = x * y; 16 | cout << total << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Greedy/Algorithmic Crush.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector< pair < int, pair < int, int> > > algo; 6 | 7 | int main (void) { 8 | int N, M, a, b, k, size; 9 | long long int maxi = 0, temp = 0; 10 | cin >> N >> M; 11 | for (int i = 0; i < M; i++) { 12 | cin >> a >> b >> k; 13 | algo.push_back (make_pair (a, make_pair (-1, k))); 14 | algo.push_back (make_pair (b, make_pair (1, k))); 15 | } 16 | sort (algo.begin (), algo.end ()); 17 | size = algo.size (); 18 | for (int i = 0; i < size; i++) { 19 | if (algo[i].second.first == -1) 20 | temp += algo[i].second.second; 21 | else 22 | temp -= algo[i].second.second; 23 | maxi = max (maxi, temp); 24 | } 25 | cout << maxi << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Greedy/Flowers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int N, K, temp, amount = 0; 7 | vector price; 8 | cin >> N >> K; 9 | for (int i = 0; i < N; i++) { 10 | cin >> temp; 11 | price.push_back (temp); 12 | } 13 | sort (price.rbegin(), price.rend()); 14 | for (int i = 0; i < N; i++) { 15 | amount += (((i/K) + 1) * price[i]); 16 | } 17 | cout << amount << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Greedy/Jim and the Orders.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector< pair > custQueue; 6 | 7 | bool myComparator (pair x, pair y) { 8 | if (x.second == y.second) 9 | return x.first < y.first; 10 | else 11 | return x.second < y.second; 12 | } 13 | 14 | int main (void) { 15 | int n, d, t; 16 | cin >> n; 17 | for (int i = 1; i <= n; i++) { 18 | cin >> t >> d; 19 | custQueue.push_back(make_pair(i, t+d)); 20 | } 21 | sort (custQueue.begin(), custQueue.end(), myComparator); 22 | for (int i = 0; i < n; i++) { 23 | cout << custQueue[i].first << " "; 24 | } 25 | cout << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Greedy/Priyanka and Toys.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector Weight; 6 | 7 | int main (void) { 8 | int N, weight, unit = 0, i; 9 | cin >> N; 10 | for (int i = 0; i < N; i++) { 11 | cin >> weight; 12 | Weight.push_back(weight); 13 | } 14 | sort(Weight.begin(), Weight.end()); 15 | i = *Weight.begin(); 16 | while (Weight.size() != 0) { 17 | unit++; 18 | while ((Weight.size() != 0) && (*Weight.begin() <= i+4)) { 19 | Weight.erase(Weight.begin(), Weight.begin() + 1); 20 | } 21 | if (Weight.size() != 0) { 22 | i = *Weight.begin(); 23 | } else { 24 | break; 25 | } 26 | } 27 | cout << unit << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Implementation/Designer PDF Viewer.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the designerPdfViewer function below. 10 | def designerPdfViewer(h, word): 11 | maxHeight = 0 12 | width = len(word) 13 | for c in word: 14 | maxHeight = max(maxHeight, h[ord(c) - ord('a')]) 15 | return maxHeight * width 16 | 17 | if __name__ == '__main__': 18 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 19 | 20 | h = list(map(int, input().rstrip().split())) 21 | 22 | word = input() 23 | 24 | result = designerPdfViewer(h, word) 25 | 26 | fptr.write(str(result) + '\n') 27 | 28 | fptr.close() 29 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Implementation/Find Digits.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the findDigits function below. 10 | def findDigits(n): 11 | countOfDivisors = 0 12 | temp = n 13 | while temp > 0: 14 | digit = temp % 10 15 | if digit != 0 and n % digit == 0: 16 | countOfDivisors += 1 17 | temp = temp // 10 18 | return countOfDivisors 19 | 20 | if __name__ == '__main__': 21 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 22 | 23 | t = int(input()) 24 | 25 | for t_itr in range(t): 26 | n = int(input()) 27 | 28 | result = findDigits(n) 29 | 30 | fptr.write(str(result) + '\n') 31 | 32 | fptr.close() 33 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Implementation/Service Lane.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int lane[100005] = {0}; 6 | 7 | int FindMin (int start, int end) { 8 | int m = 3; 9 | for (int i = start; i <= end; i++) { 10 | m = min (m, lane[i]); 11 | } 12 | return m; 13 | } 14 | 15 | int main (void) { 16 | int N, T, i, j; 17 | cin >> N >> T; 18 | for (int i = 0; i < N; i++) { 19 | cin >> lane[i]; 20 | } 21 | while (T--) { 22 | cin >> i >> j; 23 | cout << FindMin (i, j) << endl; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Implementation/Sherlock And Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, N; 7 | cin >> T; 8 | while (T--) { 9 | cin >> N; 10 | int A[N], leftSum = 0, rightSum = 0, flag = 0; 11 | for (int i = 0; i < N; i++) { 12 | cin >> A[i]; 13 | rightSum += A[i]; 14 | } 15 | rightSum -= A[0]; 16 | if (N == 1) { 17 | cout << "YES" << endl; 18 | flag = 1; 19 | } else { 20 | for (int i = 1; i < N; i++) { 21 | leftSum += A[i-1]; 22 | rightSum -= A[i]; 23 | if (leftSum == rightSum) { 24 | cout << "YES" << endl; 25 | flag = 1; 26 | break; 27 | } 28 | } 29 | } 30 | if (flag == 0) { 31 | cout << "NO" << endl; 32 | } 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Implementation/Sherlock And Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int N, M; 7 | cin >> N >> M; 8 | long long int A[N], B[M], C[M]; 9 | for (int i = 0; i < N; i++) { 10 | cin >> A[i]; 11 | } 12 | for (int i = 0; i < M; i++) { 13 | cin >> B[i]; 14 | } 15 | for (int i = 0; i < M; i++) { 16 | cin >> C[i]; 17 | } 18 | for (int i = 1; i <= M; i++) { 19 | for (int j = 1; j <= N; j++) { 20 | if ((j % B[i - 1]) == 0) { 21 | A[j - 1] = A[j - 1] * C[i - 1]; 22 | } 23 | } 24 | } 25 | for (int i = 0; i < N; i++) { 26 | cout << (A[i] % ((long long int)pow(10, 9) + 7)) << " "; 27 | } 28 | cout << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Implementation/Sherlock and Squares.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the squares function below. 10 | def squares(a, b): 11 | lower = math.ceil(math.sqrt(a)) 12 | upper = math.floor(math.sqrt(b)) 13 | return upper - lower + 1 14 | 15 | if __name__ == '__main__': 16 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 17 | 18 | q = int(input()) 19 | 20 | for q_itr in range(q): 21 | ab = input().split() 22 | 23 | a = int(ab[0]) 24 | 25 | b = int(ab[1]) 26 | 27 | result = squares(a, b) 28 | 29 | fptr.write(str(result) + '\n') 30 | 31 | fptr.close() 32 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Implementation/Utopian Tree.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the utopianTree function below. 10 | def utopianTree(n): 11 | H = 1 12 | for i in range(0, n): 13 | if i % 2 == 0: 14 | H *= 2 15 | else: 16 | H += 1 17 | return H 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | t = int(input()) 23 | 24 | for t_itr in range(t): 25 | n = int(input()) 26 | 27 | result = utopianTree(n) 28 | 29 | fptr.write(str(result) + '\n') 30 | 31 | fptr.close() 32 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Implementation/Viral Advertising.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the viralAdvertising function below. 10 | def viralAdvertising(n): 11 | shares = 5 12 | likes = shares // 2 13 | cumulativeLikes = likes 14 | for i in range(1, n): 15 | shares = likes * 3 16 | likes = shares // 2 17 | cumulativeLikes += likes 18 | return cumulativeLikes 19 | 20 | if __name__ == '__main__': 21 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 22 | 23 | n = int(input()) 24 | 25 | result = viralAdvertising(n) 26 | 27 | fptr.write(str(result) + '\n') 28 | 29 | fptr.close() 30 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Multiple Choice/A Basic Quiz on Algorithms #1.txt: -------------------------------------------------------------------------------- 1 | 1:c 2 | 2:b 3 | 3:b 4 | 4:a 5 | 5:b 6 | 6:a 7 | 7:a 8 | 8:c 9 | 9:a 10 | 10:e -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Multiple Choice/A Basic Quiz on Algorithms #2.txt: -------------------------------------------------------------------------------- 1 | 1:e 2 | 2:c 3 | 3:b 4 | 4:e 5 | 5:d 6 | 6:b 7 | 7:b 8 | 8:c 9 | 9:a 10 | 10:e -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/Detect HTML Tags.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | pattern = '<\s*(\w+).*?/?>' 4 | regex = re.compile(pattern) 5 | 6 | tags = set() 7 | 8 | for num in range(int(input())): 9 | string = input() 10 | match_tags = regex.findall(string) 11 | tags.update(match_tags) 12 | 13 | print(';'.join(t for t in sorted(tags))) 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/Detect the Domain Name.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | pattern = '(http|https)\\://(www.|ww2.|)([a-zA-Z0-9\\-\\.]+)(\\.[a-zA-Z]+)(/\\S*)?' 4 | regex = re.compile(pattern) 5 | 6 | s = set() 7 | 8 | for i in range(int(input())): 9 | string = input() 10 | iterator = regex.finditer(string) 11 | if iterator: 12 | for match in iterator: 13 | s.add(match.group(3)+match.group(4)) 14 | 15 | print(';'.join(t for t in sorted(s))) 16 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/Detecting Valid Latitude and Longitude Pairs.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | N = int(input()) 5 | pattern = '\([-+]?([1-9]\d*\.[0-9]+|[1-9]\d*),\s[-+]?([1-9]\d*\.[0-9]+|[1-9]\d*)\)' 6 | regexp = re.compile(pattern) 7 | 8 | for i in range(0, N): 9 | match = regexp.search(input()) 10 | if match: 11 | lat = float(match.group(1)) 12 | lon = float(match.group(2)) 13 | if lat >= 0.0 and lat <= 90.0 and lon >= 0.0 and lon <= 180.0: 14 | print("Valid") 15 | else: 16 | print("Invalid") 17 | else: 18 | print("Invalid") 19 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/Find HackerRank.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int N; 7 | string s, buff = "hackerrank"; 8 | cin >> N; 9 | getline (cin, s); 10 | while (N--) { 11 | getline (cin, s); 12 | int flag = -1; 13 | for (int i = 0; i < s.length(); i++) { 14 | if (!buff.compare(s.substr(i, 10))) { 15 | if (i == 0) { 16 | flag = 1; 17 | } 18 | if (i + 10 == s.length()) { 19 | if (flag == 1) { 20 | flag = 0; 21 | } else { 22 | flag = 2; 23 | } 24 | } 25 | } 26 | } 27 | cout << flag << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/HackerRank Tweets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool strEquals (string s1, string s2) { 6 | if (s1.length() == s2.length()) { 7 | for (int i = 0; i < s1.length(); i++) { 8 | if (toupper(s1[i]) != toupper(s2[i])) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } else { 14 | return false; 15 | } 16 | } 17 | 18 | int main (void) { 19 | int N, count = 0; 20 | string s, buff = "HACKERRANK"; 21 | cin >> N; 22 | getline (cin, s); 23 | while (N--) { 24 | getline (cin, s); 25 | for (int i = 0; i < s.length(); i++) { 26 | if (strEquals (buff, s.substr(i, 10))) { 27 | count++; 28 | break; 29 | } 30 | } 31 | } 32 | cout << count << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/IP Address Validation.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | patternIPv6 = '^([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}$' 5 | regexpIPv6 = re.compile(patternIPv6) 6 | patternIPv4 = '^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$' 7 | regexpIPv4 = re.compile(patternIPv4) 8 | 9 | for i in range(0, int(input())): 10 | string = input() 11 | matchIPv6 = regexpIPv6.match(string) 12 | matchIPv4 = regexpIPv4.match(string) 13 | if matchIPv6: 14 | print("IPv6") 15 | elif matchIPv4: 16 | print("IPv4") 17 | else: 18 | print("Neither") 19 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/Saying Hi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int N; 7 | string s; 8 | cin >> N; 9 | getline (cin, s); 10 | while (N--) { 11 | getline (cin, s); 12 | if ((s[0] == 'H' || s[0] == 'h') && (s[1] == 'I' || s[1] == 'i') && (s[2] == ' ') && (s[3] != 'D' && s[3] != 'd')) { 13 | cout << s << endl; 14 | } 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/UK and US - Part 2.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | text = " " 4 | for i in range(0, int(input())): 5 | text = text + input() + " " 6 | 7 | revised = re.sub('or', 'our', text) 8 | for i in range(0, int(input())): 9 | print(revised.count(input() + " ")) 10 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Regex/Utopian Identification Number.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | N = int(input()) 5 | pattern = '[a-z]{0,3}[0-9]{2,8}[A-Z]{3,}' 6 | regexp = re.compile(pattern) 7 | 8 | for i in range(0, N): 9 | match = regexp.match(input()) 10 | if match: 11 | print("VALID") 12 | else: 13 | print("INVALID") 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Search/Circle City.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long int ValidPoints (long long int R) { 6 | long long int count = 0; 7 | for (long long int i = 1; i*i <= R; i++) { 8 | double integral; 9 | if (modf(sqrt(R - (i*i)), &integral) == 0.0) { 10 | count += 1; 11 | } 12 | } 13 | return (count * 4); 14 | } 15 | 16 | int main (void) { 17 | long long int t, r, k; 18 | cin >> t; 19 | while (t--) { 20 | cin >> r >> k; 21 | if (ValidPoints(r) <= k) { 22 | cout << "possible" << endl; 23 | } else { 24 | cout << "impossible" << endl; 25 | } 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Search/Encryption.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | char str[82]; 7 | cin >> str; 8 | int l = strlen (str), w = floor (sqrt (l)), h = ceil (sqrt (l)); 9 | if ((w * h) < l) { 10 | w = h; 11 | } 12 | int k = l; 13 | while (k < (h * w)) { 14 | str[k++] = '@'; 15 | } 16 | str[k] = '\0'; 17 | char encry[w][h]; 18 | for (int i = 0, k = 0; i < w; i++) { 19 | for (int j = 0; j < h; j++) { 20 | encry[i][j] = str[k++]; 21 | } 22 | } 23 | for (int i = 0; i < h; i++) { 24 | for (int j = 0; j < w; j++) { 25 | if (encry[j][i] != '@') { 26 | cout << encry[j][i]; 27 | } 28 | } 29 | cout << " "; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Search/Ice Cream Parlor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int LinearSearch (int A[], int len, int num, int exclude) { 6 | for (int i = 0; i < len; i++) { 7 | if ((i != exclude) && (A[i] == num)) { 8 | return i; 9 | } 10 | } 11 | return -1; 12 | } 13 | 14 | int main (void) { 15 | int T, M, N, temp; 16 | cin >> T; 17 | while (T--) { 18 | cin >> M >> N; 19 | int A[N]; 20 | for (int i = 0; i < N; i++) { 21 | cin >> A[i]; 22 | } 23 | for (int i = 0; i < N; i++) { 24 | temp = LinearSearch(A, N, M - A[i], i); 25 | if (temp != -1) { 26 | cout << min (i + 1, temp + 1) << " " << max (i + 1, temp + 1) << endl; 27 | break; 28 | } 29 | } 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Search/Lonely Integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, ans = 0, temp; 7 | cin >> n; 8 | for (int i = 0; i < n; i++) { 9 | cin >> temp; 10 | ans ^= temp; 11 | } 12 | cout << ans << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Search/Pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | long long int N, K, val, ans = 0; 7 | set Set; 8 | set::iterator it; 9 | cin >> N >> K; 10 | for (long long int i = 0; i < N; i++) { 11 | cin >> val; 12 | Set.insert (val); 13 | } 14 | for (it = Set.begin(); it != Set.end(); it++) { 15 | if (Set.find((*it) + K) != Set.end()) { 16 | ans += 1; 17 | } 18 | } 19 | cout << ans << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Sorting/Insertion Sort - Part 2.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the insertionSort2 function below. 10 | def insertionSort2(n, arr): 11 | for i in range(1, len(arr)): 12 | n = arr[i] 13 | while(i > 0 and n < arr[i-1]): 14 | arr[i] = arr[i-1] 15 | i -= 1 16 | arr[i] = n 17 | print (' '.join(str(i) for i in arr)) 18 | 19 | if __name__ == '__main__': 20 | n = int(input()) 21 | 22 | arr = list(map(int, input().rstrip().split())) 23 | 24 | insertionSort2(n, arr) 25 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Strings/Anagram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, len; 7 | cin >> T; 8 | while (T--) { 9 | string s, s1, s2; 10 | int f1[26], total; 11 | cin >> s; 12 | len = s.length(); 13 | if (len % 2 == 1) { 14 | total = -1; 15 | } else { 16 | for (int i = 0; i < 26; i++) { 17 | f1[i] = 0; 18 | } 19 | s1 = s.substr(0, len / 2); 20 | s2 = s.substr(len / 2); 21 | total = s1.length(); 22 | for (int i = 0; i < s2.length(); i++) { 23 | f1[s2[i] - 'a']++; 24 | } 25 | for (int i = 0; i < s1.length(); i++) { 26 | if (f1[s1[i] - 'a'] > 0) { 27 | total--; 28 | f1[s1[i] - 'a']--; 29 | } 30 | } 31 | } 32 | cout << total << endl; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Strings/Common Child.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int LCS[5001][5001] = {0}; 6 | 7 | int main (void) { 8 | string s1, s2; 9 | cin >> s1 >> s2; 10 | int len1 = s1.length(), len2 = s2.length(); 11 | for (int i = 1; i <= len1; i++) { 12 | for (int j = 1; j <= len2; j++) { 13 | if (s1[i - 1] == s2[j - 1]) { 14 | LCS[i][j] = 1 + LCS[i - 1][j - 1]; 15 | } else { 16 | LCS[i][j] = max (LCS[i][j - 1], LCS[i - 1][j]); 17 | } 18 | } 19 | } 20 | cout << LCS[len1][len2] << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Strings/Gem Stones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int c[26] = {0}; 6 | 7 | int main (void) { 8 | int N, temp, total = 0; 9 | string s; 10 | cin >> N; 11 | temp = N; 12 | while (temp--) { 13 | cin >> s; 14 | bool flag[26]; 15 | fill (flag, flag + 26, false); 16 | for (int i = 0; i < s.length(); i++) { 17 | if (flag[s[i] - 'a'] == false) { 18 | c[s[i] - 'a']++; 19 | flag[s[i] - 'a'] = true; 20 | } 21 | } 22 | } 23 | for (int i = 0; i < 26; i++) { 24 | if (c[i] == N) { 25 | total++; 26 | } 27 | } 28 | cout << total << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Strings/Pangrams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool flag[26] = {false}; 6 | 7 | bool IsPangram () { 8 | for (int i = 0; i < 26; i++) { 9 | if (!flag[i]) { 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | 16 | int main (void) { 17 | string s; 18 | int index; 19 | getline( cin, s); 20 | for (string::iterator it = s.begin(); it != s.end(); it++) { 21 | index = tolower(*it) - 'a'; 22 | if ((index >= 0) && (index < 26)) { 23 | flag[index] = true; 24 | } 25 | } 26 | if (IsPangram ()) { 27 | cout << "pangram" << endl; 28 | } else { 29 | cout << "not pangram" << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Summation And Algebra/Summing the N series.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | long long int test, num, C = 1e9 + 7; 7 | cin >> test; 8 | while (test--) { 9 | cin >> num; 10 | num = num % C; 11 | cout << (num * num) % C << endl; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Summation And Algebra/Triangle Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int Traingle (long long int n) { 6 | if ((n == 1) || (n == 2)) { 7 | return -1; 8 | } else if (n % 2 == 1) { 9 | return 2; 10 | } else { 11 | if (n % 4 == 0) { 12 | return 3; 13 | } else { 14 | return 4; 15 | } 16 | } 17 | } 18 | 19 | int main (void) { 20 | int test; 21 | cin >> test; 22 | while (test--) { 23 | long long int N; 24 | cin >> N; 25 | cout << Traingle(N) << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Alternating Characters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, l; 7 | char str[100005]; 8 | cin >> T; 9 | while (T--) { 10 | cin >> str; 11 | l = strlen (str); 12 | int del = 0; 13 | for (int i = 0, j = 1; i < l && j < l; ) { 14 | if (str[i] == str[j]) { 15 | del += 1; 16 | j += 1; 17 | } else { 18 | i = j; 19 | j = i + 1; 20 | } 21 | } 22 | cout << del << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Angry Children.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int N, K; 7 | cin >> N >> K; 8 | long long int candies[N], unfair = INT_MAX; 9 | for (int i = 0; i < N; i++) { 10 | cin >> candies[i]; 11 | } 12 | sort (candies, candies + N); 13 | for (int k = K - 1, i = 0; k < N; k++, i++) { 14 | unfair = min (unfair, candies[k] - candies[i]); 15 | } 16 | cout << unfair << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Angry Professor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | int N, K, temp, count = 0; 10 | cin >> N >> K; 11 | for (int i = 0; i < N; i++) { 12 | cin >> temp; 13 | if (temp <= 0) 14 | count++; 15 | } 16 | if (count < K) 17 | cout << "YES" << endl; 18 | else 19 | cout << "NO" << endl; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Chocolate Feast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, N, C, M, choc, wrap; 7 | //ifstream in("ACMICPCTeam.txt"); 8 | cin >> T; 9 | while (T--) { 10 | cin >> N >> C >> M; 11 | wrap = choc = N / C; 12 | while (wrap >= M) { 13 | choc += (wrap / M); 14 | wrap = (wrap / M) + (wrap % M); 15 | } 16 | cout << choc << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Filling Jars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | long long int N, M, a, b, k, sum = 0; 7 | cin >> N >> M; 8 | for (int i = 0; i < M; i++) { 9 | cin >> a >> b >> k; 10 | sum += (b - a + 1)*k; 11 | } 12 | cout << sum/N << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Find Digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, count, digit; 7 | long long int N, temp; 8 | cin >> T; 9 | while (T--) { 10 | cin >> N; 11 | temp = N; 12 | count = 0; 13 | while (temp) { 14 | digit = temp % 10; 15 | if (digit != 0) { 16 | if (N % digit == 0) { 17 | count += 1; 18 | } 19 | } 20 | temp /= 10; 21 | } 22 | cout << count << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Flipping Bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | unsigned int T, num; 7 | cin >> T; 8 | for (unsigned int i = 0; i < T; i++) { 9 | cin >> num; 10 | cout << ~num << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Game of Thrones - I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int l, sum = 0, arr[26] = {0}; 7 | char str[100005]; 8 | cin >> str; 9 | l = strlen (str); 10 | for (int i = 0; i < l; i++) { 11 | arr[str[i] - 'a'] ^= 1; 12 | } 13 | for (int i = 0; i < 26; i++) { 14 | sum += arr[i]; 15 | } 16 | if (sum == 0 || sum == 1) { 17 | cout << "YES" << endl; 18 | } else { 19 | cout << "NO" << endl; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Halloween Party.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T; 7 | long long int maxi, K; 8 | cin >> T; 9 | while (T--) { 10 | maxi = -99999; 11 | cin >> K; 12 | for (int i = 1; i <= (K - i); i++) { 13 | maxi = max(maxi, i * (K - i)); 14 | } 15 | cout << maxi << endl; 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Maximizing XOR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int L, R, maxi = INT_MIN; 7 | cin >> L >> R; 8 | for (int i = L; i <= R; i++) { 9 | for (int j = L; j <= R; j++) { 10 | maxi = max(maxi, i^j); 11 | } 12 | } 13 | cout << maxi << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Sherlock And The Beast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, N; 7 | cin >> T; 8 | while (T--) { 9 | cin >> N; 10 | int x = -1, y = -1; 11 | for (int i = N/3; i >= 0; i--) { 12 | if (((N - (3 * i)) % 5) == 0) { 13 | x = i; 14 | y = (N - (3 * i)) / 5; 15 | break; 16 | } 17 | } 18 | if (x == -1) { 19 | cout << "-1" << endl; 20 | } else { 21 | for (int i = 0; i < 3 * x; i++) { 22 | cout << "5"; 23 | } 24 | for (int i = 0; i < 5 * y; i++) { 25 | cout << "3"; 26 | } 27 | cout << endl; 28 | } 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Sherlock and Squares.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, A, B, sum; 7 | cin >> T; 8 | while (T--) { 9 | cin >> A >> B; 10 | cout << (int)sqrt(B) - (int)sqrt(A - 1) << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Taum and B'day.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | long long int B, W, X, Y, Z; 10 | cin >> B >> W >> X >> Y >> Z; 11 | cout << min(X, Y + Z) * B + min(Y, X + Z) * W << endl; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/The Love-Letter Mystery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int T, first, last, i, l, mini, maxi, ops; 7 | char str[10005]; 8 | cin >> T; 9 | while (T--) { 10 | cin >> str; 11 | i = 0; 12 | l = strlen (str) - 1; 13 | ops = 0; 14 | while (i < l) { 15 | first = str[i++] - 'a'; 16 | last = str[l--] - 'a'; 17 | mini = min(first, last); 18 | maxi = max(first, last); 19 | ops += (maxi - mini); 20 | } 21 | cout << ops << endl; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /HackerRank Solutions/Algorithms/Warm Up/Utopian Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int height (int N) { 6 | int h = 1; 7 | for (int i = 1; i <= N; i++) { 8 | if ((i % 2) == 1) { 9 | h *= 2; 10 | } else { 11 | h += 1; 12 | } 13 | } 14 | return h; 15 | } 16 | 17 | int main (void) { 18 | int T, N; 19 | cin >> T; 20 | while (T--) { 21 | cin >> N; 22 | cout << height(N) << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /HackerRank Solutions/Artificial Intelligence/Multiple_Linear_Regression_Predicting_Housing_Prices.m: -------------------------------------------------------------------------------- 1 | C = strsplit(input('', 's')); 2 | F = str2double(C(1, 1)); 3 | N = str2double(C(1, 2)); 4 | data = zeros(N, F + 1); 5 | for i = 1 : N 6 | C = strsplit(input('', 's')); 7 | for j = 1 : F + 1 8 | data(i, j) = str2double(C(1, j)); 9 | end 10 | end 11 | X = data(:, 1 : F); 12 | y = data(:, F + 1); 13 | m = length(y); 14 | X = [ones(m, 1) X]; 15 | theta = pinv(X'*X)*(X'*y); 16 | T = str2double(input('', 's')); 17 | for i = 1 : T 18 | XTestData = ones(1, F + 1); 19 | C = strsplit(input('', 's')); 20 | for i = 2 : F + 1 21 | XTestData(1, i) = str2double(C(1, i - 1)); 22 | end 23 | price = XTestData * theta; 24 | fprintf('%.2f\n', price); 25 | end -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Inheritance/Inheritance Introduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Triangle { 9 | public: 10 | void triangle() { 11 | cout << "I am a triangle" << endl; 12 | } 13 | }; 14 | class Isosceles : public Triangle { 15 | public: 16 | void isosceles() { 17 | cout << "I am an isosceles triangle" << endl; 18 | } 19 | void description() { 20 | cout << "In an isosceles triangle two sides are equal" << endl; 21 | } 22 | }; 23 | int main() { 24 | Isosceles isc; 25 | isc.isosceles(); 26 | isc.description(); 27 | isc.triangle(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Inheritance/Rectangle Area.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | class Rectangle { 10 | public: 11 | int width, height; 12 | void Display() { 13 | cout << width << " " << height << endl; 14 | } 15 | }; 16 | class RectangleArea: public Rectangle { 17 | public: 18 | void Input() { 19 | cin >> width >> height; 20 | } 21 | void Display() { 22 | cout << width * height << endl; 23 | } 24 | }; 25 | int main() { 26 | RectangleArea r_area; 27 | r_area.Input(); 28 | Rectangle *r; 29 | r=&r_area; 30 | r->Display(); 31 | r_area.Display(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Introduction/Arrays Introduction.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 arr[N] = {0}; 12 | for (int i = 0; i < N; i++) { 13 | cin >> arr[i]; 14 | } 15 | for (int i = N-1; i >= 0; i--) { 16 | cout << arr[i] << " "; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Introduction/Basic Data Types.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int a; 7 | long b; 8 | long long c; 9 | char d; 10 | float e; 11 | double f; 12 | scanf("%d %ld %lld %c %f %lf", &a, &b, &c, &d, &e, &f); 13 | printf("%d\n%ld\n%lld\n%c\n%f\n%lf\n", a, b, c, d, e, f); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Introduction/Functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int max_of_four(int a, int b, int c, int d) { 7 | return max(max(a, b), max(c, d)); 8 | } 9 | 10 | int main() { 11 | int a, b, c, d; 12 | scanf("%d %d %d %d", &a, &b, &c, &d); 13 | int ans = max_of_four(a, b, c, d); 14 | printf("%d", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Introduction/Hello, World!.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("Hello, World!"); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Introduction/Input and Output.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int A, B, C; 11 | cin >> A >> B >> C; 12 | cout << A+B+C << endl; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Introduction/Pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void update(int *a,int *b) { 4 | int sum = *a + *b; 5 | int diff; 6 | if (*a >= *b) { 7 | diff = *a - *b; 8 | } else { 9 | diff = *b - *a; 10 | } 11 | *a = sum; 12 | *b = diff; 13 | } 14 | 15 | int main() { 16 | int a, b; 17 | int *pa = &a, *pb = &b; 18 | scanf("%d %d", &a, &b); 19 | update(pa, pb); 20 | printf("%d\n%d", a, b); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Introduction/Variable Sized Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N, Q; 6 | cin >> N >> Q; 7 | int **Sequence = new int*[N]; 8 | for (int i = 0; i < N; i++) { 9 | int k; 10 | cin >> k; 11 | int *seq = new int[k]; 12 | for (int j = 0; j < k; j++) { 13 | cin >> seq[j]; 14 | } 15 | Sequence[i] = seq; 16 | } 17 | for (int i = 0; i < Q; i++) { 18 | int j, k; 19 | cin >> j >> k; 20 | cout << Sequence[j][k] << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/STL/Sets-STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | set Set; 12 | set::iterator it; 13 | int Q, y, x; 14 | scanf("%d", &Q); 15 | for (int i = 0; i < Q; i++) { 16 | scanf("%d %d", &y, &x); 17 | if (y == 1) { 18 | Set.insert(x); 19 | } else if (y == 2) { 20 | Set.erase(x); 21 | } else if (y == 3) { 22 | it = Set.find(x); 23 | if (it == Set.end()) { 24 | printf("No\n"); 25 | } else { 26 | printf("Yes\n"); 27 | } 28 | } 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/STL/Vector-Erase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | vector arr; 10 | int N, x, a, b; 11 | scanf("%d", &N); 12 | for (int i = 0; i < N; i++) { 13 | int temp; 14 | scanf("%d", &temp); 15 | arr.push_back(temp); 16 | } 17 | scanf("%d %d %d", &x, &a, &b); 18 | arr.erase(arr.begin()+x-1); 19 | arr.erase(arr.begin()+a-1, arr.begin()+b-1); 20 | printf("%d\n", arr.size()); 21 | for (int i = 0; i < arr.size(); i++) { 22 | printf("%d ", arr[i]); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/STL/Vector-Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | vector arr; 10 | int N; 11 | scanf("%d", &N); 12 | for (int i = 0; i < N; i++) { 13 | int temp; 14 | scanf("%d", &temp); 15 | arr.push_back(temp); 16 | } 17 | sort(arr.begin(), arr.end()); 18 | for (int i = 0; i < N; i++) { 19 | printf("%d ", arr[i]); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Strings/StringStream.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector parseInts(string str) { 7 | vector temp; 8 | char tempChar; 9 | int tempInt; 10 | stringstream s(str); 11 | while(s >> tempInt) { 12 | temp.push_back(tempInt); 13 | s >> tempChar; 14 | } 15 | return temp; 16 | } 17 | 18 | int main() { 19 | string str; 20 | cin >> str; 21 | vector integers = parseInts(str); 22 | for(int i = 0; i < integers.size(); i++) { 23 | cout << integers[i] << "\n"; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /HackerRank Solutions/C++/Strings/Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | string a, b; 7 | cin >> a >> b; 8 | cout << a.size() << " " << b.size() << endl; 9 | cout << a + b << endl; 10 | char firstA = a[0], firstB = b[0]; 11 | b[0] = firstA; 12 | a[0] = firstB; 13 | cout << a << " " << b << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /HackerRank Solutions/C/Arrays and Strings/Digit Frequency.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 9 | char num[1001]; 10 | scanf("%[^\n]%*c", num); 11 | int arr[10] = {0}; 12 | for(int i=0; i='0' && num[i]<='9'){ 14 | arr[num[i] - '0']++; 15 | } 16 | } 17 | for(int i=0; i<10; i++){ 18 | printf("%d ", arr[i]); 19 | } 20 | printf("\n"); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /HackerRank Solutions/C/Functions/Calculate the Nth term.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | //Complete the following function. 6 | 7 | int find_nth_term(int n, int a, int b, int c) { 8 | //Write your code here. 9 | if (n == 1) { 10 | return a; 11 | } 12 | if (n == 2) { 13 | return b; 14 | } 15 | if (n == 3) { 16 | return c; 17 | } 18 | return find_nth_term(n - 1, a, b, c) + find_nth_term(n - 2, a, b, c) + find_nth_term(n - 3, a, b, c); 19 | } 20 | 21 | int main() { 22 | int n, a, b, c; 23 | 24 | scanf("%d %d %d %d", &n, &a, &b, &c); 25 | int ans = find_nth_term(n, a, b, c); 26 | 27 | printf("%d", ans); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /HackerRank Solutions/C/Introduction/Functions in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int max_of_four(int a, int b, int c, int d) { 4 | int max = a; 5 | if (b > max) { 6 | max = b; 7 | } 8 | if (c > max) { 9 | max = c; 10 | } 11 | if (d > max) { 12 | max = d; 13 | } 14 | return max; 15 | } 16 | 17 | int main() { 18 | int a, b, c, d; 19 | scanf("%d %d %d %d", &a, &b, &c, &d); 20 | int ans = max_of_four(a, b, c, d); 21 | printf("%d", ans); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /HackerRank Solutions/C/Introduction/Hello World! in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | char s[100]; 10 | scanf("%[^\n]%*c", &s); 11 | 12 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 13 | printf("Hello, World!\n"); 14 | printf("%s", s); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /HackerRank Solutions/C/Introduction/Playing With Characters.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 10 | char ch; 11 | scanf("%c", &ch); 12 | printf("%c\n", ch); 13 | scanf("%c", &ch); 14 | char s[100]; 15 | scanf("%s", s); 16 | printf("%s\n", s); 17 | scanf("%c", &ch); 18 | char sentence[100]; 19 | scanf("%[^\n]%*c", sentence); 20 | printf("%s\n", sentence); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /HackerRank Solutions/C/Introduction/Pointers in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void update(int *a, int *b) { 4 | // Complete this function 5 | int sum = *a + *b; 6 | int difference; 7 | if (*a >= *b) { 8 | difference = *a - *b; 9 | } else { 10 | difference = *b - *a; 11 | } 12 | *a = sum; 13 | *b = difference; 14 | } 15 | 16 | int main() { 17 | int a, b; 18 | int *pa = &a, *pb = &b; 19 | 20 | scanf("%d %d", &a, &b); 21 | update(pa, pb); 22 | printf("%d\n%d", a, b); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /HackerRank Solutions/C/Introduction/Sum and Difference of Two Numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int a, b; 9 | scanf("%d %d", &a, &b); 10 | printf("%d %d\n", a + b, a - b); 11 | float c, d; 12 | scanf("%f %f", &c, &d); 13 | printf("%.1f %.1f", c + d, c - d); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Data Structures/Arrays/2D Array.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | arr = [] 5 | for arr_i in xrange(6): 6 | arr_temp = map(int,raw_input().strip().split(' ')) 7 | arr.append(arr_temp) 8 | maxSum = -99999 9 | for i in range(1, 5): 10 | for j in range(1, 5): 11 | maxSum = max(maxSum, arr[i-1][j-1]+arr[i-1][j]+arr[i-1][j+1]+arr[i][j]+arr[i+1][j-1]+arr[i+1][j]+arr[i+1][j+1]) 12 | print maxSum -------------------------------------------------------------------------------- /HackerRank Solutions/Data Structures/Arrays/Arrays.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | n = int(raw_input().strip()) 5 | arr = map(int,raw_input().strip().split(' ')) 6 | arr.reverse() 7 | for item in arr: 8 | print item, -------------------------------------------------------------------------------- /HackerRank Solutions/Data Structures/Arrays/Left Rotation.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | if __name__ == '__main__': 10 | nd = input().split() 11 | n = int(nd[0]) 12 | d = int(nd[1]) 13 | a = list(map(int, input().rstrip().split())) 14 | result = a[d:] + a[:d] 15 | print(*result) -------------------------------------------------------------------------------- /HackerRank Solutions/Data Structures/Heap/QHEAP1.py: -------------------------------------------------------------------------------- 1 | from heapq import heappush, heappop 2 | 3 | heap = [] 4 | itemLookup = set() 5 | 6 | def addToHeap(value): 7 | heappush(heap, value) 8 | itemLookup.add(value) 9 | 10 | def deleteFromHeap(value): 11 | itemLookup.discard(value) 12 | 13 | def minimumFromHeap(): 14 | while heap[0] not in itemLookup: 15 | heappop(heap) 16 | print(heap[0]) 17 | 18 | numberOfQueries = int(input()) 19 | for _ in range(numberOfQueries): 20 | query = list(map(int, input().strip().split())) 21 | if query[0] == 1: 22 | addToHeap(query[1]) 23 | elif query[0] == 2: 24 | deleteFromHeap(query[1]) 25 | else: 26 | minimumFromHeap() -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/Dictionaries and Hashmaps/Two Strings.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the twoStrings function below. 10 | def twoStrings(s1, s2): 11 | A = set(s1) 12 | B = set(s2) 13 | if len(A & B) > 0: 14 | return "YES" 15 | else: 16 | return "NO" 17 | 18 | if __name__ == '__main__': 19 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 20 | 21 | q = int(input()) 22 | 23 | for q_itr in range(q): 24 | s1 = input() 25 | 26 | s2 = input() 27 | 28 | result = twoStrings(s1, s2) 29 | 30 | fptr.write(result + '\n') 31 | 32 | fptr.close() 33 | -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/Greedy Algorithms/Greedy Florist.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the getMinimumCost function below. 10 | def getMinimumCost(k, c): 11 | c.sort(reverse=True) 12 | amount = 0 13 | for i in range(len(c)): 14 | amount += (((i//k) + 1) * c[i]) 15 | return amount 16 | 17 | if __name__ == '__main__': 18 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 19 | 20 | nk = input().split() 21 | 22 | n = int(nk[0]) 23 | 24 | k = int(nk[1]) 25 | 26 | c = list(map(int, input().rstrip().split())) 27 | 28 | minimumCost = getMinimumCost(k, c) 29 | 30 | fptr.write(str(minimumCost) + '\n') 31 | 32 | fptr.close() 33 | -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/Greedy Algorithms/Minimum Absolute Difference in an Array.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the minimumAbsoluteDifference function below. 10 | def minimumAbsoluteDifference(arr): 11 | arr.sort() 12 | difference = sys.maxsize 13 | for i in range(len(arr) - 1): 14 | difference = min(difference, arr[i+1] - arr[i]) 15 | return difference 16 | 17 | if __name__ == '__main__': 18 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 19 | 20 | n = int(input()) 21 | 22 | arr = list(map(int, input().rstrip().split())) 23 | 24 | result = minimumAbsoluteDifference(arr) 25 | 26 | fptr.write(str(result) + '\n') 27 | 28 | fptr.close() 29 | -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/Search/Hash Tables - Ice Cream Parlour.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the whatFlavors function below. 10 | def whatFlavors(cost, money): 11 | pricesMap = dict() 12 | for index, value in enumerate(cost, 1): 13 | if money-value in pricesMap: 14 | print(pricesMap[money-value], index) 15 | pricesMap[value] = index 16 | 17 | if __name__ == '__main__': 18 | t = int(input()) 19 | 20 | for t_itr in range(t): 21 | money = int(input()) 22 | 23 | n = int(input()) 24 | 25 | cost = list(map(int, input().rstrip().split())) 26 | 27 | whatFlavors(cost, money) 28 | -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/Search/Pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | long long int N, K, val, ans = 0; 7 | set Set; 8 | set::iterator it; 9 | cin >> N >> K; 10 | for (long long int i = 0; i < N; i++) { 11 | cin >> val; 12 | Set.insert (val); 13 | } 14 | for (it = Set.begin(); it != Set.end(); it++) { 15 | if (Set.find((*it) + K) != Set.end()) { 16 | ans += 1; 17 | } 18 | } 19 | cout << ans << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/Search/Pairs.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the pairs function below. 10 | def pairs(k, arr): 11 | a = set(arr) 12 | b = set(i+k for i in arr) 13 | return len(a&b) 14 | 15 | if __name__ == '__main__': 16 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 17 | 18 | nk = input().split() 19 | 20 | n = int(nk[0]) 21 | 22 | k = int(nk[1]) 23 | 24 | arr = list(map(int, input().rstrip().split())) 25 | 26 | result = pairs(k, arr) 27 | 28 | fptr.write(str(result) + '\n') 29 | 30 | fptr.close() 31 | -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/Sorting/Mark and Toys.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector price; 6 | 7 | int main (void) { 8 | int N, count = 0; 9 | long long int K, temp; 10 | cin >> N >> K; 11 | for (int i = 0; i < N; i++) { 12 | cin >> temp; 13 | price.push_back (temp); 14 | } 15 | sort (price.begin(), price.end()); 16 | temp = 0; 17 | for (int i = 0; i < N; i++) { 18 | if (temp + price[i] <= K) { 19 | temp += price[i]; 20 | count++; 21 | } 22 | } 23 | cout << count << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/Sorting/Sorting - Bubble Sort.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the countSwaps function below. 10 | def countSwaps(a): 11 | numberOfSwaps = 0 12 | for i in range(len(a)): 13 | for j in range(len(a) - 1): 14 | if a[j] > a[j+1]: 15 | numberOfSwaps += 1 16 | a[j], a[j+1] = a[j+1], a[j] 17 | print('Array is sorted in ' + str(numberOfSwaps) + ' swaps.') 18 | print('First Element: ' + str(a[0])) 19 | print('Last Element: ' + str(a[-1])) 20 | 21 | if __name__ == '__main__': 22 | n = int(input()) 23 | 24 | a = list(map(int, input().rstrip().split())) 25 | 26 | countSwaps(a) 27 | -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/String Manipulation/Alternating Characters.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the alternatingCharacters function below. 10 | def alternatingCharacters(s): 11 | deletionsRequired = 0 12 | for i in range(1, len(s)): 13 | if s[i] == s[i-1]: 14 | deletionsRequired += 1 15 | return deletionsRequired 16 | 17 | if __name__ == '__main__': 18 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 19 | 20 | q = int(input()) 21 | 22 | for q_itr in range(q): 23 | s = input() 24 | 25 | result = alternatingCharacters(s) 26 | 27 | fptr.write(str(result) + '\n') 28 | 29 | fptr.close() 30 | -------------------------------------------------------------------------------- /HackerRank Solutions/Interview Preparation Kit/String Manipulation/Common Child.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int LCS[5001][5001] = {0}; 6 | 7 | int main (void) { 8 | string s1, s2; 9 | cin >> s1 >> s2; 10 | int len1 = s1.length(), len2 = s2.length(); 11 | for (int i = 1; i <= len1; i++) { 12 | for (int j = 1; j <= len2; j++) { 13 | if (s1[i - 1] == s2[j - 1]) { 14 | LCS[i][j] = 1 + LCS[i - 1][j - 1]; 15 | } else { 16 | LCS[i][j] = max (LCS[i][j - 1], LCS[i - 1][j]); 17 | } 18 | } 19 | } 20 | cout << LCS[len1][len2] << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Java/Java Static Initializer.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | static int B, H; 10 | static boolean flag = false; 11 | static { 12 | Scanner scan = new Scanner(System.in); 13 | B = scan.nextInt(); 14 | H = scan.nextInt(); 15 | if (B <= 0 || H <= 0) { 16 | System.out.println("java.lang.Exception: Breadth and height must be positive"); 17 | } else { 18 | flag = true; 19 | } 20 | } 21 | 22 | public static void main(String[] args){ 23 | if(flag){ 24 | int area=B*H; 25 | System.out.print(area); 26 | } 27 | 28 | }//end of main 29 | 30 | }//end of class 31 | 32 | -------------------------------------------------------------------------------- /HackerRank Solutions/Java/Java String Reverse.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | 8 | Scanner sc=new Scanner(System.in); 9 | String A=sc.next(); 10 | System.out.println( A.equals( new StringBuilder(A).reverse().toString()) 11 | ? "Yes" : "No" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Java/Pattern Syntax Checker.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.regex.*; 3 | 4 | public class Solution 5 | { 6 | public static void main(String[] args){ 7 | Scanner in = new Scanner(System.in); 8 | int testCases = Integer.parseInt(in.nextLine()); 9 | while(testCases>0){ 10 | String pattern = in.nextLine(); 11 | try { 12 | Pattern.compile(pattern); 13 | System.out.println("Valid"); 14 | } catch (Exception e) { 15 | System.out.println("Invalid"); 16 | } 17 | testCases--; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HackerRank Solutions/Java/Valid Username Regular Expression.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class UsernameValidator { 3 | public static final String regularExpression = "^[[A-Z]|[a-z]][[A-Z]|[a-z]|\\d|[_]]{7,29}$"; 4 | } 5 | 6 | 7 | public class Solution { 8 | private static final Scanner scan = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | int n = Integer.parseInt(scan.nextLine()); 12 | while (n-- != 0) { 13 | String userName = scan.nextLine(); 14 | 15 | if (userName.matches(UsernameValidator.regularExpression)) { 16 | System.out.println("Valid"); 17 | } else { 18 | System.out.println("Invalid"); 19 | } 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Java/Welcome to Java.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("Hello, World."); 5 | System.out.println("Hello, Java."); 6 | } 7 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/A Personalized Echo.bash: -------------------------------------------------------------------------------- 1 | read Name 2 | echo "Welcome $Name" -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Arithmetic Operations.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read X 4 | printf "%.3f\n" $(echo $X | bc -l) -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Comparing Numbers.bash: -------------------------------------------------------------------------------- 1 | read X 2 | read Y 3 | if [ "$X" -lt "$Y" ] 4 | then 5 | echo "X is less than Y" 6 | elif [ "$X" -gt "$Y" ] 7 | then 8 | echo "X is greater than Y" 9 | else 10 | echo "X is equal to Y" 11 | fi -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Compute the Average.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read N 4 | Total=0 5 | for i in $(seq 1 $N) 6 | do 7 | read X 8 | Total=$((Total + X)) 9 | done 10 | printf "%.3f\n" $(echo "$Total / $N" | bc -l) -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Concatanate an array with itself.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | arr=($(cat)) 4 | arr=(${arr[@]} ${arr[@]} ${arr[@]}) 5 | echo ${arr[@]} -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Count the Number of Elements in an array.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | arr=($(cat)) 4 | echo ${#arr[@]} -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Diaplay an Element of an array.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | arr=($(cat)) 4 | echo ${arr[3]} -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Filter an Array with Patterns.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | arr=($(cat)) 4 | declare -a pattern1=( ${arr[@]/*a*/} ) 5 | declare -a pattern2=( ${pattern1[@]/*A*/} ) 6 | echo ${pattern2[@]} -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Getting Started with Conditionals.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read X 4 | if [ "$X" = 'Y' ] 5 | then 6 | echo "YES" 7 | elif [ "$X" = 'y' ] 8 | then 9 | echo "YES" 10 | elif [ "$X" = 'N' ] 11 | then 12 | echo "NO" 13 | else 14 | echo "NO" 15 | fi -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Let's Echo.bash: -------------------------------------------------------------------------------- 1 | echo "HELLO" -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Looping and Skipping.bash: -------------------------------------------------------------------------------- 1 | for i in {1..100..2} 2 | do 3 | echo "$i" 4 | done -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Looping with Numbers.bash: -------------------------------------------------------------------------------- 1 | for i in {1..50} 2 | do 3 | echo "$i" 4 | done -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/More on Conditionals.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read X 4 | read Y 5 | read Z 6 | if [ "$X" -eq "$Y" ] 7 | then 8 | if [ "$X" -eq "$Z" ] 9 | then 10 | echo "EQUILATERAL" 11 | else 12 | echo "ISOSCELES" 13 | fi 14 | elif [ "$Y" -eq "$Z" ] 15 | then 16 | echo "ISOSCELES" 17 | else 18 | echo "SCALENE" 19 | fi -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Read in an Array.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo $(cat) -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Remove the first capital letter from each array element.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | i=1 4 | while read word 5 | do 6 | words[i]=$word 7 | i=$(( $i + 1 )) 8 | done 9 | echo ${words[@]/[A-Z]/.} -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/Slice an Array.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | arr=($(cat)) 4 | echo ${arr[@]:3:5} -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Bash/The World of Numbers.bash: -------------------------------------------------------------------------------- 1 | read Num1 2 | read Num2 3 | echo `expr "$Num1" + "$Num2"` 4 | echo `expr "$Num1" - "$Num2"` 5 | echo `expr "$Num1" \* "$Num2"` 6 | echo `expr "$Num1" / "$Num2"` -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Grep' #1.bash: -------------------------------------------------------------------------------- 1 | grep -w the -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Grep' #2.bash: -------------------------------------------------------------------------------- 1 | grep -w -i the -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Grep' #3.bash: -------------------------------------------------------------------------------- 1 | grep -vwi that -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Grep' #4.bash: -------------------------------------------------------------------------------- 1 | grep -wiE "(the|that|then|those)" -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Sed' command #1.bash: -------------------------------------------------------------------------------- 1 | sed -e 's/\bthe\b/this/' -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Sed' command #2.bash: -------------------------------------------------------------------------------- 1 | sed -e 's/\bthy\b/your/ig' -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Sed' command #3.bash: -------------------------------------------------------------------------------- 1 | sed -e 's/\bthy\b/{&}/ig' -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Tr' Command #1.bash: -------------------------------------------------------------------------------- 1 | tr '()' '[]' -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Tr' Command #2.bash: -------------------------------------------------------------------------------- 1 | tr -d [a-z] -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Tr' Command #3.bash: -------------------------------------------------------------------------------- 1 | tr -s ' ' -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Uniq' command #1.bash: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UtkarshPathrabe/Competitive-Coding/ae26d3c35d18312dd606cab6750e9e072c477cc1/HackerRank Solutions/Linux Shell/Text Processing/'Uniq' command #1.bash -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Uniq' command #2.bash: -------------------------------------------------------------------------------- 1 | uniq -c | cut -c7- -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Uniq' command #3.bash: -------------------------------------------------------------------------------- 1 | uniq -i -c | cut -c7- -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/'Uniq' command #4.bash: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UtkarshPathrabe/Competitive-Coding/ae26d3c35d18312dd606cab6750e9e072c477cc1/HackerRank Solutions/Linux Shell/Text Processing/'Uniq' command #4.bash -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #1.bash: -------------------------------------------------------------------------------- 1 | cut -c3 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #2.bash: -------------------------------------------------------------------------------- 1 | cut -c2,7 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #3.bash: -------------------------------------------------------------------------------- 1 | cut -c2-7 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #4.bash: -------------------------------------------------------------------------------- 1 | cut -c-4 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #5.bash: -------------------------------------------------------------------------------- 1 | cut -f-3 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #6.bash: -------------------------------------------------------------------------------- 1 | cut -c13- -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #7.bash: -------------------------------------------------------------------------------- 1 | cut -d' ' -f4 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #8.bash: -------------------------------------------------------------------------------- 1 | cut -d' ' -f-3 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Cut #9.bash: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UtkarshPathrabe/Competitive-Coding/ae26d3c35d18312dd606cab6750e9e072c477cc1/HackerRank Solutions/Linux Shell/Text Processing/Cut #9.bash -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Head of a Text File #1.bash: -------------------------------------------------------------------------------- 1 | head -n20 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Head of a Text File #2.bash: -------------------------------------------------------------------------------- 1 | head -c20 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Middle of a Text File.bash: -------------------------------------------------------------------------------- 1 | sed -n 12,22p -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Sort Command #1.bash: -------------------------------------------------------------------------------- 1 | sort -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Sort Command #2.bash: -------------------------------------------------------------------------------- 1 | sort -r -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Sort Command #3.bash: -------------------------------------------------------------------------------- 1 | sort -n -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Sort Command #4.bash: -------------------------------------------------------------------------------- 1 | sort -n -r -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Sort Command #5.bash: -------------------------------------------------------------------------------- 1 | sort -nr -t$'\t' -k2 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Sort Command #7.bash: -------------------------------------------------------------------------------- 1 | sort -n -r -t$'|' -k2 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Sort command #6.bash: -------------------------------------------------------------------------------- 1 | sort -n -t$'\t' -k2 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Tail of a Text File #1.bash: -------------------------------------------------------------------------------- 1 | tail -n20 -------------------------------------------------------------------------------- /HackerRank Solutions/Linux Shell/Text Processing/Tail of a Text File #2.bash: -------------------------------------------------------------------------------- 1 | tail -c20 -------------------------------------------------------------------------------- /HackerRank Solutions/Mathematics/Fundamentals/Find the Point.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | 6 | # 7 | # Complete the findPoint function below. 8 | # 9 | def findPoint(px, py, qx, qy): 10 | return [2 * qx - px, 2 * qy - py] 11 | 12 | if __name__ == '__main__': 13 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 14 | 15 | n = int(input()) 16 | 17 | for n_itr in range(n): 18 | pxPyQxQy = input().split() 19 | 20 | px = int(pxPyQxQy[0]) 21 | 22 | py = int(pxPyQxQy[1]) 23 | 24 | qx = int(pxPyQxQy[2]) 25 | 26 | qy = int(pxPyQxQy[3]) 27 | 28 | result = findPoint(px, py, qx, qy) 29 | 30 | fptr.write(' '.join(map(str, result))) 31 | fptr.write('\n') 32 | 33 | fptr.close() 34 | -------------------------------------------------------------------------------- /HackerRank Solutions/Mathematics/Fundamentals/Summing the N series.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | import math 6 | 7 | # 8 | # Complete the summingSeries function below. 9 | # 10 | def summingSeries(n): 11 | return (n**2) % 1000000007 12 | 13 | if __name__ == '__main__': 14 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 15 | 16 | t = int(input()) 17 | 18 | for t_itr in range(t): 19 | n = int(input()) 20 | 21 | result = summingSeries(n) 22 | 23 | fptr.write(str(result) + '\n') 24 | 25 | fptr.close() 26 | -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Algorithms/B'day Gift.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | 6 | # Complete the solve function below. 7 | def solve(balls): 8 | return round(sum(balls) / 2, 1) 9 | 10 | if __name__ == '__main__': 11 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 12 | 13 | balls_count = int(input()) 14 | 15 | balls = [] 16 | 17 | for _ in range(balls_count): 18 | balls_item = int(input()) 19 | balls.append(balls_item) 20 | 21 | result = solve(balls) 22 | 23 | fptr.write(str(result) + '\n') 24 | 25 | fptr.close() 26 | -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Algorithms/Compare the Triplets.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the compareTriplets function below. 10 | def compareTriplets(a, b): 11 | result = [0, 0] 12 | for i in range(0, 3): 13 | if a[i] > b[i]: 14 | result[0] += 1 15 | elif a[i] < b[i]: 16 | result[1] += 1 17 | return result 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | a = list(map(int, input().rstrip().split())) 23 | 24 | b = list(map(int, input().rstrip().split())) 25 | 26 | result = compareTriplets(a, b) 27 | 28 | fptr.write(' '.join(map(str, result))) 29 | fptr.write('\n') 30 | 31 | fptr.close() 32 | -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Algorithms/Staircase.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the staircase function below. 10 | def staircase(n): 11 | for i in range(0, n): 12 | for j in range(0, n): 13 | if j < n-1-i: 14 | print(' ', end='') 15 | else: 16 | print('#', end='') 17 | print('') 18 | 19 | if __name__ == '__main__': 20 | n = int(input()) 21 | 22 | staircase(n) 23 | -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Algorithms/Utopian Tree.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the utopianTree function below. 10 | def utopianTree(n): 11 | H = 1 12 | for i in range(0, n): 13 | if i % 2 == 0: 14 | H *= 2 15 | else: 16 | H += 1 17 | return H 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | t = int(input()) 23 | 24 | for t_itr in range(t): 25 | n = int(input()) 26 | 27 | result = utopianTree(n) 28 | 29 | fptr.write(str(result) + '\n') 30 | 31 | fptr.close() 32 | -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Algorithms/Viral Advertising.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the viralAdvertising function below. 10 | def viralAdvertising(n): 11 | shares = 5 12 | likes = shares // 2 13 | cumulativeLikes = likes 14 | for i in range(1, n): 15 | shares = likes * 3 16 | likes = shares // 2 17 | cumulativeLikes += likes 18 | return cumulativeLikes 19 | 20 | if __name__ == '__main__': 21 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 22 | 23 | n = int(input()) 24 | 25 | result = viralAdvertising(n) 26 | 27 | fptr.write(str(result) + '\n') 28 | 29 | fptr.close() 30 | -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Basic Data Types/Lists.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | N = int(input()) 3 | arr = []; 4 | for i in range(0,N): 5 | cmd = input().split(); 6 | if cmd[0] == "insert": 7 | arr.insert(int(cmd[1]),int(cmd[2])) 8 | elif cmd[0] == "append": 9 | arr.append(int(cmd[1])) 10 | elif cmd[0] == "pop": 11 | arr.pop(); 12 | elif cmd[0] == "print": 13 | print (arr) 14 | elif cmd[0] == "remove": 15 | arr.remove(int(cmd[1])) 16 | elif cmd[0] == "sort": 17 | arr.sort(); 18 | else: 19 | arr.reverse(); -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Basic Data Types/Nested Lists.py: -------------------------------------------------------------------------------- 1 | marksheet=[] 2 | scorelist=[] 3 | 4 | if __name__ == '__main__': 5 | for _ in range(int(input())): 6 | name = input() 7 | score = float(input()) 8 | marksheet+=[[name,score]] 9 | scorelist+=[score] 10 | secondLowestScore = sorted(list(set(scorelist)))[1] 11 | for n, s in sorted(marksheet): 12 | if s==secondLowestScore: 13 | print(n) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Built-Ins/Any or All.py: -------------------------------------------------------------------------------- 1 | N, arr = int(input()), input().split() 2 | print(all([int(i) > 0 for i in arr]) and any([i == i[::-1] for i in arr])) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Collections/Collections.OrderedDict.py: -------------------------------------------------------------------------------- 1 | from collections import OrderedDict 2 | N = input() 3 | ordDict = OrderedDict() 4 | for i in range(int(N)): 5 | item, cost = input().rsplit(' ', 1) 6 | if item in ordDict: 7 | ordDict[item] += int(cost) 8 | else: 9 | ordDict[item] = int(cost) 10 | for k, v in ordDict.items(): 11 | print(k, v) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Collections/Collections.deque().py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | d = deque() 3 | N = int(input()) 4 | for _ in range(N): 5 | cmd = input().split() 6 | if cmd[0] == 'append': 7 | d.append(cmd[1]) 8 | elif cmd[0] == 'appendleft': 9 | d.appendleft(cmd[1]) 10 | elif cmd[0] == 'pop': 11 | d.pop() 12 | elif cmd[0] == 'popleft': 13 | d.popleft() 14 | print(' '.join(d)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Collections/Collections.namedtuple().py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | from collections import namedtuple 3 | N = int(input()) 4 | Student = namedtuple('Student', ','.join(input().split())) 5 | currentSum, count = 0, 0 6 | for i in range(N): 7 | entry = input().split() 8 | student = Student(entry[0], entry[1], entry[2], entry[3]) 9 | currentSum += int(student.MARKS) 10 | count += 1 11 | print(currentSum / count) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Collections/Company Logo.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | from collections import Counter 3 | S = input() 4 | freqMap = Counter(S) 5 | l = [] 6 | for key, val in freqMap.items(): 7 | l.append((-val, key)) 8 | l.sort() 9 | for i in range(3): 10 | print(l[i][1], -l[i][0]) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Collections/Counter.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | def main(): 4 | X = int(input()) 5 | sizeCount = Counter(map(int, input().split())) 6 | earned = 0 7 | N = int(input()) 8 | for _ in range(N): 9 | size, price = map(int, input().split()) 10 | if(size in sizeCount and sizeCount[size]): 11 | sizeCount[size] -= 1 12 | earned += price 13 | print(earned) 14 | 15 | if __name__ == '__main__': 16 | main() -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Collections/DefaultDict Tutorial.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | hashMap = defaultdict(list) 3 | n, m = map(int, input().split()) 4 | for i in range(n): 5 | a = input() 6 | hashMap[a].append(str(i + 1)) 7 | for i in range(m): 8 | a = input() 9 | if a in hashMap: 10 | print(' '.join(hashMap[a])) 11 | else: 12 | print('-1') -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Collections/Word Order.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | freqMap = {} 3 | N = int(input()) 4 | for i in range(N): 5 | word = input() 6 | if word in freqMap: 7 | freqMap[word] += 1 8 | else: 9 | freqMap[word] = 1 10 | print(len(freqMap)) 11 | for key, val in freqMap.items(): 12 | print(val, end =" ") -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Data Types/Find the Second Largest Number.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | List = map(int, raw_input().split()) 3 | List.sort() 4 | List.reverse() 5 | maximum_element = List[0] 6 | for x in range(1, len(List)): 7 | if List[x] != maximum_element: 8 | print List[x] 9 | break -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Data Types/List Comprehensions.txt: -------------------------------------------------------------------------------- 1 | X = int(raw_input()) 2 | Y = int(raw_input()) 3 | Z = int(raw_input()) 4 | N = int(raw_input()) 5 | List = [[x, y, z] for x in range(X+1) for y in range(Y+1) for z in range(Z+1) if x + y + z != N] 6 | print List -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Data Types/Nested Lists.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | students = [] 3 | minimum_marks = 9999999 4 | second_minimum_marks = 9999999 5 | for i in range(0, N): 6 | name = raw_input() 7 | marks = float(raw_input()) 8 | students.append([name, marks]) 9 | if marks < minimum_marks: 10 | second_minimum_marks = minimum_marks 11 | minimum_marks = marks 12 | elif marks < second_minimum_marks and marks != minimum_marks: 13 | second_minimum_marks = marks 14 | output = [] 15 | for student in students: 16 | if student[1] == second_minimum_marks: 17 | output.append(student[0]) 18 | output.sort() 19 | for x in output: 20 | print x -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Data Types/Sets - Symmetric Difference.txt: -------------------------------------------------------------------------------- 1 | M = int(raw_input()) 2 | first_set = set(list(map(int, raw_input().split()))) 3 | N = int(raw_input()) 4 | second_set = set(list(map(int, raw_input().split()))) 5 | symmetric_difference = list(first_set.difference(second_set).union(second_set.difference(first_set))) 6 | symmetric_difference.sort() 7 | for x in symmetric_difference: 8 | print x -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Data Types/Tuples.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | string_contents = raw_input().split() 3 | List = [int(x) for x in string_contents] 4 | print hash(tuple(List)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Date and Time/Calendar Module.txt: -------------------------------------------------------------------------------- 1 | import calendar 2 | month, date, year = map(int, raw_input().split()) 3 | print (list(calendar.day_name)[calendar.weekday(year, month, date)]).upper() -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Date and Time/Time Delta.txt: -------------------------------------------------------------------------------- 1 | import calendar 2 | from datetime import datetime 3 | T = int(input()) 4 | FMT = '%a:%d:%b:%Y:%H:%M:%S:%z' 5 | for i in range(T): 6 | time_1_list = input().split() 7 | time_2_list = input().split() 8 | time_1 = time_1_list[0] + ":" + time_1_list[1] + ":" + time_1_list[2] + ":" + time_1_list[3] + ":" + time_1_list[4] + ":" + time_1_list[5] 9 | time_2 = time_2_list[0] + ":" + time_2_list[1] + ":" + time_2_list[2] + ":" + time_2_list[3] + ":" + time_2_list[4] + ":" + time_2_list[5] 10 | delta = datetime.strptime(time_2, FMT) - datetime.strptime(time_1, FMT) 11 | print (int(abs(delta.total_seconds()))) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Errors and Exceptions/Incorrect Regex.py: -------------------------------------------------------------------------------- 1 | import re 2 | for i in range(int(input())): 3 | try: 4 | s = input() 5 | re.compile(s) 6 | except: 7 | print("False") 8 | continue 9 | print("True") -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Arithmetic Operators.txt: -------------------------------------------------------------------------------- 1 | a = int(raw_input()) 2 | b = int(raw_input()) 3 | print str(a + b) 4 | print str(a - b) 5 | print str(a * b) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Finding the percentage.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | record = {} 3 | for i in range(0, N): 4 | string = raw_input() 5 | string_content = string.split() 6 | name = string_content[0] 7 | maths_marks = float(string_content[1]) 8 | physics_marks = float(string_content[2]) 9 | chemistry_marks = float(string_content[3]) 10 | record[name] = (maths_marks + physics_marks + chemistry_marks) / 3.0 11 | name_search = raw_input() 12 | print '{0:.2f}'.format(record[name_search]) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Hello World!.txt: -------------------------------------------------------------------------------- 1 | print "Hello World!" -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Integers Come In All Sizes.txt: -------------------------------------------------------------------------------- 1 | a = int(raw_input()) 2 | b = int(raw_input()) 3 | c = int(raw_input()) 4 | d = int(raw_input()) 5 | print pow(a, b) + pow(c, d) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Interchange two numbers.txt: -------------------------------------------------------------------------------- 1 | a = int(raw_input()) 2 | b = int(raw_input()) 3 | Tuple = (a, b) 4 | print Tuple[1] 5 | print Tuple[0] -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Loops.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | for i in range(0, N): 3 | print pow(i, 2) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Mod Divmod.txt: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | a = int(raw_input()) 3 | b = int(raw_input()) 4 | print a // b 5 | print a % b 6 | print divmod(a, b) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Power - Mod Power.txt: -------------------------------------------------------------------------------- 1 | a = int(raw_input()) 2 | b = int(raw_input()) 3 | m = int(raw_input()) 4 | print pow(a, b) 5 | print pow(a, b, m) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Print Function.txt: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | N = int(raw_input()) 3 | for x in range(1, N+1): 4 | print(x, end='') -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Python Division.txt: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | a = int(raw_input()) 3 | b = int(raw_input()) 4 | print a // b 5 | print a / b -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/Raw Input.txt: -------------------------------------------------------------------------------- 1 | string = raw_input() 2 | print string -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Introduction/What's Your Name.txt: -------------------------------------------------------------------------------- 1 | first_name = raw_input() 2 | last_name = raw_input() 3 | print 'Hello ' + first_name + ' ' + last_name + '! You just delved into python.' -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Itertools/itertools.combinations(iterable, r).py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | from itertools import combinations 3 | S, k = input().split() 4 | S = sorted(S) 5 | k = int(k) 6 | for i in range(1, k + 1): 7 | for c in list(combinations(S, i)): 8 | print(''.join(c)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Itertools/itertools.product().txt: -------------------------------------------------------------------------------- 1 | from itertools import product 2 | A = map(int, raw_input().split()) 3 | A.sort() 4 | B = map(int, raw_input().split()) 5 | B.sort() 6 | Output = list(product(A, B)) 7 | for i in Output: 8 | print i, -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Math/Find Angle MBC.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UtkarshPathrabe/Competitive-Coding/ae26d3c35d18312dd606cab6750e9e072c477cc1/HackerRank Solutions/Python/Math/Find Angle MBC.txt -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Math/Polar Coordinates.txt: -------------------------------------------------------------------------------- 1 | import cmath 2 | num = complex(raw_input()) 3 | print abs(num) 4 | print cmath.phase(num) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Math/Triangle Quest 2.txt: -------------------------------------------------------------------------------- 1 | for i in range(1,int(raw_input())+1): 2 | print (((10**i)-1)/9)**2 -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Math/Triangle Quest.txt: -------------------------------------------------------------------------------- 1 | for i in range(1,input()): 2 | print (((10**i)-1)/9)*i -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Numpy/Concatenate.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | N, M, P = map(int, input().split()) 3 | array_1 = numpy.array([[1,2] for i in range(N)] ) 4 | array_2 = numpy.array([[3,4] for i in range(M)] ) 5 | print (numpy.concatenate((array_1, array_2), axis = 0)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Regex and Parsing/Group(), Groups() and Groupdict().py: -------------------------------------------------------------------------------- 1 | import re 2 | expression = r"([A-Za-z0-9])\1+" 3 | S = re.search(expression, input()) 4 | if(S): 5 | print(S.group(1)) 6 | else: 7 | print(-1) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Check Strict Superset.txt: -------------------------------------------------------------------------------- 1 | A = set(raw_input().split()) 2 | flag = True 3 | for i in range(int(raw_input())): 4 | B = set(raw_input().split()) 5 | if not (A > B): 6 | flag = False 7 | break 8 | print flag -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Check Subset.txt: -------------------------------------------------------------------------------- 1 | for i in range(int(raw_input())): 2 | a = int(raw_input()); A = set(raw_input().split()) 3 | b = int(raw_input()); B = set(raw_input().split()) 4 | print A <= B -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Introduction to Sets.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | Set = set(map(int, raw_input().split())) 3 | print float(sum(Set))/float(len(Set)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/No Idea!.txt: -------------------------------------------------------------------------------- 1 | n, m = map(int, raw_input().split()) 2 | Array = map(int, raw_input().split()) 3 | Set_A = set(map(int, raw_input().split())) 4 | Set_B = set(map(int, raw_input().split())) 5 | happiness_count = 0 6 | for i in Array: 7 | if i in Set_A: 8 | happiness_count += 1 9 | elif i in Set_B: 10 | happiness_count -= 1 11 | print happiness_count -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Set .discard(), .remove() & .pop().txt: -------------------------------------------------------------------------------- 1 | n = int(raw_input()) 2 | Set = set(map(int, raw_input().split())) 3 | for i in range(0, int(raw_input())): 4 | command_components = raw_input().split() 5 | if command_components[0] == 'pop': 6 | Set.pop() 7 | elif command_components[0] == 'discard': 8 | Set.discard(int(command_components[1])) 9 | elif command_components[0] == 'remove': 10 | Set.remove(int(command_components[1])) 11 | print sum(Set) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Set Mutations.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | Set_A = set(map(int, raw_input().split())) 3 | for i in range(int(raw_input())): 4 | command_component = raw_input().split() 5 | Set_B = set(map(int, raw_input().split())) 6 | if command_component[0] == 'update': 7 | Set_A.update(Set_B) 8 | elif command_component[0] == 'intersection_update': 9 | Set_A.intersection_update(Set_B) 10 | elif command_component[0] == 'symmetric_difference_update': 11 | Set_A.symmetric_difference_update(Set_B) 12 | elif command_component[0] == 'difference_update': 13 | Set_A.difference_update(Set_B) 14 | print sum(Set_A) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Set.add().txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | country_stamps = set() 3 | for i in range(N): 4 | country_stamps.add(raw_input()) 5 | print len(country_stamps) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Set.difference().txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | Set_A = set(map(int, raw_input().split())) 3 | M = int(raw_input()) 4 | Set_B = set(map(int, raw_input().split())) 5 | print len(Set_A.difference(Set_B)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Set.intersection().txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | Set_A = set(map(int, raw_input().split())) 3 | M = int(raw_input()) 4 | Set_B = set(map(int, raw_input().split())) 5 | print len(Set_A.intersection(Set_B)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Set.symmetric_difference().txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | Set_A = set(map(int, raw_input().split())) 3 | M = int(raw_input()) 4 | Set_B = set(map(int, raw_input().split())) 5 | print len(Set_A.symmetric_difference(Set_B)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/Set.union().txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | Set_A = set(map(int, raw_input().split())) 3 | M = int(raw_input()) 4 | Set_B = set(map(int, raw_input().split())) 5 | print len(Set_A.union(Set_B)) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Sets/The Captian's Room.txt: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | N, count = int(raw_input()), Counter(map(int, raw_input().split())) 3 | for i in count.elements(): 4 | if count[i] == 1: 5 | print i 6 | break -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/Alphabet Rangoli.txt: -------------------------------------------------------------------------------- 1 | import string 2 | N = int(raw_input()) 3 | width = 2 * ((2 * N) - 1) - 1 4 | alphabets = list(string.ascii_lowercase[:N]) 5 | for i in range(-(N-1), N): 6 | print '-'.join(alphabets[N-1:abs(i):-1] + [alphabets[abs(i)]] + alphabets[abs(i)+1:N]).center(width, '-') -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/Capitalize!.txt: -------------------------------------------------------------------------------- 1 | List = raw_input().split(' ') 2 | 3 | print ' '.join([x.capitalize() for x in List]) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/Designer Door Mat.txt: -------------------------------------------------------------------------------- 1 | N, M = map(int,raw_input().split()) 2 | for i in xrange(1,N,2): 3 | print ('.|.'*i).center(M, '-') 4 | print ('WELCOME').center(M, '-') 5 | for i in xrange(N-2,-1,-2): 6 | print ('.|.'*i).center(M, '-') -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/Find a string.py: -------------------------------------------------------------------------------- 1 | def count_substring(string, sub_string): 2 | noOfOcurrence = 0 3 | for i in range(len(string)-len(sub_string)+1): 4 | if string[i:i+len(sub_string)] == sub_string: 5 | noOfOcurrence += 1 6 | return noOfOcurrence 7 | 8 | if __name__ == '__main__': 9 | string = input().strip() 10 | sub_string = input().strip() 11 | 12 | count = count_substring(string, sub_string) 13 | print(count) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/Find a string.txt: -------------------------------------------------------------------------------- 1 | string = raw_input() 2 | sub_string = raw_input() 3 | count = 0 4 | for i in range(0, len(string)): 5 | j = 0 6 | while j < len(sub_string): 7 | if i+j < len(string): 8 | if string[i+j] != sub_string[j]: 9 | break 10 | else: 11 | j += 1 12 | else: 13 | break 14 | if j == len(sub_string): 15 | count += 1 16 | print count -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/Mutations.txt: -------------------------------------------------------------------------------- 1 | String = raw_input() 2 | List = raw_input().split() 3 | index = int(List[0]) 4 | print String[:index] + List[1] + String[index+1:] -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/String Formatting.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | width = len('{0:b}'.format(N)) 3 | for i in range(1, N+1): 4 | print '{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}'.format(i, width = width) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/String Split and Case.txt: -------------------------------------------------------------------------------- 1 | List = raw_input().split() 2 | print '-'.join(List) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/String Validators.txt: -------------------------------------------------------------------------------- 1 | String = raw_input() 2 | Output = [False, False, False, False, False] 3 | for i in range(len(String)): 4 | if String[i].isalnum(): 5 | Output[0] = True 6 | if String[i].isalpha(): 7 | Output[1] = True 8 | if String[i].isdigit(): 9 | Output[2] = True 10 | if String[i].islower(): 11 | Output[3] = True 12 | if String[i].isupper(): 13 | Output[4] = True 14 | if Output[0] and Output[1] and Output[2] and Output[3] and Output[4]: 15 | break 16 | for output in Output: 17 | print output -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/Text Alignment.txt: -------------------------------------------------------------------------------- 1 | thickness = int(raw_input()) #This must be an odd number 2 | c = 'H' 3 | 4 | #Top Cone 5 | for i in range(thickness): 6 | print (c*i).rjust(thickness-1)+c+(c*i).ljust(thickness-1) 7 | 8 | #Top Pillars 9 | for i in range(thickness+1): 10 | print (c*thickness).center(thickness*2)+(c*thickness).center(thickness*6) 11 | 12 | #Middle Belt 13 | for i in range((thickness+1)/2): 14 | print (c*thickness*5).center(thickness*6) 15 | 16 | #Bottom Pillars 17 | for i in range(thickness+1): 18 | print (c*thickness).center(thickness*2)+(c*thickness).center(thickness*6) 19 | 20 | #Bottom Cone 21 | for i in range(thickness): 22 | print ((c*(thickness-i-1)).rjust(thickness)+c+(c*(thickness-i-1)).ljust(thickness)).rjust(thickness*6) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/Text Wrap.txt: -------------------------------------------------------------------------------- 1 | import textwrap 2 | String = raw_input() 3 | width = int(raw_input()) 4 | print textwrap.fill(String, width) -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/The Minion Game.txt: -------------------------------------------------------------------------------- 1 | String = raw_input() 2 | vowels = 'AEIOU' 3 | kevin_score = 0 4 | stuart_score = 0 5 | for i in range(len(String)): 6 | if String[i] in vowels: 7 | kevin_score += (len(String) - i) 8 | else: 9 | stuart_score += (len(String) - i) 10 | if stuart_score > kevin_score: 11 | print "Stuart " + str(stuart_score) 12 | elif stuart_score < kevin_score: 13 | print "Kevin " + str(kevin_score) 14 | else: 15 | print "Draw" -------------------------------------------------------------------------------- /HackerRank Solutions/Python/Strings/sWAP cASE.txt: -------------------------------------------------------------------------------- 1 | print raw_input().swapcase() -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Advanced Join/Projects.txt: -------------------------------------------------------------------------------- 1 | SELECT PROJECT_START, PROJECT_END FROM (SELECT START_DATE AS PROJECT_START, (SELECT MIN(END_DATE) AS PROJECT_END FROM PROJECTS PR2 WHERE END_DATE > PR1.START_DATE AND NOT EXISTS (SELECT * FROM PROJECTS WHERE DATEDIFF(DAY, PR2.END_DATE, END_DATE) = 1)) AS PROJECT_END FROM PROJECTS PR1 WHERE NOT EXISTS (SELECT * FROM PROJECTS WHERE DATEDIFF(DAY, START_DATE, PR1.START_DATE) = 1)) AS PROJECTS_SORTED ORDER BY DATEDIFF(DAY, PROJECT_START, PROJECT_END), PROJECT_START; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Advanced Select/The PADS.sql: -------------------------------------------------------------------------------- 1 | SELECT CONCAT(NAME, '(', LEFT(OCCUPATION,1),')') FROM OCCUPATIONS ORDER BY NAME; 2 | SELECT CONCAT('There are a total of ', COUNT(OCCUPATION), ' ', LOWER(OCCUPATION), 's.') FROM OCCUPATIONS GROUP BY OCCUPATION ORDER BY COUNT(OCCUPATION), OCCUPATION; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Average Population.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(AVG(POPULATION)) FROM CITY; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Japan Population.txt: -------------------------------------------------------------------------------- 1 | SELECT SUM(POPULATION) FROM CITY WHERE COUNTRYCODE = 'JPN'; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Population Density Difference.txt: -------------------------------------------------------------------------------- 1 | SELECT MAX(POPULATION) - MIN(POPULATION) FROM CITY; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Revising Aggregations - Averages.txt: -------------------------------------------------------------------------------- 1 | SELECT AVG(POPULATION) FROM CITY WHERE DISTRICT = 'California'; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Revising Aggregations - The Count Function.txt: -------------------------------------------------------------------------------- 1 | SELECT COUNT(NAME) FROM CITY WHERE POPULATION > 100000; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Revising Aggregations - The Sum Function.txt: -------------------------------------------------------------------------------- 1 | SELECT SUM(POPULATION) FROM CITY WHERE DISTRICT = 'California'; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/The Blunder.txt: -------------------------------------------------------------------------------- 1 | SELECT CEIL(AVG(SALARY) - AVG(REPLACE(SALARY, 0, ''))) FROM EMPLOYEES -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 13.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(SUM(LAT_N), 4) FROM STATION WHERE LAT_N > 38.7880 AND LAT_N < 137.2345; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 14.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(MAX(LAT_N), 4) FROM STATION WHERE LAT_N < 137.2345; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 15.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(LONG_W, 4) FROM STATION WHERE LAT_N = (SELECT MAX(LAT_N) FROM STATION WHERE LAT_N < 137.2345); -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 16.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(MIN(LAT_N), 4) FROM STATION WHERE LAT_N > 38.7780; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 17.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(LONG_W, 4) FROM STATION WHERE LAT_N = (SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780); -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 18.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(ABS((MAX(LONG_W) - MAX(LAT_N))) + ABS((MIN(LONG_W) - MIN(LAT_N))), 4) FROM STATION; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 19.txt: -------------------------------------------------------------------------------- 1 | SELECT TO_CHAR(ROUND(SQRT(POWER(MIN(LONG_W) - MIN(LAT_N), 2) + POWER(MAX(LONG_W) - MAX(LAT_N), 2)), 4), '00.0000') FROM STATION; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 2.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(SUM(LAT_N),2), ROUND(SUM(LONG_W),2) FROM STATION; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Aggregation/Weather Observation Station 20.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(ST1.LAT_N, 4) from STATION ST1, STATION ST2 GROUP BY ST1.LAT_N HAVING SUM(SIGN(1 - SIGN(ST2.LAT_N - ST1.LAT_N))) / COUNT(*) > .5 LIMIT 1 -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Alternative Queries/Draw The Triangle 1.sql: -------------------------------------------------------------------------------- 1 | SET @number = 21; 2 | SELECT REPEAT('* ', @number := @number - 1) FROM information_schema.tables LIMIT 20; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Alternative Queries/Draw The Triangle 2.sql: -------------------------------------------------------------------------------- 1 | SET @number = 0; 2 | SELECT REPEAT('* ', @number := @number + 1) FROM information_schema.tables LIMIT 20; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Join/African Cities.txt: -------------------------------------------------------------------------------- 1 | SELECT CITY.NAME FROM CITY, COUNTRY WHERE CITY.COUNTRYCODE = COUNTRY.CODE AND COUNTRY.CONTINENT = 'Africa'; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Join/Asian Population.txt: -------------------------------------------------------------------------------- 1 | SELECT SUM(CITY.POPULATION) FROM CITY, COUNTRY WHERE CITY.COUNTRYCODE = COUNTRY.CODE AND COUNTRY.CONTINENT = 'Asia'; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Join/Average Population of Each Continent.txt: -------------------------------------------------------------------------------- 1 | SELECT COUNTRY.CONTINENT, ROUND(AVG(CITY.POPULATION)-0.5) FROM CITY, COUNTRY WHERE CITY.COUNTRYCODE = COUNTRY.CODE GROUP BY COUNTRY.CONTINENT; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Join/The Report.txt: -------------------------------------------------------------------------------- 1 | SELECT 2 | CASE WHEN GRADES.GRADE < 8 3 | THEN 'NULL' 4 | ELSE 5 | STUDENTS.NAME 6 | END, GRADES.GRADE, STUDENTS.MARKS 7 | FROM STUDENTS, GRADES 8 | WHERE STUDENTS.MARKS >= GRADES.MIN_MARK AND STUDENTS.MARKS <= GRADES.MAX_MARK 9 | ORDER BY GRADES.GRADE DESC, STUDENTS.NAME; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Higher Than 75 Marks.txt: -------------------------------------------------------------------------------- 1 | SELECT NAME FROM STUDENTS WHERE MARKS > 75 ORDER BY SUBSTR(NAME, -3, 3), ID; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Japanese Cities' Detail.txt: -------------------------------------------------------------------------------- 1 | SELECT * FROM CITY WHERE COUNTRYCODE = 'JPN'; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Japanese Cities' Names.txt: -------------------------------------------------------------------------------- 1 | SELECT NAME FROM CITY WHERE COUNTRYCODE = 'JPN'; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Revising the Select Query - 1.txt: -------------------------------------------------------------------------------- 1 | SELECT * FROM CITY WHERE COUNTRYCODE = 'USA' AND POPULATION > 100000; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Revising the Select Query - 2.txt: -------------------------------------------------------------------------------- 1 | SELECT NAME FROM CITY WHERE COUNTRYCODE = 'USA' AND POPULATION > 120000; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Select All.txt: -------------------------------------------------------------------------------- 1 | SELECT * FROM CITY; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Select by ID.txt: -------------------------------------------------------------------------------- 1 | SELECT * FROM CITY WHERE ID = 1661; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 1.txt: -------------------------------------------------------------------------------- 1 | SELECT CITY, STATE FROM STATION; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 10.txt: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM STATION WHERE NOT (CITY LIKE '%a' OR CITY LIKE '%e' OR CITY LIKE '%i' OR CITY LIKE '%o' OR CITY LIKE '%u'); -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 11.txt: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM STATION WHERE NOT ((CITY LIKE 'A%' OR CITY LIKE 'E%' OR CITY LIKE 'I%' OR CITY LIKE 'O%' OR CITY LIKE 'U%') AND (CITY LIKE '%a' OR CITY LIKE '%e' OR CITY LIKE '%i' OR CITY LIKE '%o' OR CITY LIKE '%u')); -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 12.txt: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM STATION WHERE (NOT (CITY LIKE 'A%' OR CITY LIKE 'E%' OR CITY LIKE 'I%' OR CITY LIKE 'O%' OR CITY LIKE 'U%') AND NOT (CITY LIKE '%a' OR CITY LIKE '%e' OR CITY LIKE '%i' OR CITY LIKE '%o' OR CITY LIKE '%u')); -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 3.txt: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM STATION WHERE MOD(ID, 2) = 0 ORDER BY CITY; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 4.txt: -------------------------------------------------------------------------------- 1 | SELECT COUNT(CITY) - COUNT(DISTINCT CITY) FROM STATION; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 5.txt: -------------------------------------------------------------------------------- 1 | SELECT * FROM (SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY), CITY) WHERE ROWNUM = 1; 2 | SELECT * FROM (SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) DESC, CITY) WHERE ROWNUM = 1; -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 6.txt: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM STATION WHERE (CITY LIKE 'A%' OR CITY LIKE 'E%' OR CITY LIKE 'I%' OR CITY LIKE 'O%' OR CITY LIKE 'U%'); -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 7.txt: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM STATION WHERE (CITY LIKE '%a' OR CITY LIKE '%e' OR CITY LIKE '%i' OR CITY LIKE '%o' OR CITY LIKE '%u'); -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 8.txt: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM STATION WHERE ((CITY LIKE 'A%' OR CITY LIKE 'E%' OR CITY LIKE 'I%' OR CITY LIKE 'O%' OR CITY LIKE 'U%') AND (CITY LIKE '%a' OR CITY LIKE '%e' OR CITY LIKE '%i' OR CITY LIKE '%o' OR CITY LIKE '%u')); -------------------------------------------------------------------------------- /HackerRank Solutions/SQL/Basic Select/Weather Observation Station 9.txt: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT CITY FROM STATION WHERE NOT (CITY LIKE 'A%' OR CITY LIKE 'E%' OR CITY LIKE 'I%' OR CITY LIKE 'O%' OR CITY LIKE 'U%'); -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Functions/Security Bijective Functions.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input().strip()) 2 | Set = set(map(int, raw_input().split())) 3 | if (len(Set) == N): 4 | print 'YES' 5 | else: 6 | print 'NO' -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Functions/Security Function Inverse.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input().strip()) 2 | line = map(int, raw_input().split()) 3 | dict = {} 4 | for i in range(N): 5 | dict[line[i]] = i+1 6 | for i in range(1, N+1): 7 | print dict[i] -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Functions/Security Functions II.txt: -------------------------------------------------------------------------------- 1 | def function(x): 2 | return x % 11 -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Functions/Security Functions.txt: -------------------------------------------------------------------------------- 1 | def function(x): 2 | return x % 11 -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Functions/Security Involution.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input().strip()) 2 | line = map(int, raw_input().split()) 3 | dict = {} 4 | flag = True 5 | for i in range(N): 6 | dict[i+1] = line[i] 7 | for i in range(1, N+1): 8 | if dict[dict[i]] != i: 9 | flag = False 10 | if flag: 11 | print 'YES' 12 | else: 13 | print 'NO' -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Functions/Security Permutations.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input().strip()) 2 | line = map(int, raw_input().split()) 3 | dict = {} 4 | for i in range(N): 5 | dict[i+1] = line[i] 6 | for i in range(1, N+1): 7 | print dict[dict[i]] -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Terminology and Concepts/Security - Message Space and Ciphertext Space.txt: -------------------------------------------------------------------------------- 1 | List = map(int, list(raw_input())) 2 | for i in range(len(List)): 3 | List[i] = (List[i] + 1) % 10 4 | print ''.join(map(str, List)) -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Terminology and Concepts/Security Encryption Scheme.txt: -------------------------------------------------------------------------------- 1 | N = int(raw_input().strip()) 2 | permute = 1 3 | for i in range(1, N+1): 4 | permute *= i 5 | print permute -------------------------------------------------------------------------------- /HackerRank Solutions/Security/Terminology and Concepts/Security Key Spaces.txt: -------------------------------------------------------------------------------- 1 | List = map(int, list(raw_input())) 2 | key = int(raw_input().strip()) 3 | for i in range(len(List)): 4 | List[i] = (List[i] + key) % 10 5 | print ''.join(map(str, List)) -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Javascript/Day 8 - Buttons Container/css/buttonsGrid.css: -------------------------------------------------------------------------------- 1 | #btns { 2 | width: 75%; 3 | } 4 | 5 | #btns > .btnStyle { 6 | width: 30%; 7 | height: 48px; 8 | font-size: 24px; 9 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Javascript/Day 8 - Buttons Container/js/buttonsGrid.js: -------------------------------------------------------------------------------- 1 | const nums = [1, 2, 3, 6, 9, 8, 7, 4]; 2 | const ids = [1, 2, 3, 6, 9, 8, 7, 4]; 3 | const btn5Node = document.querySelector('#btn5'); 4 | btn5Node.addEventListener('click', (event) => { 5 | event.preventDefault(); 6 | nums.unshift(nums.pop()); 7 | for (let i = 0; i < 8; i++) { 8 | document.getElementById('btn' + ids[i]).innerHTML = nums[i]; 9 | } 10 | }); -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Javascript/Day 8 - Create a Button/css/button.css: -------------------------------------------------------------------------------- 1 | #btn { 2 | width: 96px; 3 | height: 48px; 4 | font-size: 24px; 5 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Javascript/Day 8 - Create a Button/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Button 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Javascript/Day 8 - Create a Button/js/button.js: -------------------------------------------------------------------------------- 1 | const btnNode = document.querySelector('#btn'); 2 | let count = 0; 3 | btnNode.innerHTML = count; 4 | btnNode.addEventListener('click', (event) => { 5 | count++; 6 | btnNode.innerHTML = count; 7 | }); -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Javascript/Day 9 - Binary Calculator/css/binaryCalculator.css: -------------------------------------------------------------------------------- 1 | body { 2 | width: 33%; 3 | } 4 | 5 | #res { 6 | background-color: lightgray; 7 | border: solid; 8 | height: 48px; 9 | font-size: 20px; 10 | } 11 | 12 | .btns { 13 | width: 25%; 14 | height: 36px; 15 | font-size: 18px; 16 | margin: 0; 17 | float: left; 18 | } 19 | 20 | .btnDigits { 21 | background-color: lightgreen; 22 | color: brown; 23 | } 24 | 25 | .btnOperators01 { 26 | background-color: darkgreen; 27 | color: white; 28 | } 29 | 30 | .btnOperators02 { 31 | background-color: black; 32 | color: red; 33 | } -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Statistics/Day 0 Mean Median and Mode.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | from scipy import stats 3 | 4 | N = int(input()) 5 | X = list(map(int, input().strip().split())) 6 | 7 | print(numpy.mean(X)) 8 | print(numpy.median(X)) 9 | print(stats.mode(X)[0][0]) -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Statistics/Day 0 Weighted Mean.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | X = list(map(int, input().strip().split())) 3 | W = list(map(int, input().strip().split())) 4 | weightedSum = 0 5 | weightSum = 0 6 | 7 | for i in range(0, N): 8 | weightedSum += (W[i] * X[i]) 9 | weightSum += W[i] 10 | 11 | print(round((weightedSum / weightSum), 1)) -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Statistics/Day 1 Interquartile Range.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | X = list(map(int, input().strip().split())) 3 | F = list(map(int, input().strip().split())) 4 | S = [] 5 | for i in range(0, n): 6 | S += [X[i]] * F[i] 7 | S.sort() 8 | N = len(S) 9 | from statistics import median 10 | q1 = int(median(S[:N//2])) 11 | q3 = int(median(S[(N+1)//2:])) 12 | print(round(float(q3 - q1), 1)) -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Statistics/Day 1 Quartiles.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | X = sorted(list(map(int, input().strip().split()))) 3 | from statistics import median 4 | print(int(median(X[:n//2]))) 5 | print(int(median(X))) 6 | print(int(median(X[(n+1)//2:]))) -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Statistics/Day 1 Standard Deviation.py: -------------------------------------------------------------------------------- 1 | import math 2 | N = int(input()) 3 | X = list(map(int, input().strip().split())) 4 | mean = sum(X) / N 5 | diffSum = 0 6 | for i in X: 7 | diffSum += (i - mean) ** 2 8 | print(math.sqrt(diffSum / N)) -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Statistics/Day 2 Basic Probability.txt: -------------------------------------------------------------------------------- 1 | 1,1 - Y 2 | 1,2 - Y 3 | 1,3 - Y 4 | 1,4 - Y 5 | 1,5 - Y 6 | 1,6 - Y 7 | 2,1 - Y 8 | 2,2 - Y 9 | 2,3 - Y 10 | 2,4 - Y 11 | 2,5 - Y 12 | 2,6 - Y 13 | 3,1 - Y 14 | 3,2 - Y 15 | 3,3 - Y 16 | 3,4 - Y 17 | 3,5 - Y 18 | 3,6 - Y 19 | 4,1 - Y 20 | 4,2 - Y 21 | 4,3 - Y 22 | 4,4 - Y 23 | 4,5 - Y 24 | 4,6 - N 25 | 5,1 - Y 26 | 5,2 - Y 27 | 5,3 - Y 28 | 5,4 - Y 29 | 5,5 - N 30 | 5,6 - N 31 | 6,1 - Y 32 | 6,2 - Y 33 | 6,3 - Y 34 | 6,4 - N 35 | 6,5 - N 36 | 6,6 - N 37 | 38 | Favourable Outcomes = 30 39 | Total Outcomes = 36 40 | 41 | P(O) = 30 / 36 = 5 / 6 -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/10 Days of Statistics/Day 2 More Dice.txt: -------------------------------------------------------------------------------- 1 | 1,1 - N 2 | 1,2 - N 3 | 1,3 - N 4 | 1,4 - N 5 | 1,5 - Y 6 | 1,6 - N 7 | 2,1 - N 8 | 2,2 - N 9 | 2,3 - N 10 | 2,4 - Y 11 | 2,5 - N 12 | 2,6 - N 13 | 3,1 - N 14 | 3,2 - N 15 | 3,3 - N 16 | 3,4 - N 17 | 3,5 - N 18 | 3,6 - N 19 | 4,1 - N 20 | 4,2 - Y 21 | 4,3 - N 22 | 4,4 - N 23 | 4,5 - N 24 | 4,6 - N 25 | 5,1 - Y 26 | 5,2 - N 27 | 5,3 - N 28 | 5,4 - N 29 | 5,5 - N 30 | 5,6 - N 31 | 6,1 - N 32 | 6,2 - N 33 | 6,3 - N 34 | 6,4 - N 35 | 6,5 - N 36 | 6,6 - N 37 | 38 | Favourable Outcomes = 4 39 | Total Outcomes = 36 40 | 41 | P(O) = 4 / 36 = 1 / 9 -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 0 Hello World.py: -------------------------------------------------------------------------------- 1 | inputString = raw_input() # get a line of input from stdin and save it to our variable 2 | 3 | # Your first line of output goes here 4 | print 'Hello, World.' 5 | 6 | # Write the second line of output 7 | print inputString -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 1 Data Types.py: -------------------------------------------------------------------------------- 1 | # Declare second integer, double, and String variables. 2 | # Read and save an integer, double, and String to your variables. 3 | j = int(raw_input()) 4 | e = float(raw_input()) 5 | t = raw_input() 6 | 7 | # Print the sum of both integer variables on a new line. 8 | print i + j 9 | # Print the sum of the double variables on a new line. 10 | print d + e 11 | # Concatenate and print the String variables on a new line 12 | # The 's' variable above should be printed first. 13 | print s + t -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 10 Binary Numbers.py: -------------------------------------------------------------------------------- 1 | import sys 2 | binary_N = '{0:b}'.format(int(raw_input().strip())) 3 | max_ones, ones = 0, 0 4 | 5 | for i in range(len(binary_N)): 6 | if binary_N[i] == '1': 7 | ones += 1 8 | if max_ones < ones: 9 | max_ones = ones 10 | else: 11 | ones = 0 12 | 13 | print max_ones -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 11 2D-Arrays.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | arr = [] 5 | for arr_i in xrange(6): 6 | arr_temp = map(int, raw_input().strip().split(' ')) 7 | arr.append(arr_temp) 8 | 9 | max_sum = -9999999 10 | for i in range(1,5): 11 | for j in range(1,5): 12 | max_sum = max(arr[i][j] + arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1] + arr[i+1][j-1] + arr[i+1][j] + arr[i+1][j+1], max_sum) 13 | 14 | print max_sum -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 13 Abstract Classes.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | class Book: 3 | __metaclass__ = ABCMeta 4 | def __init__(self,title,author): 5 | self.title=title 6 | self.author=author 7 | @abstractmethod 8 | def display(): pass 9 | 10 | class MyBook(Book): 11 | def __init__(self, title, author, price): 12 | Book.__init__(self, title, author) 13 | self.price = price 14 | 15 | def display(self): 16 | print 'Title: ' + self.title 17 | print 'Author: ' + self.author 18 | print 'Price: ' + str(self.price) 19 | 20 | title=raw_input() 21 | author=raw_input() 22 | price=int(raw_input()) 23 | new_novel=MyBook(title,author,price) 24 | new_novel.display() -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 14 Scope.py: -------------------------------------------------------------------------------- 1 | class Difference: 2 | def __init__(self, a): 3 | self.__elements = a 4 | 5 | def computeDifference(self): 6 | self.maximumDifference = 0 7 | length, i, j = len(self.__elements), 0, 0 8 | while i < length: 9 | j = i + 1 10 | while j < length: 11 | self.maximumDifference = max(self.maximumDifference, abs(self.__elements[i]-self.__elements[j])) 12 | j += 1 13 | i += 1 14 | 15 | # End of Difference class 16 | 17 | _ = raw_input() 18 | a = [int(e) for e in raw_input().split(' ')] 19 | 20 | d = Difference(a) 21 | d.computeDifference() 22 | 23 | print d.maximumDifference -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 16 Exceptions-String to Integer.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import sys 3 | S = raw_input().strip() 4 | try: 5 | print int(S) 6 | except: 7 | print 'Bad String' -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 17 More Exceptions.py: -------------------------------------------------------------------------------- 1 | class Calculator: 2 | def power(self, n, p): 3 | if n < 0 or p < 0: 4 | raise Exception('n and p should be non-negative') 5 | else: 6 | return n**p 7 | myCalculator=Calculator() 8 | T=int(raw_input()) 9 | for i in range(T): 10 | n,p = map(int, raw_input().split()) 11 | try: 12 | ans=myCalculator.power(n,p) 13 | print ans 14 | except Exception,e: 15 | print e -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 2 Operators.py: -------------------------------------------------------------------------------- 1 | meal_cost, tip_percent, tax_percent = float(raw_input()), float(raw_input()), float(raw_input()) 2 | tip = meal_cost * tip_percent / 100.0 3 | tax = meal_cost * tax_percent / 100.0 4 | total_cost = meal_cost + tip + tax 5 | print 'The total meal cost is ' + str(int(round(total_cost))) + ' dollars.' -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 20 Sorting.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import sys 3 | n = int(raw_input().strip()) 4 | a = map(int,raw_input().strip().split(' ')) 5 | i, numberOfSwaps = 0, 0 6 | while i < n: 7 | j = 0 8 | while j < n-1: 9 | if a[j] > a[j+1]: 10 | a[j], a[j+1] = a[j+1], a[j] 11 | numberOfSwaps += 1 12 | j += 1 13 | if numberOfSwaps == 0: 14 | break 15 | i += 1 16 | print 'Array is sorted in ' + str(numberOfSwaps) + ' swaps.' 17 | print 'First Element: ' + str(a[0]) 18 | print 'Last Element: ' + str(a[n-1]) -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 21 Generics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | template 7 | void printArray(vector &arr) { 8 | int len = arr.size(); 9 | for (int i = 0; i < len; i++) { 10 | cout << arr[i] << endl; 11 | } 12 | } 13 | 14 | int main() { 15 | 16 | vector vInt{1, 2, 3}; 17 | vector vString{"Hello", "World"}; 18 | 19 | printArray(vInt); 20 | printArray(vString); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 25 Running Time and Complexity.py: -------------------------------------------------------------------------------- 1 | import math 2 | for test in range(int(raw_input().strip())): 3 | N = int(raw_input().strip()) 4 | flag = True 5 | for i in range(2, int(math.sqrt(N))+1): 6 | if N % i == 0: 7 | flag = False 8 | break 9 | if flag and N > 1: 10 | print 'Prime' 11 | else: 12 | print 'Not prime' -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 26 Nested Logic.py: -------------------------------------------------------------------------------- 1 | Actual_Date = map(int, raw_input().split(' ')) 2 | Expected_Date = map(int, raw_input().split(' ')) 3 | fine = 0 4 | if Expected_Date[2] < Actual_Date[2]: 5 | fine = 10000 6 | elif (Expected_Date[2] == Actual_Date[2]) and (Expected_Date[1] < Actual_Date[1]): 7 | fine = 500 * (Actual_Date[1] - Expected_Date[1]) 8 | elif (Expected_Date[2] == Actual_Date[2]) and (Expected_Date[1] == Actual_Date[1]) and (Expected_Date[0] < Actual_Date[0]): 9 | fine = 15 * (Actual_Date[0] - Expected_Date[0]) 10 | print fine -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 27 Testing.py: -------------------------------------------------------------------------------- 1 | print '5' 2 | print '3 3' 3 | print '0 2 -1' 4 | print '4 2' 5 | print '0 2 0 -1' 6 | print '5 4' 7 | print '2 0 -1 -2 1' 8 | print '6 2' 9 | print '3 2 1 0 -1 -2' 10 | print '7 6' 11 | print '3 2 1 0 -1 -2 -3' -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 28 RegEx, Patterns and Intro to Databases.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import sys 3 | import re 4 | N = int(raw_input().strip()) 5 | List = [] 6 | for a0 in xrange(N): 7 | firstName, emailID = raw_input().strip().split(' ') 8 | firstName, emailID = [str(firstName),str(emailID)] 9 | searchObj = re.search(r'[a-z]*@gmail.com', emailID) 10 | if searchObj: 11 | List.append(firstName) 12 | List.sort() 13 | for item in List: 14 | print item -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 29 Bitwise AND.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | import sys 3 | 4 | t = int(input().strip()) 5 | for a0 in range(t): 6 | n, k = input().strip().split(' ') 7 | n, k = (int(n), int(k)) 8 | a = k - 1 9 | b = (~a) & -(~a) 10 | if (a | b) > n: 11 | print (a - 1) 12 | else: 13 | print (a) -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 3 Intro to Conditionals.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import sys 3 | N = int(raw_input().strip()) 4 | if N % 2 == 0: 5 | if N <= 5 or N > 20: 6 | print 'Not Weird' 7 | else: 8 | print 'Weird' 9 | else: 10 | print 'Weird' -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 5 Loops.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import sys 3 | N = int(raw_input()) 4 | for i in range(1, 11): 5 | print N, 'x', i, '=', N*i -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 6 Let's Review.py: -------------------------------------------------------------------------------- 1 | for x in range(int(raw_input())): 2 | S = raw_input() 3 | R = S[::-1] 4 | funny_flag = True 5 | for i in range(1, len(S)): 6 | if abs(ord(S[i]) - ord(S[i-1])) != abs(ord(R[i]) - ord(R[i-1])): 7 | funny_flag = False 8 | break 9 | if funny_flag: 10 | print 'Funny' 11 | else: 12 | print 'Not Funny' -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 7 Arrays.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import sys 3 | n = int(raw_input().strip()) 4 | arr = map(int,raw_input().strip().split(' ')) 5 | arr.reverse() 6 | for i in arr: 7 | print i, -------------------------------------------------------------------------------- /HackerRank Solutions/Tutorials/30 Days of Code/Day 9 Recursion.py: -------------------------------------------------------------------------------- 1 | def factorial(N): 2 | if N <= 1: 3 | return 1 4 | else: 5 | return N * factorial(N-1) 6 | 7 | print factorial(int(raw_input())) -------------------------------------------------------------------------------- /LeetCode/README.md: -------------------------------------------------------------------------------- 1 | Content has been moved to [LeetCode Solutions](https://github.com/UtkarshPathrabe/LeetCode-Solutions). -------------------------------------------------------------------------------- /Minimum Subsequence in Non-Increasing Order.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSubsequence(self, nums: List[int]) -> List[int]: 3 | sortedNums = sorted(nums, reverse=True) 4 | total, result, currentSum = sum(sortedNums), [], 0 5 | for num in sortedNums: 6 | if currentSum + num > total - num: 7 | result.append(num) 8 | return result 9 | else: 10 | currentSum += num 11 | result.append(num) 12 | total -= num -------------------------------------------------------------------------------- /Model Algorithms/Binary Search Tree/Sorted Linked List to BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct lNode { 6 | int data; 7 | struct lNode* next; 8 | }; 9 | 10 | struct tNode { 11 | int data; 12 | struct node* left; 13 | struct node* right; 14 | }; 15 | 16 | struct tNode* newNode(int num) { 17 | struct tNode* temp = (struct node*)malloc(sizeof(struct node)); 18 | temp->data = num; 19 | temp->left = NULL; 20 | temp->right = NULL; 21 | return temp; 22 | } 23 | 24 | void push(struct lNode** head_ref, int data) { 25 | struct lNode* temp = (struct lNode*)malloc(sizeof(struct lNode)); 26 | temp->data = data; 27 | temp->next = *head; 28 | *head = temp; 29 | } 30 | 31 | int main (void) { 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Model Algorithms/Divide And Conquer/Number of Strings of Matched Parenthesis/Count Matched Parenthesis - Brute Force.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int CountMatchedPairs (int k) { 6 | if (k == 0) { 7 | return 1; 8 | } else { 9 | int count = 0; 10 | for (int j = 1; j <= k; j++) { 11 | count += CountMatchedPairs (j - 1) * CountMatchedPairs (k - j); 12 | } 13 | return count; 14 | } 15 | } 16 | 17 | int main (void) { 18 | int k; 19 | cout << "Enter the value of k:\t"; 20 | cin >> k; 21 | cout << "The number of strings of matched parenthesis of length " << 2*k << " is " << CountMatchedPairs (k) << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Model Algorithms/Dynamic Programming/Edit Distance/Edit Distance - Bottom Up.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 100 3 | 4 | using namespace std; 5 | 6 | string s1, s2; 7 | int edit[2][N] = {{0}}; 8 | 9 | int Edit () { 10 | for (int i = 0; i < N; i++) { 11 | edit[0][i] = i; 12 | } 13 | for (int i = 1; i <= s1.length(); i++) { 14 | for (int j = 0; j <= s2.length(); j++) { 15 | if (j == 0) { 16 | edit[i%2][j] = i; 17 | } else { 18 | edit[i%2][j] = min (min (edit[(i-1)%2][j] + 1, edit[i%2][j-1]+1), edit[(i-1)%2][j-1] + ((s1[i-1] == s2[j-1]) ? 0 : 1)); 19 | } 20 | } 21 | } 22 | return edit[s1.length()%2][s2.length()]; 23 | } 24 | 25 | int main (void) { 26 | cin >> s1 >> s2; 27 | cout << Edit () << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Model Algorithms/Dynamic Programming/Edit Distance/Edit Distance - Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string s1, s2; 6 | 7 | int EditDis (int i, int j) { 8 | if ((i >= 0) && (j >= 0)) { 9 | return min (min (EditDis (i, j - 1) + 1, EditDis (i - 1, j) + 1), EditDis (i - 1, j - 1) + ((s1[i] == s2[j]) ? 0 : 1)); 10 | } 11 | return 0; 12 | } 13 | 14 | int main (void) { 15 | cin >> s1 >> s2; 16 | cout << EditDis (s1.length() - 1, s2.length() -1) << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Model Algorithms/Dynamic Programming/Fibonacci Numbers Space Optimized.cpp: -------------------------------------------------------------------------------- 1 | /* Time Complexity: O(n); Space Complexity: O(1) */ 2 | 3 | #include 4 | #define LLI long long int 5 | 6 | using namespace std; 7 | 8 | LLI fib (int n) { 9 | LLI a = 0, b = 1, c; 10 | for (int i = 2; i <= n; i++) { 11 | c = a + b; 12 | a = b; 13 | b = c; 14 | } 15 | return b; 16 | } 17 | 18 | int main (void) { 19 | int n = 10; 20 | cout << "Fibonacci of " << n << " is " << fib(n) << "." << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Model Algorithms/Dynamic Programming/Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* Time Complexity: O(m*n) */ 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int LCS (string X, string Y) { 8 | int m = X.length(), n = Y.length(); 9 | int lcs[m+1][n+1]; 10 | for (int i = 0; i <= m; i++) { 11 | for (int j = 0; j <= n; j++) { 12 | if (i == 0 || j == 0) { 13 | lcs[i][j] = 0; 14 | } else if (X[i-1] == Y[j-1]) { 15 | lcs[i][j] = 1 + lcs[i-1][j-1]; 16 | } else { 17 | lcs[i][j] = max (lcs[i][j-1], lcs[i-1][j]); 18 | } 19 | } 20 | } 21 | return lcs[m][n]; 22 | } 23 | 24 | int main (void) { 25 | string X = "AGGTAB"; 26 | string Y = "GXTXAYB"; 27 | cout << "Length of LCS is " << LCS (X, Y) << "." << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Model Algorithms/Dynamic Programming/Rod Cutting.cpp: -------------------------------------------------------------------------------- 1 | /* Time Complexity: O(n^2); Space Complexity: O(n) */ 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int cutRod (int price[], int n) { 8 | int* val = (int*) calloc (n+1, sizeof(int)); 9 | for (int i = 1; i <= n; i++) { 10 | int max_val = INT_MIN; 11 | for (int j = 0; j < i; j++) { 12 | max_val = max (max_val, price[j] + val[i-j-1]); 13 | } 14 | val[i] = max_val; 15 | } 16 | return val[n]; 17 | } 18 | 19 | int main (void) { 20 | int price[] = {1, 5, 8, 9, 10, 17, 17, 20}; 21 | int size = sizeof (price) / sizeof (price[0]); 22 | cout << "Maximum Obtainable Value is " << cutRod (price, size) << "." << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Model Algorithms/Dynamic Programming/Sequence Allignment/Sequence Allignment - Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define GAP -1 3 | #define MATCH 2 4 | #define MISMATCH -2 5 | 6 | using namespace std; 7 | 8 | string s1, s2; 9 | 10 | int NWScore (int i, int j) { 11 | if (i >= 0 && j >= 0) { 12 | return max (max (NWScore (i, j - 1) + GAP, NWScore (i - 1, j) + GAP), NWScore (i - 1, j - 1) + ((s1[i] == s2[j]) ? MATCH:MISMATCH)); 13 | } 14 | return 0; 15 | } 16 | 17 | int main (void) { 18 | cin >> s1 >> s2; 19 | cout << NWScore (s1.length() - 1, s2.length() - 1) << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Model Algorithms/Mathematical Algorithms/Babylonian Method for Square Root.cpp: -------------------------------------------------------------------------------- 1 | /* Author: Utkarsh Ashok Pathrabe 2 | * Algorithm: Mathematical Algorithms 3 | */ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | float SquareRoot (float Number) { 10 | float x = Number, y = 1.0, error = 0.000001; 11 | while ((x - y) > error) { 12 | x = (x + y) / 2; 13 | y = Number / x; 14 | } 15 | return x; 16 | } 17 | 18 | int main (void) { 19 | float n; 20 | cout << "Enter any positive integer: " << endl; 21 | cin >> n; 22 | cout << "The Square Root of " << n << " is " << SquareRoot(n) << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Model Algorithms/Mathematical Algorithms/Multiplication Without Using Multiplication, Division Or Bitwise Operators and No Loops.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int Multiply (int x, int y) { 6 | if (y == 0) { 7 | return 0; 8 | } else if (y > 0) { 9 | return (x + Multiply (x, y - 1)); 10 | } else { 11 | return (-Multiply (x, -y)); 12 | } 13 | } 14 | 15 | int main (void) { 16 | int x, y; 17 | cout << "Enter two integers to multiply:" << endl; 18 | cin >> x >> y; 19 | cout << "The Product is " << Multiply (x, y) << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Model Algorithms/Mathematical Algorithms/Multiply By Seven.cpp: -------------------------------------------------------------------------------- 1 | /* Author: Utkarsh Ashok Pathrabe 2 | * Algorithm: Mathematical Algorithms */ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | long long int MultiplyBySeven (long long int number) { 9 | return ((number << 3) - number); 10 | } 11 | 12 | int main (void) { 13 | long long int n; 14 | cout << "Enter any number:" << endl; 15 | cin >> n; 16 | cout << n << " * 7 = " << MultiplyBySeven (n) << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/DataFile.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UtkarshPathrabe/Competitive-Coding/ae26d3c35d18312dd606cab6750e9e072c477cc1/OS Tutorial Practice Codes/DataFile.txt -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/DataFile2.txt: -------------------------------------------------------------------------------- 1 | Hello World!!! 2 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/DataFile3.txt: -------------------------------------------------------------------------------- 1 | File Descriptor after dup() = 1. 2 | Hello World!!! 3 | 4 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut03Code-1(16).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* defines S_IREAD & S_IWRITE*/ 3 | #include /* defines types used by sys/stat.h*/ 4 | 5 | int main(void) { 6 | int fd = creat("DataFile.txt", S_IREAD | S_IWRITE); 7 | if(fd == -1) { 8 | fprintf(stderr, "Error in opening DataFile.txt.\n"); 9 | }else{ 10 | fprintf(stdout, "DataFile.txt opened for read/write access.\nDataFile.txt is currently empty.\n"); 11 | close(fd); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut03Code-3(30).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* defines S_IREAD & S_IWRITE */ 3 | #include /* defines types used by sys/stat.h*/ 4 | #include /* defines options flags */ 5 | 6 | int main() { 7 | int fd, fdNew; 8 | fd = open("DataFile3.txt", O_WRONLY | O_CREAT, S_IREAD | S_IWRITE); 9 | fprintf(stdout, "\nOriginal File Descriptor = %d\n", fd); 10 | if(fd == -1) { 11 | fprintf(stderr, "*****ERROR*****\n"); 12 | return 1; 13 | } 14 | close(1); /* close standard output */ 15 | fdNew = dup(fd); 16 | printf("File Descriptor after dup() = %d.\n", fdNew); 17 | close(fd); 18 | printf("Hello World!!!\n"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut03Code-4(34).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define PATH_MAX 255 7 | 8 | int main(void) { 9 | char dirName[PATH_MAX + 1]; 10 | if(getcwd(dirName, PATH_MAX) == NULL) { 11 | fprintf(stderr, "Could not obtain current working directory.\n"); 12 | return 1; 13 | }else { 14 | fprintf(stdout, "Current working directory: < %s >\n", dirName); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut03Code-5(37).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | const char * const path = "C:\\Users\\Utkarsh\\Desktop\\OS Codes\\Temp"; 7 | 8 | int main(void) { 9 | fprintf(stdout, "Changing directory to < %s >\n", path); 10 | if(chdir(path) == -1) { 11 | fprintf(stderr, "*****ERROR chdir failed :- %s.\n", strerror(errno)); 12 | }else { 13 | fprintf(stdout, "chdir done!!!\nDirectory content of < %s >.\n", path); 14 | system("dir"); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut04Codes-01(11).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char *argv[]) { 8 | int md, rd; 9 | DIR *ds; 10 | struct dirent *dir; 11 | md = mkdir(argv[1]); 12 | if(md == 0) { 13 | fprintf(stdout, "%s directory is created.\n", argv[1]); 14 | }else { 15 | fprintf(stdout, "%s directory is not created.\n", argv[1]); 16 | } 17 | rd = rmdir(argv[2]); 18 | if(rd == 0) { 19 | fprintf(stdout, "%s directory is removed.\n", argv[2]); 20 | }else { 21 | fprintf(stdout, "%s directory is not revoved.\n", argv[2]); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut06Codes-01(37).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | alarm(5); /* schedule an alarm signal in 5 seconds */ 6 | printf("Looping forever ...\n"); 7 | while(1); 8 | printf("This line should never be executed.\n"); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut07Codes-01(17).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void signalHandler(int signum) { 6 | printf("Interrupt signal %d received.\n", signum); 7 | exit(signum); 8 | } 9 | 10 | int main(void) { 11 | signal(SIGINT, signalHandler); 12 | while(1) { 13 | printf("Going to sleep...\n"); 14 | sleep(1); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut07Codes-02(20).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int alarmFlag = 0; 8 | 9 | void alarmHandler() { /* signal handler */ 10 | printf("An ALARM clock signal was received\n"); 11 | alarmFlag = 1; 12 | } 13 | 14 | int main() { 15 | signal(14, alarmHandler); /* Install signal Handler */ 16 | alarm(5); 17 | printf("Looping ...\n"); 18 | while(!alarmFlag){ 19 | pause(); /* wait for a signal */ 20 | } 21 | printf("Loop ends due to alarm signal\n"); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut07Codes-03(22).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void alarmHandler(int signum) { 7 | printf("BITS Pilani!!!\n"); 8 | } 9 | 10 | int main(void) { 11 | signal(14, alarmHandler); //Set up alarm handler 12 | alarm(1); //Schedule alarm for 1 second 13 | pause(); //Do not proceed until signal is handled 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Tut08Codes-01(11).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void main() { 7 | int pfd[2]; 8 | char buf[30]; 9 | if (pipe(pfd) == -1){ 10 | perror("\n Error in pipe creation \n"); 11 | exit(1); 12 | } 13 | printf("\n writing to file descriptor %d\n", pfd[1]); 14 | write(pfd[1], "test", 5); 15 | printf("\n reading from file descriptor %d\n", pfd[0]); 16 | read(pfd[0], buf, 5); 17 | printf("read: %s \n", buf); 18 | } 19 | -------------------------------------------------------------------------------- /OS Tutorial Practice Codes/Untitled1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i; 6 | for(i = 0; i < 5; i++) { 7 | fork(); 8 | printf("Hello World!!!\n"); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /SPOJ Solutions/1112. Number Steps.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int N, x, y, p; 4 | scanf("%d", &N); 5 | while(N--){ 6 | p = 0; 7 | scanf("%d %d", &x, &y); 8 | if((x==y) || ((y+2)==x)){ 9 | if((y%2)==0){ 10 | p = x + y; 11 | }else{ 12 | p = (x + y) - 1; 13 | } 14 | printf("%d\n", p); 15 | }else{ 16 | printf("No Number\n"); 17 | } 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /SPOJ Solutions/1401 Factorial Problem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int T, N, i, p, sum; 5 | scanf("%d", &T); 6 | while(T > 0){ 7 | scanf("%d", &N); 8 | i = 1; 9 | sum = 0; 10 | p = pow(5, i); 11 | while(p <= N){ 12 | sum += (N/p); 13 | i++; 14 | p = pow(5, i); 15 | } 16 | printf("%d\n", sum); 17 | T--; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /SPOJ Solutions/2123. Candy I C Code.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int N, sum, i, array[1000]; 4 | scanf("%d", &N); 5 | while(N != -1){ 6 | sum = 0; 7 | for(i=0;iavg){ 17 | c += (array[i]-avg); 18 | } 19 | } 20 | printf("%d\n",c); 21 | } 22 | scanf("%d", &N); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /SPOJ Solutions/2123. Candy I C++ Code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int N; 8 | scanf("%d",&N); 9 | while(N!=-1){ 10 | int * A = new int[N]; 11 | int sum=0; 12 | if(A!=0){ 13 | for(int i=0;iavg) 23 | c+=(A[i]-avg); 24 | } 25 | printf("%d\n",c); 26 | } 27 | delete [] A; 28 | scanf("%d",&N); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /SPOJ Solutions/2148. Candy III C Code.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | long int N, sum, i, temp, t; 4 | scanf("%ld", &t); 5 | while(t--){ 6 | sum = 0; 7 | scanf("%ld", &N); 8 | for(i=0;i 2 | int pow(int a, int b){ 3 | if(b==0) return 1; 4 | int ret = pow(a,b/2); 5 | ret *= ret; 6 | if(b&1) 7 | ret *= a; 8 | return ret%10; 9 | } 10 | int main(){ 11 | int T,a,b; 12 | scanf("%d",&T); 13 | while(T--){ 14 | scanf("%d%d",&a,&b); 15 | printf("%d\n",pow(a%10,b)); 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /SPOJ Solutions/346. Bytelandian gold coins C Code.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int n, m; 4 | while(scanf("%d", &n)==1){ 5 | m = (n/2)+(n/3)+(n/4); 6 | if(n < m){ 7 | printf("%d\n", m); 8 | }else{ 9 | printf("%d\n", n); 10 | } 11 | } 12 | return 0; 13 | } -------------------------------------------------------------------------------- /SPOJ Solutions/346. Bytelandian gold coins C++ Code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | map dp; 8 | 9 | long long f(int n){ 10 | if(n==0) return 0; 11 | 12 | if(dp[n]!=0) return dp[n]; 13 | 14 | long long aux=f(n/2)+f(n/3)+f(n/4); 15 | 16 | if(aux>n) dp[n]=aux; 17 | else dp[n]=n; 18 | 19 | return dp[n]; 20 | } 21 | 22 | int main(){ 23 | int n; 24 | 25 | while(scanf("%d",&n)==1) printf("%lld\n",f(n)); 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /SPOJ Solutions/42. Adding Reversed Numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int N, a, b, sum, revA, revB, revS; 4 | scanf("%d", &N); 5 | while(N--){ 6 | sum = 0, revA = 0, revB = 0, revS = 0; 7 | scanf("%d %d", &a, &b); 8 | while(a){ 9 | revA = (revA*10) + (a%10); 10 | a /= 10; 11 | } 12 | while(b){ 13 | revB = (revB*10) + (b%10); 14 | b /= 10; 15 | } 16 | revS = revA + revB; 17 | while(revS){ 18 | sum = (sum*10) + (revS%10); 19 | revS /= 10; 20 | } 21 | printf("%d\n", sum); 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /SPOJ Solutions/4300. Rectangles..c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int N, ans=0, i; 4 | scanf("%d", &N); 5 | for(i=1;i<=N/i;i++){ 6 | ans += N / i - i + 1; 7 | } 8 | printf("%d\n", ans); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /SPOJ Solutions/7974. What’s Next.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int a, b, c; 4 | scanf("%d %d %d", &a, &b, &c); 5 | while(!((a==0)&&(b==0)&&(c==0))){ 6 | if((b-a)==(c-b)){ 7 | printf("AP %d\n", (2*c)-b); 8 | }else{ 9 | printf("GP %d\n", (c*c)/b); 10 | } 11 | scanf("%d %d %d", &a, &b, &c); 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /SPOJ Solutions/ABSP1_Solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int T; 5 | scanf("%d", &T); 6 | while(T--){ 7 | int N, i; 8 | scanf("%d", &N); 9 | long long int A[N], sum = 0; 10 | for(i=0;i 2 | 3 | int min(int a, int b){ 4 | if(a >= b) 5 | return b; 6 | else 7 | return a; 8 | } 9 | 10 | int main(){ 11 | int tc = 1, N, a, b, c, dp[2][3]; 12 | while(1){ 13 | scanf("%d", &N); 14 | if(N==0) 15 | break; 16 | scanf("%d %d %d", &a, &b, &c); 17 | dp[0][0] = 100000000; 18 | dp[0][1] = b; 19 | dp[0][2] = b+c; 20 | int i, r; 21 | for(i=1,r=1;i 2 | #define N 1000000 3 | 4 | long long int list[N]; 5 | long long int sequence[N]; 6 | 7 | void pre(){ 8 | int i, j, num; 9 | for(i=0;i 2 | 3 | int a[3]={1,1,1}; 4 | int p[3]={1,1,1}; 5 | 6 | int main(){ 7 | int n; 8 | scanf("%d", &n); 9 | while(--n){ 10 | a[0]=p[0]+p[1]; 11 | a[1]=p[0]+p[1]+p[2]; 12 | a[2]=p[1]+p[2]; 13 | 14 | p[0]=a[0]; 15 | p[1]=a[1]; 16 | p[2]=a[2]; 17 | } 18 | printf("%d\n", a[0]+a[1]+a[2]); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /SPOJ Solutions/APS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 10000000 3 | 4 | int smallestPrime[N]; 5 | long long int sequence[N]; 6 | 7 | void pre(){ 8 | int i, j; 9 | for(i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n, i, anger, array[100000] = {0}; 8 | long long total, ans, m; 9 | 10 | int main(){ 11 | scanf("%lld %d", &m, &n); 12 | total = ans = 0; 13 | for(i=0; i 2 | int main(){ 3 | int t; 4 | long long n; 5 | scanf("%i", &t); 6 | while(t--){ 7 | scanf("%lli", &n); 8 | (n%3) ? printf("0\n") : printf("%lld\n", n/3); 9 | } 10 | return 0; 11 | } -------------------------------------------------------------------------------- /SPOJ Solutions/CPTTRN1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int test, l, c; 7 | cin >> test; 8 | for (int t = 1; t <= test; t++) { 9 | cin >> l >> c; 10 | for (int i = 0; i < l; i++) { 11 | for (int j = 0; j < c; j++) { 12 | if ((i+j)%2 == 0) { 13 | cout << "*"; 14 | } else { 15 | cout << "."; 16 | } 17 | } 18 | cout << endl; 19 | } 20 | cout << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /SPOJ Solutions/CPTTRN2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int test, l, c; 7 | cin >> test; 8 | for (int t = 1; t <= test; t++) { 9 | cin >> l >> c; 10 | for (int i = 0; i < l; i++) { 11 | for (int j = 0; j < c; j++) { 12 | if ((i == 0) || (i == l-1) || (j == 0) || (j == c-1)) { 13 | cout << "*"; 14 | } else { 15 | cout << "."; 16 | } 17 | } 18 | cout << endl; 19 | } 20 | cout << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /SPOJ Solutions/CPTTRN3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int test, l, c; 7 | cin >> test; 8 | for (int t = 1; t <= test; t++) { 9 | cin >> l >> c; 10 | for (int i = 0; i < (3*l + 1); i++) { 11 | for (int j = 0; j < (3*c + 1); j++) { 12 | if ((i%3 == 0) || (j %3 == 0)) { 13 | cout << "*"; 14 | } else { 15 | cout << "."; 16 | } 17 | } 18 | cout << endl; 19 | } 20 | cout << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /SPOJ Solutions/CPTTRN4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int test, l, c, h, w; 7 | cin >> test; 8 | for (int t = 1; t <= test; t++) { 9 | cin >> l >> c >> h >> w; 10 | for (int i = 0; i < ((h+1)*l + 1); i++) { 11 | for (int j = 0; j < ((w+1)*c + 1); j++) { 12 | if ((i%(h+1) == 0) || (j%(w+1) == 0)) { 13 | cout << "*"; 14 | } else { 15 | cout << "."; 16 | } 17 | } 18 | cout << endl; 19 | } 20 | cout << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /SPOJ Solutions/CRAN01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | scanf("%d",&t); 7 | while(t--) 8 | { 9 | int n,m,x,y; 10 | scanf("%d%d%d%d",&n,&m,&x,&y); 11 | int r1,r2,r3,r4,result; 12 | r1 = abs(x-1) + abs(y-1); 13 | r2 = abs(x-1) + abs(y-m); 14 | r3 = abs(x-n) + abs(y-1); 15 | r4 = abs(x-n) + abs(y-m); 16 | result = max(r1,max(r2,max(r3,r4))); 17 | printf("%d\n",result); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /SPOJ Solutions/Circle Problem.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | double x1, y1, x2, y2, x3, y3, x, y, mr, mt, area; 5 | 6 | return 0; 7 | } -------------------------------------------------------------------------------- /SPOJ Solutions/DCEPC11B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | LL pow_mod(LL a,LL b,LL m) 5 | { 6 | LL x=1,y=a; 7 | while(b>0) 8 | { 9 | if(b & 1) 10 | x=(x*y)%m; 11 | y=(y*y)%m; 12 | b>>=1; 13 | } 14 | return x; 15 | } 16 | int main() 17 | { 18 | int t; 19 | scanf("%d",&t); 20 | while(t--) 21 | { 22 | LL n,p,i,result=-1,temp; 23 | scanf("%lld%lld",&n,&p); 24 | if(n>=p) 25 | { 26 | printf("0\n"); 27 | continue; 28 | } 29 | for(i=n+1;i 2 | #include 3 | int j,k,N,X,l,y,d,m,r; 4 | char *o[]={"January","February","March","April","May","June","July","August","September","October","November","December"}; 5 | main(){ 6 | scanf("%d",&N); 7 | while(N--){ 8 | scanf("%d",&X); 9 | j=k=l=d=m=y=0; 10 | while(X){ 11 | r=X%2; 12 | (j<5)?(d+=r*pow(2,j++)):(k<4)?(m+=r*pow(2,k++)):(y+=r*pow(2,l++)); 13 | X/=2; 14 | } 15 | printf("%d %s %d\n",d,o[m-1],y); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /SPOJ Solutions/DIVISION.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define ULL unsigned long long 6 | #define MOD 1000000007 7 | #define INV 333333336 //Inverse modulo of 3 with 1000000007 8 | 9 | ULL pow_mod(ULL n){ 10 | ULL x=1,p=2; 11 | while(n) 12 | { 13 | if(n&1) 14 | x=(x*p)%MOD; 15 | p=(p*p)%MOD; 16 | n>>=1; 17 | } 18 | return x; 19 | } 20 | int main(){ 21 | ULL n, result; 22 | while(scanf("%llu", &n) != EOF){ 23 | if(n&1){ 24 | result = ((pow_mod(n)+1)*INV)%MOD; 25 | printf("%llu\n",result); 26 | } 27 | else{ 28 | result = ((pow_mod(n)+2)*INV)%MOD; 29 | printf("%llu\n",result); 30 | } 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /SPOJ Solutions/FRND.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n,temp,t; 6 | long long bit[30]={0}; 7 | cin>>n; 8 | t=n; 9 | if(n==1) 10 | { 11 | cin>>temp; 12 | cout<>temp; 17 | for(int i=0;i<20;i++) 18 | { 19 | if((temp>>i) & 1) 20 | bit[i]++; 21 | } 22 | } 23 | long long res=0; 24 | for(int i=0;i<20;i++){ 25 | res+=bit[i] * (n-bit[i])* (1< 2 | int main(c,p,i){ 3 | i=10; 4 | while(i--){ 5 | p=1; 6 | while((c=getchar())>'@'){ 7 | p*=(c=='T'||c=='D'||c=='L'||c=='F')?2:1; 8 | } 9 | printf("%d\n",p); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /SPOJ Solutions/LARSUBP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MOD 1000000007 4 | int main() { 5 | int t; 6 | scanf("%d ",&t); 7 | for(int p=1;p<=t;p++) { 8 | char s[10009]; 9 | scanf("%s", s); 10 | int prev[11]; 11 | for(int i=0; i<10; i++) 12 | prev[i] = 0; 13 | int sum = 0; 14 | int j=-1, k, temp; 15 | while(s[++j] != '\0') { 16 | k = s[j] - 48; 17 | temp = k; 18 | while(k--) 19 | prev[temp] = (prev[temp]+ prev[k])%MOD; 20 | prev[temp]++; 21 | } 22 | for(int i=0;i<=9;i++) 23 | sum = (sum + prev[i])%MOD; 24 | printf("Case %d: %d\n",p,sum); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /SPOJ Solutions/LOOPEXP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | double res[100009]; 12 | void pre() 13 | { 14 | int i; 15 | res[1]=1; 16 | for(i=2;i<=100000;i++) 17 | res[i]=res[i-1] + 1.0/i; 18 | } 19 | int main() 20 | { 21 | int t; 22 | pre(); 23 | scanf("%d",&t); 24 | while(t--) 25 | { 26 | int n; 27 | scanf("%d",&n); 28 | printf("%.15lf\n",res[n]); 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /SPOJ Solutions/PIVAL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define NDIGITS 10000 /* maximum digits to compute */ 5 | #define LEN ((NDIGITS/4+1)* 14) /* array length */ 6 | long a[LEN]; 7 | long b; 8 | long c = LEN; 9 | long d; 10 | long e = 0; 11 | long f = 10000; 12 | long g; 13 | long h = 0; 14 | int dot = 1; 15 | 16 | int myprint(long val) { 17 | if (dot) { 18 | printf("%d.%03d", val/1000, val%1000); 19 | dot = 0; 20 | return 4; 21 | } 22 | else { 23 | return printf("%04d", val); 24 | } 25 | } 26 | 27 | main(){ 28 | for(; b=c-=14 ; h=myprint(e+d/f)) 29 | //printf("%04d",e+d/f) ) 30 | for(e=d%=f;g=--b*2;d/=g) 31 | d=d*b+f*(h?a[b]:f/5),a[b]=d%--g; 32 | } 33 | -------------------------------------------------------------------------------- /SPOJ Solutions/POTIONS.py: -------------------------------------------------------------------------------- 1 | import sys 2 | t=int(sys.stdin.readline()) 3 | while t: 4 | n,q=map(int,sys.stdin.readline().split()) 5 | s=raw_input().split() 6 | cum=[] 7 | cum2=[] 8 | for i in range(0,n+1): 9 | cum.append(0) 10 | cum2.append(0) 11 | for i in range(1,n+1): 12 | cum[i]=cum[i-1] + int(s[i-1]) 13 | cum2[i]=cum2[i-1]+i*(int(s[i-1])) 14 | for i in range(0,q): 15 | w,x,y,z=map(int,sys.stdin.readline().split()) 16 | lower = y+x 17 | higher = z+x 18 | result=(cum2[higher]-cum2[lower-1] + (w-x)*(cum[higher]-cum[lower-1]))%1000000007 19 | sys.stdout.write(str(result)) 20 | sys.stdout.write("\n") 21 | t=t-1 22 | -------------------------------------------------------------------------------- /SPOJ Solutions/PSPHERE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | int nTestCases = 0; 10 | scanf("%d", &nTestCases); 11 | for(int i =0; i 2 | 3 | int main(void){ 4 | int t, m, n, i, j, flag; 5 | scanf("%d", &t); 6 | while(t--){ 7 | scanf("%d %d", &m, &n); 8 | for(i = m; i <= n; i++){ 9 | if(i <= 1){ 10 | i = 2; 11 | } 12 | flag = 0; 13 | for(j = 2; j <= i/2; j++){ 14 | if(i%j==0){ 15 | flag = 1; 16 | break; 17 | } 18 | } 19 | if(!flag){ 20 | printf("%d\n", i); 21 | } 22 | } 23 | printf("\n"); 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /SPOJ Solutions/QN01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int n,start,end,tempstart; 7 | scanf("%d",&n); 8 | int array[n]; 9 | for (int i=0; i 2 | #include 3 | 4 | int main(){ 5 | int t; 6 | double d; 7 | scanf("%i", &t); 8 | while(t--){ 9 | scanf("%lf", &d); 10 | printf("%0.6lf\n", 1 - 1/(3*sqrt(d/2))); 11 | } 12 | return 0; 13 | } -------------------------------------------------------------------------------- /SPOJ Solutions/SIRNUMS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | scanf("%d",&t); 7 | while(t--){ 8 | int k,x; 9 | scanf("%d%d",&k,&x); 10 | char a[1000009]; 11 | int temp,carry,temp1; 12 | int flag=0; 13 | for(int i=x;i<=9;i++) 14 | { 15 | a[k-1]=i+48; 16 | carry=0; 17 | for(int j=k-2;j>=0;j--) 18 | { 19 | a[j]=((a[j+1]-48)*x + carry)%10 + 48; 20 | carry = ((a[j+1]-48)*x + carry)/10; 21 | } 22 | a[k]=0; 23 | temp=a[0]-48; 24 | temp1= (temp*x + carry)%10; 25 | carry=(temp*x + carry)/10; 26 | if(temp1 == i && carry==0){ 27 | printf("%s\n",a); 28 | flag=1; 29 | break; 30 | } 31 | } 32 | if(!flag) 33 | printf("Impossible\n"); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /SPOJ Solutions/SIZECON.c: -------------------------------------------------------------------------------- 1 | #define s scanf("%d",& 2 | main(r,n,x){ 3 | for(s n);n--;r+=x<0?0:x) 4 | s x); 5 | return!printf("%d",r-1); 6 | } 7 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPCIRC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int test; 7 | double x1, x2, y1, y2, r1, r2, dist; 8 | cin >> test; 9 | while (test--) { 10 | cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2; 11 | dist = sqrt (pow (x2 - x1, 2) + pow (y2 - y1, 2)); 12 | if ((dist == abs (r1 + r2)) || (dist == abs (r1 - r2))) { 13 | cout << "E" << endl; 14 | } else if (dist < abs (r1 - r2)) { 15 | cout << "I" << endl; 16 | } else { 17 | cout << "O" << endl; 18 | } 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPCPH1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, m; 7 | cin >> n; 8 | char seq[n+1], buff[2]; 9 | cin >> seq; 10 | map mymap; 11 | map :: iterator itmap; 12 | for (int i = 0; i < n; i++) { 13 | mymap[seq[i]] = seq[(i+1)%n]; 14 | } 15 | cin >> m; 16 | gets (buff); 17 | for (int i = 0; i < m; i++) { 18 | char text[1005] = {'\0'}; 19 | gets (text); 20 | for (int j = 0; text[j] != '\0'; j++) { 21 | itmap = mymap.find (text[j]); 22 | if (itmap != mymap.end()) { 23 | cout << itmap->second; 24 | } else { 25 | cout << text[j]; 26 | } 27 | } 28 | cout << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPDIV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int test, n, x, y, a; 7 | cin >> test; 8 | while (test--) { 9 | cin >> n >> x >> y; 10 | a = x; 11 | while (a < n) { 12 | if (a % y != 0) { 13 | cout << a << " "; 14 | } 15 | a += x; 16 | } 17 | cout << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPSEQ3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, m; 7 | cin >> n; 8 | int a[n+1]; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | } 12 | cin >> m; 13 | int b[m+1]; 14 | for (int i = 0; i < m; i++) { 15 | cin >> b[i]; 16 | } 17 | int i, j; 18 | for (i = 0, j = 0; (i < n) && (j < m); ) { 19 | if (a[i] > b[j]) { 20 | j++; 21 | } else if (a[i] < b[j]) { 22 | cout << a[i] << " "; 23 | i++; 24 | } else { 25 | i++; 26 | j++; 27 | } 28 | } 29 | if (j == m) { 30 | while (i < n) { 31 | cout << a[i] << " "; 32 | i++; 33 | } 34 | } 35 | cout << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPSEQ4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, m; 7 | cin >> n; 8 | int a[n+1]; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | } 12 | cin >> m; 13 | int b[m+1]; 14 | for (int i = 0; i < m; i++) { 15 | cin >> b[i]; 16 | } 17 | int i, j; 18 | for (i = 0, j = 0; (i < n) && (j < m); ) { 19 | if (a[i] > b[j]) { 20 | j++; 21 | } else if (a[i] < b[j]) { 22 | i++; 23 | } else { 24 | cout << a[i] << " "; 25 | i++; 26 | j++; 27 | } 28 | } 29 | cout << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPSEQ5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, m; 7 | cin >> n; 8 | int a[n+1]; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | } 12 | cin >> m; 13 | int b[m+1]; 14 | for (int i = 0; i < m; i++) { 15 | cin >> b[i]; 16 | } 17 | for (int i = 0; i < n; i++) { 18 | if (a[i] == b[i]) { 19 | cout << i+1 << " "; 20 | } 21 | } 22 | cout << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPSEQ6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int n, x; 7 | cin >> n >> x; 8 | int a[n+1], b[n+1]; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | } 12 | for (int i = 0; i < n; i++) { 13 | cin >> b[i]; 14 | } 15 | for (int i = 0; i < n; i++) { 16 | for (int y = -x; y <= x; y++) { 17 | if ((i+y >= 0) && (i+y < n) && (a[i] == b[i+y])) { 18 | cout << i+1 << " "; 19 | continue; 20 | } 21 | } 22 | } 23 | cout << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPSUM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int a, b, sum = 0; 7 | cin >> a >> b; 8 | for (int i = a; i <= b; i++) { 9 | sum += (i*i); 10 | } 11 | cout << sum << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /SPOJ Solutions/SMPWOW.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int x; 7 | cin >> x; 8 | cout << "W"; 9 | for (int i = 0; i < x; i++) { 10 | cout << "o"; 11 | } 12 | cout << "w" << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /SPOJ Solutions/STRHH.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int t; 7 | cin >> t; 8 | while (t--) { 9 | string str; 10 | cin >> str; 11 | int len = str.length() / 2; 12 | for (int i = 0; i < len; i += 2) { 13 | cout << str[i]; 14 | } 15 | cout << endl; 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /SPOJ Solutions/TESTINT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int a, b; 7 | cin >> a >> b; 8 | cout << a + b << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /SPOJ Solutions/VENOM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long 4 | int main(){ 5 | int t; 6 | scanf("%d",&t); 7 | while(t--){ 8 | int h,p,a; 9 | scanf("%d%d%d",&h,&p,&a); 10 | if(h<=p){ 11 | printf("1\n"); 12 | continue; 13 | } 14 | double y,z,d; 15 | LL result=0; 16 | y= p - 2*a; 17 | z= 2*(a-h); 18 | d= sqrt(y*y - 4*p*z); 19 | result = ceil((-1*y + d)/(2*p)); 20 | result = result + (result-1); 21 | printf("%lld\n",result); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /SPOJ Solutions/WAYS.c: -------------------------------------------------------------------------------- 1 | f(j){return j<2?2:f(j-1)*(4*j-2)/j;}l="%d\n";main(t,m){for(scanf(l,&t);t--;scanf(l,&m),printf(l,f(m)));return 0;} 2 | -------------------------------------------------------------------------------- /Stanford Competetive Programming/01Intro01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | int a, b; 7 | cin >> a >> b; 8 | cout << a + b << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Stanford Competetive Programming/01Intro02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main (void) { 6 | double a, sum = 0; 7 | for (int i = 0; i < 12; i++) { 8 | scanf ("%lf", &a); 9 | sum += a; 10 | } 11 | cout << (sum / 12.0) << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Stanford Competetive Programming/04DP01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long int D[10000]; 6 | 7 | int main(void) { 8 | long long int n; 9 | cout << "Enter a number" << endl; 10 | cin >> n; 11 | if(n < 0) 12 | cout << "0" << endl; 13 | else { 14 | D[0] = 1; 15 | D[1] = 1; 16 | D[2] = 1; 17 | D[3] = 2; 18 | for(long long int i = 4; i <= n; i++) { 19 | D[i] = D[i-1] + D[i-3] + D[i-4]; 20 | } 21 | cout << D[n] << endl; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Stanford Competetive Programming/04DP03(Longest Common Subsequence).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXLENGTH 51 3 | 4 | using namespace std; 5 | 6 | int array[2][MAXLENGTH] = {{0}}; 7 | 8 | int main(void) { 9 | char string1[MAXLENGTH], string2[MAXLENGTH]; 10 | cout << "Enter two Strings(with a space in between):" << endl; 11 | cin >> string1 >> string2; 12 | for (int i = 0; i < strlen(string1); i++) { 13 | for (int j = 0; j < strlen(string2); j++) { 14 | if (string1[i] == string2[j]) { 15 | array[i%2][j] = array[(i-1)%2][j-1] + 1; 16 | }else { 17 | array[i%2][j] = max(array[(i-1)%2][j], array[i%2][j-1]); 18 | } 19 | } 20 | } 21 | cout << array[(strlen(string1) - 1)%2][strlen(string2) - 1] << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Stanford Competetive Programming/04DP04(Convert to Palindrome).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXLENGTH 51 3 | 4 | using namespace std; 5 | 6 | int D[MAXLENGTH][MAXLENGTH] = {{0}}; 7 | 8 | int main(void) { 9 | char string[MAXLENGTH]; 10 | cout << "Enter the String:" << endl; 11 | cin >> string; 12 | for (int t = 2; t <= strlen(string); t++) { 13 | for (int i= 1, j = t; j <= strlen(string); i++, j++) { 14 | if (string[i] == string[j]) { 15 | D[i][j] = D[i+1][j-1]; 16 | }else { 17 | D[i][j] = 1 + min(D[i][j-1], D[i+1][j]); 18 | } 19 | } 20 | } 21 | cout << D[MAXLENGTH-1][MAXLENGTH-1] << endl; 22 | return 0; 23 | } 24 | --------------------------------------------------------------------------------