├── HackerRank ├── Jumping on the Clouds ├── Equalize the Array.py ├── Birthday Cake Candles.py ├── Designer PDF Viewer.py ├── Viral Advertising.py ├── Handshake.py ├── Filling Jars.py ├── Save the Prisoner!.py ├── Find Digits.py ├── Repeated String.py ├── Sock Merchant.py ├── Jump Out.py ├── The Hurdle Race.py ├── Utopian Tree.py ├── Counting Valleys.py ├── Mini-Max Sum.py ├── Sherlock and Squares.py ├── Bon Appétit.py ├── Kangaroo.py ├── Is Fibo.py ├── Divisible Sum Pairs.py ├── Jumping on the Clouds.PY ├── Picking Numbers.py ├── Migratory Birds.py ├── Intro to Conditional Statements.py ├── Beautiful Word.py ├── Angry Professor.py ├── Beautiful Days at the Movies.py ├── Cats and a Mouse.py ├── Algorithmic Crush.py ├── Drawing Book.py ├── Between Two Sets.py ├── Grading Students.py ├── Dynamic Array.py ├── Encryption.py ├── Electronics Shop.py ├── Breaking the Records.py ├── 2D Array - DS.py ├── Cut the sticks.py ├── Taum and B'day.py ├── Birthday Chocolate.py ├── Apple and Orange.py ├── Minimum Absolute Difference in an Array.py ├── Migratory Birds.java ├── Compare two linked lists.cpp ├── Simple Text Editor.py ├── Circular Array Rotation.py ├── Maximum Element.py ├── HackerRank in a String!.java ├── Balanced Brackets.py └── Queen's Attack II.py ├── HackerEarth ├── Dexter plays with GP.py ├── 10 Types of People.py ├── Subtraction Problem.rb ├── String length.py ├── Game of Subsets.py ├── ASCII Value.c ├── Subtraction Problem.py ├── Subtraction Problem.php ├── The Wars to Come.py ├── Subtraction Problem.c ├── Alupuri Treat.py ├── Sums and Factorials.py ├── Breaking Bad.py ├── Toss ka Boss.py ├── You Divide Me.py ├── Even in Fibo.py ├── Simple summation.py ├── Dalton and Compiler Design.py ├── Neo Integration.py ├── Game of multiples.py ├── Sherlock and Ciphers.py ├── Magic Pencil.py ├── Remains.py ├── Scoring in Exam.py ├── Life, the Universe, and Everything.c ├── Circles in the text.PY ├── Word Frequency counter.py ├── Help the hungry Grasshopper.py ├── Sports Trophy.py ├── Subtraction Problem.java ├── Ankit and Numbers.c ├── Rohan and flower.py ├── Maximum Power.py ├── The Old Monk.py ├── String length.java ├── Ravi's Trouble.py ├── Reverse.py ├── Solve for the Pastry Lover.py ├── Restaurant Problem.java ├── Algorithm Guru.java ├── Kejal Color Problem.java ├── Tom and Coins.java ├── Average sequence.java ├── Alupuri Treat.java ├── Terrible Chandu.java ├── Guruji And Hearts.java ├── Bubble Sort.c ├── THREESOME PAIRING.java ├── Wonderful Sequence.java ├── Bob and Lines.py ├── Happy Birthday.java ├── Commutative.java ├── Train Reservation.java ├── A Summing Punishment!.java ├── Chandu and Consecutive Letters.java ├── Magic Pencil.java ├── G - Game with Letters.py ├── Simple summation.java ├── IT solves Maths.java ├── All Vowels.java ├── Encoded Strings.java ├── Game of multiples.java ├── Rounding Of.java ├── IndiaHacks and Ola Cabs.java ├── Doraemon's Birthday.py ├── Paula Distance Problem.java ├── Bear and Bowling 3.java ├── Angry Lambda.java ├── Find the Students.java ├── Rohan and flower.java ├── Will you be my friend.java ├── Fight in Indiahacks.java ├── Magic Pass Code.java ├── Chandu and Primes.py ├── Srishti and the candies.java ├── Trip Analyser.java ├── Tom and Jerry with Numbers!.java ├── Code GJ'd.java ├── IndiaHacks Pb-2.java ├── Karan's Teacher.java ├── Ferb's Stripes.java ├── SKIT's Patties.java ├── Milly and Chocolates.java ├── Scoring in Exam.java ├── Maximum Resistance.java ├── Bakku Numbers.java ├── Even From End.java ├── Flop Flip.java ├── HANUMAN LANGUAGE.java ├── Play with Queries.java ├── Anil And Long Title.java ├── Count Carry Problem.java ├── Artsy Lulu.java ├── Felicity Bind's.java ├── Deadpool and his valentine.java ├── Breaking Bad.java ├── Changu Mangu in IndiaHacks.java ├── IndiaHacks Pb-1.java ├── IndiaHacks Servers.java ├── Tripping on Nothingness.java ├── IndiaHacks Pb-11.java ├── Strings Matching.java ├── RAHUL'S SHOPPING.java ├── Suresh's array.java ├── What is the maximum distance that you can cover.java ├── Castle's Stones.java ├── Ravi's Trouble.java ├── String Division.java ├── Cormen Boy and ACM ICPC.java ├── Equal Candies Distribution.java ├── SubPalindrome.java ├── Barbie's Typography.java ├── Organize Contest.java ├── G - Game with Letters.java ├── Hungry Lemurs.java └── End Semester treat.java ├── README.md ├── .gitignore ├── GeeksforGeeks ├── a^b.py ├── Small Factorial.py ├── Swap two numbers.py ├── Odd or Even.py ├── Sum of Series.py ├── Middle of Three.py ├── Remove Spaces.py ├── AND operation.py ├── Greatest of three numbers.py ├── Reverse digit.py ├── Second Largest.py ├── Sum Of Digits.py ├── Remove Duplicates.py ├── Sum of array elements.py ├── Print the Kth Digit.py ├── Sum of Array.py ├── Maximum and Minimum Of Array Elements.py ├── Average.py ├── Palindrome.py ├── Vowel or Not.py ├── Armstrong Numbers.py ├── Sort The Array.py ├── k largest elements.py ├── Addition of Two Numbers.c ├── Maximum Index.py ├── Count numbers.py ├── Greater on right side.py ├── Missing number in array.py ├── Reverse an Array.py ├── Sort a String.py ├── Word Sort.py ├── Palindrome numbers.py ├── Remove recurring digits.py ├── Smaller and Larger.py ├── Swap kth node.py ├── Area of Rectange, Right Angled Triangle and Circ.py ├── Leap Year.c ├── Leaders in an array.py ├── Number of occurrence.py ├── Palindrome String.py ├── Searching a number.py ├── Design a tiny URL or URL shortener.py ├── Value equal to index value.py ├── Count Multiples of Five.py ├── Print Elements of Array.java ├── Evaluate the x + a = b statement.py ├── Sum of all prime numbers between 1 and N.py ├── Frequency of Array Elements.py ├── Bubble Sort.c ├── Search an Element in an array.c ├── n'th node from end of linked list.c ├── Selection Sort.c └── Find Prime numbers in a range.java ├── Codechef ├── Factorial.py ├── Coins And Triangle.py ├── Minimum Maximum.py ├── Chef And Operators.py ├── Small Factorial.py ├── Splitting Candies.py ├── The Block Game.py ├── Greedy puppy.py ├── Mahasena.py ├── codeground.py ├── Chef and Feedback.py ├── Team Formation For Snackdown.py ├── Lapindromes.py ├── Simple Statistics.py ├── Nothing in Common.py ├── Similar Dishes.py ├── Tanu and Head-bob.py ├── Chef and his Sequence.py ├── Uncle Johny.py ├── Finding Square Roots.java ├── Movie Weekend.py ├── Chef and Two Strings.py ├── Your Name is Mine.py ├── Sum of palindromic numbers.py ├── Sum of Digits.java ├── Chef and String.py ├── Add Two Numbers.java ├── The Block Game.java ├── Snape and Ladder.java ├── Snake Procession.py ├── Holes in the text.java ├── Reverse The Number.java ├── Temple Land.py ├── Ciel and Receipt.java ├── Lucky Four.java ├── Turbo Sort.java ├── Is It a Snake.java ├── Chef and his Students.java ├── snackdown.py ├── The Next Palindrome.py ├── Download file.java ├── Chef and His Apartment Dues.java ├── Is It a Snake.py ├── The Lead Game.java └── Turbo Sort.c ├── FCFS OS.c ├── Learning └── Sorting │ └── quickSort.c ├── banker.c └── Cohen-Sutherland algo.cpp /HackerRank/Jumping on the Clouds: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /HackerEarth/Dexter plays with GP.py: -------------------------------------------------------------------------------- 1 | # Dexter plays with GP 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive_coding 2 | All my programming solutions. 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Added Incomplete Files 2 | 3 | HackerRank/Minimum Absolute Difference in an Array.java 4 | -------------------------------------------------------------------------------- /HackerEarth/10 Types of People.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitjain28may/Competitive_coding/HEAD/HackerEarth/10 Types of People.py -------------------------------------------------------------------------------- /HackerEarth/Subtraction Problem.rb: -------------------------------------------------------------------------------- 1 | # Subtraction Problem 2 | 3 | # https://www.hackerearth.com/problem/algorithm/subtraction-problem/ 4 | 5 | 6 | a,b= gets.chomp.split(' ').map(&:to_i) 7 | c=a-b+1 8 | puts c -------------------------------------------------------------------------------- /GeeksforGeeks/a^b.py: -------------------------------------------------------------------------------- 1 | # a^b (Set 1) 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=199 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | a,b=map(int,raw_input().split()) 7 | print a**b 8 | -------------------------------------------------------------------------------- /GeeksforGeeks/Small Factorial.py: -------------------------------------------------------------------------------- 1 | # Small Factorial 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=274 3 | 4 | from math import factorial 5 | t=int(raw_input()) 6 | for i in range(t): 7 | print factorial(int(raw_input())) 8 | -------------------------------------------------------------------------------- /GeeksforGeeks/Swap two numbers.py: -------------------------------------------------------------------------------- 1 | # Swap two numbers 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=229 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | a,b=map(int,raw_input().split()) 7 | b,a=a,b 8 | print'%d %d'%(a,b) -------------------------------------------------------------------------------- /HackerEarth/String length.py: -------------------------------------------------------------------------------- 1 | # // String length 2 | 3 | # // https://www.hackerearth.com/problem/algorithm/string-length-117/ 4 | 5 | 6 | 7 | 8 | t=int(raw_input()) 9 | for i in range(0,t): 10 | s=raw_input() 11 | print len(s) 12 | -------------------------------------------------------------------------------- /HackerRank/Equalize the Array.py: -------------------------------------------------------------------------------- 1 | # Equalize the Array 2 | # https://www.hackerrank.com/challenges/equality-in-a-array 3 | 4 | N = int(raw_input()) 5 | li = map(int, raw_input().split()) 6 | li_c = [li.count(i) for i in li] 7 | print N - max(li_c) 8 | -------------------------------------------------------------------------------- /HackerEarth/Game of Subsets.py: -------------------------------------------------------------------------------- 1 | # Game of Subsets 2 | 3 | # https://www.hackerearth.com/problem/algorithm/game-of-subsets-1/ 4 | 5 | t = int(raw_input()) 6 | while t: 7 | n = int(raw_input()) 8 | a=((10**9) +(7)) 9 | print pow(3,n,a) 10 | t=t-1 -------------------------------------------------------------------------------- /GeeksforGeeks/Odd or Even.py: -------------------------------------------------------------------------------- 1 | # Odd or Even 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=198 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | n=int(raw_input()) 7 | if n%2==0: 8 | print "even" 9 | else: 10 | print "odd" -------------------------------------------------------------------------------- /GeeksforGeeks/Sum of Series.py: -------------------------------------------------------------------------------- 1 | # Sum of Series 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=231 3 | 4 | 5 | 6 | t=int(raw_input()) 7 | for i in range(t): 8 | n=int(raw_input()) 9 | a=int((n*(n+1)/2)) 10 | print a 11 | -------------------------------------------------------------------------------- /HackerEarth/ASCII Value.c: -------------------------------------------------------------------------------- 1 | // ASCII Value 2 | 3 | //https://www.hackerearth.com/problem/algorithm/ascii-value 4 | 5 | #include 6 | 7 | int main() 8 | { 9 | char c; 10 | scanf("%c",&c); 11 | printf("%d",c); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /GeeksforGeeks/Middle of Three.py: -------------------------------------------------------------------------------- 1 | # Middle of Three 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=736 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | a=[] 8 | a=map(int,raw_input().split()) 9 | a.sort() 10 | print a[1] -------------------------------------------------------------------------------- /GeeksforGeeks/Remove Spaces.py: -------------------------------------------------------------------------------- 1 | # Remove Spaces 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=454 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | s=raw_input() 7 | r="" 8 | for j in s: 9 | if j!=' ': 10 | r=r+j 11 | print r 12 | -------------------------------------------------------------------------------- /HackerEarth/Subtraction Problem.py: -------------------------------------------------------------------------------- 1 | # Subtraction Problem 2 | 3 | # https://www.hackerearth.com/problem/algorithm/subtraction-problem/ 4 | 5 | 6 | a,b = raw_input().split() 7 | a,b=int(a),int(b) 8 | if a>b: 9 | c=a-b+1 10 | else: 11 | c=b-a+1 12 | print c -------------------------------------------------------------------------------- /HackerRank/Birthday Cake Candles.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/birthday-cake-candles 2 | # Birthday Cake Candles 3 | 4 | 5 | n = int(raw_input().strip()) 6 | height = map(int,raw_input().strip().split(' ')) 7 | ma = max(height) 8 | print height.count(ma) 9 | -------------------------------------------------------------------------------- /HackerEarth/Subtraction Problem.php: -------------------------------------------------------------------------------- 1 | 5 | 14 | -------------------------------------------------------------------------------- /HackerRank/Designer PDF Viewer.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/designer-pdf-viewer 2 | # Designer PDF Viewer 3 | 4 | h = map(int, raw_input().strip().split(' ')) 5 | word = raw_input().strip() 6 | w = len(word) 7 | hei = [h[ord(i)-97] for i in word] 8 | print max(hei)*w 9 | -------------------------------------------------------------------------------- /HackerRank/Viral Advertising.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/strange-advertising 2 | # Viral Advertising 3 | 4 | n = int(raw_input()) 5 | m = int(5) 6 | count=int(0) 7 | for i in xrange(n): 8 | y = m >> 1 9 | m = y*3 10 | count+=y 11 | print count 12 | -------------------------------------------------------------------------------- /GeeksforGeeks/AND operation.py: -------------------------------------------------------------------------------- 1 | # AND operation 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=712 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | a,b=map(int,raw_input().split()) 7 | s=int(a) 8 | for j in range(a+1,b+1): 9 | s= s&j 10 | print s 11 | -------------------------------------------------------------------------------- /GeeksforGeeks/Greatest of three numbers.py: -------------------------------------------------------------------------------- 1 | # Greatest of three numbers 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=228 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | a=[] 7 | a=map(int,raw_input().split()) 8 | a.sort() 9 | print a[2] 10 | -------------------------------------------------------------------------------- /GeeksforGeeks/Reverse digit.py: -------------------------------------------------------------------------------- 1 | # Reverse digit 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=554 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | a=long(raw_input()) 7 | s=long(0) 8 | while a!=0: 9 | s=s*10+(a%10) 10 | a=a/10 11 | print s 12 | 13 | -------------------------------------------------------------------------------- /GeeksforGeeks/Second Largest.py: -------------------------------------------------------------------------------- 1 | # Second Largest 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=538 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | a=[] 9 | a=map(int,raw_input().split()) 10 | a.sort() 11 | print a[n-2] 12 | -------------------------------------------------------------------------------- /Codechef/Factorial.py: -------------------------------------------------------------------------------- 1 | # Factorial 2 | # Problem code: FCTRL 3 | # https://www.codechef.com/problems/FCTRL 4 | 5 | 6 | 7 | t=int(raw_input()) 8 | for i in range(t): 9 | n=int(raw_input()) 10 | c=int(0) 11 | m=int(5) 12 | while n/m!=0: 13 | c=c+(n/m) 14 | m=m*5 15 | print c 16 | -------------------------------------------------------------------------------- /HackerRank/Handshake.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | # Handshake 4 | # https://www.hackerrank.com/challenges/handshake 5 | 6 | import sys 7 | 8 | 9 | T = int(raw_input().strip()) 10 | for a0 in xrange(T): 11 | N = int(raw_input().strip()) 12 | n = int(N*N - N)/2 13 | print n 14 | -------------------------------------------------------------------------------- /GeeksforGeeks/Sum Of Digits.py: -------------------------------------------------------------------------------- 1 | # Sum Of Digits 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=86 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | n=int(raw_input()) 7 | s=int(0) 8 | while n!=0: 9 | s=s+(n%10) 10 | n=n/10 11 | print s 12 | -------------------------------------------------------------------------------- /HackerRank/Filling Jars.py: -------------------------------------------------------------------------------- 1 | # Filling Jars 2 | # https://www.hackerrank.com/challenges/filling-jars 3 | 4 | 5 | N, M = map(int, raw_input().split()) 6 | sum = 0 7 | for i in xrange(M): 8 | a, b, k = map(int, raw_input().split()) 9 | sum += ((b - a + 1) * k) 10 | print(sum / N) 11 | -------------------------------------------------------------------------------- /HackerRank/Save the Prisoner!.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/save-the-prisoner 2 | # Save the Prisoner! 3 | 4 | T = int(raw_input()) 5 | for T in xrange(T): 6 | N, M, S = map(int,raw_input().split()) 7 | x = int((S+M-1)%N) 8 | if x == 0: 9 | x = N 10 | print x -------------------------------------------------------------------------------- /Codechef/Coins And Triangle.py: -------------------------------------------------------------------------------- 1 | # Coins And Triangle 2 | # Problem code: TRICOIN 3 | # https://www.codechef.com/problems/TRICOIN 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | s=int(1) 9 | j=int(2) 10 | while n>=s: 11 | s=(j*(j+1))/2 12 | j=j+1 13 | print j-2 14 | -------------------------------------------------------------------------------- /Codechef/Minimum Maximum.py: -------------------------------------------------------------------------------- 1 | # Minimum Maximum 2 | # Problem code: MNMX 3 | # https://www.codechef.com/problems/MNMX 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | a=[] 9 | a=map(int,raw_input().split()) 10 | a.sort() 11 | l=len(a) 12 | s=a[0] 13 | s=s*(l-1) 14 | print s -------------------------------------------------------------------------------- /GeeksforGeeks/Remove Duplicates.py: -------------------------------------------------------------------------------- 1 | # Remove Duplicates 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=462 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=raw_input() 8 | s="" 9 | for j in n: 10 | if j not in s: 11 | s=s+j 12 | print s 13 | -------------------------------------------------------------------------------- /GeeksforGeeks/Sum of array elements.py: -------------------------------------------------------------------------------- 1 | # Sum of array elements 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=75 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | a=[] 7 | n=int(raw_input()) 8 | a=map(int,raw_input().split()) 9 | s=int(0) 10 | for j in a: 11 | s=s+j 12 | print s -------------------------------------------------------------------------------- /HackerRank/Find Digits.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/find-digits 2 | # Find Digits 3 | 4 | 5 | t = int(raw_input().strip()) 6 | for a0 in xrange(t): 7 | sn = raw_input().strip().replace('0', "") 8 | n = int(sn) 9 | li = [(n % int(i)) for i in sn] 10 | print li.count(0) 11 | -------------------------------------------------------------------------------- /HackerRank/Repeated String.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/repeated-string 2 | # Repeated String 3 | 4 | 5 | s = raw_input().strip() 6 | n = long(raw_input().strip()) 7 | l = len(s) 8 | la = s.count('a') 9 | x = long(n / l) * la 10 | xa = long(n % l) 11 | x += s[:xa].count('a') 12 | print x 13 | -------------------------------------------------------------------------------- /HackerRank/Sock Merchant.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/sock-merchant 2 | # Sock Merchant 3 | 4 | n = int(raw_input().strip()) 5 | c = map(int, raw_input().strip().split(' ')) 6 | cs = list(set(c)) 7 | s = 0 8 | li = [(c.count(i) / 2) for i in cs] 9 | for j in li: 10 | s += j 11 | print s 12 | -------------------------------------------------------------------------------- /Codechef/Chef And Operators.py: -------------------------------------------------------------------------------- 1 | # Chef And Operators 2 | # Problem code: CHOPRT 3 | # https://www.codechef.com/problems/CHOPRT 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | a,b=map(int,raw_input().split()) 8 | if ab: 11 | print(">") 12 | else: 13 | print("=") 14 | 15 | -------------------------------------------------------------------------------- /Codechef/Small Factorial.py: -------------------------------------------------------------------------------- 1 | # Small Factorial 2 | # Problem code: FLOW018 3 | # https://www.codechef.com/problems/FLOW018 4 | 5 | def fact(n): 6 | if n==0: 7 | return 1 8 | else: 9 | return n*fact(n-1) 10 | 11 | t=int(raw_input()) 12 | for i in range(t): 13 | n=int(raw_input()) 14 | print fact(n) 15 | 16 | -------------------------------------------------------------------------------- /GeeksforGeeks/Print the Kth Digit.py: -------------------------------------------------------------------------------- 1 | # Print the Kth Digit 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=302 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | a,b,k=map(int,raw_input().split()) 7 | s=int(0) 8 | s=a**b 9 | s=str(s) 10 | l=len(s) 11 | print s[l-k] 12 | -------------------------------------------------------------------------------- /HackerEarth/The Wars to Come.py: -------------------------------------------------------------------------------- 1 | # The Wars to Come 2 | 3 | # https://www.hackerearth.com/problem/algorithm/the-wars-to-come-7/ 4 | 5 | t=int(raw_input()) 6 | a=[] 7 | a=map(int,raw_input().split()) 8 | m=int(raw_input()) 9 | a.sort() 10 | c=0 11 | x=t-1 12 | while c 9 | 10 | int main() 11 | { 12 | int x,y,z; 13 | scanf("%d%d",&x,&y); 14 | z=x-y; 15 | z=z+1; 16 | printf("%d",z); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /HackerRank/Jump Out.py: -------------------------------------------------------------------------------- 1 | # Jump Out 2 | # https://www.hackerearth.com/challenge/competitive/june-circuits-17/algorithm/jump-out-34/ 3 | 4 | n = int(raw_input()) 5 | li = map(int, raw_input().split()) 6 | s = 0 7 | m = 0 8 | for i in xrange(n, 0, -1): 9 | if li[i - 1] > s: 10 | m = i - 1 11 | s += 1 12 | print m + 1 13 | -------------------------------------------------------------------------------- /GeeksforGeeks/Average.py: -------------------------------------------------------------------------------- 1 | # Average 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=582 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | n=int(raw_input()) 7 | a=[] 8 | r="" 9 | s=int(0) 10 | a=map(int,raw_input().split()) 11 | for j in range(n): 12 | s=s+a[j] 13 | r=r+str(s/(j+1))+" " 14 | print r 15 | 16 | -------------------------------------------------------------------------------- /GeeksforGeeks/Palindrome.py: -------------------------------------------------------------------------------- 1 | # Palindrome 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=652 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | a=int(raw_input()) 8 | n=int(a) 9 | s=int(0) 10 | while n!=0: 11 | s=(s*10)+(n%10) 12 | n=n/10 13 | if s==a: 14 | print "Yes" 15 | else: 16 | print "No" 17 | -------------------------------------------------------------------------------- /GeeksforGeeks/Vowel or Not.py: -------------------------------------------------------------------------------- 1 | # Vowel or Not 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=280 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | c=raw_input() 7 | if c=='A'or c=='E' or c=='I' or c=='O' or c=='U' or c=='a' or c=='e' or c=='i' or c=='o' or c=='u': 8 | print "YES" 9 | else: 10 | print "NO" 11 | -------------------------------------------------------------------------------- /HackerEarth/Alupuri Treat.py: -------------------------------------------------------------------------------- 1 | # Alupuri Treat 2 | 3 | # https://www.hackerearth.com/problem/algorithm/alupuri-treat/ 4 | 5 | 6 | t=int(raw_input()) 7 | su=int(0) 8 | r=int(0) 9 | for i in range(t): 10 | a=[] 11 | a=map(int,raw_input().split()) 12 | su=su+a[i] 13 | r=r+a[t-i-1] 14 | if su>=r: 15 | print su-r 16 | else: 17 | print r-su -------------------------------------------------------------------------------- /HackerEarth/Sums and Factorials.py: -------------------------------------------------------------------------------- 1 | # Sums and Factorials 2 | 3 | # https://www.hackerearth.com/problem/algorithm/the-game-of-factorials/ 4 | 5 | import math 6 | t=int(raw_input()) 7 | for i in range(t): 8 | n=int(raw_input()) 9 | k=math.factorial(n) 10 | s=int(0) 11 | while k!=0: 12 | r=k%10 13 | s=s+r 14 | k=k/10 15 | print s -------------------------------------------------------------------------------- /HackerRank/The Hurdle Race.py: -------------------------------------------------------------------------------- 1 | # The Hurdle Race 2 | # https://www.hackerrank.com/challenges/the-hurdle-race 3 | 4 | 5 | n, k = raw_input().strip().split(' ') 6 | n, k = [int(n), int(k)] 7 | height = map(int, raw_input().strip().split(' ')) 8 | hurdle = max(height) - k 9 | if hurdle < 0: 10 | print 0 11 | else: 12 | print hurdle 13 | -------------------------------------------------------------------------------- /HackerRank/Utopian Tree.py: -------------------------------------------------------------------------------- 1 | # Utopian Tree 2 | # https://www.hackerrank.com/challenges/utopian-tree 3 | 4 | 5 | t = int(raw_input().strip()) 6 | for a0 in xrange(t): 7 | n = int(raw_input().strip()) 8 | x = int(n / 2) + 1 9 | x = (2**x) - 1 10 | if n % 2 == 0: 11 | print x 12 | else: 13 | print 2 * x 14 | -------------------------------------------------------------------------------- /Codechef/Splitting Candies.py: -------------------------------------------------------------------------------- 1 | # Splitting Candies 2 | # Problem code: SPCANDY 3 | # https://www.codechef.com/problems/SPCANDY 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n,k=map(int,raw_input().split()) 8 | s=int(0) 9 | r=int(0) 10 | if k!=0: 11 | s=int(n/k) 12 | r=int(n-(s*k)) 13 | else: 14 | r=n 15 | print "%d %d"%(s,r) -------------------------------------------------------------------------------- /HackerEarth/Breaking Bad.py: -------------------------------------------------------------------------------- 1 | # Breaking Bad 2 | 3 | # https://www.hackerearth.com/problem/algorithm/breaking-bad-3/ 4 | 5 | t=int(raw_input()) 6 | for i in range(0,t): 7 | l=int(raw_input()) 8 | a=[] 9 | a=map(int,raw_input().split()) 10 | a.sort() 11 | s=int(0) 12 | j=0 13 | while ja: 10 | max=a 11 | else: 12 | for i in range(2,b+1): 13 | if a%i>max: 14 | max=a%i 15 | print max -------------------------------------------------------------------------------- /GeeksforGeeks/Addition of Two Numbers.c: -------------------------------------------------------------------------------- 1 | // Addition of Two Numbers 2 | // http://www.practice.geeksforgeeks.org/problem-page.php?pid=275 3 | 4 | #include 5 | int main() 6 | { 7 | int t=0,a=0,b=0; 8 | scanf("%d",&t); 9 | while(t!=0) 10 | { 11 | scanf("%d %d",&a,&b); 12 | a=a+b; 13 | printf("%d\n",a); 14 | t--; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /HackerEarth/Dalton and Compiler Design.py: -------------------------------------------------------------------------------- 1 | # Dalton and Compiler Design 2 | 3 | # https://www.hackerearth.com/problem/algorithm/dalton-and-compiler-design/ 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | b=int(0) 9 | a=int(0) 10 | if n%2==0: 11 | n=n/2 12 | print n,n 13 | else: 14 | b=(n-1)/2 15 | a=n-b 16 | print b,a -------------------------------------------------------------------------------- /HackerRank/Mini-Max Sum.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/mini-max-sum 2 | # Mini-Max Sum 3 | 4 | def sum(start, end, li): 5 | s = int(0) 6 | for j in range(start, end): 7 | s+=li[j] 8 | return s 9 | 10 | li = map(int, raw_input().strip().split(' ')) 11 | li.sort() 12 | ma = sum(1, 5, li) 13 | mi = sum(0, 4, li) 14 | print mi, ma -------------------------------------------------------------------------------- /Codechef/Mahasena.py: -------------------------------------------------------------------------------- 1 | # Mahasena 2 | # Problem code: AMR15A 3 | # https://www.codechef.com/problems/AMR15A 4 | 5 | t=int(raw_input()) 6 | a=[] 7 | a=map(int,raw_input().split()) 8 | even=int(0) 9 | odd=int(0) 10 | for i in a: 11 | if i%2==0: 12 | even+=1 13 | else: 14 | odd+=1 15 | if even>odd: 16 | print("READY FOR BATTLE") 17 | else: 18 | print("NOT READY") -------------------------------------------------------------------------------- /HackerRank/Sherlock and Squares.py: -------------------------------------------------------------------------------- 1 | # Sherlock and Squares 2 | # https://www.hackerrank.com/challenges/sherlock-and-squares 3 | 4 | from math import * 5 | 6 | N = int(raw_input()) 7 | for i in xrange(N): 8 | i, j = map(int, raw_input().split()) 9 | ce = ceil(sqrt(i)) 10 | fl = floor(sqrt(j)) 11 | count = int(fl - ce) + 1 12 | print count 13 | -------------------------------------------------------------------------------- /GeeksforGeeks/Maximum Index.py: -------------------------------------------------------------------------------- 1 | # Maximum Index 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=129 3 | 4 | t=int(raw_input()) 5 | for k in range(t): 6 | l=int(raw_input()) 7 | a=[] 8 | ma=int(0) 9 | a=map(int,raw_input().split()) 10 | for i in range(l-1): 11 | for j in range(i+1,l): 12 | if (j-i)>ma and a[i]<=a[j]: 13 | ma=j-i 14 | print ma -------------------------------------------------------------------------------- /HackerEarth/Neo Integration.py: -------------------------------------------------------------------------------- 1 | # Neo Integration 2 | 3 | # https://www.hackerearth.com/problem/algorithm/integration-1/ 4 | 5 | t=int(raw_input()) 6 | for i in range(1,t+1): 7 | n,p,u,r,s=map(int,raw_input().split()) 8 | for j in range(0,n): 9 | if p+r<=u: 10 | p=p+r 11 | elif p-s>=0: 12 | p=p-s 13 | else: 14 | p=p 15 | print 'Case #%d: %d' %(i,p) -------------------------------------------------------------------------------- /Codechef/codeground.py: -------------------------------------------------------------------------------- 1 | import math 2 | n,ra=map(int,raw_input().split()) 3 | x=[] 4 | y=[] 5 | c=int(0) 6 | for i in range(n): 7 | a,b=map(int,raw_input().split()) 8 | x=x+[a] 9 | y=y+[b] 10 | for i in range(n-1): 11 | for j in range(i+1,n): 12 | if True: 13 | r=((x[i]-x[j])**2)+((y[i]-y[j])**2) 14 | if r>((2*ra)**2): 15 | c=c+1 16 | print c 17 | 18 | 19 | -------------------------------------------------------------------------------- /HackerRank/Bon Appétit.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/bon-appetit 2 | # Bon Appétit 3 | 4 | n, k = map(int, raw_input().split()) 5 | li = map(int, raw_input().split()) 6 | charged = int(raw_input()) 7 | s = 0 8 | for j in li: 9 | s += j 10 | s -= li[k] 11 | s = s >> 1 12 | if charged > s: 13 | print charged - s 14 | else: 15 | print "Bon Appetit" 16 | -------------------------------------------------------------------------------- /HackerEarth/Game of multiples.py: -------------------------------------------------------------------------------- 1 | # Game of multiples 2 | 3 | # https://www.hackerearth.com/problem/algorithm/multiples/ 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | n=n-1 9 | th=int(n/3) 10 | fi=int(n/5) 11 | s=int(0) 12 | fif=int(n/15) 13 | s=(th*(6+(th-1)*3))/2 14 | s=s+(fi*(10+(fi-1)*5))/2 15 | s=s-(fif*(30+(fif-1)*15))/2 16 | print s -------------------------------------------------------------------------------- /HackerEarth/Sherlock and Ciphers.py: -------------------------------------------------------------------------------- 1 | # Sherlock and Ciphers 2 | 3 | # https://www.hackerearth.com/problem/algorithm/sherlock-and-ciphers-5/ 4 | 5 | import math 6 | t=int(raw_input()) 7 | for i in range(t): 8 | s=raw_input() 9 | l=len(s) 10 | p=int(math.sqrt(l)) 11 | ss="" 12 | for i in range(0,p): 13 | j=i 14 | while j=0: 14 | s=s+str(a[j])+" " 15 | j-=1 16 | print s 17 | -------------------------------------------------------------------------------- /GeeksforGeeks/Sort a String.py: -------------------------------------------------------------------------------- 1 | # Sort a String 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=295 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | s=raw_input() 9 | a=[] 10 | for j in s: 11 | a=a+[ord(j)] 12 | a.sort() 13 | s="" 14 | for j in a: 15 | s=s+str(unichr(j)) 16 | print s 17 | -------------------------------------------------------------------------------- /HackerEarth/Magic Pencil.py: -------------------------------------------------------------------------------- 1 | # Magic Pencil 2 | 3 | # https://www.hackerearth.com/problem/algorithm/problem-1-39/ 4 | 5 | 6 | n=int(raw_input()) 7 | for i in range(1,n+1): 8 | lt=map(int,raw_input().split()) 9 | d=int(pow(lt[0],lt[1])) 10 | e=int((d/lt[2])*lt[2]) 11 | f=int((d/lt[2]+1)*lt[2]) 12 | g=int(d-e) 13 | h=int(f-d) 14 | if gb: 11 | n=a-b 12 | a=b 13 | b=n 14 | sum=sum+n 15 | else: 16 | n=b-a 17 | a=b 18 | b=n 19 | sum=sum+n 20 | print sum -------------------------------------------------------------------------------- /HackerEarth/Scoring in Exam.py: -------------------------------------------------------------------------------- 1 | # Scoring in Exam* 2 | 3 | # https://www.hackerearth.com/problem/algorithm/scoring-in-exam-1/ 4 | 5 | n,q=map(int,raw_input().split()) 6 | t=map(long,raw_input().split()) 7 | s=map(long,raw_input().split()) 8 | for i in range(1,q+1): 9 | a=int(raw_input()) 10 | t.sort() 11 | sum=long(0) 12 | for j in range(-a,0): 13 | sum=sum+t[j] 14 | print sum -------------------------------------------------------------------------------- /Codechef/Chef and Feedback.py: -------------------------------------------------------------------------------- 1 | # Chef and Feedback 2 | # Problem code: ERROR 3 | # https://www.codechef.com/problems/ERROR 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | s=raw_input() 8 | l=len(s) 9 | flag=int(0) 10 | for j in range(l-2): 11 | if s[j:j+3]=="101" or s[j:j+3]=="010": 12 | flag=1 13 | break 14 | if flag==1: 15 | print("Good") 16 | else: 17 | print("Bad") -------------------------------------------------------------------------------- /HackerEarth/Life, the Universe, and Everything.c: -------------------------------------------------------------------------------- 1 | // Life, the Universe, and Everything 2 | 3 | //https://www.hackerearth.com/problem/algorithm/life-the-universe-and-everything/ 4 | 5 | #include 6 | 7 | int main() 8 | { 9 | int i; 10 | scanf("%d",&i); 11 | while(i!=42) 12 | { 13 | printf("%d\n",i); 14 | scanf("%d",&i); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /HackerRank/Is Fibo.py: -------------------------------------------------------------------------------- 1 | # Is Fibo 2 | # https://www.hackerrank.com/challenges/is-fibo/ 3 | 4 | from math import * 5 | 6 | N = int(raw_input()) 7 | for i in xrange(N): 8 | k = int(raw_input()) 9 | k = 5 * (k * k) 10 | if ceil(sqrt(k + 4)) == floor(sqrt(k + 4)) or ceil(sqrt(k - 4)) == floor(sqrt(k - 4)): 11 | print "IsFibo" 12 | else: 13 | print "IsNotFibo" 14 | -------------------------------------------------------------------------------- /GeeksforGeeks/Word Sort.py: -------------------------------------------------------------------------------- 1 | # Word Sort 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=657 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | n=int(raw_input()) 7 | a=[] 8 | a=raw_input().split() 9 | s=[] 10 | p="" 11 | x=int(0) 12 | for j in a: 13 | s=s+[len(j)] 14 | x=x+len(j) 15 | s.sort() 16 | s=s+[x] 17 | for j in s: 18 | p=p+str(j)+" " 19 | print p 20 | -------------------------------------------------------------------------------- /HackerRank/Divisible Sum Pairs.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/divisible-sum-pairs 2 | # Divisible Sum Pairs 3 | 4 | n, k = raw_input().strip().split(' ') 5 | n, k = [int(n), int(k)] 6 | a = map(int, raw_input().strip().split(' ')) 7 | x = 0 8 | for i in xrange(n - 1): 9 | for j in xrange(i + 1, n): 10 | if (a[i] + a[j]) % k == 0: 11 | x += 1 12 | print x 13 | -------------------------------------------------------------------------------- /GeeksforGeeks/Palindrome numbers.py: -------------------------------------------------------------------------------- 1 | # Palindrome numbers 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=151 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | n=int(raw_input()) 7 | p= bin(n)[2:] 8 | l=len(p) 9 | flag=int(0) 10 | for j in range(0,l/2): 11 | if p[j]!=p[l-j-1]: 12 | flag=1 13 | break 14 | if flag==1: 15 | print "0" 16 | else: 17 | print "1" 18 | -------------------------------------------------------------------------------- /GeeksforGeeks/Remove recurring digits.py: -------------------------------------------------------------------------------- 1 | # Remove recurring digits 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=617 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=raw_input() 8 | a=[] 9 | a=a+[n[0]] 10 | m=int(0) 11 | for j in range(1,len(n)): 12 | if n[j]!=a[m]: 13 | a=a+[n[j]] 14 | m=m+1 15 | print ''.join(a) -------------------------------------------------------------------------------- /GeeksforGeeks/Smaller and Larger.py: -------------------------------------------------------------------------------- 1 | # Smaller and Larger 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=572 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | n,x=map(int,raw_input().split()) 7 | a=[] 8 | a=map(int,raw_input().split()) 9 | mi=int(0) 10 | ma=int(0) 11 | for j in a: 12 | if j<=x: 13 | mi=mi+1 14 | if j>=x: 15 | ma=ma+1 16 | print '%d %d'%(mi,ma) -------------------------------------------------------------------------------- /GeeksforGeeks/Swap kth node.py: -------------------------------------------------------------------------------- 1 | # Swap kth node 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=535 3 | 4 | 5 | 6 | t=int(raw_input()) 7 | for i in range(t): 8 | n,k=map(int,raw_input().split()) 9 | a=[] 10 | a=map(int,raw_input().split()) 11 | a[k-1],a[n-k]=a[n-k],a[k-1] 12 | s="" 13 | for j in a: 14 | s=s+str(j)+" " 15 | print s 16 | 17 | -------------------------------------------------------------------------------- /HackerEarth/Circles in the text.PY: -------------------------------------------------------------------------------- 1 | # Circles in the text 2 | 3 | # https://www.hackerearth.com/problem/algorithm/hole/ 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=raw_input() 8 | l=len(n) 9 | c=int(0) 10 | for j in n: 11 | if j=='B': 12 | c=c+2 13 | elif (j=='A' or j=='D' or j=='O' or j=='P' or j=='R'): 14 | c=c+1 15 | print c -------------------------------------------------------------------------------- /HackerRank/Jumping on the Clouds.PY: -------------------------------------------------------------------------------- 1 | # Jumping on the Clouds 2 | # https://www.hackerrank.com/challenges/jumping-on-the-clouds 3 | 4 | 5 | n = int(raw_input().strip()) 6 | c = map(int, raw_input().strip().split(' ')) 7 | count = 0 8 | i = 0 9 | while(i != n - 1): 10 | if i + 2 < n and c[i + 2] != 1: 11 | i += 2 12 | else: 13 | i += 1 14 | count += 1 15 | print count 16 | -------------------------------------------------------------------------------- /HackerRank/Picking Numbers.py: -------------------------------------------------------------------------------- 1 | # Picking Numbers 2 | # https://www.hackerrank.com/challenges/picking-numbers 3 | 4 | #!/bin/python 5 | 6 | import sys 7 | 8 | 9 | n = int(raw_input().strip()) 10 | a = map(int, raw_input().strip().split(' ')) 11 | l = len(a) 12 | count = 0 13 | li = [0 for i in range(n)] 14 | for i in xrange(0, l): 15 | li[i] = a.count(a[i]) + a.count(a[i] + 1) 16 | print max(li) 17 | -------------------------------------------------------------------------------- /Codechef/Team Formation For Snackdown.py: -------------------------------------------------------------------------------- 1 | # Team Formation For Snackdown 2 | # Problem Code: TEAMFORM 3 | 4 | # https://www.codechef.com/SNCKPA17/problems/TEAMFORM 5 | 6 | T = int(raw_input()) 7 | for i in xrange(T): 8 | n, m = map(int, raw_input().split()) 9 | for j in xrange(m): 10 | s = raw_input() 11 | if n % 2 == 0: 12 | print "yes" 13 | else: 14 | print "no" 15 | -------------------------------------------------------------------------------- /HackerEarth/Word Frequency counter.py: -------------------------------------------------------------------------------- 1 | # Word Frequency counter 2 | 3 | # https://www.hackerearth.com/problem/algorithm/word-frequency-counter/ 4 | 5 | a=[] 6 | a=raw_input().split() 7 | l=len(a) 8 | c=int(1) 9 | for i in range(0,l): 10 | for j in range(i,l): 11 | if a[i].upper()==a[j].upper() and a[i]!=' ' and i!=j: 12 | c=c+1 13 | a[j]=' ' 14 | if a[i]!=' ': 15 | print a[i].upper(),c 16 | c=1 -------------------------------------------------------------------------------- /HackerRank/Migratory Birds.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/migratory-birds 2 | # Migratory Birds 3 | 4 | 5 | n = int(raw_input().strip()) 6 | types = map(int, raw_input().strip().split(' ')) 7 | li = [0] * 5 8 | ma, mt = 0, 0 9 | for i in xrange(5): 10 | li[i] = types.count(i + 1) 11 | for j in xrange(5): 12 | if li[j] > ma: 13 | ma = li[j] 14 | mt = j + 1 15 | print mt 16 | -------------------------------------------------------------------------------- /Codechef/Lapindromes.py: -------------------------------------------------------------------------------- 1 | # Lapindromes 2 | # Problem code: LAPIN 3 | # https://www.codechef.com/problems/LAPIN 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | s=raw_input() 8 | l=len(s) 9 | a=[] 10 | b=[] 11 | for j in range(l/2): 12 | a=a+[ord(s[j])] 13 | b=b+[ord(s[l-j-1])] 14 | a.sort() 15 | b.sort() 16 | if a==b: 17 | print("YES") 18 | else: 19 | print("NO") 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Codechef/Simple Statistics.py: -------------------------------------------------------------------------------- 1 | # Simple Statistics 2 | # Problem code: SIMPSTAT 3 | # https://www.codechef.com/problems/SIMPSTAT 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n,k=map(int,raw_input().split()) 8 | a=[] 9 | f=float(0) 10 | s=float(0) 11 | a=map(int,raw_input().split()) 12 | a.sort() 13 | e=int(n-k) 14 | for i in range(k,e): 15 | s=s+a[i] 16 | e=n-(2*k) 17 | f=s/e 18 | print '%.6f'%(f) -------------------------------------------------------------------------------- /GeeksforGeeks/Area of Rectange, Right Angled Triangle and Circ.py: -------------------------------------------------------------------------------- 1 | # Area of Rectange, Right Angled Triangle and Circle 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=277 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | a=[] 7 | a=map(int,raw_input().split()) 8 | area=int(a[0]*a[1]) 9 | ar=int((a[2]*a[3])/2) 10 | ci=int(3.14*a[4]*a[4]) 11 | print '%d %d %d'%(area,ar,ci) 12 | -------------------------------------------------------------------------------- /GeeksforGeeks/Leap Year.c: -------------------------------------------------------------------------------- 1 | // Leap Year 2 | // http://www.practice.geeksforgeeks.org/problem-page.php?pid=203 3 | 4 | #include 5 | 6 | int main() { 7 | int t=0,n=0; 8 | scanf("%d",&t); 9 | while(t!=0) 10 | { 11 | scanf("%d",&n); 12 | if(n%400==0 || (n%4==0 && n%100!=0) ) 13 | printf("Yes\n"); 14 | else 15 | printf("No\n"); 16 | t--; 17 | 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /HackerEarth/Help the hungry Grasshopper.py: -------------------------------------------------------------------------------- 1 | # Help the hungry Grasshopper 2 | 3 | # https://www.hackerearth.com/problem/algorithm/help-the-hungry-grasshopper/ 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | p=1 9 | flag=0 10 | j=int(0) 11 | while j<=n: 12 | j=j+p 13 | p=p*2 14 | if j==n: 15 | flag=1 16 | break 17 | if flag==1: 18 | print "YES" 19 | else: 20 | print "NO" -------------------------------------------------------------------------------- /HackerRank/Intro to Conditional Statements.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/30-conditional-statements 2 | # Day 3: Intro to Conditional Statements 3 | 4 | N = int(raw_input().strip()) 5 | 6 | if N%2 != 0: 7 | print "Weird" 8 | else: 9 | if (N>=2 and N<=5): 10 | print "Not Weird" 11 | elif (N>=6 and N<=20): 12 | print "Weird" 13 | else: 14 | print "Not Weird" 15 | 16 | -------------------------------------------------------------------------------- /GeeksforGeeks/Leaders in an array.py: -------------------------------------------------------------------------------- 1 | # Leaders in an array 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=623 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | a=[] 9 | a=map(int,raw_input().split()) 10 | s="" 11 | for j in range(0,n-1): 12 | if a[j]>max(a[j+1:]): 13 | s=s+str(a[j])+" " 14 | s=s+str(a[n-1]) 15 | print s 16 | -------------------------------------------------------------------------------- /Codechef/Nothing in Common.py: -------------------------------------------------------------------------------- 1 | # Nothing in Common 2 | # Problem code: NOTINCOM 3 | # https://www.codechef.com/problems/NOTINCOM 4 | 5 | n = int(raw_input()); 6 | for i in range(n): 7 | a, b = map(int, raw_input().split()) 8 | aList = list(map(int, raw_input().split())) 9 | aList = aList + list(map(int, raw_input().split())) 10 | aList = list(set(aList)) 11 | le = len(aList) 12 | print (a+b) - le 13 | 14 | -------------------------------------------------------------------------------- /HackerEarth/Sports Trophy.py: -------------------------------------------------------------------------------- 1 | # Sports Trophy 2 | 3 | # https://www.hackerearth.com/problem/algorithm/interstellar/\ 4 | 5 | 6 | t=int(raw_input()) 7 | for i in range(0,t): 8 | n,k=map(int,raw_input().split()) 9 | a=[] 10 | a=map(int,raw_input().split()) 11 | l=len(a) 12 | s=0 13 | for j in a: 14 | s=s+j 15 | if((s/l)>=k): 16 | print "Yes" 17 | else: 18 | print "No" 19 | -------------------------------------------------------------------------------- /HackerRank/Beautiful Word.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/contests/w31/challenges/beautiful-word 2 | # Beautiful Word 3 | 4 | w = raw_input().strip() 5 | li = ['a', 'e', 'i', 'o', 'u', 'y'] 6 | l = len(w) 7 | flag = int(0) 8 | for j in range(0, l-1): 9 | if w[j] == w[j+1] or (w[j] in li and w[j+1] in li): 10 | flag = 1 11 | break 12 | if flag == 1: 13 | print "No" 14 | else: 15 | print "Yes" 16 | 17 | -------------------------------------------------------------------------------- /Codechef/Similar Dishes.py: -------------------------------------------------------------------------------- 1 | # Similar Dishes 2 | # Problem code: SIMDISH 3 | # https://www.codechef.com/APRIL17/problems/SIMDISH 4 | 5 | n = int(raw_input()) 6 | for i in range(n): 7 | d1 = raw_input().split() 8 | d2 = raw_input().split() 9 | c = int(0) 10 | for j in d1: 11 | if j in d2: 12 | c += 1 13 | if c >= 2: 14 | print "similar" 15 | else: 16 | print "dissimilar" 17 | -------------------------------------------------------------------------------- /HackerEarth/Subtraction Problem.java: -------------------------------------------------------------------------------- 1 | // Subtraction Problem 2 | 3 | // https://www.hackerearth.com/problem/algorithm/subtraction-problem/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s= new Scanner(System.in); 10 | int a=s.nextInt(); 11 | int b=s.nextInt(); 12 | int c=a-b; 13 | c=c+1; 14 | System.out.println(c); 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /HackerRank/Angry Professor.py: -------------------------------------------------------------------------------- 1 | # Angry Professor 2 | # https://www.hackerrank.com/challenges/angry-professor 3 | 4 | t = int(raw_input().strip()) 5 | for a0 in xrange(t): 6 | n, k = raw_input().strip().split(' ') 7 | n, k = [int(n), int(k)] 8 | a = map(int, raw_input().strip().split(' ')) 9 | li = [i <= 0 for i in a] 10 | if li.count(True) >= k: 11 | print "NO" 12 | else: 13 | print "YES" 14 | -------------------------------------------------------------------------------- /HackerRank/Beautiful Days at the Movies.py: -------------------------------------------------------------------------------- 1 | # Beautiful Days at the Movies 2 | # https://www.hackerrank.com/challenges/beautiful-days-at-the-movies 3 | 4 | i, j, k = map(int, raw_input().strip().split(' ')) 5 | count = 0 6 | for x in range(i, j + 1): 7 | n = x 8 | s = 0 9 | while(n != 0): 10 | s = (s * 10) + n % 10 11 | n = n / 10 12 | if abs(s - x) % k == 0: 13 | count += 1 14 | print count 15 | -------------------------------------------------------------------------------- /Codechef/Tanu and Head-bob.py: -------------------------------------------------------------------------------- 1 | # Tanu and Head-bob 2 | # Problem code: HEADBOB 3 | # https://www.codechef.com/problems/HEADBOB 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | s=raw_input() 9 | flag=int(0) 10 | for j in s: 11 | if j=='I': 12 | print("INDIAN") 13 | flag=1 14 | break 15 | elif j=='Y': 16 | print("NOT INDIAN") 17 | flag=1 18 | break 19 | if flag==0: 20 | print("NOT SURE") -------------------------------------------------------------------------------- /GeeksforGeeks/Number of occurrence.py: -------------------------------------------------------------------------------- 1 | # Number of occurrence 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=577 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n,e=map(int,raw_input().split()) 8 | a=[] 9 | a=map(int,raw_input().split()) 10 | c=int(0) 11 | for j in a: 12 | if j==e: 13 | c+=1 14 | if c==0: 15 | print "-1" 16 | else: 17 | print c 18 | -------------------------------------------------------------------------------- /HackerEarth/Ankit and Numbers.c: -------------------------------------------------------------------------------- 1 | // Ankit and Numbers 2 | 3 | // https://www.hackerearth.com/problem/algorithm/ankit-and-numbers-8/ 4 | 5 | #include 6 | #include 7 | int main() 8 | { 9 | int n=0; 10 | scanf("%d",&n); 11 | while(n!=0) 12 | { 13 | int a=0; 14 | scanf("%d",&a); 15 | long sum=(a*(a+1))/2*(pow(2,a-1)); 16 | 17 | printf("%ld",sum); 18 | n--; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /HackerEarth/Rohan and flower.py: -------------------------------------------------------------------------------- 1 | # // Rohan and flower* 2 | 3 | # // https://www.hackerearth.com/problem/algorithm/rohan-and-flower/ 4 | 5 | 6 | 7 | f=int(raw_input()) 8 | h=int(raw_input()) 9 | a=[] 10 | a=map(int,raw_input().split()) 11 | a.sort() 12 | l=len(a) 13 | c=0 14 | for i in range(0,l-2): 15 | for j in range(i+1,l-1): 16 | for k in range(j+1,l): 17 | p=int(a[i]*a[j]*a[k]) 18 | if p%h==0: 19 | c=c+1 20 | print c -------------------------------------------------------------------------------- /HackerRank/Cats and a Mouse.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/cats-and-a-mouse 2 | # Cats and a Mouse 3 | 4 | q = int(raw_input().strip()) 5 | for a0 in xrange(q): 6 | x, y, z = raw_input().strip().split(' ') 7 | x, y, z = [int(x), int(y), int(z)] 8 | a = abs(x - z) 9 | b = abs(y - z) 10 | if a < b: 11 | print "Cat A" 12 | elif a > b: 13 | print "Cat B" 14 | else: 15 | print "Mouse C" 16 | -------------------------------------------------------------------------------- /HackerEarth/Maximum Power.py: -------------------------------------------------------------------------------- 1 | # Maximum Power* 2 | 3 | # https://www.hackerearth.com/problem/algorithm/maximum-power/ 4 | 5 | n,d=map(int,raw_input().split()) 6 | a=[] 7 | q=[] 8 | a=map(int,raw_input().split()) 9 | for j in range(d): 10 | k,b,c=raw_input().split() 11 | b=int(b) 12 | c=int(c) 13 | if k=='P': 14 | a[b-1]=c 15 | else: 16 | ma=int(-100000) 17 | q=a[b-1:c] 18 | q.sort() 19 | l=len(q) 20 | print max(q[0]*q[1],q[l-2]*q[l-1]) -------------------------------------------------------------------------------- /HackerEarth/The Old Monk.py: -------------------------------------------------------------------------------- 1 | # The Old Monk 2 | 3 | # https://www.hackerearth.com/problem/algorithm/the-old-monk/ 4 | 5 | 6 | t=int(raw_input()) 7 | for i in range(0,t): 8 | l=int(raw_input()) 9 | a=map(long,raw_input().split()) 10 | b=map(long,raw_input().split()) 11 | max=0 12 | c=0 13 | for j in range(0,l): 14 | for k in range(0,l): 15 | if ((b[k]>=a[j]) and (k>=j)): 16 | c=k-j 17 | if c>max: 18 | max=c 19 | print max 20 | -------------------------------------------------------------------------------- /HackerRank/Algorithmic Crush.py: -------------------------------------------------------------------------------- 1 | # Algorithmic Crush 2 | # https://www.hackerrank.com/challenges/crush 3 | 4 | 5 | N, M = map(int, raw_input().split()) 6 | li = [0 for i in xrange(N)] 7 | for i in xrange(M): 8 | a, b, k = map(int, raw_input().split()) 9 | li[a - 1] = li[a - 1] + k 10 | if b < N: 11 | li[b] -= k 12 | s = 0 13 | maxx = 0 14 | for i in li: 15 | s += i 16 | if s > maxx: 17 | maxx = s 18 | print maxx 19 | -------------------------------------------------------------------------------- /Codechef/Chef and his Sequence.py: -------------------------------------------------------------------------------- 1 | # Chef and his Sequence 2 | # Problem code: CHEFSQ 3 | # https://www.codechef.com/problems/CHEFSQ 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=[] 8 | f=[] 9 | ln=int(raw_input()) 10 | n=map(int,raw_input().split()) 11 | lf=int(raw_input()) 12 | f=map(int,raw_input().split()) 13 | flag=int(0) 14 | for j in f: 15 | if j in n: 16 | flag+=1 17 | if flag==lf: 18 | print("Yes") 19 | else: 20 | print("No") -------------------------------------------------------------------------------- /Codechef/Uncle Johny.py: -------------------------------------------------------------------------------- 1 | # Uncle Johny 2 | # Problem code: JOHNY 3 | # https://www.codechef.com/problems/JOHNY 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | a=[] 9 | a=map(int,raw_input().split()) 10 | k=int(raw_input()) 11 | s=a[k-1] 12 | st=int(0) 13 | a.sort() 14 | if a[n/2]>=s: 15 | e=(n/2) 16 | else: 17 | st=(n/2) 18 | e=n-1 19 | for j in range(0,(e+1-st)): 20 | if a[j+st]==s: 21 | break 22 | print (j+st+1) -------------------------------------------------------------------------------- /GeeksforGeeks/Palindrome String.py: -------------------------------------------------------------------------------- 1 | # Palindrome String 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=222 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | s=raw_input() 9 | l=int(n/2) 10 | flag=int(0) 11 | for j in range(0,l): 12 | if s[j]!=s[n-j-1]: 13 | flag=1 14 | break 15 | if flag==1: 16 | print "No" 17 | else: 18 | print "Yes" 19 | -------------------------------------------------------------------------------- /HackerEarth/String length.java: -------------------------------------------------------------------------------- 1 | // String length 2 | 3 | // https://www.hackerearth.com/problem/algorithm/string-length-117/ 4 | 5 | 6 | 7 | import java.util.*; 8 | class TestClass { 9 | public static void main(String args[] ) throws Exception { 10 | 11 | Scanner s=new Scanner(System.in); 12 | int n=s.nextInt(); 13 | while(n!=0) 14 | { 15 | String st=s.next(); 16 | int l=st.length(); 17 | System.out.println(l); 18 | n--; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /HackerRank/Drawing Book.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/drawing-book 2 | # Drawing Book 3 | 4 | 5 | def solve(n, p): 6 | z = 0 7 | x = n - p 8 | y = p - 1 9 | if n % 2 != 0: 10 | x -= 1 11 | if x > y: 12 | z = ((y >> 1) + y % 2) 13 | else: 14 | z = ((x >> 1) + x % 2) 15 | return z 16 | 17 | 18 | n = int(raw_input().strip()) 19 | p = int(raw_input().strip()) 20 | result = solve(n, p) 21 | print(result) 22 | -------------------------------------------------------------------------------- /HackerEarth/Ravi's Trouble.py: -------------------------------------------------------------------------------- 1 | # Ravi's Trouble 2 | 3 | # https://www.hackerearth.com/problem/algorithm/ravis-trouble-3/ 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | s=raw_input() 8 | ss=raw_input() 9 | l=len(s) 10 | ll=len(ss) 11 | k=int(0) 12 | flag=int(0) 13 | for j in ss: 14 | flag=0 15 | for k in range(k,l): 16 | if j==s[k]: 17 | flag=1 18 | break 19 | if flag==0: 20 | break 21 | if flag==0: 22 | print "NO" 23 | else: 24 | print "YES" -------------------------------------------------------------------------------- /Codechef/Finding Square Roots.java: -------------------------------------------------------------------------------- 1 | // Finding Square Roots 2 | // Problem code: FSQRT 3 | // https://www.codechef.com/problems/FSQRT 4 | 5 | 6 | import java.io.*; 7 | class sq{ 8 | public static void main(String arg[])throws IOException{ 9 | BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 10 | int t=Integer.parseInt(br.readLine()); 11 | while(t!=0){ 12 | int N =Integer.parseInt(br.readLine()); 13 | System.out.println((int)Math.sqrt(N)); 14 | t--; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /GeeksforGeeks/Searching a number.py: -------------------------------------------------------------------------------- 1 | # Searching a number 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=237 3 | 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n,e=map(int,raw_input().split()) 8 | a=[] 9 | a=map(int,raw_input().split()) 10 | flag=int(0) 11 | for j in range(0,n): 12 | if a[j]==e: 13 | flag=1 14 | break 15 | if flag==1: 16 | print j+1 17 | else: 18 | print "-1" 19 | -------------------------------------------------------------------------------- /HackerEarth/Reverse.py: -------------------------------------------------------------------------------- 1 | # Reverse* 2 | 3 | # https://www.hackerearth.com/problem/algorithm/reverse/ 4 | 5 | t=int(raw_input()) 6 | for i in range(1,t+1): 7 | print i 8 | a,b=raw_input().split() 9 | e=((10**long(a))) 10 | c=0 11 | if b=='10': 12 | c=1 13 | else: 14 | b=long(b) 15 | e=(e/b)+1 16 | for j in range(0,e): 17 | n=j*b 18 | r=0 19 | s=0 20 | while n!=0: 21 | r=n%10 22 | s=s*10+r 23 | n=n/10 24 | if s%b==0: 25 | c=c+1 26 | print c%1000000007 -------------------------------------------------------------------------------- /Codechef/Movie Weekend.py: -------------------------------------------------------------------------------- 1 | # Movie Weekend 2 | # Problem code: MOVIEWKN 3 | # https://www.codechef.com/problems/MOVIEWKN 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n=int(raw_input()) 8 | r=[] 9 | l=[] 10 | max=int(0) 11 | ind=int(n) 12 | rate=int(0) 13 | l=map(int,raw_input().split()) 14 | r=map(int,raw_input().split()) 15 | for j in range(n): 16 | if r[j]*l[j]>=max: 17 | max=r[j]*l[j] 18 | if r[j]>rate: 19 | rate=r[j] 20 | ind=j 21 | print (ind+1) 22 | -------------------------------------------------------------------------------- /HackerEarth/Solve for the Pastry Lover.py: -------------------------------------------------------------------------------- 1 | # Solve for the Pastry Lover 2 | 3 | # https://www.hackerearth.com/problem/algorithm/solve-for-the-pastry-lover/ 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | l=int(raw_input()) 8 | s=raw_input() 9 | min=10000 10 | c=0 11 | flag=0 12 | for j in range(0,l-1): 13 | for k in range(j+1,l): 14 | if s[j]==s[k]: 15 | c=k-j 16 | if(c= 38: 8 | r = int(i/5) 9 | s = int((r+1)*5) 10 | if s-i < 3: 11 | i = s 12 | print i 13 | 14 | n = int(raw_input().strip()) 15 | grades = [] 16 | grades_i = 0 17 | for grades_i in xrange(n): 18 | grades_t = int(raw_input().strip()) 19 | grades.append(grades_t) 20 | solve(grades) 21 | -------------------------------------------------------------------------------- /Codechef/Your Name is Mine.py: -------------------------------------------------------------------------------- 1 | # Your Name is Mine 2 | # Problem code: NAME2 3 | # https://www.codechef.com/problems/NAME2 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | m,n=raw_input().split() 8 | l1=len(m) 9 | l2=len(n) 10 | flag=int(0) 11 | if l1>l2: 12 | for j in range(l1-l2+1): 13 | if m[j:j+l2]==n: 14 | flag=1 15 | break 16 | else: 17 | for j in range(l2-l1+1): 18 | if n[j:j+l1]==m: 19 | flag=1 20 | break 21 | if flag==1: 22 | print("YES") 23 | else: 24 | print("NO") 25 | 26 | # efn -------------------------------------------------------------------------------- /HackerEarth/Algorithm Guru.java: -------------------------------------------------------------------------------- 1 | // Algorithm Guru* 2 | 3 | // https://www.hackerearth.com/problem/algorithm/algorithm-guru 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s=new Scanner(System.in); 10 | int t=s.nextInt(); 11 | while(t!=0) 12 | { 13 | int n=s.nextInt(); 14 | int sum=s.nextInt(); 15 | int a[]=new int[n]; 16 | for(int i=0;is] 26 | print s -------------------------------------------------------------------------------- /HackerEarth/Kejal Color Problem.java: -------------------------------------------------------------------------------- 1 | // Kejal Color Problem 2 | 3 | // https://www.hackerearth.com/problem/algorithm/kejal-color-problem/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int n=s.nextInt(); 12 | int a[]= new int[n]; 13 | int c=1; 14 | for(int i=0;i a: 15 | a = i + j 16 | print a 17 | -------------------------------------------------------------------------------- /Codechef/Sum of palindromic numbers.py: -------------------------------------------------------------------------------- 1 | # Sum of palindromic numbers 2 | # Problem code: SPALNUM 3 | # https://www.codechef.com/problems/SPALNUM/ 4 | 5 | 6 | 7 | n=int(raw_input()) 8 | for i in range(0,n): 9 | a,b=map(int,raw_input().split()) 10 | c=int(0) 11 | for j in range(a,b+1): 12 | x=j 13 | s=0 14 | y=str(j) 15 | l=len(y) 16 | if l%2==0: 17 | if y[:l/2]==y[l/2:l]: 18 | c=c+j 19 | else: 20 | if y[:l/2]==y[(l+1)/2:l]: 21 | c=c+j 22 | print c -------------------------------------------------------------------------------- /HackerEarth/Average sequence.java: -------------------------------------------------------------------------------- 1 | // Average sequence 2 | 3 | // https://www.hackerearth.com/problem/algorithm/average-sequence-4/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s=new Scanner(System.in); 10 | int l=s.nextInt(); 11 | long a[]=new long[l]; 12 | for(int i=0;i ma: 13 | ma = i 14 | mas += 1 15 | return mas, mis 16 | 17 | 18 | n = int(raw_input().strip()) 19 | s = map(int, raw_input().strip().split(' ')) 20 | result = getRecord(s) 21 | print " ".join(map(str, result)) 22 | -------------------------------------------------------------------------------- /HackerEarth/Alupuri Treat.java: -------------------------------------------------------------------------------- 1 | // Alupuri Treat 2 | 3 | // https://www.hackerearth.com/problem/algorithm/alupuri-treat/ 4 | 5 | 6 | import java.util.*; 7 | class test 8 | { 9 | public static void main(String args[]) 10 | { 11 | Scanner s=new Scanner(System.in); 12 | int m=s.nextInt(); 13 | int p=0,r=0; 14 | for(int k=0;k=r) 24 | x=p-r; 25 | else 26 | x=r-p; 27 | System.out.println(x); 28 | } 29 | } -------------------------------------------------------------------------------- /HackerEarth/Terrible Chandu.java: -------------------------------------------------------------------------------- 1 | // Terrible Chandu 2 | 3 | // https://www.hackerearth.com/problem/algorithm/terrible-chandu/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String arg[]) 8 | { 9 | Scanner s =new Scanner(System.in); 10 | 11 | int t=s.nextInt(); 12 | while(t!=0) 13 | { 14 | String ss=s.next(); 15 | int l=ss.length(); 16 | for(int i=l-1;i>=0;i--) 17 | { 18 | System.out.print(ss.charAt(i)); 19 | } 20 | System.out.println(); 21 | t--; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /HackerRank/2D Array - DS.py: -------------------------------------------------------------------------------- 1 | # 2D Array - DS 2 | # https://www.hackerrank.com/challenges/2d-array 3 | 4 | 5 | arr = [] 6 | for arr_i in xrange(6): 7 | arr_temp = map(int, raw_input().strip().split(' ')) 8 | arr.append(arr_temp) 9 | pre = -1000 10 | for i in range(4): 11 | for j in range(4): 12 | s = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 13 | 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2] 14 | if s > pre: 15 | pre = s 16 | print pre 17 | -------------------------------------------------------------------------------- /HackerRank/Cut the sticks.py: -------------------------------------------------------------------------------- 1 | # Cut the sticks 2 | # https://www.hackerrank.com/challenges/cut-the-sticks 3 | 4 | #!/bin/python 5 | 6 | import sys 7 | 8 | 9 | n = int(raw_input().strip()) 10 | arr = map(int, raw_input().strip().split(' ')) 11 | while(True): 12 | l = len(arr) 13 | if l == 0: 14 | break 15 | print l 16 | mi = min(arr) 17 | i = 0 18 | while(i != l): 19 | if arr[i] <= mi: 20 | arr.remove(arr[i]) 21 | l -= 1 22 | i -= 1 23 | else: 24 | arr[i] -= mi 25 | i += 1 26 | -------------------------------------------------------------------------------- /Codechef/Sum of Digits.java: -------------------------------------------------------------------------------- 1 | // Sum of Digits 2 | // Problem code: FLOW006 3 | // https://www.codechef.com/problems/FLOW006 4 | 5 | 6 | import java.io.*; 7 | class Main 8 | { 9 | public static void main(String arg[])throws IOException 10 | { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | int t=Integer.parseInt(br.readLine()); 13 | while(t!=0) 14 | { 15 | int n=Integer.parseInt(br.readLine()); 16 | int c=0,s=0; 17 | while(n!=0) 18 | { 19 | c= n%10; 20 | s=s+c; 21 | n=n/10; 22 | } 23 | System.out.println(s); 24 | t--; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /HackerEarth/Guruji And Hearts.java: -------------------------------------------------------------------------------- 1 | // Guruji And Hearts 2 | 3 | // https://www.hackerearth.com/problem/algorithm/guruji-and-hearts-2/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s=new Scanner(System.in); 10 | String t=s.nextLine(); 11 | int l=t.length(); 12 | int c=0; 13 | for(int i=0;i 5 | 6 | int main() 7 | { 8 | int a[100], n,i,s=0,j,t; 9 | scanf("%d",&n); 10 | for(i=0;i a[j+1]) 19 | { 20 | s++; 21 | t = a[j]; 22 | a[j] = a[j+1]; 23 | a[j+1] = t; 24 | } 25 | } 26 | } 27 | printf("%d",s); 28 | } 29 | -------------------------------------------------------------------------------- /Codechef/Chef and String.py: -------------------------------------------------------------------------------- 1 | # Chef and String 2 | # Problem code: CHRL2 3 | # https://www.codechef.com/problems/CHRL2 4 | import math 5 | s=raw_input() 6 | l=len(s) 7 | c=int(0) 8 | a=int(0) 9 | b=int(0) 10 | d=int(0) 11 | for i in range(l): 12 | if s[i]=='C': 13 | a=max(i+1,a) 14 | while a=s and a+i <=t) for i in apple] 14 | orange_list = [(b+i >=s and b+i <=t) for i in orange] 15 | print apple_list.count(True) 16 | print orange_list.count(True) 17 | -------------------------------------------------------------------------------- /Codechef/Add Two Numbers.java: -------------------------------------------------------------------------------- 1 | // Add Two Numbers 2 | // Problem code: FLOW001 3 | // https://www.codechef.com/problems/FLOW001 4 | 5 | 6 | import java.util.*; 7 | class Main 8 | { 9 | public static void main(String arg[]) 10 | { 11 | Scanner s=new Scanner(System.in); 12 | 13 | int t=s.nextInt(); 14 | 15 | while(t!=0) 16 | { 17 | 18 | int a,b; 19 | a=s.nextInt(); 20 | b=s.nextInt(); 21 | System.out.println(a+b); 22 | t=t-1; 23 | } 24 | 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /Codechef/The Block Game.java: -------------------------------------------------------------------------------- 1 | // The Block Game 2 | // Problem code: PALL01 3 | // https://www.codechef.com/problems/PALL01 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s=new Scanner(System.in); 10 | int t=s.nextInt(); 11 | while(t!=0) 12 | { 13 | int n=s.nextInt(); 14 | int a=n; 15 | int x=0; 16 | while(a!=0) 17 | { 18 | x=(x*10)+(a%10); 19 | a=a/10; 20 | } 21 | if(x==n) 22 | System.out.println("wins"); 23 | else 24 | System.out.println("losses"); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /GeeksforGeeks/Frequency of Array Elements.py: -------------------------------------------------------------------------------- 1 | # Frequency of Array Elements 2 | # http://www.practice.geeksforgeeks.org/problem-page.php?pid=84 3 | 4 | t=int(raw_input()) 5 | for i in range(t): 6 | n=int(raw_input()) 7 | a=[] 8 | a=map(int,raw_input().split()) 9 | b=[] 10 | s=int(0) 11 | for j in range(0,n): 12 | s=int(0) 13 | if j+1 in a: 14 | for k in a: 15 | if k==j+1: 16 | s+=1 17 | b=b+[s] 18 | else: 19 | b=b+[0] 20 | s="" 21 | for j in b: 22 | s=s+str(j)+" " 23 | print s 24 | -------------------------------------------------------------------------------- /HackerEarth/THREESOME PAIRING.java: -------------------------------------------------------------------------------- 1 | // THREESOME PAIRING 2 | 3 | // https://www.hackerearth.com/problem/algorithm/threesome-pairing/ 4 | 5 | import java.util.*; 6 | class Testcases 7 | { 8 | public static void main(String args[] ) throws Exception 9 | { 10 | Scanner s=new Scanner(System.in); 11 | int t=s.nextInt(); 12 | while(t!=0) 13 | { 14 | int l=s.nextInt(); 15 | int a[]=new int[l]; 16 | for(int i=0;ib[i+1]) 20 | c++; 21 | } 22 | System.out.println(c); 23 | n--; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /HackerEarth/Bob and Lines.py: -------------------------------------------------------------------------------- 1 | # Bob and Lines 2 | 3 | # https://www.hackerearth.com/problem/algorithm/bob-and-lines/ 4 | 5 | t=int(raw_input()) 6 | for i in range(t): 7 | n,q=map(int,raw_input().split()) 8 | a=[] 9 | c=int(0) 10 | for j in range(n): 11 | a=a+[map(int,raw_input().split())] 12 | for k in range(q): 13 | c=0 14 | x,y=map(int,raw_input().split()) 15 | if x==0: 16 | for j in range(n): 17 | if (a[j][1]<=y and a[j][3]>=y) or (a[j][1]>=y and a[j][3]<=y): 18 | c=c+1 19 | else: 20 | for j in range(n): 21 | if (a[j][0]<=x and a[j][2]>=x) or (a[j][0]>=x and a[j][2]<=x): 22 | c=c+1 23 | print c -------------------------------------------------------------------------------- /HackerEarth/Happy Birthday.java: -------------------------------------------------------------------------------- 1 | // Happy Birthday 2 | 3 | // https://www.hackerearth.com/problem/algorithm/happy-birthday/ 4 | 5 | 6 | import java.util.*; 7 | 8 | class TestClass { 9 | public static void main(String args[] ){ 10 | Scanner s=new Scanner(System.in); 11 | int n=s.nextInt(); 12 | int sum[]=new int[n]; 13 | int ss=0, c=0; 14 | for(int i=0; iarr[j+1]) 20 | { 21 | int t=arr[j]; 22 | arr[j]=arr[j+1]; 23 | arr[j+1]=t; 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /HackerEarth/Commutative.java: -------------------------------------------------------------------------------- 1 | // Commutative 2 | 3 | // https://www.hackerearth.com/problem/algorithm/commutative/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int n=s.nextInt(); 12 | while(n!=0) 13 | { int c=0; 14 | int p=s.nextInt(); 15 | int l=p*(p-1); 16 | for(int i=1;i<=l;i++) 17 | { 18 | for(int j=1;j<=l;j++) 19 | { 20 | double nm=Math.pow(j,i); 21 | double mn=Math.pow(i,j); 22 | double d=nm-mn; 23 | if(d%p==0) 24 | c++; 25 | 26 | } 27 | } 28 | System.out.println(c); 29 | n--; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /HackerEarth/Train Reservation.java: -------------------------------------------------------------------------------- 1 | // Train Reservation 2 | 3 | // https://www.hackerearth.com/problem/algorithm/train-reservation/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | Scanner s=new Scanner(System.in); 9 | int t=s.nextInt(); 10 | while(t!=0) 11 | { 12 | String st=""; 13 | int n=s.nextInt(); 14 | int a=n%8; 15 | if(a==0) 16 | st="Side Upper"; 17 | else if(a==1 || a==4) 18 | st="Lower"; 19 | else if(a==2 || a==5) 20 | st="Middle"; 21 | else if(a==3 || a==6) 22 | st="Upper"; 23 | else 24 | st="Side Lower"; 25 | t--; 26 | System.out.println(st); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /HackerEarth/A Summing Punishment!.java: -------------------------------------------------------------------------------- 1 | // A Summing Punishment! 2 | 3 | // https://www.hackerearth.com/problem/algorithm/a-summing-punishment-6/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int n=s.nextInt(); 12 | int a[]=new int[n]; 13 | for(int i=0;i=d-sum) 23 | System.out.println(sum); 24 | else 25 | System.out.println(ss); 26 | 27 | t--; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /GeeksforGeeks/Search an Element in an array.c: -------------------------------------------------------------------------------- 1 | // Search an Element in an array 2 | // http://www.practice.geeksforgeeks.org/problem-page.php?pid=77 3 | 4 | 5 | 6 | #include 7 | 8 | int main() { 9 | int t=0,n=0,e=0,i=0,x=0,flag=0; 10 | int a[100]; 11 | scanf("%d",&t); 12 | while(t!=0) 13 | { 14 | flag=0; 15 | scanf("%d",&n); 16 | for(i=0;ima: 28 | ma=su 29 | x=j 30 | l=len(g[j]) 31 | elif su==ma and l>len(g[j]): 32 | ma=su 33 | x=j 34 | l=len(g[j]) 35 | print x+1 -------------------------------------------------------------------------------- /HackerEarth/Simple summation.java: -------------------------------------------------------------------------------- 1 | // Simple summation 2 | 3 | // https://www.hackerearth.com/problem/algorithm/simple-summation/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s=new Scanner(System.in); 10 | int t=s.nextInt(); 11 | while(t!=0) 12 | { 13 | int n=s.nextInt(); 14 | int k=s.nextInt(); 15 | int q=n/k; 16 | int r=n%k; 17 | int sum=0; 18 | sum=(k*(k-1))/2; 19 | sum=sum*q; 20 | sum=sum+(r*(r+1)/2); 21 | t--; 22 | System.out.println(sum); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Codechef/Ciel and Receipt.java: -------------------------------------------------------------------------------- 1 | // Ciel and Receipt 2 | // Problem code: CIELRCPT 3 | // https://www.codechef.com/problems/CIELRCPT 4 | 5 | import java.util.*; 6 | import java.io.*; 7 | class Main 8 | { 9 | public static void main(String arg[])throws IOException 10 | { 11 | Scanner s = new Scanner(System.in); 12 | int quo, rem, v; 13 | int t = s.nextInt(); 14 | while (t != 0) 15 | { 16 | v = s.nextInt(); 17 | quo = v / 2048; 18 | rem = v % 2048; 19 | 20 | while (rem > 0) { 21 | if (rem%2 == 1) { 22 | quo ++; 23 | } 24 | rem/=2; 25 | } 26 | System.out.println(quo); 27 | t--; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /HackerEarth/IT solves Maths.java: -------------------------------------------------------------------------------- 1 | // IT solves Maths 2 | 3 | // https://www.hackerearth.com/problem/algorithm/game-of-numbers-16/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | Scanner s=new Scanner(System.in); 10 | int t=s.nextInt(); 11 | while(t!=0) 12 | { 13 | int c=0; 14 | int n=s.nextInt(); 15 | for(int i=1;i= 2*ch: 25 | print ch 26 | elif int(a[0]/2) <=ch: 27 | print int(a[0]/2) 28 | 29 | -------------------------------------------------------------------------------- /HackerEarth/Paula Distance Problem.java: -------------------------------------------------------------------------------- 1 | // Paula Distance Problem 2 | 3 | // https://www.hackerearth.com/problem/algorithm/paula-distance-problem/ 4 | 5 | 6 | 7 | 8 | import java.util.*; 9 | class TestClass { 10 | public static void main(String args[] ) throws Exception { 11 | 12 | Scanner s=new Scanner(System.in); 13 | int n=s.nextInt(); 14 | while(n!=0) 15 | { int c=0; 16 | String a=s.next(); 17 | String b=s.next(); 18 | int la=a.length(); 19 | int lb=b.length(); 20 | if(la!=lb) 21 | c=-1; 22 | else 23 | { 24 | for(int i=0;i=0) 24 | { 25 | sum=sum+(a[p+j]*(p+1)); 26 | p--; 27 | } 28 | } 29 | if(i>0) 30 | for(int j=i+1;j=k) 22 | System.out.println("NO"); 23 | else 24 | System.out.println("YES"); 25 | t--; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /HackerEarth/Find the Students.java: -------------------------------------------------------------------------------- 1 | // Find the Students 2 | 3 | // https://www.hackerearth.com/problem/algorithm/find-the-students-7/ 4 | 5 | import java.util.*; 6 | 7 | class TestClass { 8 | public static void main(String args[] ){ 9 | Scanner s=new Scanner(System.in); 10 | int n=s.nextInt(); 11 | int a[]= new int[n]; 12 | for(int i=0;ia[i] && a[i]>0) 21 | min=a[i]; 22 | for(int i=0;idata; 22 | head = head->next; 23 | i+=1; 24 | } 25 | if(n>i) 26 | return -1; 27 | return x[i-n]; 28 | } -------------------------------------------------------------------------------- /HackerEarth/Rohan and flower.java: -------------------------------------------------------------------------------- 1 | // Rohan and flower* 2 | 3 | // https://www.hackerearth.com/problem/algorithm/rohan-and-flower/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int l=s.nextInt(); 12 | int h=s.nextInt(); 13 | int a[]=new int[l]; 14 | for(int i=0;i=0) 18 | { 19 | if(x%2==0) 20 | { 21 | if(a>=c) 22 | a=a-c; 23 | else 24 | { 25 | a=a-c; 26 | System.out.println("1"); 27 | } 28 | } 29 | else 30 | { 31 | if(a>=b) 32 | a=a-b; 33 | else 34 | { 35 | a=a-b; 36 | System.out.println("0"); 37 | } 38 | } 39 | x=x+1; 40 | } 41 | n--; 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /HackerEarth/Magic Pass Code.java: -------------------------------------------------------------------------------- 1 | // Magic Pass Code 2 | 3 | // https://www.hackerearth.com/problem/algorithm/magic-pass-code-1/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s=new Scanner(System.in); 10 | int max=0,x=0; 11 | while(s.hasNextInt()) 12 | { 13 | int sum=0; 14 | int n=s.nextInt(); 15 | int a=n; 16 | while(n!=0) 17 | { 18 | int r=n%10; 19 | n=n/10; 20 | sum=sum+r; 21 | } 22 | if(sum>max) 23 | { 24 | max=sum; 25 | x=a; 26 | } 27 | } 28 | System.out.println(x); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /GeeksforGeeks/Selection Sort.c: -------------------------------------------------------------------------------- 1 | // Selection Sort 2 | // http://www.practice.geeksforgeeks.org/probfunc-page.php?pid=700147 3 | 4 | 5 | /* The task is to complete select() which is used 6 | as shown below to implement selectionSort() */ 7 | 8 | /* A function to implement selection sort 9 | void selectionSort(int arr[], int n) 10 | { 11 | int i, j; 12 | for (i = n-1; i >=0; i--) 13 | { 14 | int j = select(arr, i); 15 | swap(&arr[i], &arr[j]); 16 | } 17 | } 18 | */ 19 | 20 | int select(int arr[], int i) 21 | { 22 | int max=i; 23 | for(int j=i;j>=0;j--) 24 | { 25 | if(arr[i]arr[max]) 26 | { 27 | max=j; 28 | } 29 | } 30 | return max; 31 | } 32 | -------------------------------------------------------------------------------- /HackerEarth/Chandu and Primes.py: -------------------------------------------------------------------------------- 1 | # Chandu and Primes* 2 | 3 | # https://www.hackerearth.com/problem/algorithm/chandu-and-primes-1/ 4 | 5 | def isPrime2(n): 6 | if n==2 or n==3: 7 | return True 8 | if n%2==0 or n<2: 9 | return False 10 | for i in range(3,int(n**0.5)+1,2): 11 | if n%i==0: 12 | return False 13 | return True 14 | 15 | def isPrime(j): 16 | p=str(j) 17 | y=len(p) 18 | if y in range(0,y+1): 19 | if isPrime2(j/(10**y)): 20 | return True 21 | return False 22 | 23 | t=int(raw_input()) 24 | x=[2,3,5,7] 25 | c=0 26 | for i in range(0,t): 27 | c=0 28 | a,b=map(int,raw_input().split()) 29 | for j in range(a,b+1): 30 | if isPrime2(j): 31 | c=c+1 32 | elif isPrime(j): 33 | c=c+1 34 | print c -------------------------------------------------------------------------------- /HackerEarth/Srishti and the candies.java: -------------------------------------------------------------------------------- 1 | // Srishti and the candies* 2 | 3 | // https://www.hackerearth.com/problem/algorithm/srishti-and-the-candies/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int t=s.nextInt(); 12 | while(t!=0) 13 | { 14 | int a=s.nextInt(); 15 | int b=s.nextInt(); 16 | int c=s.nextInt(); 17 | int d=a/b; 18 | int f=d; 19 | int x=d/c; 20 | if(d>=c) 21 | { 22 | f=f+x; 23 | } 24 | int e=d%c; 25 | int y=0; 26 | if((x+e)>=c) 27 | { 28 | y=(x+e)/c; 29 | f=f+y; 30 | } 31 | d=(x+e)%c; 32 | if((y+d)>=c) 33 | { 34 | f=f+d; 35 | } 36 | System.out.println(f); 37 | 38 | t--; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /HackerEarth/Trip Analyser.java: -------------------------------------------------------------------------------- 1 | // Trip Analyser 2 | 3 | // https://www.hackerearth.com/problem/approximate/trip-analyser/ 4 | 5 | 6 | import java.util.*; 7 | import java.text.*; 8 | import java.io.*; 9 | class TestClass { 10 | public static void main(String args[] ) throws Exception { 11 | 12 | Scanner s=new Scanner(System.in); 13 | int n=s.nextInt(); 14 | while(n!=0) 15 | { 16 | DecimalFormat df=new DecimalFormat("#0.0000000000"); 17 | double a=s.nextDouble(); 18 | double b=s.nextDouble(); 19 | String d=""; 20 | if(a') { 24 | talk++; 25 | } 26 | } 27 | System.out.println(talk); 28 | t--; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /HackerEarth/Karan's Teacher.java: -------------------------------------------------------------------------------- 1 | // Karan's Teacher* 2 | 3 | // https://www.hackerearth.com/problem/algorithm/karans-teacher/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int t=s.nextInt(); 12 | while(t!=0) 13 | { int i=0,flag=0; 14 | int p=s.nextInt(); 15 | for( i=0; i<=1016; i++) 16 | { 17 | int r=i; 18 | long st=1; 19 | while(r!=0) 20 | { 21 | st=st*r; 22 | r--; 23 | } 24 | double c=Math.pow(10,p); 25 | if(st%c==0) 26 | { 27 | flag=1; 28 | break; 29 | } 30 | } 31 | if(flag==1) 32 | System.out.println(i); 33 | else 34 | System.out.println(i+1); 35 | t--; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /HackerEarth/Ferb's Stripes.java: -------------------------------------------------------------------------------- 1 | // Ferb's Stripes 2 | 3 | // https://www.hackerearth.com/problem/algorithm/ferbs-stripes-1/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) { 9 | 10 | Scanner s= new Scanner(System.in); 11 | int n=s.nextInt(); 12 | while(n!=0) 13 | { int flag=0; 14 | int l=s.nextInt(); 15 | int a[]=new int[l]; 16 | for(int i=0;ima and k!=1: 23 | ma=b[k]-b[i] 24 | st=i 25 | en=k 26 | mi=int(min(a[st:en+1])) 27 | if ma+a[st-1]-mi>ma and st-1>=0 and st!=en: 28 | print ma+s[st-1]-mi 29 | elif en+1ma and st!=en: 30 | print ma+s[en+1]-mi 31 | else: 32 | print ma -------------------------------------------------------------------------------- /HackerEarth/Scoring in Exam.java: -------------------------------------------------------------------------------- 1 | // Scoring in Exam* 2 | 3 | // https://www.hackerearth.com/problem/algorithm/scoring-in-exam-1/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int n=s.nextInt(); 12 | int q=s.nextInt(); 13 | long a[]=new long[n]; 14 | long b[]=new long[n]; 15 | for(int i=0;i=0; i--) 27 | { 28 | char ch=ss.charAt(i); 29 | if(ch=='A') 30 | { 31 | max=max+a[c-2]; 32 | c--; 33 | } 34 | else if(ch=='B') 35 | { 36 | if(max=0;i--) 26 | System.out.print(a[i]+" "); 27 | else 28 | System.out.print("None to display"); 29 | 30 | System.out.println(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /HackerEarth/Flop Flip.java: -------------------------------------------------------------------------------- 1 | // Flop Flip 2 | 3 | // https://www.hackerearth.com/problem/algorithm/flip-flop-6/ 4 | 5 | 6 | import java.util.*; 7 | import java.io.*; 8 | class TestClass { 9 | public static void main(String args[] )throws IOException { 10 | 11 | Scanner s=new Scanner(System.in); 12 | int n=s.nextInt(); 13 | while(n!=0) 14 | { 15 | int c=0,j=0,t=0; 16 | char chx; 17 | String ss=s.next(); 18 | int l=ss.length(); 19 | for(int i=0;idata != headB->data) 21 | return 0; 22 | headA = headA->next; 23 | headB = headB->next; 24 | } 25 | if(headA == NULL && headB!= NULL) { 26 | return 0; 27 | } 28 | else if (headA != NULL && headB == NULL) { 29 | return 0; 30 | } 31 | return 1; 32 | } 33 | -------------------------------------------------------------------------------- /Codechef/The Next Palindrome.py: -------------------------------------------------------------------------------- 1 | # The Next Palindrome 2 | # Problem code: PALIN 3 | # https://www.codechef.com/problems/PALIN 4 | 5 | def rev(no): 6 | r=int(0) 7 | while no!=0: 8 | r=r*10+(no%10) 9 | no=no/10 10 | return r 11 | 12 | t=int(raw_input()) 13 | for i in range(t): 14 | n=raw_input() 15 | l=len(n) 16 | a=int(n) 17 | if a<9 and a>0: 18 | print a+1 19 | elif l%2==0: 20 | h=l/2 21 | start=int(a/(10**h)) 22 | end=int(a%(10**h)) 23 | if start>=end: 24 | x=int(start*(10**h)+rev(start)) 25 | else: 26 | start=start+1 27 | x=int(start*(10**h)+rev(start)) 28 | print x 29 | else: 30 | h=(l/2) 31 | start=int(a/(10**h)) 32 | end=int(a-(start*(10**(h)))) 33 | m=int(start%10) 34 | start=start/10 35 | re=rev(start) 36 | if re>end: 37 | x=int(((start*10)+m)*(10**(h))+re) 38 | else: 39 | x=int(((start*10)+(m+1))*(10**(h))+re) 40 | print x 41 | 42 | 43 | -------------------------------------------------------------------------------- /HackerEarth/HANUMAN LANGUAGE.java: -------------------------------------------------------------------------------- 1 | // HANUMAN LANGUAGE 2 | 3 | // https://www.hackerearth.com/problem/algorithm/hanuman-language/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | Scanner s=new Scanner(System.in); 9 | int t=s.nextInt(); 10 | while(t>=0) 11 | { 12 | String st=s.nextLine()+" "; 13 | int l=st.length(); 14 | int p=0,m=0; 15 | String sp[]=new String[l]; 16 | for(int i=0;i=0;i--) 27 | System.out.print(sp[i]+" "); 28 | System.out.println(); 29 | t--; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /HackerEarth/Play with Queries.java: -------------------------------------------------------------------------------- 1 | // Play with Queries 2 | 3 | // https://www.hackerearth.com/problem/algorithm/play-with-queries/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int n=s.nextInt(); 12 | int r=s.nextInt(); 13 | while(r!=0) 14 | { 15 | int c=0; 16 | int t=s.nextInt(); 17 | int k=s.nextInt(); 18 | if(t==1) 19 | { 20 | for(int i=1;i<=n;i++) 21 | { 22 | if((n%i==0) && (k%i==0)) 23 | c++; 24 | } 25 | } 26 | else if(t==2) 27 | { 28 | for(int i=1;i<=n;i++) 29 | { 30 | if((n%i==0) && (i%k==0)) 31 | c++; 32 | } 33 | } 34 | else 35 | { 36 | for(int i=1;i<=n;i++) 37 | { 38 | if((n%i==0) && (i%k!=0)) 39 | c++; 40 | } 41 | } 42 | 43 | System.out.println(c); 44 | r--; 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /HackerEarth/Anil And Long Title.java: -------------------------------------------------------------------------------- 1 | // Anil And Long Title 2 | 3 | // https://www.hackerearth.com/problem/algorithm/anil-and-long-title-2/ 4 | 5 | import java.util.*; 6 | import java.lang.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | String st=s.nextLine(); 12 | int l=st.length(); 13 | String ss=""; 14 | int i=0; 15 | if(st.charAt(0)!='_') 16 | { 17 | ss=ss+Character.toUpperCase(st.charAt(0)); 18 | } 19 | 20 | for(i=1;i=b) 20 | { 21 | rr=a; 22 | r=b; 23 | } 24 | while(r!=0) 25 | { 26 | long m=r%10; 27 | long x=rr%10; 28 | if(m+x>=10) 29 | c++; 30 | r=r/10; 31 | rr=rr/10; 32 | } 33 | if(rr==9) 34 | c++; 35 | if(c==0) 36 | System.out.println("No carry operation"); 37 | else if(c==1) 38 | System.out.println(1+" carry operation"); 39 | else 40 | System.out.println(c+" carry operations"); 41 | n--; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /HackerEarth/Artsy Lulu.java: -------------------------------------------------------------------------------- 1 | // Artsy Lulu 2 | 3 | // https://www.hackerearth.com/problem/algorithm/artsy-lulu-1/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | char z='z'; 12 | while(s.hasNext()) 13 | { 14 | String st=s.next(); 15 | char ch=st.charAt(0); 16 | int h=s.nextInt(); 17 | if(h<2 || ((z-ch+1)m[j]) 27 | { 28 | int t=m[i]; 29 | m[i]=m[j]; 30 | m[j]=t; 31 | } 32 | if(f[i]>f[j]) 33 | { 34 | int t=f[i]; 35 | f[i]=f[j]; 36 | f[j]=t; 37 | } 38 | } 39 | } 40 | for(int i=0;i0) 23 | { 24 | p=a[i]; 25 | while(p!=0) 26 | { 27 | if(a[p+i]>0) 28 | 29 | } 30 | } 31 | else if(a[i]==(-1)) 32 | { 33 | c=c+1-p; 34 | p=0; 35 | } 36 | } 37 | 38 | System.out.println(c); 39 | n--; 40 | } 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /HackerEarth/Tripping on Nothingness.java: -------------------------------------------------------------------------------- 1 | // Tripping on Nothingness 2 | 3 | // https://www.hackerearth.com/problem/algorithm/tripping-on-nothingness/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | Scanner s=new Scanner(System.in); 10 | int n=s.nextInt(); 11 | int q=s.nextInt(); 12 | int a[]=new int[q]; 13 | for(int i=0;i=1;j--) 24 | { 25 | if(a[i]%j==0 && n%j==0) 26 | { 27 | for(int k=0;k=0;k--) 22 | st=st+a[i].charAt(k); 23 | for(int j=i+1;jl2) 24 | p=l2; 25 | else if(l2>l1) 26 | p=l1; 27 | else 28 | p=l1; 29 | for(int i=0; i=11 && a[i]<=20) 27 | dis=ss*0.30; 28 | else 29 | dis=ss*0.20; 30 | double ma=Math.round(dis); 31 | ss=ss-ma; 32 | fin=fin+ss; 33 | } 34 | 35 | System.out.println((int)fin); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /HackerEarth/Suresh's array.java: -------------------------------------------------------------------------------- 1 | // Suresh's array 2 | 3 | // https://www.hackerearth.com/problem/algorithm/sureshs-array-3/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int t=s.nextInt(); 12 | while(t!=0) 13 | { 14 | int l=s.nextInt(); 15 | int a[]=new int[l]; 16 | for(int i=0;ia[j]) 24 | { 25 | t=a[i]; 26 | a[i]=a[j]; 27 | a[j]=t; 28 | } 29 | } 30 | } 31 | for(int i=0;i=i;j--) 35 | { 36 | if((a[i]+a[j]<=max) && a[i]!=0 && a[j]!=0) 37 | { 38 | if(a[j]>p) 39 | { 40 | p=a[j]; 41 | b=j; 42 | } 43 | } 44 | } 45 | if(p!=0) 46 | { 47 | c++; 48 | a[i]=0; 49 | a[b]=0; 50 | } 51 | else if(a[i]!=0 && a[i]<=max) 52 | c++; 53 | } 54 | System.out.println(c); 55 | n--; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /HackerEarth/Ravi's Trouble.java: -------------------------------------------------------------------------------- 1 | // Ravi's Trouble 2 | 3 | // https://www.hackerearth.com/problem/algorithm/ravis-trouble-3/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | Scanner s=new Scanner(System.in); 9 | int t=s.nextInt(); 10 | while(t!=0) 11 | { 12 | String st=s.next(); 13 | String ss=s.next(); 14 | int l=st.length(); 15 | 16 | char a[]=new char[l]; 17 | for(int i=0;i=p) 28 | { 29 | flag=1; 30 | break; 31 | } 32 | 33 | } 34 | if(flag==1) 35 | System.out.println("NO"); 36 | else 37 | System.out.println("YES"); 38 | t--; 39 | } 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /HackerEarth/Cormen Boy and ACM ICPC.java: -------------------------------------------------------------------------------- 1 | // Cormen Boy and ACM ICPC* 2 | 3 | // https://www.hackerearth.com/problem/algorithm/cormen-boy-and-acm-icpc/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s=new Scanner(System.in); 10 | int n=s.nextInt(); 11 | int t=s.nextInt(); 12 | int p=0; 13 | if(n%2==0) 14 | p=n/2; 15 | else 16 | p=(n+1)/2; 17 | int max=0; 18 | while(t!=0) 19 | { 20 | int sum=0; 21 | int a[]=new int[n]; 22 | for(int i=0;imax) 39 | max=sum; 40 | t--; 41 | } 42 | System.out.println(max); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /HackerEarth/Equal Candies Distribution.java: -------------------------------------------------------------------------------- 1 | // Equal Candies Distribution* 2 | 3 | // https://www.hackerearth.com/problem/algorithm/equal-candies-distribution-3/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int t=s.nextInt(); 12 | while(t!=0) 13 | { 14 | int l=s.nextInt(); 15 | int a[]=new int[l]; 16 | for(int i=0;ia[j]) 23 | { 24 | int r=a[i]; 25 | a[i]=a[j]; 26 | a[j]=r; 27 | } 28 | } 29 | } 30 | int c=0; 31 | for(int i=l-1;i>1;i--) 32 | { 33 | int p=a[i]-a[i-1]; 34 | if(p==1) 35 | { 36 | for(int j=i-1;j>0;j--) 37 | { 38 | a[j]=a[j]+1; 39 | } 40 | c++; 41 | } 42 | else if(p==2) 43 | { 44 | for(int j=i-1;j>0;j--) 45 | { 46 | a[j]=a[j]+2; 47 | } 48 | c++; 49 | } 50 | else if(p==5) 51 | { 52 | for(int j=i-1;j>0;j--) 53 | { 54 | a[j]=a[j]+5; 55 | } 56 | c++; 57 | } 58 | } 59 | t--; 60 | System.out.println(c); 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /GeeksforGeeks/Find Prime numbers in a range.java: -------------------------------------------------------------------------------- 1 | // Find Prime numbers in a range 2 | // http://www.practice.geeksforgeeks.org/problem-page.php?pid=374 3 | 4 | // Java program to find prime number greater than a 5 | // given number using built in method 6 | 7 | import java.util.*; 8 | import java.math.*; 9 | 10 | class NextPrimeTest 11 | { 12 | // Function to get nextPrimeNumber 13 | static long nextPrime(long n) 14 | { 15 | BigInteger b = new BigInteger(String.valueOf(n)); 16 | return Long.parseLong(b.nextProbablePrime().toString()); 17 | } 18 | 19 | // Driver method 20 | public static void main (String[] args) 21 | throws java.lang.Exception 22 | { 23 | Scanner s=new Scanner(System.in); 24 | int t=s.nextInt(); 25 | while(t!=0) 26 | { 27 | long st=s.nextLong(); 28 | long en=s.nextLong(); 29 | for(long i=st-1;i<=en;) 30 | { 31 | i=nextPrime(i); 32 | if(i<=en) 33 | System.out.print(i+" "); 34 | } 35 | t--; 36 | System.out.println(); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /HackerEarth/SubPalindrome.java: -------------------------------------------------------------------------------- 1 | // SubPalindrome 2 | 3 | // https://www.hackerearth.com/code-mania-2c/algorithm/subpalindrome-3/ 4 | 5 | 6 | import java.util.Scanner; 7 | import java.io.*; 8 | 9 | class TestClass { 10 | public static void main(String args[] )throws IOException { 11 | Scanner s=new Scanner(System.in); 12 | int n=s.nextInt(); 13 | while(n!=0) { 14 | String st = s.nextLine(); 15 | int l=st.length(); 16 | String ar = new String[l]; 17 | for(int p=0; pi; j--) 27 | { 28 | flag=1; 29 | ch1=st.charAt(j); 30 | if(ch==ch1) 31 | { 32 | t=(j-i)/2; 33 | a=i; 34 | b=j; 35 | while(t!=0) 36 | { 37 | ch=st.charAt(a++); 38 | ch1=st.charAt(b--); 39 | if(ch!=ch1) 40 | { 41 | flag=0; 42 | break; 43 | } 44 | } 45 | for(int k=0; klen) 27 | len=y.length(); 28 | x++; 29 | y=""; 30 | } 31 | else 32 | y=y+ch; 33 | } 34 | for(int j=0;j -1: 47 | print "NO" 48 | else : 49 | print "YES" -------------------------------------------------------------------------------- /HackerEarth/Organize Contest.java: -------------------------------------------------------------------------------- 1 | // Organize Contest 2 | 3 | // https://www.hackerearth.com/problem/algorithm/organize-contest/ 4 | 5 | 6 | import java.util.*; 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | 10 | Scanner s=new Scanner(System.in); 11 | int t=s.nextInt(); 12 | String st[]=new String[t]; 13 | int a[]=new int[t]; 14 | int score[]=new int[t]; 15 | String name[]=new String[t]; 16 | int max=-100; 17 | String na=""; 18 | int m=0; 19 | for(int i=0;imax) 44 | { 45 | max=score[k]; 46 | na=name[k]; 47 | } 48 | } 49 | } 50 | 51 | System.out.println(na); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /FCFS OS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | int arrT[10], netBT[10], finT[10], turnAT[10], waitT[10]; 9 | int i = 0, n; 10 | float avgWT = 0, avgTAT = 0; 11 | 12 | printf("Enter the no. of Process\n"); 13 | scanf("%d", &n); 14 | 15 | printf("Enter the Arrival Time for the processes\n"); 16 | for(i = 0; i < n; i++) 17 | { 18 | scanf("%d", &arrT[i]); 19 | } 20 | 21 | printf("Enter the Net Burst Time for the processes\n"); 22 | for(i = 0; i < n; i++) 23 | { 24 | scanf("%d", &netBT[i]); 25 | } 26 | 27 | 28 | 29 | finT[0] = netBT[0] - arrT[0]; 30 | turnAT[0] = finT[0] - arrT[0]; 31 | waitT[0] = turnAT[0] - netBT[0]; 32 | 33 | for(i = 1; i < n; i++) 34 | { 35 | finT[i] = finT[i-1] + netBT[i]; 36 | turnAT[i] = finT[i] - arrT[i]; 37 | waitT[i] = turnAT[i] - netBT[i]; 38 | avgWT += waitT[i]; 39 | avgTAT += turnAT[i]; 40 | } 41 | avgTAT += turnAT[0]; 42 | avgTAT = avgTAT/n; 43 | avgWT = avgWT/n; 44 | 45 | printf("Pro \t Ar.Tm \t N.B.T \t F.T \t T.A.T \t Wait Tme \n"); 46 | for(i = 0; i < n; i++) 47 | { 48 | printf("P[%d] \t %d \t %d \t %d \t %d \t %d\n",i , arrT[i], netBT[i], finT[i], turnAT[i], waitT[i]); 49 | } 50 | 51 | printf("Avg. waiting Time = %.2f ms \n", avgWT); 52 | printf("Avg. Turn Around Time = %.2f ms \n", avgTAT); 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /HackerEarth/G - Game with Letters.java: -------------------------------------------------------------------------------- 1 | // G - Game with Letters* 2 | 3 | // https://www.hackerearth.com/problem/algorithm/g-game-with-letters/ 4 | 5 | import java.util.*; 6 | class TestClass { 7 | public static void main(String args[] ) throws Exception { 8 | 9 | Scanner s=new Scanner(System.in); 10 | int t=s.nextInt(); 11 | while(t!=0) 12 | { 13 | int n=s.nextInt(); 14 | String st=s.next(); 15 | String g[]=new String[n]; 16 | for(int i=0;imax) 36 | { 37 | max=sum; 38 | x=i; 39 | y=g[i].length(); 40 | } 41 | else if(sum==max && g[i].length() p2) { 24 | run[i] = p1 - p2; 25 | play[i] = 1; 26 | } else { 27 | run[i] = p2 - p1; 28 | play[i] = 2; 29 | } 30 | i++; 31 | t--; 32 | } 33 | t = n; 34 | for (i = 0; i < t-1; i++) { 35 | for(int j = i+1; j < t; j++) { 36 | if (run[i] > run[j]) { 37 | temp = run[i]; 38 | run[i] = run[j]; 39 | run[j] = temp; 40 | 41 | temp = play[i]; 42 | play[i] = play[j]; 43 | play[j] = temp; 44 | } 45 | } 46 | } 47 | System.out.println(play[t-1] + " " + run[t-1]); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Codechef/Turbo Sort.c: -------------------------------------------------------------------------------- 1 | // Turbo Sort 2 | // https://www.codechef.com/problems/TSORT 3 | 4 | #include 5 | #include 6 | 7 | void merge(int left[], int right[], int A[], int nl, int nr) 8 | { 9 | int i=0,j=0,k=0; 10 | while(i < nl && j < nr) 11 | { 12 | if(left[i] < right[j]) 13 | { 14 | A[k] = left[i]; 15 | k = k+1; 16 | i = i+1; 17 | } 18 | else 19 | { 20 | A[k] = right[j]; 21 | k = k+1; 22 | j = j+1; 23 | } 24 | } 25 | while(i < nl) 26 | { 27 | A[k] = left[i]; 28 | i+=1; 29 | k+=1; 30 | } 31 | while(j < nr) 32 | { 33 | A[k] = right[j]; 34 | j+=1; 35 | k+=1; 36 | } 37 | } 38 | 39 | int mergeSort(int A[],int n) 40 | { 41 | int i=0; 42 | int m = n/2; 43 | int left[m],right[n-m]; 44 | if(n<2) 45 | { 46 | return; 47 | } 48 | for(i=0;i 2 | 3 | // Partition function for arranging elements greater than pivot to this right side and those lesser than pivot to its left side. 4 | int partition(int arr[], int low, int high) { 5 | int pivot = arr[high]; 6 | int i = low-1; 7 | int j, temp; 8 | 9 | for (j = low; j < high; j++) { 10 | 11 | if (arr[j] < pivot) { 12 | 13 | i++; 14 | // Swap the elements. 15 | temp = arr[i]; 16 | arr[i] = arr[j]; 17 | arr[j] = temp; 18 | } 19 | } 20 | // Place pivot element to this correct place. 21 | temp = arr[i+1]; 22 | arr[i+1] = pivot; 23 | arr[high] = temp; 24 | 25 | return i+1; 26 | 27 | } 28 | 29 | // Recursively call the sort method for partitioning the array. 30 | void sort(int arr[], int low, int high) { 31 | int pi=0; 32 | if (low < high) { 33 | pi = partition(arr, low, high); 34 | 35 | sort(arr, low, pi-1); 36 | sort(arr, pi+1, high); 37 | } 38 | } 39 | 40 | 41 | int main(void) 42 | { 43 | int arr[10]; 44 | int i=0, n; 45 | // Enter the no. of elemets 46 | printf("Enter the no. of elements less than 10 for sorting\n"); 47 | scanf("%d", &n); 48 | for(i = 0; i < n; i++) { 49 | scanf("%d", &arr[i]); 50 | } 51 | 52 | printf("Array before sorting\n"); 53 | for(i = 0; i < n; i++) { 54 | printf("%d ", arr[i]); 55 | } 56 | 57 | sort(arr, 0, n-1); 58 | printf("\nArray after sorting\n"); 59 | for(i = 0; i < n; i++) { 60 | printf("%d ", arr[i]); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /HackerRank/Queen's Attack II.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | n, k = raw_input().strip().split(' ') 7 | n, k = [int(n), int(k)] 8 | rQueen, cQueen = raw_input().strip().split(' ') 9 | rQueen, cQueen = [int(rQueen), int(cQueen)] 10 | obstacle = [] 11 | attack = 0 12 | for a0 in xrange(k): 13 | obstacle.append(":".join(raw_input().strip().split(' '))) 14 | 15 | # Right & Left 16 | xr = xl = cQueen 17 | xdr = xdl = rQueen 18 | fl = fr = fdr = fdl = 0 19 | while(True): 20 | xr += 1 21 | xdr += 1 22 | xdl -= 1 23 | xl -= 1 24 | sr = str(rQueen) + ":" + str(xr) 25 | sl = str(rQueen) + ":" + str(xl) 26 | sdr = str(xdr) + ":" + str(xr) 27 | sdl = str(xdl) + ":" + str(xl) 28 | if xr <= n and sr not in obstacle and fr != 1: 29 | attack += 1 30 | else: 31 | fr = 1 32 | if xl >= 1 and sl not in obstacle and fl != 1: 33 | attack += 1 34 | else: 35 | fl = 1 36 | if xdr <= n and xr <= n and sdr not in obstacle and fdr != 1: 37 | attack += 1 38 | else: 39 | fdr = 1 40 | if xdl >= 1 and xl >= 1 and sdl not in obstacle and fdl != 1: 41 | attack += 1 42 | else: 43 | fdl = 1 44 | if fl == fr == fdl == fdr == 1: 45 | break 46 | 47 | # Top & Bottim 48 | yt = yb = rQueen 49 | fb = ft = 0 50 | while(True): 51 | yt += 1 52 | yb -= 1 53 | st = str(yt) + ":" + str(cQueen) 54 | sb = str(yb) + ":" + str(cQueen) 55 | if yt <= n and st not in obstacle and ft != 1: 56 | attack += 1 57 | else: 58 | ft = 1 59 | if yb >= 1 and sb not in obstacle and fb != 1: 60 | attack += 1 61 | else: 62 | fb = 1 63 | if ft == fb == 1: 64 | break 65 | 66 | 67 | print attack 68 | -------------------------------------------------------------------------------- /banker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() { 4 | int k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz, count=0; 5 | // clrscr(); 6 | printf("\n Enter the number of resources : "); 7 | scanf("%d", &rz); 8 | printf("\n enter the max instances of each resources\n"); 9 | for (i=0;i"); 64 | } -------------------------------------------------------------------------------- /HackerEarth/Hungry Lemurs.java: -------------------------------------------------------------------------------- 1 | // # Hungry Lemurs* 2 | 3 | // # https://www.hackerearth.com/problem/algorithm/hungry-lemurs/ 4 | 5 | /* IMPORTANT: Multiple classes and nested static classes are supported */ 6 | 7 | /* 8 | * uncomment this if you want to read input. 9 | import java.io.BufferedReader; 10 | import java.io.InputStreamReader; 11 | */ 12 | import java.util.*; 13 | class TestClass { 14 | public static void main(String args[] ) throws Exception { 15 | /* 16 | * Read input from stdin and provide input before running 17 | 18 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 19 | String line = br.readLine(); 20 | int N = Integer.parseInt(line); 21 | for (int i = 0; i < N; i++) { 22 | System.out.println("hello world"); 23 | } 24 | */ 25 | Scanner s=new Scanner(System.in); 26 | int a=s.nextInt(); 27 | int b=s.nextInt(); 28 | int c=a%b; 29 | int min=100000, m=1, x=0; 30 | int d=((a/b)+1)*b; 31 | int e= d-a; 32 | if(c>=e) 33 | { int p=b; 34 | while(a%b!=0) 35 | { 36 | b--; 37 | c=((a/b)+1)*b-a; 38 | x=c+m; 39 | m++; 40 | if(x Long.parseLong(resources[j])){ 38 | break; 39 | } 40 | } 41 | 42 | if(j==resourcesRequired[z].length){ 43 | 44 | for(int k=0;k 2 | #include 3 | #include 4 | #define MAX 20 5 | 6 | enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 }; 7 | 8 | enum { FALSE, TRUE }; 9 | typedef unsigned int outcode; 10 | 11 | outcode compute_outcode(int x, int y, 12 | int xmin, int ymin, int xmax, int ymax) 13 | { 14 | outcode oc = 0; 15 | 16 | if (y > ymax) 17 | oc |= TOP; 18 | else if (y < ymin) 19 | oc |= BOTTOM; 20 | 21 | 22 | if (x > xmax) 23 | oc |= RIGHT; 24 | else if (x < xmin) 25 | oc |= LEFT; 26 | 27 | return oc; 28 | } 29 | 30 | void cohen_sutherland (double x1, double y1, double x2, double y2, 31 | double xmin, double ymin, double xmax, double ymax) 32 | { 33 | int accept; 34 | int done; 35 | outcode outcode1, outcode2; 36 | 37 | accept = FALSE; 38 | done = FALSE; 39 | 40 | outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax); 41 | outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax); 42 | do 43 | { 44 | if (outcode1 == 0 && outcode2 == 0) 45 | { 46 | accept = TRUE; 47 | done = TRUE; 48 | } 49 | else if (outcode1 & outcode2) 50 | { 51 | done = TRUE; 52 | } 53 | else 54 | { 55 | double x, y; 56 | int outcode_ex = outcode1 ? outcode1 : outcode2; 57 | if (outcode_ex & TOP) 58 | { 59 | x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1); 60 | y = ymax; 61 | } 62 | 63 | else if (outcode_ex & BOTTOM) 64 | { 65 | x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1); 66 | y = ymin; 67 | } 68 | else if (outcode_ex & RIGHT) 69 | { 70 | y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1); 71 | x = xmax; 72 | } 73 | else 74 | { 75 | y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1); 76 | x = xmin; 77 | } 78 | if (outcode_ex == outcode1) 79 | { 80 | x1 = x; 81 | y1 = y; 82 | outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax); 83 | } 84 | else 85 | { 86 | x2 = x; 87 | y2 = y; 88 | outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax); 89 | } 90 | } 91 | } while (done == FALSE); 92 | 93 | if (accept == TRUE) 94 | line (x1, y1, x2, y2); 95 | } 96 | 97 | 98 | 99 | void main() 100 | { 101 | int n; 102 | int i, j; 103 | int ln[MAX][4]; 104 | int clip[4]; 105 | int gd = DETECT, gm; 106 | 107 | printf ("Enter the number of lines to be clipped"); 108 | scanf ("%d", &n); 109 | 110 | printf ("Enter the x- and y-coordinates of the line-endpoints:\n"); 111 | for (i=0; i