├── .metadata
├── .lock
├── .mylyn
│ ├── .taskListIndex
│ │ ├── write.lock
│ │ └── segments_1
│ ├── .tasks.xml.zip
│ ├── tasks.xml.zip
│ └── repositories.xml.zip
├── .plugins
│ ├── org.eclipse.jdt.core
│ │ ├── externalFilesCache
│ │ ├── javaLikeNames.txt
│ │ ├── nonChainingJarsCache
│ │ ├── assumedExternalFilesCache
│ │ ├── variablesAndContainers.dat
│ │ ├── externalLibsTimeStamps
│ │ └── savedIndexNames.txt
│ ├── org.eclipse.core.resources
│ │ ├── .root
│ │ │ ├── .indexes
│ │ │ │ ├── history.version
│ │ │ │ ├── properties.version
│ │ │ │ └── properties.index
│ │ │ └── 4.tree
│ │ ├── .history
│ │ │ ├── 2
│ │ │ │ └── e0be0065db9c001a1dfded76c40eda9f
│ │ │ ├── 8
│ │ │ │ └── 40ec817ada9c001a1dfded76c40eda9f
│ │ │ ├── 21
│ │ │ │ └── 7066bc44db9c001a1dfded76c40eda9f
│ │ │ ├── 46
│ │ │ │ └── 80c15549d79c001a1dfded76c40eda9f
│ │ │ ├── 51
│ │ │ │ └── 900952d9d99c001a1dfded76c40eda9f
│ │ │ ├── 53
│ │ │ │ └── f0be58e2d79c001a1dfded76c40eda9f
│ │ │ ├── 59
│ │ │ │ └── 80130649da9c001a1dfded76c40eda9f
│ │ │ ├── 60
│ │ │ │ └── f0804a58db9c001a1dfded76c40eda9f
│ │ │ ├── 65
│ │ │ │ └── a05c6fd8d79c001a1dfded76c40eda9f
│ │ │ ├── 82
│ │ │ │ └── c0e98645d89c001a1dfded76c40eda9f
│ │ │ ├── 86
│ │ │ │ └── 80d4535dda9c001a1dfded76c40eda9f
│ │ │ ├── 89
│ │ │ │ └── 60d2d5deda9c001a1dfded76c40eda9f
│ │ │ ├── 90
│ │ │ │ └── 80fb4f67da9c001a1dfded76c40eda9f
│ │ │ ├── 91
│ │ │ │ └── b04b442fda9c001a1dfded76c40eda9f
│ │ │ ├── 95
│ │ │ │ └── 108e59c8d99c001a1dfded76c40eda9f
│ │ │ ├── 96
│ │ │ │ └── 20e32ebada9c001a1dfded76c40eda9f
│ │ │ ├── b9
│ │ │ │ └── 809f0e3ad79c001a1dfded76c40eda9f
│ │ │ ├── 5b
│ │ │ │ └── 902392a9d79c001a1dfded76c40eda9f
│ │ │ ├── 3c
│ │ │ │ └── 909ec4fbd79c001a1dfded76c40eda9f
│ │ │ ├── f
│ │ │ │ └── 505affccd79c001a1dfded76c40eda9f
│ │ │ ├── c7
│ │ │ │ ├── c0c84e30d89c001a1dfded76c40eda9f
│ │ │ │ └── f0fd4888da9c001a1dfded76c40eda9f
│ │ │ ├── d9
│ │ │ │ └── 90a7bd2ad99c001a1dfded76c40eda9f
│ │ │ ├── 8e
│ │ │ │ └── d078a727d79c001a1dfded76c40eda9f
│ │ │ ├── 8c
│ │ │ │ ├── 803ab544db9c001a1dfded76c40eda9f
│ │ │ │ └── 3024b772da9c001a1dfded76c40eda9f
│ │ │ ├── e7
│ │ │ │ └── 700b5b6fdb9c001a1dfded76c40eda9f
│ │ │ ├── ac
│ │ │ │ └── c0b1bd68d99c001a1dfded76c40eda9f
│ │ │ ├── 8f
│ │ │ │ └── a03b0912da9c001a1dfded76c40eda9f
│ │ │ ├── e4
│ │ │ │ └── 50d078efd99c001a1dfded76c40eda9f
│ │ │ ├── 5f
│ │ │ │ └── b0bc248ed99c001a1dfded76c40eda9f
│ │ │ ├── ad
│ │ │ │ └── 20112a82da9c001a1dfded76c40eda9f
│ │ │ ├── b3
│ │ │ │ └── d08ee594d99c001a1dfded76c40eda9f
│ │ │ ├── c2
│ │ │ │ └── e0e7009dd99c001a1dfded76c40eda9f
│ │ │ └── fc
│ │ │ │ └── 1085eea5d99c001a1dfded76c40eda9f
│ │ ├── .projects
│ │ │ ├── Bit Manipulation
│ │ │ │ ├── 4.tree
│ │ │ │ ├── .indexes
│ │ │ │ │ ├── af
│ │ │ │ │ │ └── history.index
│ │ │ │ │ └── e4
│ │ │ │ │ │ └── history.index
│ │ │ │ └── org.eclipse.jdt.core
│ │ │ │ │ └── state.dat
│ │ │ └── STL Challenges
│ │ │ │ ├── .markers
│ │ │ │ ├── 1.tree
│ │ │ │ ├── .indexes
│ │ │ │ ├── af
│ │ │ │ │ └── history.index
│ │ │ │ └── e4
│ │ │ │ │ ├── history.index
│ │ │ │ │ └── e3
│ │ │ │ │ └── history.index
│ │ │ │ └── org.eclipse.jdt.core
│ │ │ │ └── state.dat
│ │ └── .safetable
│ │ │ └── org.eclipse.core.resources
│ ├── org.eclipse.ui.intro
│ │ └── introstate
│ ├── org.eclipse.core.runtime
│ │ └── .settings
│ │ │ ├── org.eclipse.ui.prefs
│ │ │ ├── org.eclipse.core.resources.prefs
│ │ │ ├── org.eclipse.mylyn.context.core.prefs
│ │ │ ├── org.eclipse.ui.editors.prefs
│ │ │ ├── org.eclipse.m2e.discovery.prefs
│ │ │ ├── org.eclipse.jsch.core.prefs
│ │ │ ├── org.eclipse.mylyn.monitor.ui.prefs
│ │ │ ├── org.eclipse.jdt.junit.prefs
│ │ │ ├── org.eclipse.mylyn.java.ui.prefs
│ │ │ ├── org.eclipse.ui.ide.prefs
│ │ │ ├── org.eclipse.ui.workbench.prefs
│ │ │ ├── org.eclipse.mylyn.tasks.ui.prefs
│ │ │ ├── org.eclipse.debug.ui.prefs
│ │ │ ├── org.eclipse.jdt.launching.prefs
│ │ │ ├── org.eclipse.jdt.core.prefs
│ │ │ └── org.eclipse.jdt.ui.prefs
│ ├── org.eclipse.jdt.ui
│ │ ├── OpenTypeHistory.xml
│ │ └── QualifiedTypeNameHistory.xml
│ ├── org.eclipse.tips.ide
│ │ └── dialog_settings.xml
│ ├── org.eclipse.ltk.core.refactoring
│ │ └── .refactorings
│ │ │ ├── STL Challenges
│ │ │ └── 2020
│ │ │ │ └── 5
│ │ │ │ └── 21
│ │ │ │ ├── refactorings.index
│ │ │ │ └── refactorings.history
│ │ │ └── Bit Manipulation
│ │ │ └── 2020
│ │ │ └── 5
│ │ │ └── 21
│ │ │ └── refactorings.index
│ ├── org.eclipse.ui.workbench
│ │ ├── dialog_settings.xml
│ │ └── workingsets.xml
│ ├── org.eclipse.jdt.launching
│ │ ├── .install.xml
│ │ └── libraryInfos.xml
│ ├── org.eclipse.oomph.setup
│ │ └── workspace.setup
│ ├── org.eclipse.ltk.ui.refactoring
│ │ └── dialog_settings.xml
│ ├── org.eclipse.ui.ide
│ │ └── dialog_settings.xml
│ ├── org.eclipse.debug.ui
│ │ ├── dialog_settings.xml
│ │ └── launchConfigurationHistory.xml
│ ├── org.eclipse.m2e.logback.configuration
│ │ └── 0.log
│ └── org.eclipse.debug.core
│ │ └── .launches
│ │ ├── String_Sort (1).launch
│ │ └── String_Sort.launch
└── version.ini
├── 01. STL Challenges
├── test.py
├── bubble_sort.py
├── String_Sort.java
└── binary_search.py
├── 15. Number Theory Challenges
├── 0. 21 Questions total.txt
├── 02. Virat and Factorials.py
├── 03. Evaluating Functions.py
├── 17. Big GCD.py
├── 14. Max XOR Array.py
├── 10. Modular Exponentiation.py
├── 21. Even and Odd Divisors.py
├── 01. Multiplicative Inverse.py
├── 05. Deepak and Primes.py
├── 04. T Prime.py
├── 09. Alice and Totient.py
├── 20. Vivek and Factors.py
├── 12. Virat and nCr.py
├── 05. Deepak_and_Primes.java
├── 19. Let's Play Game.py
├── 07. Find_It.java
├── 13. Linear Congerence.py
└── 18. Primality Testing.py
├── 09. Challenges - Mathematics
├── 16. Dice expectations - 1.py
├── 20. head_expectation.py
├── 18. interview_candidates.py
├── 17. choosing_numbers.py
├── 19. dice_roll_expectation.py
├── 10. evaluating_Functions.py
├── 6. Catalan_Numbers.py
├── 21. FAVDICE - Coupon Collector problem.py
├── 1. Power.py
├── 2. Birthday_Paradox.py
├── 4. marbles.py
├── 7. Random_Query.py
├── 11. Divisible_Subarrays.py
├── 10. Evaluating_Functions.java
├── 3. Boston_Numbers.py
├── 12. Fast_Fibonacci.py
├── 9. Math_Day.java
└── 8. XOR-4.py
├── .gitattributes
├── .vscode
└── settings.json
├── 16. Recursion and backtracking
├── 1. Factorial.py
├── 07. Multiply.py
├── 2. Fibbonacci.py
├── 08.Tiling Problem.py
├── no of unique paths.py
├── 10. Linear Search.py
├── 05. Increasing Decreasing.py
├── 15. Subsequence Generation.py
├── 17. Permutation.py
├── 4. Power Function.py
├── 06. Binary Search.py
├── 3. Is Sorted.py
├── 12. Tower of Hanoi.py
├── 16. Keypad Problem.py
├── 14. Quick Sort.py
├── 09. 2048 Problem.py
├── print unique paths in matrix.py
├── 13. Merge Sort.py
├── 18. Rat in Maze.py
├── 13. Merge Sort Space Optimized.py
└── 11. Bubble Sort.py
├── 11. Number Theory-II GCD & Extended Euclidean
├── GCD.py
└── Extended_Euclid.py
├── 02. Bit Manipulation
├── unique_no_in_even_freq_list.py
├── basics.py
├── fast_exponentiaiton.py
├── decimal_to_binary.py
├── binary_to_decimal.py
├── replace_bits_in_n_by_m.py
├── count_set_bits.py
├── generate_subsets.py
├── unique_no_3.py
├── unique_nos_2.py
└── common_bit_tasks.py
├── 03. Challenges - Bit Manipulation
├── 2. unique_number_1.py
├── 8. incredible_hulk.py
├── 9. Tavas_and_sadas.py
├── 5. ultra_fast_mathematician.py
├── 1. playing_with_bits.py
├── 4. unique_no_3.py
├── 3. unique_nos_2.py
└── 7. not_so_easy_maths.py
├── 04. Maths-I Big Integers
├── big_factorial.py
├── julka_spoj.py
├── big_factorial.java
└── julka_spoj.java
├── 08. Maths -V Mathematical Expectations
├── Favourite_Dice.py
└── Random_Query.py
├── 05. Maths-II Combinatorics
├── binets_formula.py
└── birthday_paradox.py
├── 12. Number Theory - III Modulo Problems
├── Fast_Modulo_Exponentiation.py
├── Big_GCD.java
└── Multiplicative_Modulo_Inverse.py
├── 06. Maths-III Solving Linear Recurrences
└── fast_exponentiation.py
├── 44. Dynamic Programming
├── 02. Fibonnaci Bottom Up.py
├── 12. Rod Cutting Bottom Up.py
├── 01. Fibbonacci Top Down.py
├── 09. Ladders Top Down.py
├── 13. Longest Increasing Subsequence.py
├── 07. Wines Problem Top Down.py
├── 05. Min Coin Change Bottom Up.py
├── 08. Max Subarray Sum.py
├── 06. Min Coin Change Top Down.py
├── 11. Rod Cutting Top Down.py
├── 04. Min Steps to One Bottom Up.py
├── Wines Problem Bottom Up.py
├── 03. Min Steps to One Top Down.py
├── 10. Ladder Bottom Up.py
└── 13. Longest Common Subsequence.py
├── 10. Number Theory - 1 Primes and Factorization
├── Optimized_prime_factorization.py
├── prime_sieve.py
├── Bitset_Sieve.java
├── Almost_Primes.java
├── A_Factorial_Problem.py
├── Product_Of_Three_Numbers_Codeforces.java
├── Fermat_Little_Primality_Testing.py
└── Large_Prime.java
├── 17. Challenges - Recursion And Backtracking
├── 04. Friends Pairing Problem.py
├── 02. Generate Paranthesis.py
├── 08. Smart Keypad - I.py
├── 03. Count Number of Binary.py
├── 10. Tricky Permutations.py
├── 07. Class Assignment.py
├── 11. No same Permutations.py
├── 15. Subset Sum Easy.py
├── 18. Recursion Dictionary Order.py
├── 20. n_queen_hard.py
├── 12. Sum It Up.py
├── 01. Tiling_Problem_2.java
└── 05. Codes_of_the_String.java
├── 29. Greedy Algorithms
├── 06. Chopstick.py
├── 01. Indian Coin Change.py
├── 05. Defense of a Kingdom.py
├── 03. Biased_Standing.java
├── 04. BALIFE_Load_Balancing.java
└── 02. Activity_Selection.java
├── 45. DP Problems Set - I
├── 1. Problem A - Frog 1.py
├── 2. Problem B - Frog 2.py
├── 3. Problem C - Vacation.py
└── 4. Problem D - Knapsack 1.py
├── 18. Divide & Conquer
├── 4. Binary Search.py
├── 11. Winning CB Scholarship.py
├── 6. HELP RAHUL TO SEARCH.py
├── 3. Randomized Quick Sort.py
├── 7. Square root using binary search.py
├── 13. EKO SPOJ.py
├── 5. Binary Search - First and Last.py
├── 1. Merge sort.py
├── test.py
├── 9. Book Allocation.py
├── 12. Painter's Partition Problem.py
├── 2. Inversion Count.py
└── 8. Aggressive Cows.py
├── 55. Advanced DS - Hashing Problems
├── 03. Largest subarray with 0 sum.py
├── 01. Subarray with 0 sum.py
├── 04. Longest Sub-Array with Sum K.py
├── 02. Subarray with given sum.py
├── 05. Longest Consecutive Sequence.py
└── 07. Unique Words Pair.py
├── 19. Challenges - Divide And Conquer
├── Winning CB Scholarship.py
├── Maximum Subarray Sum - Kadanes Algo.py
├── Simple Enough.py
├── Are They Same.py
├── PIVOT OF SORTED AND ROTATED ARRAY.py
├── Pair of Roses.py
├── 7. HELP RAHUL TO SEARCH.py
├── Aggressive Cows.py
├── Max Subarray Sum - DnC.py
├── 2. Inversion Count.py
├── Alpha Score.py
└── It's Murder SPOJ.py
├── 47. DP Problems Set - III
└── 1. coins.py
├── 20. Segment Trees I - Basics
├── segment_tree_update_range.py
├── segment_tree_query.py
├── segment_tree_point_update.py
└── segment_tree.py
├── 14. Number Theory - V Theorems
├── 1. Totient.py
├── 4. Mobius Function.py
├── 2. Light's New Car.py
├── 6. Boring_Factorials.java
└── 3. Chinese Remainder Theorem.py
├── 07. Maths - IV Pigeonhole Principle
├── Divisible_Subarrays.py
├── Divisible_Subset.py
└── Gray_Similar_Code.py
├── 50. String Pattern Matching
├── 1. Naive Approach.py
└── 2. Rabin karp.py
├── 46. DP Problems Set - II
├── 2. Grid - I.py
└── 1. Problem F - LCS.py
├── 47. Grid Based DP
├── 02. Rat & Elephant Ways.py
├── 01. min cost path.py
└── 04. Gold Grid.py
├── 36. Graph 05 - Disjoint Set Union
└── Cycle_Detection_DSU.java
├── 13. Number Theory - IV Linear Diophantine Equations
├── Linear_Diophantine_Equation.py
└── The_Football_Season.py
├── 34. Graph 03 - Undirected Graph Problems
├── Flood fill Algorithm.py
└── Bipartite Graph.py
├── 32. Graph 01 - Introduction
└── Adjacency_List.java
├── 50. Challenges - Dynamic Programming
└── 02. Stock Selling.java
├── 56. Advanced DS - Trie Problems
└── 01. Trie Implmentation.py
├── README.md
└── 21. Segment Trees II - Lazy Propagation
└── 1. Lazy Propogation.py
/.metadata/.lock:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/01. STL Challenges/test.py:
--------------------------------------------------------------------------------
1 | print(10%1)
--------------------------------------------------------------------------------
/.metadata/.mylyn/.taskListIndex/write.lock:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/15. Number Theory Challenges/0. 21 Questions total.txt:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.core/javaLikeNames.txt:
--------------------------------------------------------------------------------
1 | java
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.core/nonChainingJarsCache:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/16. Dice expectations - 1.py:
--------------------------------------------------------------------------------
1 | print(6)
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.core/assumedExternalFilesCache:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/history.version:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.version:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/b9/809f0e3ad79c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/20. head_expectation.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 | print(2**(n+1)-2)
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/18. interview_candidates.py:
--------------------------------------------------------------------------------
1 | import math
2 | print(math.ceil(1/0.16))
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/17. choosing_numbers.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 | print(format((9*n)/100,'.2f'))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.ui.intro/introstate:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | showIntro=false
3 |
--------------------------------------------------------------------------------
/.metadata/version.ini:
--------------------------------------------------------------------------------
1 | #Thu May 28 17:15:28 IST 2020
2 | org.eclipse.core.runtime=2
3 | org.eclipse.platform=4.15.0.v20200305-0155
4 |
--------------------------------------------------------------------------------
/.metadata/.mylyn/.tasks.xml.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.mylyn/.tasks.xml.zip
--------------------------------------------------------------------------------
/.metadata/.mylyn/tasks.xml.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.mylyn/tasks.xml.zip
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | version=1
3 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.ui/OpenTypeHistory.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.tips.ide/dialog_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/19. dice_roll_expectation.py:
--------------------------------------------------------------------------------
1 | n = 6
2 | res = 0
3 | for i in range(1,n+1):
4 | res += 1/i
5 |
6 | print(round(res*n))
--------------------------------------------------------------------------------
/15. Number Theory Challenges/02. Virat and Factorials.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 | res = 1
3 | for i in range(1,n+1):
4 | res *= i
5 |
6 | print(res)
--------------------------------------------------------------------------------
/.metadata/.mylyn/repositories.xml.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.mylyn/repositories.xml.zip
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.context.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | mylyn.attention.migrated=true
3 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.editors.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | overviewRuler_migration=migrated_3.1
3 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "C_Cpp.errorSquiggles": "Enabled",
3 | "python.pythonPath": "C:\\Users\\sinha\\anaconda3\\envs\\env_dlib\\python.exe"
4 | }
--------------------------------------------------------------------------------
/16. Recursion and backtracking/1. Factorial.py:
--------------------------------------------------------------------------------
1 | def fact(n):
2 |
3 | if n==1:
4 | return 1
5 |
6 | return n*fact(n-1)
7 |
8 | print(fact(5))
--------------------------------------------------------------------------------
/.metadata/.mylyn/.taskListIndex/segments_1:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.mylyn/.taskListIndex/segments_1
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/10. evaluating_Functions.py:
--------------------------------------------------------------------------------
1 | x = int(input())
2 |
3 | fx = (3*(x**2))-x+10
4 | gx = (4*(x**3))+(2*(x**2))-(5*x)+4
5 |
6 | print(fx+gx)
--------------------------------------------------------------------------------
/15. Number Theory Challenges/03. Evaluating Functions.py:
--------------------------------------------------------------------------------
1 | x = int(input())
2 |
3 | fx = (3*(x**2))-x+10
4 | gx = (4*(x**3))+(2*(x**2))-(5*x)+4
5 |
6 | print(fx+gx)
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.m2e.discovery.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.m2e.discovery.pref.projects=
3 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jsch.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jsch.core.hasChangedDefaultWin32SshHome=true
3 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/STL Challenges/2020/5/21/refactorings.index:
--------------------------------------------------------------------------------
1 | 1590227669473 Delete element
2 | 1590227673399 Delete element
3 |
--------------------------------------------------------------------------------
/11. Number Theory-II GCD & Extended Euclidean/GCD.py:
--------------------------------------------------------------------------------
1 | # a x b = LCM(a, b) * GCD (a, b)
2 | def GCD(a,b):
3 | return a if b==0 else GCD(b,a%b)
4 |
5 | print(GCD(12,20))
--------------------------------------------------------------------------------
/15. Number Theory Challenges/17. Big GCD.py:
--------------------------------------------------------------------------------
1 |
2 | def GCD(a,b):
3 | return a if b==0 else GCD(b,a%b)
4 |
5 | a,b = [int(i) for i in input().split()]
6 |
7 | print(GCD(a,b))
--------------------------------------------------------------------------------
/16. Recursion and backtracking/07. Multiply.py:
--------------------------------------------------------------------------------
1 | def multiply(a,b):
2 |
3 | if b==1:
4 | return a
5 |
6 | return a + multiply(a,b-1)
7 |
8 | print(multiply(5,10))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat:
--------------------------------------------------------------------------------
1 | JRE_LIB JRE_SRCROOT
2 | JUNIT_HOME JRE_SRC JUNIT_SRC_HOME M2_REPO
--------------------------------------------------------------------------------
/02. Bit Manipulation/unique_no_in_even_freq_list.py:
--------------------------------------------------------------------------------
1 | arr = [int(i) for i in input().split()]
2 |
3 | ans = 0
4 | for i in arr:
5 | ans = ans ^ i
6 |
7 | print("Unique no is:",ans)
8 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.root/4.tree:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.root/4.tree
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.monitor.ui.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.mylyn.monitor.activity.tracking.enabled.checked=true
3 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.junit.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.junit.content_assist_favorite_static_members_migrated=true
3 |
--------------------------------------------------------------------------------
/03. Challenges - Bit Manipulation/2. unique_number_1.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 |
3 | arr = [int(i) for i in input().split()]
4 |
5 | ans = 0
6 | for i in arr:
7 | ans = ans ^ i
8 |
9 | print(ans)
--------------------------------------------------------------------------------
/04. Maths-I Big Integers/big_factorial.py:
--------------------------------------------------------------------------------
1 | def fact(N):
2 | res = 1
3 | for i in range(1,N+1):
4 | res *= i
5 |
6 | return res
7 |
8 |
9 | print(fact(100))
10 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/2/e0be0065db9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | package snippet;
2 |
3 | public class Snippet {
4 | 3
5 | bat
6 | apple
7 | batman
8 | }
9 |
10 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.core/externalLibsTimeStamps:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.jdt.core/externalLibsTimeStamps
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.ui.workbench/dialog_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.java.ui.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.mylyn.java.ui.run.count.3_10_0=1
3 | org.eclipse.mylyn.java.ui.run.count.3_1_0=1
4 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.launching/.install.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/2. Fibbonacci.py:
--------------------------------------------------------------------------------
1 | def Fibb(n):
2 |
3 | if n<=0:
4 | return 0
5 | if n==1:
6 | return 1
7 |
8 | return Fibb(n-1)+Fibb(n-2)
9 |
10 | print(Fibb(6))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.ide.prefs:
--------------------------------------------------------------------------------
1 | PROBLEMS_FILTERS_MIGRATE=true
2 | eclipse.preferences.version=1
3 | platformState=1560766426310
4 | quickStart=false
5 | tipsAndTricks=true
6 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/Bit Manipulation/2020/5/21/refactorings.index:
--------------------------------------------------------------------------------
1 | 1590227615441 Rename type 'test'
2 | 1590227677782 Copy compilation unit
3 | 1590227686834 Delete element
4 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index:
--------------------------------------------------------------------------------
1 | / org.eclipse.core.resources contentCacheState 2 contentCacheTimestamp
1560766426310 org.eclipse.jdt.core stateVersionNumber 34
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/Bit Manipulation/4.tree:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/Bit Manipulation/4.tree
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/.markers:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/.markers
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/1.tree:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/1.tree
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt:
--------------------------------------------------------------------------------
1 | INDEX VERSION 1.131+C:\Users\sinha\Desktop\Luicifer\Study\Java\Competitive Programming\.metadata\.plugins\org.eclipse.jdt.core
2 | 2524005488.index
3 | 3924257938.index
4 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.jdt.launching/libraryInfos.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/46/80c15549d79c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 |
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/08.Tiling Problem.py:
--------------------------------------------------------------------------------
1 | def WaysOfTiling(n):
2 |
3 | if n==4:
4 | return 2
5 |
6 | if n<4:
7 | return 1
8 |
9 | return WaysOfTiling(n-1) + WaysOfTiling(n-4)
10 |
11 |
12 | print(WaysOfTiling(5))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/.indexes/af/history.index:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/.indexes/af/history.index
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/.indexes/e4/history.index:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/.indexes/e4/history.index
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/6. Catalan_Numbers.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 |
3 | num = 1
4 | deno = 1
5 |
6 | for i in range(n+1,(2*n)+1):
7 | num*=i
8 |
9 | for i in range(1,n+1):
10 | deno *= i
11 |
12 | ans = num//(deno*(n+1))
13 |
14 | print(ans)
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/Bit Manipulation/.indexes/af/history.index:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/Bit Manipulation/.indexes/af/history.index
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/Bit Manipulation/.indexes/e4/history.index:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/Bit Manipulation/.indexes/e4/history.index
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/.indexes/e4/e3/history.index:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/.indexes/e4/e3/history.index
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/org.eclipse.jdt.core/state.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/STL Challenges/org.eclipse.jdt.core/state.dat
--------------------------------------------------------------------------------
/16. Recursion and backtracking/no of unique paths.py:
--------------------------------------------------------------------------------
1 | def solve(m,n):
2 |
3 | if m==0 or n==0:
4 | return 1
5 |
6 | # Recc case
7 | return solve(m-1,n) + solve(m,n-1)
8 |
9 |
10 | m,n = [int(i) for i in input().split()]
11 | print(solve(m-1,n-1))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.projects/Bit Manipulation/org.eclipse.jdt.core/state.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashankSinha98/Competitive-Programming/HEAD/.metadata/.plugins/org.eclipse.core.resources/.projects/Bit Manipulation/org.eclipse.jdt.core/state.dat
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.oomph.setup/workspace.setup:
--------------------------------------------------------------------------------
1 |
2 |
7 |
--------------------------------------------------------------------------------
/02. Bit Manipulation/basics.py:
--------------------------------------------------------------------------------
1 | a = 11 # 00001011
2 | b = 7 # 00000111
3 |
4 | print("a & b:",a&b) #00000011
5 | print("a | b:",a|b) #00001111
6 | print("a ^ b:",a^b) #00001100
7 | print("~ a:",~a) #11110100 = -12
8 | print("a << 2:",a<<2) #11*2*2
9 | print("a >> 2:",a>>2) #11/2/2
--------------------------------------------------------------------------------
/08. Maths -V Mathematical Expectations/Favourite_Dice.py:
--------------------------------------------------------------------------------
1 | # Accepted on SPOJ
2 | t = int(input())
3 |
4 | while(t!=0):
5 | t-=1
6 | n = int(input())
7 |
8 | ans = 0
9 | for i in range(1,n+1):
10 | ans+=1/i
11 |
12 |
13 | print(round(ans*n,2))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/5b/902392a9d79c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 |
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | System.out.print("Hello World");
8 |
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/05. Maths-II Combinatorics/binets_formula.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 | def nth_fibb(N):
4 |
5 | a = math.pow(((1+math.sqrt(5))/2),N)
6 | b = math.pow(((1-math.sqrt(5))/2),N)
7 |
8 | res = (a-b)/math.sqrt(5)
9 |
10 | return int(res)
11 |
12 | print(nth_fibb(9))
13 |
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/21. FAVDICE - Coupon Collector problem.py:
--------------------------------------------------------------------------------
1 | # Accepted on SPOJ
2 | t = int(input())
3 |
4 | while(t!=0):
5 | t-=1
6 | n = int(input())
7 |
8 | ans = 0
9 | for i in range(1,n+1):
10 | ans+=1/i
11 |
12 |
13 | print(round(ans*n,2))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.ltk.ui.refactoring/dialog_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
--------------------------------------------------------------------------------
/03. Challenges - Bit Manipulation/8. incredible_hulk.py:
--------------------------------------------------------------------------------
1 | def count_set_bits_optimised(N):
2 | ans = 0
3 | while(N!=0):
4 | N = N & (N-1)
5 | ans+=1
6 | return ans
7 |
8 | t = int(input())
9 |
10 | for i in range(t):
11 | print(count_set_bits_optimised(int(input())))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs:
--------------------------------------------------------------------------------
1 | //org.eclipse.ui.commands/state/org.eclipse.ui.navigator.resources.nested.changeProjectPresentation/org.eclipse.ui.commands.radioState=false
2 | PLUGINS_NOT_ACTIVATED_ON_STARTUP=;org.eclipse.m2e.discovery;
3 | eclipse.preferences.version=1
4 |
--------------------------------------------------------------------------------
/02. Bit Manipulation/fast_exponentiaiton.py:
--------------------------------------------------------------------------------
1 | def fast_expoentiation(a,b):
2 |
3 | res=1
4 |
5 | while(b!=0):
6 | bit = b & 1
7 |
8 | if bit==1:
9 | res=res*a
10 |
11 | a*=a
12 | b=b>>1
13 |
14 | return res
15 |
16 | print(fast_expoentiation(3,5))
--------------------------------------------------------------------------------
/16. Recursion and backtracking/10. Linear Search.py:
--------------------------------------------------------------------------------
1 | def LinearSearch(arr,k,idx):
2 |
3 | if idx == len(arr):
4 | return -1
5 |
6 | if arr[idx] == k:
7 | return idx
8 |
9 | return LinearSearch(arr,k,idx+1)
10 |
11 | arr = [1,5,2,9,7]
12 | key = 0
13 | print(LinearSearch(arr,key,0))
14 |
--------------------------------------------------------------------------------
/12. Number Theory - III Modulo Problems/Fast_Modulo_Exponentiation.py:
--------------------------------------------------------------------------------
1 | def Fast_Exp(a,b,m):
2 |
3 | res = 1
4 | while b!=0:
5 |
6 | if b&1==1:
7 | res=(res*a)%m
8 |
9 | b = b>>1
10 | a=(a*a)%m
11 |
12 | return res
13 |
14 | print(Fast_Exp(5,3,100))
15 |
16 |
--------------------------------------------------------------------------------
/06. Maths-III Solving Linear Recurrences/fast_exponentiation.py:
--------------------------------------------------------------------------------
1 | a = int(input())
2 | b = int(input())
3 |
4 | def fast_exp(a,b):
5 |
6 | if b==0:
7 | return 1
8 |
9 | if (b&1):
10 | return a*((fast_exp(a,b//2))**2)
11 | else:
12 | return ((fast_exp(a,b//2))**2)
13 |
14 | print(fast_exp(a,b))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.tasks.ui.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | migrated.task.repositories.secure.store=true
3 | org.eclipse.mylyn.tasks.ui.filters.nonmatching=true
4 | org.eclipse.mylyn.tasks.ui.filters.nonmatching.encouraged=true
5 | org.eclipse.mylyn.tasks.ui.welcome.message=true
6 |
--------------------------------------------------------------------------------
/01. STL Challenges/bubble_sort.py:
--------------------------------------------------------------------------------
1 | def bubble_sort(arr):
2 |
3 | for i in range(len(arr)-1):
4 | for j in range(0,len(arr)-i-1):
5 | if arr[j] > arr[j+1]:
6 | arr[j+1],arr[j] = arr[j], arr[j+1]
7 |
8 | return arr
9 |
10 | arr = [5,4,3,2,1,-4,-10]
11 | arr = bubble_sort(arr)
12 | print(arr)
--------------------------------------------------------------------------------
/02. Bit Manipulation/decimal_to_binary.py:
--------------------------------------------------------------------------------
1 | def decimal_to_binary(N):
2 | ans = 0
3 | p=1
4 |
5 | while(N!=0):
6 | last_bit = N&1
7 | ans += p*last_bit
8 | p*=10
9 | N = N>>1
10 |
11 | return ans
12 |
13 | print(decimal_to_binary(13)) #1101
14 | print(decimal_to_binary(23)) #10111
--------------------------------------------------------------------------------
/44. Dynamic Programming/02. Fibonnaci Bottom Up.py:
--------------------------------------------------------------------------------
1 | LIM = 10**3
2 | arr = [0 for i in range(LIM)]
3 |
4 | def Fibb(num):
5 | global arr
6 |
7 | arr[1] = 1
8 |
9 | for i in range(2,num+1):
10 | arr[i] = arr[i-1]+arr[i-2]
11 |
12 | return arr[num]
13 |
14 |
15 |
16 | n = int(input())
17 | print(Fibb(n))
18 |
--------------------------------------------------------------------------------
/02. Bit Manipulation/binary_to_decimal.py:
--------------------------------------------------------------------------------
1 | def bin_to_dec(bin):
2 | bin = str(bin)
3 | mul = 1
4 | res=0
5 | for i in range(len(bin)-1,-1,-1):
6 | if bin[i]=='1':
7 | res+=mul
8 |
9 | mul=mul*2
10 |
11 | return res
12 |
13 | print(bin_to_dec("1101100"))
14 | #89
15 | #108
--------------------------------------------------------------------------------
/02. Bit Manipulation/replace_bits_in_n_by_m.py:
--------------------------------------------------------------------------------
1 | def clear_range_of_bits(N,i,j):
2 | a = (-1<<(j+1))
3 | b = (1<\r\n\r\n
3 | org.eclipse.debug.ui.save_dirty_editors_before_launch=always
4 | preferredTargets=default\:default|
5 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/05. Increasing Decreasing.py:
--------------------------------------------------------------------------------
1 | def increasing(n):
2 |
3 | if n==0:
4 | return
5 |
6 | increasing(n-1)
7 | print(n)
8 |
9 | #increasing(10)
10 |
11 | def decreasing(n):
12 |
13 | if n==0:
14 | return
15 |
16 | print(n)
17 | decreasing(n-1)
18 |
19 | decreasing(10)
20 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/15. Subsequence Generation.py:
--------------------------------------------------------------------------------
1 | def generateSubseq(arr,subarr,i):
2 |
3 | if i==len(arr):
4 | print(subarr)
5 | return
6 |
7 | generateSubseq(arr,subarr,i+1)
8 | subarr = subarr+[arr[i]]
9 | generateSubseq(arr,subarr,i+1)
10 |
11 | arr = ['a','b','c']
12 | generateSubseq(arr,[],0)
--------------------------------------------------------------------------------
/03. Challenges - Bit Manipulation/9. Tavas_and_sadas.py:
--------------------------------------------------------------------------------
1 | # Accepted on Codeforces
2 |
3 | n = input()
4 |
5 | x = len(n)
6 |
7 | small_count = 2*(2**(x-1)-1)
8 |
9 | big_count = 1 # counting that no' itself
10 |
11 | for i in range(0,x):
12 |
13 | if n[i] == '7':
14 | big_count += 2**(x-i-1)
15 |
16 | print(big_count+small_count)
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/1. Power.py:
--------------------------------------------------------------------------------
1 | a = int(input())
2 | b = int(input())
3 |
4 | def power(a,b):
5 |
6 | if b == 0:
7 | return 1
8 |
9 | elif b == 1:
10 | return a
11 |
12 | X = power(a,b//2)
13 | if (b&1)==1:
14 | return a*X*X
15 | else:
16 | return X*X
17 |
18 | print(power(a,b))
19 |
--------------------------------------------------------------------------------
/15. Number Theory Challenges/14. Max XOR Array.py:
--------------------------------------------------------------------------------
1 | import sys
2 | n = int(input())
3 | arr = []
4 | xor = 0
5 |
6 | for i in range(n):
7 | num = int(input())
8 | arr.append(num)
9 | xor ^= num
10 |
11 | max_xor = -1 * sys.maxsize
12 |
13 | for i in arr:
14 | if xor^i > max_xor:
15 | max_xor = xor^i
16 |
17 | print(max_xor)
--------------------------------------------------------------------------------
/10. Number Theory - 1 Primes and Factorization/Optimized_prime_factorization.py:
--------------------------------------------------------------------------------
1 | N = int(input())
2 |
3 | factors = []
4 |
5 | i = 2
6 | while i*i<=N:
7 | if N%i == 0:
8 | while N%i == 0:
9 | factors.append(i)
10 | N //= i
11 | i+=1
12 |
13 | if N!=1:
14 | factors.append(N)
15 |
16 | print(factors)
17 |
18 |
19 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/17. Permutation.py:
--------------------------------------------------------------------------------
1 | def permutation(txt,idx):
2 |
3 | if idx==len(txt):
4 | print(txt)
5 | return
6 |
7 | for i in range(idx,len(txt)):
8 | txt[i],txt[idx] = txt[idx],txt[i]
9 | permutation(txt,idx+1)
10 | txt[i],txt[idx] = txt[idx],txt[i]
11 |
12 |
13 | permutation(["a","b","c"],0)
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/04. Friends Pairing Problem.py:
--------------------------------------------------------------------------------
1 | LIM = 31
2 | sieve = [0 for i in range(LIM)]
3 | sieve[1] = 1
4 | sieve[2] = 2
5 |
6 | for i in range(3,LIM):
7 | sieve[i] = sieve[i-1] + ( (i-1) * sieve[i-2] )
8 |
9 | t = int(input())
10 |
11 | while t!=0:
12 | t-=1
13 | n = int(input())
14 | print(sieve[n])
15 |
16 |
--------------------------------------------------------------------------------
/44. Dynamic Programming/12. Rod Cutting Bottom Up.py:
--------------------------------------------------------------------------------
1 | def max_profit(cost):
2 | n = len(cost)
3 | dp = [0] * (n+1)
4 |
5 | for l in range(1,n+1):
6 | best = 0
7 | for cut in range(1,l+1):
8 | best = max(best,cost[cut-1]+dp[l-cut])
9 | dp[l] = best
10 |
11 | return dp[n]
12 |
13 | cost = [2,3,2,5]
14 | print(max_profit(cost))
--------------------------------------------------------------------------------
/29. Greedy Algorithms/06. Chopstick.py:
--------------------------------------------------------------------------------
1 | n,d = [int(i) for i in input().split()]
2 |
3 | chops = []
4 |
5 | for i in range(n):
6 | inp = int(input())
7 | chops.append(inp)
8 |
9 | chops.sort()
10 |
11 | cnt = 0
12 | i = 0
13 | while i+1!=n:
14 | if chops[i+1]-chops[i]<=d:
15 | cnt+=1
16 | i+=2
17 | else:
18 | i+=1
19 |
20 | print(cnt)
21 |
22 |
--------------------------------------------------------------------------------
/03. Challenges - Bit Manipulation/5. ultra_fast_mathematician.py:
--------------------------------------------------------------------------------
1 | t = int(input())
2 |
3 | for i in range(t):
4 | inp = input().split()
5 | a,b = inp
6 |
7 | pos=0
8 | res=""
9 | while pos=(1-min_percent)):
10 | prob *= (365-count)/365
11 | count+=1
12 |
13 |
14 | return count
15 |
16 | prob = float(input())
17 | print(no_of_persons(prob))
18 |
--------------------------------------------------------------------------------
/15. Number Theory Challenges/10. Modular Exponentiation.py:
--------------------------------------------------------------------------------
1 | def ModPow(a,b,c):
2 |
3 | if b==0:
4 | return 1
5 |
6 | smallAns = ModPow(a,b//2,c)
7 | smallAns = (smallAns*smallAns) % c
8 |
9 | if b&1 == 0:
10 | return smallAns%c
11 | else:
12 | return (a*smallAns)%c
13 |
14 |
15 | a,b,c = [int(i) for i in input().split()]
16 | print(ModPow(a,b,c))
--------------------------------------------------------------------------------
/44. Dynamic Programming/01. Fibbonacci Top Down.py:
--------------------------------------------------------------------------------
1 | LIM = 10**6
2 | arr = [0 for i in range(LIM)]
3 |
4 | def Fibb(num):
5 | global arr
6 |
7 | if num==0 or num==1:
8 | return num
9 |
10 | if arr[num]!=0:
11 | return arr[num]
12 |
13 | ans = Fibb(num-1)+Fibb(num-2)
14 | arr[num] = ans
15 | return ans
16 |
17 |
18 | n = int(input())
19 | print(Fibb(n))
--------------------------------------------------------------------------------
/15. Number Theory Challenges/21. Even and Odd Divisors.py:
--------------------------------------------------------------------------------
1 | LIM =10**5+1
2 |
3 | sum_arr = [0 for i in range(LIM)]
4 |
5 | for i in range(1,LIM):
6 | for j in range(i,LIM,i):
7 | if i&1==0:
8 | sum_arr[j]+=i
9 | else:
10 | sum_arr[j]-=i
11 |
12 | t = int(input())
13 |
14 |
15 | while t!=0:
16 | t-=1
17 | n = int(input())
18 | print(sum_arr[n])
19 |
--------------------------------------------------------------------------------
/44. Dynamic Programming/09. Ladders Top Down.py:
--------------------------------------------------------------------------------
1 | dp = [0]*100
2 |
3 | def solve(n,k):
4 |
5 | if n == 0:
6 | return 1 # 1 way to stand on ground
7 |
8 | if dp[n] != 0:
9 | return dp[n]
10 |
11 | ans = 0
12 | for i in range(1,k+1):
13 | if n-i>=0:
14 | ans += solve(n-i,k)
15 | dp[n] = ans
16 | return dp[n]
17 |
18 | print(solve(4,3))
19 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/4. Power Function.py:
--------------------------------------------------------------------------------
1 | def Pow1(a,b):
2 | if b==1:
3 | return a
4 |
5 | return a * Pow1(a,b-1)
6 |
7 |
8 | def Pow2(a,b):
9 |
10 | if b==1:
11 | return a
12 |
13 | small_ans = Pow2(a,b>>1)
14 | small_ans*=small_ans
15 | if b&1:
16 | return a*small_ans
17 | else:
18 | return small_ans
19 |
20 |
21 | print(Pow2(2,10))
--------------------------------------------------------------------------------
/44. Dynamic Programming/13. Longest Increasing Subsequence.py:
--------------------------------------------------------------------------------
1 | def solve(arr,n):
2 |
3 | dp = [1]*n
4 |
5 | for i in range(1,n):
6 | best = 0
7 | for j in range(i):
8 | if arr[j]
2 |
12 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.debug.ui/dialog_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/02. Generate Paranthesis.py:
--------------------------------------------------------------------------------
1 | def generate_paranthesis(n,i,j,out):
2 |
3 | if j == n:
4 | print(out)
5 | return
6 |
7 |
8 | if i > j:
9 | generate_paranthesis(n,i,j+1,out+')')
10 |
11 | if i < n:
12 | generate_paranthesis(n,i+1,j,out+'(')
13 |
14 |
15 |
16 |
17 | n = int(input())
18 | generate_paranthesis(n,0,0,"")
19 |
20 |
--------------------------------------------------------------------------------
/04. Maths-I Big Integers/julka_spoj.py:
--------------------------------------------------------------------------------
1 | t=10
2 | while t!=0:
3 | total = int(input())
4 | excess = int(input())
5 |
6 | # Klaudia + Natalia = total
7 | # Klaudia = Natalia + excess
8 | # So, Natalia + excess + Natalia = total
9 | # Natalia = (total-excess)/2
10 |
11 | natalia = (total-excess)//2
12 | klaudia = natalia + excess
13 |
14 | print(klaudia)
15 | print(natalia)
16 | t-=1
17 |
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/2. Birthday_Paradox.py:
--------------------------------------------------------------------------------
1 | p = float(input())
2 |
3 | def compute(min_percent):
4 |
5 | if min_percent==1.0:
6 | return 366
7 |
8 | persons=0
9 |
10 | deno = 365
11 | num = 365
12 | res=num/deno
13 |
14 | while(res>=(1-min_percent)):
15 | res = res*((num-persons)/deno)
16 | persons+=1
17 |
18 | return persons
19 |
20 | print(compute(p))
21 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/06. Binary Search.py:
--------------------------------------------------------------------------------
1 | def BinarySearch(arr,k,l,r):
2 |
3 | if l>r:
4 | return -1
5 |
6 | mid = (l+r)>>1
7 |
8 | if arr[mid]==k:
9 | return mid
10 |
11 | if k > arr[mid]:
12 | return BinarySearch(arr,k,mid+1,r)
13 | else:
14 | return BinarySearch(arr,k,l,mid-1)
15 |
16 |
17 | arr = [1,2,3,4,5]
18 | key = 2
19 | print(BinarySearch(arr,key,0,len(arr)-1))
--------------------------------------------------------------------------------
/02. Bit Manipulation/count_set_bits.py:
--------------------------------------------------------------------------------
1 | def count_set_bits(N):
2 | ans = 0
3 |
4 | while(N!=0):
5 | ans += (N&1)
6 | N = N>>1
7 |
8 | return ans
9 |
10 | def count_set_bits_optimised(N):
11 | ans = 0
12 | while(N!=0):
13 | N = N & (N-1)
14 | ans+=1
15 | return ans
16 |
17 | print(count_set_bits(13)) # 1101 O(logn)
18 | print(count_set_bits_optimised(5)) # 0101 O(no of set bits)
19 |
--------------------------------------------------------------------------------
/29. Greedy Algorithms/01. Indian Coin Change.py:
--------------------------------------------------------------------------------
1 |
2 |
3 | def no_of_coins(amt,coins):
4 |
5 | cnt = 0
6 | while amt!=0:
7 | cnt+=1
8 | for i in range(len(coins)-1,-1,-1):
9 | if amt-coins[i]>=0:
10 | amt = amt - coins[i]
11 | break
12 |
13 | return cnt
14 |
15 |
16 |
17 | coins = [1,2,5,10,20,50,100,500,2000]
18 | amt = int(input())
19 | print(no_of_coins(amt,coins))
--------------------------------------------------------------------------------
/45. DP Problems Set - I/1. Problem A - Frog 1.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 | arr = [int(i) for i in input().split()]
3 |
4 | def solve(arr,n):
5 |
6 | dp = [0]*n
7 |
8 | for i in range(1,n):
9 |
10 | if i-2>=0:
11 | dp[i] = min(abs(arr[i]-arr[i-1])+dp[i-1],abs(arr[i]-arr[i-2])+dp[i-2])
12 | else:
13 | dp[i] = abs(arr[i]-arr[i-1])
14 | #print(dp)
15 | return dp[-1]
16 |
17 | print(solve(arr,n))
--------------------------------------------------------------------------------
/11. Number Theory-II GCD & Extended Euclidean/Extended_Euclid.py:
--------------------------------------------------------------------------------
1 | import sys
2 | x = sys.maxsize
3 | y = sys.maxsize
4 | GCD = sys.maxsize
5 |
6 | def ExtendedEuclid(a,b):
7 | global x, y, GCD
8 |
9 | if b==0:
10 | x=1
11 | y=0
12 | GCD = a
13 | return
14 |
15 | ExtendedEuclid(b,a%b)
16 | cX = y
17 | cY = x - (a//b)*y
18 |
19 | x = cX
20 | y = cY
21 |
22 | ExtendedEuclid(15,25)
23 | print(x,y,GCD)
24 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/3. Is Sorted.py:
--------------------------------------------------------------------------------
1 | def isSortedhalf(arr):
2 | n = len(arr)
3 |
4 | if n==1:
5 | return True
6 |
7 | if n==2:
8 | return arr[0]arr[mid]:
14 | left = mid+1
15 |
16 | else:
17 | right = mid-1
18 |
19 | return -1
20 |
21 | arr = [1,3,5,7,9,11]
22 | k = 13
23 | print(binary_search(arr,k))
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/08. Smart Keypad - I.py:
--------------------------------------------------------------------------------
1 | table = [ " ", ".+@$", "abc", "def", "ghi", "jkl" , "mno", "pqrs" , "tuv", "wxyz" ]
2 |
3 | def print_soln(n,i,out):
4 |
5 | if i==len(n):
6 | print(out)
7 | return
8 |
9 |
10 | word = table[int(n[i])]
11 |
12 | #if n[i] == "0":
13 | # print_soln(n,i+1,out)
14 | #else:
15 | for j in word:
16 | print_soln(n,i+1,out+j)
17 |
18 | n = input()
19 | print_soln(n,0,"")
20 |
--------------------------------------------------------------------------------
/44. Dynamic Programming/07. Wines Problem Top Down.py:
--------------------------------------------------------------------------------
1 | wines = [2,4,6,2,5]
2 | dp = [[0]*len(wines) for i in range(len(wines))]
3 |
4 |
5 | def solve(wines,i,j,y):
6 | global dp
7 |
8 | if i>j:
9 | return 0
10 |
11 | if dp[i][j]!=0:
12 | return dp[i][j]
13 |
14 | ans = max(wines[i]*y+solve(wines,i+1,j,y+1),wines[j]*y+solve(wines,i,j-1,y+1))
15 | dp[i][j] = ans
16 | return ans
17 |
18 |
19 | print(solve(wines,0,len(wines)-1,1))
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/3c/909ec4fbd79c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 |
10 | String[] arr = new String[n];
11 |
12 | for(int i=0;i arr = new ArrayList<>();
11 |
12 | for(int i=0;i=0:
11 | temp = min(dp[i-c],temp)
12 | dp[i] = temp+1
13 |
14 | print(dp)
15 | return dp[val]
16 |
17 |
18 | n = int(input())
19 | coins = [int(i) for i in input().split()]
20 | dp = [0 for i in range(n+1)]
21 | print(solve(n))
--------------------------------------------------------------------------------
/44. Dynamic Programming/08. Max Subarray Sum.py:
--------------------------------------------------------------------------------
1 | import sys
2 | # Kadane's Algo
3 | def solve(arr):
4 |
5 | max_so_far = -sys.maxsize
6 | max_till_ith = 0
7 | n = len(arr)
8 |
9 | for i in range(n):
10 | max_till_ith += arr[i]
11 |
12 | if max_till_ith>max_so_far:
13 | max_so_far = max_till_ith
14 | if max_till_ith < 0:
15 | max_till_ith = 0
16 |
17 | return max_so_far
18 |
19 | arr = [-2, -3, 4, -1, -2, 1, 5, -3]
20 | print(solve(arr))
21 |
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/4. marbles.py:
--------------------------------------------------------------------------------
1 | # Accepted on Code chef :D
2 | t = int(input())
3 |
4 | def combination(a,b):
5 |
6 | if a==b or b==0:
7 | return 1
8 |
9 | if b==1:
10 | return a
11 |
12 | ans = 1
13 |
14 | if b>a-b:
15 | b = a-b
16 |
17 | for i in range(0,b):
18 | ans *= ((a-i)/(i+1))
19 |
20 | return round(ans)
21 |
22 | while(t!=0):
23 | t-=1
24 | n,k = [int(i) for i in input().split()]
25 |
26 | print(combination(n-1,k-1))
--------------------------------------------------------------------------------
/16. Recursion and backtracking/12. Tower of Hanoi.py:
--------------------------------------------------------------------------------
1 | def TowerOfHanoi(n,src,dest,helper):
2 |
3 | # Base case
4 | if n==0:
5 | return
6 |
7 | # Moving n-1 disks from src to helper using dest
8 | TowerOfHanoi(n-1,src,helper,dest)
9 | # Moving nth disk from src to dest directly
10 | print("Move",n,"disk from",src,"to",dest)
11 | # Moving n-1 disks from helper to dest using src
12 | TowerOfHanoi(n-1,helper,dest,src)
13 |
14 |
15 | n = int(input())
16 |
17 | TowerOfHanoi(n,'A','C','B')
--------------------------------------------------------------------------------
/44. Dynamic Programming/06. Min Coin Change Top Down.py:
--------------------------------------------------------------------------------
1 | coins = [1,7,10]
2 | dp = [0]*100
3 | import sys
4 |
5 |
6 | def solve(v):
7 | global coins,dp
8 |
9 | if v==0:
10 | return 0
11 |
12 | if dp[v]!=0:
13 | return dp[v]
14 |
15 | temp = sys.maxsize
16 | for c in coins:
17 | if v-c>=0:
18 | temp = min(temp,solve(v-c))
19 |
20 | dp[v] = temp + 1
21 | return dp[v]
22 |
23 |
24 | print(solve(15))
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/55. Advanced DS - Hashing Problems/03. Largest subarray with 0 sum.py:
--------------------------------------------------------------------------------
1 | def maxLen(n, arr):
2 | #Code here
3 |
4 | maxDiff = 0
5 | mydict = {}
6 | cumm = [0]*(n+1)
7 | mydict[0] = 0
8 |
9 | for i in range(1,n+1):
10 | cumm[i] = cumm[i-1] + arr[i-1]
11 | if cumm[i] not in mydict:
12 | mydict[cumm[i]] = i
13 | else:
14 | st = mydict[cumm[i]]
15 | diff = i-st
16 | maxDiff = max(maxDiff,diff)
17 |
18 | return maxDiff
19 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/65/a05c6fd8d79c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | System.out.println("H");
9 | int n = s.nextInt();
10 |
11 | String[] arr = new String[n];
12 |
13 | for(int i=0;i\r\n\r\n \r\n \r\n \r\n\r\n
3 |
--------------------------------------------------------------------------------
/02. Bit Manipulation/generate_subsets.py:
--------------------------------------------------------------------------------
1 | def filter_chars(n,str):
2 |
3 | res=""
4 | j=0
5 | while(n!=0):
6 | last_bit = n & 1
7 |
8 | if last_bit == 1:
9 | res+=str[j]
10 | j+=1
11 | n=n>>1
12 | return res
13 |
14 | def generate_subsets(string):
15 |
16 | n = len(string)
17 | res = ""
18 | for i in range(1<
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/44. Dynamic Programming/11. Rod Cutting Top Down.py:
--------------------------------------------------------------------------------
1 | cost = [1,5,8,9,10,17,17,20]
2 | dp = [0]*100
3 |
4 | def max_profit(n,cost):
5 | global dp
6 |
7 | if n == 0:
8 | return 0
9 |
10 | if dp[n]!=0:
11 | return dp[n]
12 |
13 | best_profit = 0
14 | for i in range(1,n+1):
15 | profit = cost[i-1]+max_profit(n-i,cost)
16 | best_profit = max(best_profit,profit)
17 |
18 | dp[n] = best_profit
19 |
20 | return best_profit
21 |
22 | print(max_profit(8,cost))
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/45. DP Problems Set - I/2. Problem B - Frog 2.py:
--------------------------------------------------------------------------------
1 | import sys
2 | n,k = [int(i) for i in input().split()]
3 | arr = [int(i) for i in input().split()]
4 |
5 | def solve(arr,n,k):
6 |
7 | dp = [0]*n
8 |
9 | for i in range(1,n):
10 | best = sys.maxsize
11 | for j in range(i-1,i-k-1,-1):
12 | if j<0:
13 | break
14 | else:
15 | best = min(abs(arr[i]-arr[j])+dp[j],best)
16 |
17 | dp[i] = best
18 |
19 | return dp[n-1]
20 |
21 |
22 | print(solve(arr,n,k))
23 |
--------------------------------------------------------------------------------
/44. Dynamic Programming/04. Min Steps to One Bottom Up.py:
--------------------------------------------------------------------------------
1 | import sys
2 | LIM=10**3
3 | dp = [0 for i in range(LIM)]
4 |
5 | def solve(num):
6 | global dp
7 |
8 | for i in range(2,num+1):
9 | x = sys.maxsize
10 | y = sys.maxsize
11 |
12 | if i%3==0:
13 | x = dp[i//3]
14 | if i%2==0:
15 | y = dp[i//2]
16 | z = dp[i-1]
17 |
18 | ans = min(x,y,z)
19 | dp[i] = ans + 1
20 |
21 | return dp[num]
22 |
23 | num = int(input())
24 | print(solve(num))
25 |
26 |
--------------------------------------------------------------------------------
/10. Number Theory - 1 Primes and Factorization/prime_sieve.py:
--------------------------------------------------------------------------------
1 | n = 100
2 |
3 | def prime_sieve(n):
4 |
5 | arr = [1 for i in range(n+1)]
6 | arr[0] = 0
7 | arr[1] = 0
8 | arr[2] = 1
9 |
10 | for i in range(2*2,n+1,2):
11 | arr[i] = 0
12 |
13 | for i in range(3,n,2):
14 | if arr[i] == 1:
15 | for j in range(i*i,n+1,2*i):
16 | arr[j] = 0
17 |
18 | return arr
19 |
20 | primes = prime_sieve(n)
21 | for i in range(1,n+1):
22 | if primes[i]==1:
23 | print(i,end=" ")
24 |
25 |
--------------------------------------------------------------------------------
/47. DP Problems Set - III/1. coins.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 | arr = [float(i) for i in input().split()]
3 |
4 |
5 | def solve(arr,n,x):
6 | global dp
7 | if x==0:
8 | return 1
9 |
10 | if n==0:
11 | return 0
12 |
13 | if dp[n][x]> -0.9:
14 | return dp[n][x]
15 |
16 | dp[n][x] = round(arr[n-1]*solve(arr,n-1,x-1),3) + round((1-arr[n-1])*solve(arr,n-1,x),3)
17 |
18 | return round(dp[n][x],3)
19 |
20 | x = (n+1)//2
21 | dp = [[-1]*(x+1) for i in range(n+1)]
22 | print(solve(arr,n,x))
23 | print(dp)
24 |
25 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.codeComplete.visibilityCheck=enabled
3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
5 | org.eclipse.jdt.core.compiler.compliance=12
6 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8 | org.eclipse.jdt.core.compiler.release=enabled
9 | org.eclipse.jdt.core.compiler.source=12
10 |
--------------------------------------------------------------------------------
/15. Number Theory Challenges/05. Deepak and Primes.py:
--------------------------------------------------------------------------------
1 | LEN_LIM = 500000
2 | LIM = 10000000
3 |
4 | primes = []
5 | primes.append(2)
6 |
7 | prime_sieve = [1 for i in range(LIM+1)]
8 | prime_sieve[0] = prime_sieve[1] = 0
9 | for i in range(4,LIM+1,2):
10 | prime_sieve[i] = 0
11 |
12 | for i in range(3,LIM+1):
13 | if prime_sieve[i] == 1:
14 | primes.append(i)
15 | if len(primes) == LEN_LIM:
16 | break
17 |
18 | for j in range(i*i,LIM+1,2*i):
19 | prime_sieve[j] = 0
20 |
21 | n = int(input())
22 | print(primes[n-1])
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/Maximum Subarray Sum - Kadanes Algo.py:
--------------------------------------------------------------------------------
1 | def maxSubArr(arr):
2 |
3 | max_res = 0
4 | max_till = 0
5 |
6 | for i in arr:
7 | max_till+=i
8 |
9 | if max_till>max_res:
10 | max_res = max_till
11 |
12 | if max_till < 0:
13 | max_till = 0
14 |
15 | return max_res
16 |
17 |
18 |
19 | t = int(input())
20 |
21 | while t!=0:
22 | t-=1
23 |
24 | n = int(input())
25 | arr = [int(i) for i in input().split()]
26 |
27 | ans = maxSubArr(arr)
28 | print(ans)
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/d9/90a7bd2ad99c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 |
10 | ArrayList arr = new ArrayList<>();
11 |
12 | for(int i=0;iend):
5 | return
6 |
7 | if(st == end):
8 | tree[index]+=inc
9 | return
10 |
11 | mid = (st+end)//2
12 |
13 | do_update_range(tree,st,mid,qs,qe,inc,2*index)
14 | do_update_range(tree,mid+1,end,qs,qe,inc,2*index+1)
15 |
16 | tree[index] = min(tree[2*index],tree[2*index+1])
17 |
18 |
19 |
20 |
21 | def update_range(tree,qs,qe,inc,n):
22 | do_update_range(tree,0,n-1,qs,qe,inc,1)
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/7. Random_Query.py:
--------------------------------------------------------------------------------
1 | # Accepted on Hacker blocks, giving RTE on test case 6 on Codeforces
2 |
3 | lim = 10**6
4 | last_occ = [ 0 for i in range(lim)]
5 |
6 | n = int(input())
7 | ans = [0 for i in range(n+1)]
8 |
9 | arr = [int(i) for i in input().split()]
10 | arr.insert(0,0)
11 | #print("arr",arr)
12 |
13 | for i in range(1,n+1):
14 | ans[i] = ans[i-1] + (i-last_occ[arr[i]])
15 | last_occ[arr[i]] = i
16 |
17 | #print("ans",ans)
18 | total_sum = sum(ans)
19 |
20 | ans = round((2*(total_sum-n)+n)/n**2,6)
21 | #print(ans)
22 | print(format(ans,'.6f'))
23 |
24 |
--------------------------------------------------------------------------------
/04. Maths-I Big Integers/big_factorial.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | import java.math.BigInteger;
3 | public class big_factorial {
4 |
5 | public static void main(String[] args){
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 | System.out.println(fact(n));
12 | }
13 |
14 | public static BigInteger fact(int n){
15 |
16 | BigInteger res = BigInteger.valueOf(1);
17 |
18 | for(int i=1;i<=n;i++){
19 | res = res.multiply(BigInteger.valueOf(i));
20 | }
21 |
22 | return res;
23 | }
24 |
25 | }
--------------------------------------------------------------------------------
/44. Dynamic Programming/Wines Problem Bottom Up.py:
--------------------------------------------------------------------------------
1 |
2 | wines = [2,4,6,2,5]
3 |
4 | n = len(wines)
5 | dp = [[0]*n for i in range(n)]
6 |
7 | for i in range(n-1,-n,-1):
8 | for x in range(0,n):
9 | y = x-i
10 |
11 | if y<0 or y>=n:
12 | continue
13 | else:
14 | if x>y:
15 | dp[x][y] = 0
16 | elif x-y==0:
17 | dp[x][y] = n*wines[x]
18 | else:
19 | yr = n-(y-x)
20 | dp[x][y] = max(wines[x]*yr+dp[x+1][y],wines[y]*yr+dp[x][y-1])
21 |
22 |
23 | #print(dp)
24 | print(dp[0][n-1])
--------------------------------------------------------------------------------
/44. Dynamic Programming/03. Min Steps to One Top Down.py:
--------------------------------------------------------------------------------
1 |
2 | import sys
3 |
4 | def solve(n: int):
5 | global dp
6 |
7 | if n==1:
8 | return 0
9 |
10 | if dp[n]!=0:
11 | return dp[n]
12 |
13 | x = y = z = sys.maxsize
14 |
15 | if n%3==0:
16 | x = dp[n//3] if dp[n//3]!=0 else solve(n//3)
17 |
18 | if n%2==0:
19 | y = dp[n//2] if dp[n//2]!=0 else solve(n//2)
20 |
21 | z = dp[n-1] if dp[n-1]!=0 else solve(n-1)
22 |
23 | ans = min(x,y,z)+1
24 | dp[n] = ans
25 | return dp[n]
26 |
27 | n = int(input())
28 | dp = [0]*(n+1)
29 | print(solve(n))
30 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/16. Keypad Problem.py:
--------------------------------------------------------------------------------
1 | keypad = ["","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"]
2 |
3 | def generate_names(arr,out,i):
4 |
5 | if i==len(arr) or len(out)==len(arr):
6 | print(out)
7 | return
8 |
9 |
10 |
11 | num = arr[i]
12 | if num == 0 or num==1: # For cases having "" string in keypad
13 | generate_names(arr,out,i+1)
14 | else :
15 | word = keypad[num]
16 | for j in word:
17 | generate_names(arr,out+[j],i+1)
18 |
19 |
20 | arr = [1,1,2,3]
21 | #arr = [6,6,6]
22 | generate_names(arr,[],0)
23 |
24 |
25 |
--------------------------------------------------------------------------------
/02. Bit Manipulation/unique_no_3.py:
--------------------------------------------------------------------------------
1 | def unique_no_3(arr):
2 |
3 | sum_arr = [0]*64
4 |
5 | for i in arr:
6 | pos=0
7 | while(i!=0):
8 | bit=i&1
9 | sum_arr[pos]+=bit
10 | i = i>>1
11 | pos+=1
12 |
13 | sum_arr = [i%3 for i in sum_arr]
14 |
15 | res = 0
16 | p=0
17 |
18 | for i in sum_arr:
19 |
20 | if i == 1:
21 | res+=2**p
22 | p+=1
23 |
24 | return res
25 |
26 |
27 |
28 | n = int(input())
29 | inp_arr = [int(i) for i in input().split()]
30 | ans = unique_no_3(inp_arr)
31 | print(ans)
32 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/14. Quick Sort.py:
--------------------------------------------------------------------------------
1 | arr=[2,7,8,6,1,5,4]
2 |
3 | def quick_sort(arr,st,end):
4 |
5 |
6 | if st>=end:
7 | return
8 |
9 | arr,p = partition(arr,st,end)
10 |
11 | quick_sort(arr,st,p-1)
12 | quick_sort(arr,p+1,end)
13 |
14 |
15 | def partition(arr,st,end):
16 |
17 | pivot = arr[end]
18 | i = st-1
19 |
20 | for j in range(st,end):
21 |
22 | if arr[j]<=pivot:
23 | i+=1
24 | arr[i],arr[j] = arr[j],arr[i]
25 |
26 | arr[i+1],arr[end] = arr[end],arr[i+1]
27 |
28 | return arr,i+1
29 |
30 |
31 | quick_sort(arr,0,len(arr)-1)
32 | print(arr)
--------------------------------------------------------------------------------
/16. Recursion and backtracking/09. 2048 Problem.py:
--------------------------------------------------------------------------------
1 | word_arr = ["zero ","one ","two ","three ","four ","five ","six ","seven ","eight ","nine "]
2 |
3 | res = ""
4 |
5 | # Top Down
6 | def numToWord(num):
7 | global res
8 |
9 | if num==0:
10 | return
11 |
12 | w = word_arr[num%10]
13 |
14 | res = w + res
15 |
16 | numToWord(num//10)
17 |
18 | def numToWordBottomUp(num):
19 |
20 | if num==0:
21 | return
22 |
23 | numToWordBottomUp(num//10)
24 | print(word_arr[num%10],end=" ")
25 |
26 | numToWordBottomUp(1023)
27 |
28 |
29 |
30 |
31 | numToWord(2048)
32 | print()
33 | print(res)
34 |
35 |
--------------------------------------------------------------------------------
/44. Dynamic Programming/10. Ladder Bottom Up.py:
--------------------------------------------------------------------------------
1 | def solve(n,k):
2 |
3 | dp = [0]*(n+1)
4 | dp[0] = 1
5 |
6 | for i in range(1,n+1):
7 | ans = 0
8 | for j in range(1,k+1):
9 | if i-j>=0:
10 | ans += dp[i-j]
11 | dp[i] = ans
12 | print(dp)
13 | return dp[n]
14 |
15 |
16 | def optimized_solve(n,k):
17 |
18 | dp = [0]*(n+1)
19 | dp[0] = dp[1] = 1
20 | for i in range(2,k+1):
21 | dp[i] = 2*dp[i-1]
22 |
23 | for i in range(k+1,n+1):
24 | dp[i] = 2*dp[i-1] - dp[i-k-1]
25 |
26 | return dp[n]
27 |
28 | print(optimized_solve(6,3))
29 |
30 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/60/f0804a58db9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
4 | org.eclipse.jdt.core.compiler.compliance=12
5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
9 | org.eclipse.jdt.core.compiler.release=enabled
10 | org.eclipse.jdt.core.compiler.source=12
11 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/8e/d078a727d79c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
4 | org.eclipse.jdt.core.compiler.compliance=12
5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
9 | org.eclipse.jdt.core.compiler.release=enabled
10 | org.eclipse.jdt.core.compiler.source=12
11 |
--------------------------------------------------------------------------------
/03. Challenges - Bit Manipulation/4. unique_no_3.py:
--------------------------------------------------------------------------------
1 | def unique_no_3(arr):
2 |
3 | sum_arr = [0]*64
4 |
5 | for i in arr:
6 | pos=0
7 | while(i!=0):
8 | bit=i&1
9 | sum_arr[pos]+=bit
10 | i = i>>1
11 | pos+=1
12 |
13 | sum_arr = [i%3 for i in sum_arr]
14 |
15 | res = 0
16 | p=0
17 |
18 | for i in sum_arr:
19 |
20 | if i == 1:
21 | res+=2**p
22 | p+=1
23 |
24 | return res
25 |
26 |
27 |
28 | n = int(input())
29 | inp_arr = [int(i) for i in input().split()]
30 | ans = unique_no_3(inp_arr)
31 | print(ans)
32 |
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/11. Divisible_Subarrays.py:
--------------------------------------------------------------------------------
1 | t = int(input())
2 |
3 | while(t!=0):
4 | t-=1
5 | n = int(input())
6 | a = [int(i) for i in input().split()]
7 |
8 | cumm_sum = []
9 |
10 | curr_sum = 0
11 | for i in a:
12 | curr_sum+=i
13 | cumm_sum.append((curr_sum+n) % n)
14 |
15 | #print(cumm_sum)
16 |
17 | count_arr = [0 for i in range(n)]
18 |
19 | count_arr[0] = 1 # bcz of null subarr in beginning
20 |
21 | for i in cumm_sum:
22 | count_arr[i]+=1
23 |
24 | res = 0
25 | for i in count_arr:
26 | res += (((i)*(i-1))//2)
27 |
28 | print(res)
29 |
30 |
31 |
--------------------------------------------------------------------------------
/14. Number Theory - V Theorems/1. Totient.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | LIM = 10**5
3 |
4 | prime_sieve = [0 for i in range(LIM+1)]
5 |
6 | for i in range(2,LIM+1):
7 | if prime_sieve[i] == 0:
8 | for j in range(i, LIM+1,i):
9 | if prime_sieve[j]==0:
10 | prime_sieve[j] = i
11 |
12 | prime_sieve[1] = prime_sieve[0] = 1
13 |
14 | N = int(input())
15 | p = N
16 | primes = []
17 | while p!=1:
18 | x = prime_sieve[p]
19 | primes.append(x)
20 | p //= x
21 |
22 | #print(primes)
23 |
24 | unique_primes = np.unique(primes)
25 |
26 | res = N
27 | for i in unique_primes:
28 | res *= (1-1/i)
29 |
30 | print(int(round(res)))
31 |
32 |
33 |
--------------------------------------------------------------------------------
/15. Number Theory Challenges/04. T Prime.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 | LIM = 10**6
4 |
5 | n = int(input())
6 | arr = [int(i) for i in input().split()]
7 |
8 | primes = []
9 | primes.append(2)
10 | prime_sieve = [1 for i in range(LIM+1)]
11 | prime_sieve[0] = prime_sieve[1] = 0
12 | for i in range(4,LIM+1,2):
13 | prime_sieve[i]=0
14 |
15 | for i in range(3,LIM+1):
16 | if prime_sieve[i]==1:
17 | primes.append(i)
18 | for j in range(i*i,LIM+1,2*i):
19 | prime_sieve[j] = 0
20 |
21 |
22 | for i in arr:
23 | if (int(math.sqrt(i))*int(math.sqrt(i))==i and prime_sieve[int(math.sqrt(i))]==1):
24 | print("YES")
25 | else:
26 | print("NO")
--------------------------------------------------------------------------------
/16. Recursion and backtracking/print unique paths in matrix.py:
--------------------------------------------------------------------------------
1 | def solve(arr,i,j,m,n):
2 |
3 | if i==m-1 and j==n-1:
4 | arr[m-1][n-1] = 1
5 | for x in range(m):
6 | for y in range(n):
7 | print(arr[x][y],end=" ")
8 | print()
9 | print()
10 | arr[m-1][n-1] = 0
11 | return
12 |
13 |
14 | if i>=m or j>=n:
15 | return
16 |
17 | arr[i][j] = 1
18 | # Move Right
19 | solve(arr,i+1,j,m,n)
20 | # Move Down
21 | solve(arr,i,j+1,m,n)
22 |
23 | arr[i][j] = 0
24 |
25 |
26 | m,n = [int(i) for i in input().split()]
27 | arr = [[0]*n for i in range(m)]
28 |
29 | solve(arr,0,0,m,n)
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/03. Count Number of Binary.py:
--------------------------------------------------------------------------------
1 | LIM = 91
2 |
3 | dp = [[0]*2 for i in range(LIM)]
4 | dp[0][0] = dp[0][1] = 1
5 | for i in range(1,LIM):
6 | dp[i][0] = dp[i-1][0]+dp[i-1][1]
7 | dp[i][1] = dp[i-1][0]
8 |
9 |
10 | def solve_opt(n):
11 | print(dp[n][0])
12 |
13 |
14 | def solve(n,i,out):
15 | global ans
16 | if i==n:
17 | ans+=1
18 | return
19 |
20 | if len(out)==0 or out[-1]=="0":
21 | solve(n,i+1,out+"0")
22 | solve(n,i+1,out+"1")
23 |
24 | else:
25 | solve(n,i+1,out+"0")
26 |
27 | t = int(input())
28 | while t!=0:
29 | t-=1
30 | ans = 0
31 | n = int(input())
32 | solve_opt(n)
33 | #print(ans)
--------------------------------------------------------------------------------
/12. Number Theory - III Modulo Problems/Big_GCD.java:
--------------------------------------------------------------------------------
1 | import java.math.BigInteger;
2 | import java.util.Scanner;
3 |
4 | public class Big_GCD{
5 |
6 | public static void main(String[] args) {
7 | // TODO Auto-generated method stub
8 |
9 | Scanner s = new Scanner(System.in);
10 |
11 | System.out.println("Input a(10^250):");
12 | BigInteger big_a = s.nextBigInteger();
13 | System.out.println("Input b(10^7):");
14 | int b = s.nextInt();
15 |
16 | BigInteger b2 = BigInteger.valueOf(b);
17 | int mod = big_a.mod(b2).intValue();
18 |
19 | System.out.println("GCD: "+GCD(mod,b));
20 |
21 |
22 | }
23 |
24 | static int GCD(int a,int b) {
25 |
26 | return b==0?a:GCD(b,a%b);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/12. Number Theory - III Modulo Problems/Multiplicative_Modulo_Inverse.py:
--------------------------------------------------------------------------------
1 | import sys
2 | x = sys.maxsize
3 | y = sys.maxsize
4 | GCD = sys.maxsize
5 |
6 | def GCD(a,b):
7 | return a if b==0 else GCD(b,a%b)
8 |
9 | def ExtendedEuclid(a,b):
10 | global x, y, GCD
11 |
12 | if b==0:
13 | x=1
14 | y=0
15 | GCD = a
16 | return
17 |
18 | ExtendedEuclid(b,a%b)
19 | cX = y
20 | cY = x - (a//b)*y
21 |
22 | x = cX
23 | y = cY
24 |
25 | def invModulo(a,m):
26 | if GCD(a,m) != 1:
27 | raise Exception('No inverse modulo exist for given a and m')
28 | else:
29 | ExtendedEuclid(a,m)
30 | return (x+m)%m
31 |
32 |
33 | print(invModulo(2,1000000007))
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/Simple Enough.py:
--------------------------------------------------------------------------------
1 | def cal_range(n):
2 |
3 | res = 0
4 | while n!=0:
5 | res+=1
6 | n = n>>1
7 | return ((2**res)-1)
8 |
9 |
10 | def solve(n,st,end,l,r):
11 | global ans
12 |
13 | if(st>end):
14 | return
15 |
16 | if (str and end>r):
17 | return
18 |
19 | if (st==end and l<=st<=r):
20 | ans+=(n % 2)
21 | return
22 |
23 | mid = (st+end)//2
24 |
25 | if (l<=mid<=r):
26 | ans+=(n % 2)
27 |
28 | solve(n//2,st,mid-1,l,r)
29 | solve(n//2,mid+1,end,l,r)
30 |
31 | n,l,r = [int(i) for i in input().split()]
32 | ans = 0
33 | solve(n,1,cal_range(n),l,r)
34 | print(ans)
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/Are They Same.py:
--------------------------------------------------------------------------------
1 | def areEqual(a,b):
2 |
3 | if a==b:
4 | return True
5 |
6 | a_len = len(a)
7 | b_len = len(b)
8 |
9 | if a_len != b_len or a_len%2 != 0 or b_len%2 != 0:
10 | return False
11 |
12 |
13 | a_mid = a_len//2
14 | b_mid = b_len//2
15 |
16 | a1 = a[0:a_mid]
17 | a2 = a[a_mid:]
18 |
19 | b1 = b[0:b_mid]
20 | b2 = b[b_mid:]
21 |
22 | return (areEqual(a1,b1) and areEqual(a2,b2) or areEqual(a1,b2) and areEqual(a2,b1))
23 |
24 |
25 |
26 | t = int(input())
27 |
28 | while t!=0:
29 | t-=1
30 |
31 | a = input()
32 | b = input()
33 |
34 | ans = areEqual(a,b)
35 | res = "YES" if ans else "NO"
36 | print(res)
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/10. Tricky Permutations.py:
--------------------------------------------------------------------------------
1 | ansset = set()
2 | def print_unique_permutation(inp,i,out):
3 | global ansset
4 |
5 | if i == len(inp):
6 | res = ""
7 | for j in inp:
8 | res+=j
9 | ansset.add(res)
10 |
11 | return
12 |
13 |
14 | for j in range(i,len(inp)):
15 | if i==j or inp[i] != inp[j]:
16 |
17 | inp[i],inp[j] = inp[j],inp[i]
18 | print_unique_permutation(inp,i+1,out)
19 | inp[i],inp[j] = inp[j],inp[i]
20 |
21 | inp = [i for i in input()]
22 | inp.sort()
23 | print_unique_permutation(inp,0,"")
24 | ans = [i for i in ansset]
25 | ans.sort()
26 | for j in ans:
27 | print(j)
--------------------------------------------------------------------------------
/45. DP Problems Set - I/3. Problem C - Vacation.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 | inp_arr = [[0]*3 for i in range(n)]
3 | for i in range(n):
4 | inp = [int(j) for j in input().split()]
5 | for j in range(3):
6 | inp_arr[i][j] = inp[j]
7 |
8 |
9 | def solve(arr,n):
10 |
11 | dp = [[0]*3 for i in range(n)]
12 |
13 | dp[0][0] = inp_arr[0][0]
14 | dp[0][1] = inp_arr[0][1]
15 | dp[0][2] = inp_arr[0][2]
16 |
17 | for i in range(1,n):
18 | dp[i][0] = arr[i][0] + max(dp[i-1][1],dp[i-1][2])
19 | dp[i][1] = arr[i][1] + max(dp[i-1][0],dp[i-1][2])
20 | dp[i][2] = arr[i][2] + max(dp[i-1][0],dp[i-1][1])
21 |
22 | return max(dp[n-1][0],dp[n-1][1],dp[n-1][2])
23 |
24 | print(solve(inp_arr,n))
25 |
--------------------------------------------------------------------------------
/55. Advanced DS - Hashing Problems/01. Subarray with 0 sum.py:
--------------------------------------------------------------------------------
1 | class Soln:
2 | def solve(self,arr,n):
3 |
4 | if(0 in arr):
5 | return "YES"
6 |
7 | cumm = [0]*(n+1)
8 | for i in range(1,n+1):
9 | cumm[i] = cumm[i-1] + arr[i-1]
10 |
11 | myset = set()
12 |
13 | for i in cumm:
14 | if i in myset:
15 | return "YES"
16 | else:
17 | myset.add(i)
18 |
19 | return "NO"
20 |
21 |
22 |
23 | t = int(input())
24 |
25 | while t!=0:
26 | t-=1
27 |
28 | n = int(input())
29 | arr = [int(i) for i in input().split()]
30 | ans = Soln().solve(arr,n)
31 | print(ans)
32 |
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/PIVOT OF SORTED AND ROTATED ARRAY.py:
--------------------------------------------------------------------------------
1 | def pivot(arr,n):
2 |
3 | st = 0
4 | end = n-1
5 |
6 | while(st<=end):
7 |
8 | mid = (st+end)//2
9 |
10 | if(mid+1<=n and arr[mid]>arr[mid+1]):
11 | return mid
12 |
13 | if(mid-1>=0 and arr[mid-1]>arr[mid]):
14 | return mid-1
15 |
16 | if arr[st]>arr[mid]=1:
28 | res+=count_arr[0]
29 |
30 | for i in count_arr:
31 | res += (((i)*(i-1))//2)
32 |
33 | print(res)
34 |
35 |
36 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.m2e.logback.configuration/0.log:
--------------------------------------------------------------------------------
1 | 2020-05-23 14:48:09,771 [Worker-2: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is out-of-date. Trying to update.
2 | 2020-05-23 14:52:44,207 [Worker-2: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is up-to-date. Trying to read.
3 | 2020-05-27 00:19:45,785 [Worker-0: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is up-to-date. Trying to read.
4 | 2020-05-28 17:16:20,717 [Worker-5: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is out-of-date. Trying to update.
5 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/13. Merge Sort.py:
--------------------------------------------------------------------------------
1 | def divide(arr):
2 |
3 | if len(arr)==1:
4 | return arr
5 |
6 | n = len(arr)
7 |
8 | a = divide(arr[:n//2])
9 | b = divide(arr[n//2:])
10 | c = merge(a,b)
11 |
12 | return c
13 |
14 |
15 | def merge(a,b):
16 |
17 | c = []
18 |
19 | la,lb = len(a),len(b)
20 | i,j=0,0
21 |
22 | while i1:
20 | res*=((N**2-1)//(N-1))
21 | return res
22 |
23 | A[1] = 1
24 | for i in range(2,10**5+1):
25 | A[divisors_sum(i)] = i
26 |
27 | while True:
28 | S = int(input())
29 |
30 | if S==0:
31 | break
32 |
33 | if A[S] !=0:
34 | print(A[S])
35 | else:
36 | print(-1)
37 |
38 |
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/Pair of Roses.py:
--------------------------------------------------------------------------------
1 | import sys
2 |
3 | def pairs(arr,n,m):
4 |
5 | min_diff = sys.maxsize
6 |
7 | a,b = -1,-1
8 |
9 | for i in range(0,n-1):
10 | for j in range(i+1,n):
11 |
12 | if(arr[i]+arr[j]==m):
13 |
14 | diff = arr[j] - arr[i]
15 |
16 | if diff < min_diff:
17 | min_diff = diff
18 | a,b = arr[i],arr[j]
19 |
20 | return a,b
21 |
22 |
23 |
24 | t = int(input())
25 |
26 | while t!=0:
27 | t-=1
28 | n = int(input())
29 | arr = [int(i) for i in input().split()]
30 | arr.sort()
31 | M = int(input())
32 |
33 | a,b = pairs(arr,n,M)
34 | print("Deepak should buy roses whose prices are %d and %d."%(a,b))
35 |
36 | input()
--------------------------------------------------------------------------------
/10. Number Theory - 1 Primes and Factorization/Bitset_Sieve.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Bitset_Sieve {
3 |
4 | static int lim = 100;
5 | static ArrayList primes;
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 |
10 | BitSet b = new BitSet(lim+1);
11 | primes = new ArrayList<>();
12 |
13 | for(int i=0; i<=lim; i++) {
14 | b.set(i);
15 | }
16 | b.clear(0);
17 | b.clear(1);
18 |
19 | for(int i=2; i*i<=lim; i++) {
20 |
21 | if(b.get(i)) {
22 | for(int j=2*i; j<=lim; j+=i) {
23 | b.clear(j);
24 | }
25 | }
26 | }
27 |
28 | for(int i=0; i<=lim; i++) {
29 | if(b.get(i))
30 | primes.add(i);
31 | }
32 |
33 | System.out.println(primes);
34 |
35 |
36 |
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/7. HELP RAHUL TO SEARCH.py:
--------------------------------------------------------------------------------
1 | def find(arr,k):
2 |
3 | st = 0
4 | end = len(arr)-1
5 |
6 | while st<=end:
7 |
8 | mid = (st+end)//2
9 |
10 | if arr[mid]==k:
11 | return mid
12 |
13 | elif arr[st]<=arr[mid]:
14 |
15 | if arr[st]<= k <= arr[end]:
16 | end = mid-1
17 | else:
18 | st = mid+1
19 |
20 | elif arr[mid]<=arr[end]:
21 | if arr[mid]<= k <=arr[end]:
22 | st = mid+1
23 | else:
24 | end = mid-1
25 |
26 | return -1
27 |
28 | n = int(input())
29 |
30 | arr = []
31 | for i in range(n):
32 | arr.append(int(input()))
33 |
34 | k = int(input())
35 |
36 | print(find(arr,k))
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/15. Subset Sum Easy.py:
--------------------------------------------------------------------------------
1 |
2 | def generate_subseq(arr,n,i,out):
3 | global res
4 |
5 | if i==n:
6 | if len(out)!=0 and sum(out) == 0:
7 | #print(out)
8 | res = True
9 | return
10 |
11 | if len(out)!=0 and sum(out)==0:
12 | #print(out)
13 | res = True
14 | return
15 |
16 |
17 | generate_subseq(arr,n,i+1,out)
18 | out = out + [arr[i]]
19 | generate_subseq(arr,n,i+1,out)
20 |
21 |
22 |
23 |
24 |
25 | t = int(input())
26 |
27 | while t!=0:
28 | t-=1
29 | n = int(input())
30 | arr = [int(i) for i in input().split()]
31 | res = False
32 |
33 | generate_subseq(arr,n,0,[])
34 | if res:
35 | print("Yes")
36 | else:
37 | print("No")
--------------------------------------------------------------------------------
/20. Segment Trees I - Basics/segment_tree_query.py:
--------------------------------------------------------------------------------
1 | #import segment_tree
2 | import sys
3 |
4 | def do_query(tree,qs,qe,st,end,index):
5 |
6 | # Complete overlap
7 | if(qs<=st and qe>=end):
8 | return tree[index]
9 |
10 |
11 | # No Overlap
12 | if(qsend and qe>end):
13 | return sys.maxsize
14 |
15 | mid = (st+end)//2
16 |
17 | left = do_query(tree,qs,qe,st,mid,2*index)
18 | right = do_query(tree,qs,qe,mid+1,end,2*index+1)
19 |
20 | return min(left,right)
21 |
22 | def min_query(tree,qs,qe,n):
23 | return do_query(tree,qs,qe,0,n-1,1)
24 |
25 |
26 | #arr = [int(i) for i in input().split()]
27 | #n = len(arr)
28 | #tree = build(arr)
29 | #qs,qe = [int(i) for i in input().split()]
30 | #print(min_query(tree,qs,qe,n))
31 |
32 |
33 |
--------------------------------------------------------------------------------
/18. Divide & Conquer/3. Randomized Quick Sort.py:
--------------------------------------------------------------------------------
1 | # TLE in 2nd test case, Code correct
2 | import random
3 | def quick_sort(arr,i,j,n):
4 |
5 | if i>=j:
6 | return
7 |
8 |
9 | p = partition(arr,i,j)
10 |
11 | quick_sort(arr,i,p-1,n)
12 | quick_sort(arr,p,n-1,n)
13 |
14 |
15 | def partition(arr,st,end):
16 |
17 | r = random.randint(st,end)
18 | arr[r],arr[end] = arr[end],arr[r]
19 |
20 | pivot = arr[end]
21 |
22 | j = st-1
23 |
24 | for i in range(st,end):
25 |
26 | if arr[i] <= pivot:
27 | j+=1
28 | arr[i],arr[j] = arr[j], arr[i]
29 |
30 |
31 | arr[j+1],arr[end] = arr[end],arr[j+1]
32 | return j+1
33 |
34 |
35 | n = int(input())
36 | arr = [int(i) for i in input().split()]
37 | quick_sort(arr,0,n-1,n)
38 | print(arr)
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/10. Evaluating_Functions.java:
--------------------------------------------------------------------------------
1 | import java.math.BigInteger;
2 | import java.util.*;
3 | public class Evaluating_Functions {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 |
8 | Scanner s = new Scanner(System.in);
9 | BigInteger X = s.nextBigInteger();
10 |
11 | BigInteger FX = BigInteger.valueOf(3).multiply(X.multiply(X)).subtract(X).add(BigInteger.valueOf(10));
12 | System.out.println(FX);
13 | BigInteger GX = (BigInteger.valueOf(4).multiply(X.multiply(X).multiply(X)))
14 | .add(BigInteger.valueOf(2).multiply(X.multiply(X)))
15 | .subtract(BigInteger.valueOf(5).multiply(X))
16 | .add(BigInteger.valueOf(4));
17 | System.out.println(GX);
18 | System.out.println(FX.add(GX));
19 |
20 |
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/18. Recursion Dictionary Order.py:
--------------------------------------------------------------------------------
1 | resset = set()
2 |
3 | def print_permutations(str_arr,n,i,str_inp):
4 |
5 | if i==n:
6 | #print(str_inp)
7 | temp = ""
8 | for k in str_arr:
9 | temp+=k
10 | if temp > str_inp:
11 | resset.add(temp)
12 | return
13 |
14 |
15 | for j in range(i,n):
16 | str_arr[i],str_arr[j] = str_arr[j],str_arr[i]
17 | print_permutations(str_arr,n,i+1,str_inp)
18 | str_arr[i],str_arr[j] = str_arr[j],str_arr[i]
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | str_inp = input()
29 | str_arr = [i for i in str_inp]
30 | print_permutations(str_arr,len(str_inp),0,str_inp)
31 | res = list(resset)
32 |
33 | res.sort()
34 | for i in res:
35 | print(i)
36 |
37 |
--------------------------------------------------------------------------------
/04. Maths-I Big Integers/julka_spoj.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | import java.lang.*;
3 | import java.math.BigInteger;
4 |
5 | class julka_spoj
6 | {
7 | public static void main (String[] args) throws java.lang.Exception
8 | {
9 | Scanner s = new Scanner(System.in);
10 |
11 | int t = 10;
12 | for(int i=0; i<10; i++){
13 | BigInteger total = s.nextBigInteger();
14 | BigInteger excess = s.nextBigInteger();
15 |
16 | apples_count(total,excess);
17 | }
18 | }
19 |
20 | public static void apples_count(BigInteger total, BigInteger excess){
21 |
22 |
23 | BigInteger natalia = (total.subtract(excess)).divide(BigInteger.valueOf(2));
24 | BigInteger klaudia = natalia.add(excess);
25 |
26 | System.out.println(klaudia);
27 | System.out.println(natalia);
28 | }
29 | }
--------------------------------------------------------------------------------
/14. Number Theory - V Theorems/2. Light's New Car.py:
--------------------------------------------------------------------------------
1 | # Accepted on hackerblocks
2 | # Problem link - https://hack.codingblocks.com/app/practice/1/52/problem
3 |
4 | MOD = 10**9 + 7
5 | t = int(input())
6 |
7 |
8 | def StringToInt(s,MOD):
9 | res = 0
10 | for i in s:
11 | res = ((res*10)%MOD + int(i))%MOD
12 |
13 | return res%MOD
14 |
15 | def Power(x,y):
16 |
17 | if y==0:
18 | return 1
19 |
20 | small_ans = Power(x,y//2)
21 | small_ans = (small_ans*small_ans)%MOD
22 | if y&1 == 0:
23 | return (small_ans)%MOD
24 | else:
25 | return (x * small_ans)%MOD
26 |
27 |
28 |
29 | while(t!=0):
30 | t-=1
31 | A,B = [i for i in input().split()]
32 |
33 |
34 | X = StringToInt(A,MOD)
35 | Y = StringToInt(B,MOD-1)
36 |
37 | ans = Power(X,Y)%MOD
38 |
39 | print(ans)
--------------------------------------------------------------------------------
/18. Divide & Conquer/7. Square root using binary search.py:
--------------------------------------------------------------------------------
1 | def square_root_bin(n,p):
2 |
3 | st = 1
4 | end = n
5 |
6 | bef_dec = 0
7 |
8 | while(st<=end):
9 |
10 | mid = (st+end)//2
11 |
12 | if mid*mid == n:
13 | bef_dec = mid
14 | break
15 | elif (mid*mid)>n:
16 | end = mid-1
17 | else:
18 | bef_dec = mid
19 | st = mid+1
20 |
21 |
22 | curr_no = bef_dec
23 |
24 | if curr_no*curr_no == n:
25 | return curr_no
26 |
27 |
28 | inc = 0.1
29 | k = p
30 | while k!=0:
31 | k-=1
32 | while curr_no*curr_no <=n:
33 | curr_no += inc
34 | curr_no -= inc
35 | inc = inc/10
36 |
37 |
38 | return round(curr_no,p)
39 |
40 | n = int(input())
41 | print(square_root_bin(n,3))
42 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/18. Divide & Conquer/13. EKO SPOJ.py:
--------------------------------------------------------------------------------
1 | # TLE on SPOJ
2 |
3 | def min_height(T,W,trees):
4 |
5 | # search space
6 | st = 0
7 | end = max(trees)
8 |
9 | ans = -1
10 |
11 | while(st<=end):
12 |
13 | mid = (st+end)//2
14 |
15 | fullfilled = woodRequirementOver(T,W,trees,mid)
16 |
17 | if fullfilled:
18 | ans = max(ans,mid)
19 | st = mid+1
20 | else:
21 | end = mid-1
22 |
23 | return ans
24 |
25 |
26 | def woodRequirementOver(T,W,trees,height):
27 |
28 | woods = 0
29 | for t in trees:
30 | rem = t-height
31 | if rem>0:
32 | woods+=rem
33 |
34 | return woods>=W
35 |
36 |
37 |
38 |
39 | T, W = [int(i) for i in input().split()]
40 | trees = [int(i) for i in input().split()]
41 |
42 | ans = min_height(T,W,trees)
43 | print(ans)
--------------------------------------------------------------------------------
/15. Number Theory Challenges/12. Virat and nCr.py:
--------------------------------------------------------------------------------
1 | import sys
2 | x = sys.maxsize
3 | y = sys.maxsize
4 |
5 | def GCD(a,b):
6 | return a if b==0 else GCD(b,a%b)
7 |
8 | def ExtendedEuclid(a,b):
9 | global x, y
10 |
11 | if b==0:
12 | x=1
13 | y=0
14 | return
15 |
16 | ExtendedEuclid(b,a%b)
17 | cX = y
18 | cY = x - (a//b)*y
19 |
20 | x = cX
21 | y = cY
22 |
23 | def invModulo(a,m):
24 | if GCD(a,m) != 1:
25 | raise Exception('No inverse modulo exist for given a and m')
26 | else:
27 | ExtendedEuclid(a,m)
28 | return (x+m)%m
29 |
30 |
31 | MOD = 1000000007
32 | n,r = [int(i) for i in input().split()]
33 |
34 | if r > n-r:
35 | r = n-r
36 |
37 | ans = 1
38 | for i in range(r):
39 | ans = (ans* (n-i))%MOD
40 | ans = (ans * invModulo((r-i),MOD))%MOD
41 |
42 | print(round(ans))
43 |
--------------------------------------------------------------------------------
/20. Segment Trees I - Basics/segment_tree_point_update.py:
--------------------------------------------------------------------------------
1 | #import segment_tree
2 |
3 | def do_point_update(tree,st,end,updt_idx,inc,index):
4 |
5 | if (updt_idxend):
6 | return
7 |
8 | if(st==end==updt_idx):
9 | tree[index] = tree[index]+inc
10 | return
11 |
12 |
13 | mid = (st+end)//2
14 |
15 | do_point_update(tree,st,mid,updt_idx,inc,2*index)
16 | do_point_update(tree,mid+1,end,updt_idx,inc,2*index+1)
17 |
18 | tree[index] = min(tree[2*index],tree[2*index+1])
19 |
20 | return
21 |
22 | def point_update(tree,updt_idx,inc,n):
23 | do_point_update(tree,0,n-1,updt_idx,inc,1)
24 |
25 |
26 |
27 |
28 | #arr = [int(i) for i in input().split()]
29 | #n = len(arr)
30 | #tree = build(arr)
31 | #updt_index,inc = [int(i) for i in input().split()]
32 | #point_update(tree,updt_index,inc,n)
33 |
34 |
35 |
--------------------------------------------------------------------------------
/46. DP Problems Set - II/2. Grid - I.py:
--------------------------------------------------------------------------------
1 | n, m = [int(i) for i in input().split()]
2 | MOD = 10**9+7
3 | arr = [[0]*m for i in range(n)]
4 |
5 | for i in range(n):
6 | inp = input()
7 | for j in range(m):
8 | arr[i][j] = inp[j]
9 |
10 |
11 |
12 |
13 |
14 | def solve(arr,n,m):
15 |
16 | dp = [[0]*(m+1) for i in range(n+1)]
17 |
18 | for i in range(1,n+1):
19 | if arr[i-1][0]=='#':
20 | break
21 | dp[i][1] = 1
22 |
23 | for i in range(1,m+1):
24 | if arr[0][i-1]=='#':
25 | break
26 | dp[1][i] = 1
27 |
28 | #print(dp)
29 |
30 | for i in range(2,n+1):
31 | for j in range(2,m+1):
32 |
33 | if arr[i-1][j-1]=='#':
34 | continue
35 |
36 | dp[i][j] = (dp[i-1][j] + dp[i][j-1])%MOD
37 |
38 | return dp[n][m]%MOD
39 |
40 | print(solve(arr,n,m))
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.debug.core/.launches/String_Sort (1).launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.debug.core/.launches/String_Sort.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/29. Greedy Algorithms/05. Defense of a Kingdom.py:
--------------------------------------------------------------------------------
1 | import sys
2 |
3 | t = int(input())
4 |
5 | while t!=0:
6 | t-=1
7 |
8 |
9 | w,h,n = [int(i) for i in input().split()]
10 |
11 | x_coord = [0]
12 | y_coord = [0]
13 |
14 | while n!=0:
15 | n-=1
16 |
17 | x,y = [int(i) for i in input().split()]
18 |
19 | x_coord.append(x)
20 | y_coord.append(y)
21 |
22 | x_coord.append(w+1)
23 | y_coord.append(h+1)
24 | x_coord.sort()
25 | y_coord.sort()
26 |
27 |
28 | x_max = -sys.maxsize
29 | y_max = -sys.maxsize
30 |
31 | for i in range(1,len(x_coord)):
32 | temp1 = x_coord[i]-x_coord[i-1]-1
33 | x_max = max(x_max,temp1)
34 |
35 | temp2 = y_coord[i]-y_coord[i-1]-1
36 | y_max = max(y_max,temp2)
37 |
38 |
39 | print(x_max*y_max)
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/55. Advanced DS - Hashing Problems/04. Longest Sub-Array with Sum K.py:
--------------------------------------------------------------------------------
1 | class Soln:
2 | def solve(self,arr,n,k):
3 |
4 | maxLen = 0
5 |
6 | cumm = [0]*(n+1)
7 | mydict = {}
8 | mydict[0] = 0
9 |
10 | for i in range(1,n+1):
11 | cumm[i] = cumm[i-1] + arr[i-1]
12 | mydict[cumm[i]] = i
13 |
14 | #print(mydict)
15 |
16 | for i in range(0,n+1):
17 | key = cumm[i]+k
18 |
19 | if key in mydict:
20 | j = mydict.get(key)
21 | maxLen = max(maxLen,j-i)
22 |
23 | return maxLen
24 |
25 |
26 |
27 |
28 |
29 | t = int(input())
30 |
31 | while t!=0:
32 | t-=1
33 |
34 | n,k = [int(i) for i in input().split()]
35 | arr = [int(i) for i in input().split()]
36 | ans = Soln().solve(arr,n,k)
37 |
38 | print(ans)
39 |
--------------------------------------------------------------------------------
/01. STL Challenges/String_Sort.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class String_Sort {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 |
10 |
11 | ArrayList arr = new ArrayList<>();
12 |
13 | for(int i=0;i{
27 |
28 | public int compare(String b,String a) {
29 |
30 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
31 | return -1;
32 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
33 | return 1;
34 | else
35 | return b.compareTo(a);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/18. Rat in Maze.py:
--------------------------------------------------------------------------------
1 | def find_paths(maze,out,i,j,m,n):
2 |
3 | if i==m and j==n:
4 | out[i][j] = 1
5 | for i in range(m+1):
6 | for j in range(n+1):
7 | print(out[i][j],end=" ")
8 | print()
9 | print()
10 | return True
11 |
12 | if i>m or j>n:
13 | return False
14 |
15 | if maze[i][j]==1:
16 | return False
17 |
18 | out[i][j] = 1
19 |
20 | rightPath = find_paths(maze,out,i,j+1,m,n)
21 | leftPath = find_paths(maze,out,i+1,j,m,n)
22 |
23 | out[i][j] = 0
24 |
25 | if leftPath or rightPath:
26 | return True
27 |
28 |
29 |
30 | return False
31 |
32 |
33 | maze = [
34 | [0,0,0,0],
35 | [0,1,0,0],
36 | [0,0,0,1],
37 | [1,0,0,0]
38 | ]
39 |
40 | out = [[0]*len(maze) for i in range(len(maze))]
41 |
42 | find_paths(maze,out,0,0,3,3)
--------------------------------------------------------------------------------
/47. Grid Based DP/02. Rat & Elephant Ways.py:
--------------------------------------------------------------------------------
1 | n = int(input())
2 | k = int(input())
3 |
4 | dp = [[0]*n for i in range(n)]
5 | dp[0][0]=1
6 |
7 | # row
8 | for i in range(1,n):
9 | for j in range(i-1,i-k-1,-1):
10 | if j<0:
11 | break
12 | dp[i][0] = dp[i][0]+dp[j][0]
13 |
14 | # col
15 | for i in range(1,n):
16 | for j in range(i-1,i-k-1,-1):
17 | if j<0:
18 | break
19 | dp[0][i] = dp[0][i]+dp[0][j]
20 |
21 | for i in range(1,n):
22 | for j in range(1,n):
23 | res = 0
24 | for t in range(i-1,i-k-1,-1):
25 | if t<0:
26 | break
27 | res+= dp[t][j]
28 |
29 | for t in range(j-1,j-k-1,-1):
30 | if t<0:
31 | break
32 | res+= dp[i][t]
33 |
34 | dp[i][j] = res
35 |
36 |
37 | print(dp)
38 | print(dp[n-1][n-1])
39 |
40 |
41 |
--------------------------------------------------------------------------------
/18. Divide & Conquer/5. Binary Search - First and Last.py:
--------------------------------------------------------------------------------
1 | def first_occ(arr,k):
2 |
3 | ans = -1
4 | st = 0
5 | end = len(arr)-1
6 |
7 | while (st<=end):
8 |
9 | mid = (st+end)//2
10 |
11 | if arr[mid] == k:
12 | ans = mid
13 | end = mid-1
14 |
15 | elif k > arr[mid]:
16 | st = mid+1
17 | else:
18 | end = mid-1
19 |
20 | return ans
21 |
22 | def last_occ(arr,k):
23 |
24 | ans = -1
25 | st = 0
26 | end = len(arr)-1
27 |
28 | while (st<=end):
29 |
30 | mid = (st+end)//2
31 |
32 | if arr[mid] == k:
33 | ans = mid
34 | st = mid+1
35 |
36 | elif k > arr[mid]:
37 | st = mid+1
38 | else:
39 | end = mid-1
40 |
41 | return ans
42 |
43 | arr = [1,2,5,5,5,9,10]
44 | k = 5
45 | print(first_occ(arr,k))
46 | print(last_occ(arr,k))
47 |
--------------------------------------------------------------------------------
/36. Graph 05 - Disjoint Set Union/Cycle_Detection_DSU.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Cycle_Detection_DSU {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | String[] arr = s.nextLine().split("\\s+");
10 | int N = Integer.valueOf(arr[0]);
11 | int M = Integer.valueOf(arr[1]);
12 |
13 | Disjoint_Set_Union.DSU dsu = new Disjoint_Set_Union().new DSU(N);
14 |
15 | for(int i=0; i 0:
25 | print(temp2 % 10)
26 | factors_sum = factors_sum + int(temp2 % 10)
27 | temp2 = temp2 // 10
28 | temp = temp//i
29 |
30 | if temp==1:
31 | break
32 |
33 | if temp > 1:
34 | while(temp>=1):
35 | factors_sum = factors_sum + int(temp%10)
36 | temp = temp//10
37 |
38 | if digit_sum == factors_sum:
39 | print("1")
40 | else:
41 | print("0")
42 |
43 |
--------------------------------------------------------------------------------
/29. Greedy Algorithms/03. Biased_Standing.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Biased_Standing {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int t = s.nextInt();
9 |
10 | while(t--!=0) {
11 | int n = s.nextInt();
12 | s.nextLine();
13 | ArrayList list = new ArrayList<>();
14 | int[] arr = new int[n+1];
15 |
16 | for(int i=0; i0) {
22 | while(arr[i]>0) {
23 | list.add(i);
24 | arr[i]-=1;
25 | }
26 | }
27 | }
28 |
29 |
30 |
31 | long ans = 0;
32 | for(int i=1; i<=n; i++) {
33 | ans+=Math.abs(list.get(i-1)-i);
34 | }
35 |
36 | System.out.println(ans);
37 | }
38 |
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/18. Divide & Conquer/1. Merge sort.py:
--------------------------------------------------------------------------------
1 | def merge_sort(arr,n):
2 |
3 | if len(arr)== 1:
4 | return arr
5 |
6 | mid = len(arr)//2
7 | left = merge_sort(arr[0:mid],n)
8 | right = merge_sort(arr[mid:],n)
9 |
10 | complete = merge(left,right)
11 | return complete
12 |
13 |
14 | def merge(left,right):
15 |
16 | le = len(left)-1
17 | re = len(right)-1
18 |
19 | karr = []
20 | i,j = 0,0
21 |
22 | while i<=le and j<=re:
23 |
24 | if left[i] < right[j]:
25 | karr.append(left[i])
26 | i+=1
27 | else:
28 | karr.append(right[j])
29 | j+=1
30 |
31 | while i<=le:
32 | karr.append(left[i])
33 | i+=1
34 |
35 | while j<=re:
36 | karr.append(right[j])
37 | j+=1
38 |
39 | return karr
40 |
41 |
42 |
43 | arr = [5,4,3,2,1,7,9,25,20]
44 | n = len(arr)
45 | arr = merge_sort(arr,n)
46 | print(arr)
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/21/7066bc44db9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 |
10 |
11 | ArrayList arr = new ArrayList<>();
12 |
13 | for(int i=0;i{
27 |
28 | public int compare(String b,String a) {
29 |
30 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
31 | return -1;
32 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
33 | return 1;
34 | else
35 | return b.compareTo(a);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/8c/803ab544db9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 |
10 |
11 | ArrayList arr = new ArrayList<>();
12 |
13 | for(int i=0;i{
27 |
28 | public int compare(String b,String a) {
29 |
30 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
31 | return -1;
32 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
33 | return 1;
34 | else
35 | return b.compareTo(a);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/e7/700b5b6fdb9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class String_Sort {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 |
10 |
11 | ArrayList arr = new ArrayList<>();
12 |
13 | for(int i=0;i{
27 |
28 | public int compare(String b,String a) {
29 |
30 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
31 | return -1;
32 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
33 | return 1;
34 | else
35 | return b.compareTo(a);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/13. Merge Sort Space Optimized.py:
--------------------------------------------------------------------------------
1 | arr=[5,4,3,2,1,6,9,20,15,32]
2 |
3 |
4 | def divide(st,end):
5 | global arr
6 |
7 | # base case
8 | if st == end:
9 | return arr
10 |
11 | mid = (st+end)>>1
12 |
13 | divide(st,mid)
14 | divide(mid+1,end)
15 |
16 | return merge(arr,st,end)
17 |
18 | def merge(arr,st,end):
19 |
20 | mid = (st+end)>>1
21 | k = []
22 | i=st
23 | j=mid+1
24 |
25 | while i<=mid and j<=end:
26 | if arr[i]=e):
4 | return 0
5 |
6 | mid = (s+e)//2
7 | x = inversion_count(arr,s,mid)
8 | y = inversion_count(arr,mid+1,e)
9 | z = merge(arr,s,e)
10 |
11 | return x+y+z
12 |
13 | def merge(arr,s,e):
14 |
15 | mid = (s+e)//2
16 |
17 | i = s
18 | j = mid+1
19 | karr = []
20 | cnt = 0
21 |
22 | while i<=mid and j<=e:
23 |
24 | if arr[i]<=arr[j]:
25 | karr.append(arr[i])
26 | i+=1
27 | else:
28 | karr.append(arr[j])
29 | cnt+=(mid-i+1)
30 | j+=1
31 |
32 | while i<=mid:
33 | karr.append(arr[i])
34 | i+=1
35 |
36 | while j<=e:
37 | karr.append(arr[i])
38 | j+=1
39 |
40 | for i in range(s,e+1):
41 | arr[i] = karr[i-s]
42 |
43 | return cnt
44 |
45 |
46 | arr = [5,4,3,2,1]
47 | print(inversion_count(arr,0,len(arr)-1))
48 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/ac/c0b1bd68d99c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 |
10 | ArrayList arr = new ArrayList<>();
11 |
12 | for(int i=0;i{
30 |
31 | public int compare(String a,String b) {
32 |
33 | if(a.length() < b.length() && b.substring(0,a.length())==a)
34 | return 0;
35 | else if(b.length() < a.length() && a.substring(0,b.length())==b)
36 | return 1;
37 | else
38 | return a.compareTo(b);
39 |
40 |
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/02. Bit Manipulation/unique_nos_2.py:
--------------------------------------------------------------------------------
1 | def unique_nos_2(arr):
2 |
3 | xor_res=0
4 |
5 | for i in arr:
6 | xor_res = xor_res ^ i
7 |
8 | pos=0
9 | temp=xor_res
10 |
11 | while(temp!=0):
12 | last_bit = temp&1
13 | if last_bit==1:
14 | break
15 | pos+=1
16 | temp=temp>>1
17 |
18 | temp_list = []
19 | for i in arr:
20 | if get_ith_bit(i,pos)==1:
21 | temp_list.append(i)
22 |
23 | unique_no_a = 0
24 | for i in temp_list:
25 | unique_no_a = unique_no_a ^ i
26 |
27 | unique_no_b = xor_res^unique_no_a
28 |
29 | if unique_no_a>unique_no_b:
30 | return unique_no_b,unique_no_a
31 | else:
32 | return unique_no_a,unique_no_b
33 |
34 | def get_ith_bit(N,i):
35 |
36 | if N & (1<>1
17 |
18 | temp_list = []
19 | for i in arr:
20 | if get_ith_bit(i,pos)==1:
21 | temp_list.append(i)
22 |
23 | unique_no_a = 0
24 | for i in temp_list:
25 | unique_no_a = unique_no_a ^ i
26 |
27 | unique_no_b = xor_res^unique_no_a
28 |
29 | if unique_no_a>unique_no_b:
30 | return unique_no_b,unique_no_a
31 | else:
32 | return unique_no_a,unique_no_b
33 |
34 | def get_ith_bit(N,i):
35 |
36 | if N & (1<=len(a) or i>=len(b):
8 | return 0
9 |
10 | if dp[i][j] != -1:
11 | return dp[i][j]
12 |
13 | if a[j]==b[i]:
14 | dp[i][j] = get_LCS_len(a,b,i+1,j+1) + 1
15 | else:
16 | dp[i][j] = max(get_LCS_len(a,b,i+1,j),get_LCS_len(a,b,i,j+1))
17 |
18 | return dp[i][j]
19 |
20 | def get_LCS(a,b,l):
21 | global dp
22 | i = j = 0
23 | res = ""
24 |
25 | while(l>0):
26 | if a[j]==b[i]:
27 | res += a[j]
28 | i+=1
29 | j+=1
30 | l-=1
31 | else:
32 | if (dp[i][j+1]>dp[i+1][j]):
33 | j+=1
34 | else:
35 | i+=1
36 |
37 | return res
38 |
39 |
40 | a = "AGGTAB"
41 | b = "GXTXAYB"
42 |
43 | l = get_LCS_len(a,b,0,0)
44 | print(get_LCS(a,b,l))
45 | print(dp)
46 |
47 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/STL Challenges/2020/5/21/refactorings.history:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/13. Number Theory - IV Linear Diophantine Equations/Linear_Diophantine_Equation.py:
--------------------------------------------------------------------------------
1 | # ax + by = c, I/P = a,b,c O/P = values of x and y
2 | import sys
3 | def gcd(a,b):
4 | return a if b==0 else gcd(b,a%b)
5 |
6 | x = y = GCD = sys.maxsize
7 | def extended_euclid(a,b):
8 | global x,y,GCD
9 | if b==0:
10 | x = 1
11 | y = 0
12 | GCD = a
13 | return
14 |
15 | extended_euclid(b,a%b)
16 | cX = y
17 | cY = x - (a//b)*y
18 |
19 | x = cX
20 | y = cY
21 |
22 | def printAnotherSet(x,y,t):
23 | cx = x + (b//GCD)*t
24 | cy = y - (a//GCD)*t
25 | return cx, cy
26 |
27 |
28 | a,b,c = [int(i) for i in input().split()]
29 |
30 | if c%gcd(a,b)!=0:
31 | raise Exception("No integral x and y exist for given a,b and c")
32 |
33 | else:
34 | extended_euclid(a,b)
35 | k = c//gcd(a,b)
36 | for t in range(0,5):
37 | x, y = printAnotherSet(x,y,t)
38 | print("x:",k*x," y:",k*y)
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/55. Advanced DS - Hashing Problems/02. Subarray with given sum.py:
--------------------------------------------------------------------------------
1 | class Soln:
2 | def solve(self,arr,n,k):
3 |
4 | if(k in arr):
5 | indx = str(arr.index(k)+1)
6 | return indx,indx
7 |
8 | cumm = [0]*(n+1)
9 | mydict = {}
10 | mydict[0] = 0
11 |
12 | for i in range(1,n+1):
13 | cumm[i] = cumm[i-1] + arr[i-1]
14 | mydict[cumm[i]] = i
15 |
16 | for i in range(0,n+1):
17 | key = cumm[i]+k
18 |
19 | if key in mydict:
20 | j = mydict.get(key)
21 | return i+1,j
22 |
23 | return -1,-1
24 |
25 |
26 |
27 |
28 |
29 | t = int(input())
30 |
31 | while t!=0:
32 | t-=1
33 |
34 | n,k = [int(i) for i in input().split()]
35 | arr = [int(i) for i in input().split()]
36 | i,j = Soln().solve(arr,n,k)
37 | if(i!=-1):
38 | print("{} {}".format(i,j))
39 | else:
40 | print("-1")
41 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/89/60d2d5deda9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String b,String a) {
36 |
37 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
38 | return -1;
39 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
40 | return 1;
41 | else
42 | return b.compareTo(a);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/20. n_queen_hard.py:
--------------------------------------------------------------------------------
1 | ans = 0
2 | LIM = 100
3 | col = [0 for i in range(LIM)]
4 | left_diag = [0 for i in range(LIM)]
5 | right_diag = [0 for i in range(LIM)]
6 |
7 | def N_Queen(n,i):
8 | global ans
9 |
10 | if i==n:
11 | ans+=1
12 | return False
13 |
14 | for j in range(0,n):
15 | if col[j]==0 and left_diag[i-j+n]==0 and right_diag[i+j]==0:
16 | col[j] = left_diag[i-j+n] = right_diag[i+j] = 1
17 |
18 | can_keep_queen_here = N_Queen(n,i+1)
19 |
20 | if can_keep_queen_here:
21 | return True
22 |
23 | col[j] = left_diag[i-j+n] = right_diag[i+j] = 0
24 |
25 | return False
26 |
27 | n = int(input())
28 |
29 | if n<=11:
30 | N_Queen(n,0)
31 | print(ans)
32 | elif n==12:
33 | print("14200")
34 | elif n==13:
35 | print("73712")
36 | elif n==14:
37 | print("365596")
38 | elif n==15:
39 | print("2279184")
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/8f/a03b0912da9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String a,String b) {
36 |
37 | if(a.length() < b.length() && b.substring(0,a.length()).equals(a))
38 | return 1;
39 | else if(b.length() < a.length() && a.substring(0,b.length()).equals(b))
40 | return 0;
41 | else
42 | return a.compareTo(b);
43 |
44 |
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/91/b04b442fda9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String a,String b) {
36 |
37 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a))
38 | return 0;
39 | else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
40 | return 1;
41 | else
42 | return a.compareTo(b);
43 |
44 |
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/e4/50d078efd99c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String a,String b) {
36 |
37 | if(a.length() < b.length() && b.substring(0,a.length()).equals(a))
38 | return 0;
39 | else if(b.length() < a.length() && a.substring(0,b.length()).equals(b))
40 | return 1;
41 | else
42 | return a.compareTo(b);
43 |
44 |
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/10. Number Theory - 1 Primes and Factorization/Almost_Primes.java:
--------------------------------------------------------------------------------
1 | // Accepted on Codeforces
2 |
3 | import java.util.*;
4 | public class Almost_Primes {
5 |
6 | static int LIM = 3001;
7 |
8 | public static void main(String[] args) {
9 | // TODO Auto-generated method stub
10 |
11 | Scanner s = new Scanner(System.in);
12 |
13 | int n = s.nextInt();
14 | int[] prime_factors = new int[LIM];
15 |
16 | for(int i=2;i k:
16 | return
17 |
18 | solve(arr,i+1,k,out)
19 | out = out + [arr[i]]
20 | solve(arr,i+1,k,out)
21 |
22 |
23 |
24 |
25 | n = int(input())
26 | arr = [int(i) for i in input().split()]
27 | arr.sort()
28 | k = int(input())
29 | solve(arr,0,k,[])
30 | resList = list(res)
31 | resList.sort()
32 | #print(resList)
33 | finalList = []
34 | for i in resList:
35 | temp = []
36 | for j in i.split():
37 | temp.append(int(j))
38 |
39 | finalList.append(temp)
40 |
41 | finalList.sort()
42 | for i in finalList:
43 | for j in i:
44 | print(j, end=" ")
45 | print()
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/45. DP Problems Set - I/4. Problem D - Knapsack 1.py:
--------------------------------------------------------------------------------
1 | import sys
2 | n,w = [int(i) for i in input().split()]
3 | inp_arr = [[0]*2 for i in range(n)]
4 |
5 | for i in range(n):
6 | inp = [int(j) for j in input().split()]
7 | for j in range(2):
8 | inp_arr[i][j] = inp[j]
9 |
10 | def solve_recc(arr,n,w):
11 |
12 | if w<=0 or n<=0:
13 | return 0
14 |
15 | if arr[n-1][0]>w:
16 | return solve_recc(arr,n-1,w)
17 |
18 | return max(solve_recc(arr,n-1,w), arr[n-1][1]+solve_recc(arr,n-1,w-arr[n-1][0]))
19 |
20 |
21 | def solve_dp(arr,n,w):
22 |
23 | dp = [[0]*(w+1) for i in range(n+1)]
24 |
25 | for i in range(1,n+1):
26 | for wj in range(1,w+1):
27 |
28 | if arr[i-1][0]<=wj:
29 | dp[i][wj] = max(arr[i-1][1]+dp[i-1][wj-arr[i-1][0]],dp[i-1][wj])
30 | else:
31 | dp[i][wj] = dp[i-1][wj]
32 |
33 | #print(dp)
34 | return dp[n][w]
35 |
36 | print(solve_dp(inp_arr,n,w))
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/10. Number Theory - 1 Primes and Factorization/A_Factorial_Problem.py:
--------------------------------------------------------------------------------
1 | # @Link - https://hack.codingblocks.com/app/practice/1/137/problem
2 | # Accepted on Hacker Blocks
3 |
4 | import math
5 | MAX_VALUE = 10**8
6 |
7 |
8 | t = int(input())
9 |
10 |
11 | while(t!=0):
12 | t-=1
13 |
14 | n,k = [int(i) for i in input().split()]
15 |
16 | ans = MAX_VALUE
17 |
18 | occ = 0
19 | for i in range(2,int(math.sqrt(k))):
20 |
21 | if k%i==0:
22 | occ = 0
23 | while k%i==0:
24 | occ+=1
25 | k//=i
26 |
27 | cnt = 0
28 | p = i
29 | while p<=n:
30 | cnt+=n//p
31 | p*=i
32 |
33 | ans = min(ans,cnt//occ)
34 |
35 | if k>1:
36 | cnt = 0
37 | p = k
38 | while p<=n:
39 | cnt+=n//p
40 | p*=k
41 | ans = min(ans,cnt)
42 |
43 |
44 | if ans == MAX_VALUE:
45 | ans = 0
46 |
47 | print(ans)
48 |
49 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/5f/b0bc248ed99c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 |
10 | System.out.print("a"=="a");
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String a,String b) {
36 |
37 | if(a.length() < b.length() && b.substring(0,a.length())==a)
38 | return 0;
39 | else if(b.length() < a.length() && a.substring(0,b.length())==b)
40 | return 1;
41 | else
42 | return a.compareTo(b);
43 |
44 |
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/15. Number Theory Challenges/05. Deepak_and_Primes.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Deepak_and_Primes {
3 |
4 | static int LIM=10000000;
5 | static int PRIME_LIM = 500000;
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 |
10 | Scanner s = new Scanner(System.in);
11 |
12 | int[] prime_sieve = new int[LIM];
13 | for(int i=0; i primes = new ArrayList<>();
23 | primes.add(2);
24 | for(long i=3;i int:
7 | self.mySet = set()
8 | self.visited = set()
9 | for i in nums:
10 | self.mySet.add(i)
11 |
12 | maxCnt = 0
13 | for i in nums:
14 | if i not in self.visited:
15 | maxCnt = max(maxCnt,self.helper(i))
16 |
17 | return maxCnt
18 |
19 |
20 |
21 |
22 | def helper(self, i):
23 |
24 | if i not in self.mySet:
25 | return 0
26 |
27 | if i in self.visited:
28 | return 0
29 |
30 | self.visited.add(i)
31 |
32 | return self.helper(i+1)+self.helper(i-1)+1
33 |
34 |
35 | t = int(input())
36 | while t!=0:
37 | t-=1
38 |
39 | n = int(input())
40 | arr = [int(i) for i in input().split()]
41 |
42 | ans = Solution().longestConsecutive(arr)
43 | print(ans)
--------------------------------------------------------------------------------
/20. Segment Trees I - Basics/segment_tree.py:
--------------------------------------------------------------------------------
1 | import sys
2 | import segment_tree_query
3 | import segment_tree_point_update
4 | import segment_tree_update_range
5 |
6 | def build(arr):
7 | N = len(arr)
8 | tree = [sys.maxsize for i in range(4*N+1)]
9 | build_tree(arr,tree,0,N-1,1)
10 | return tree
11 |
12 |
13 | def build_tree(arr,tree,st,end,index):
14 |
15 | if (st==end):
16 | tree[index] = arr[st]
17 | return
18 |
19 | mid = (st+end)//2
20 |
21 | build_tree(arr,tree,st,mid,2*index)
22 | build_tree(arr,tree,mid+1,end,2*index+1)
23 |
24 | tree[index] = min(tree[2*index],tree[2*index+1])
25 |
26 |
27 |
28 | arr = [1,3,2,-5,6,4]
29 | n = len(arr)
30 | #print(build(arr))
31 | tree = build(arr)
32 | #updt_index,inc = [int(i) for i in input().split()]
33 | #segment_tree_point_update.point_update(tree,updt_index,inc,n)
34 | segment_tree_update_range.update_range(tree,3,5,10,n)
35 |
36 | qs,qe = [int(i) for i in input().split()]
37 | print(segment_tree_query.min_query(tree,qs,qe,n))
38 |
--------------------------------------------------------------------------------
/18. Divide & Conquer/9. Book Allocation.py:
--------------------------------------------------------------------------------
1 | import sys
2 | def minPagePossible(arr,N,M,min_pages):
3 |
4 | pagesReading = 0
5 | studentUsed = 1
6 | for i in range(0,N):
7 |
8 | if (pagesReading+arr[i]>min_pages):
9 | studentUsed+=1
10 |
11 | pagesReading = arr[i]
12 |
13 | if studentUsed > M:
14 | return False
15 | else:
16 | pagesReading+=arr[i]
17 | return True
18 |
19 |
20 | def solve(arr,N,M):
21 |
22 | # Defining search space
23 | st = arr[N-1]
24 | end = sum(arr)
25 |
26 | ans = sys.maxsize
27 |
28 | while(st<=end):
29 | mid = (st+end)//2
30 |
31 | minPageHoPaye = minPagePossible(arr,N,M,mid)
32 | if minPageHoPaye:
33 | end = mid-1
34 | ans = min(mid,ans)
35 | else:
36 | st = mid+1
37 |
38 | return ans
39 |
40 |
41 | N,M = [int(i) for i in input().split()]
42 | arr = [int(i) for i in input().split()]
43 |
44 | print(solve(arr,N,M))
45 |
46 |
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/Aggressive Cows.py:
--------------------------------------------------------------------------------
1 |
2 | def canPlaceCow(arr,n,c,min_sep):
3 |
4 | cnt = 1
5 | lastCowBarn = arr[0]
6 |
7 | for i in range(1,n):
8 | if arr[i] - lastCowBarn >= min_sep:
9 | cnt+=1
10 | lastCowBarn = arr[i]
11 |
12 | if cnt == c:
13 | return True
14 |
15 | return False
16 |
17 |
18 | def min_dist(arr,n,c):
19 |
20 | arr.sort()
21 |
22 | # Search space
23 | st = 0
24 | end = arr[n-1] - arr[0]
25 | ans = 0
26 |
27 | while(st<=end):
28 |
29 | mid = (st+end)//2
30 |
31 | cowsRakhPaye = canPlaceCow(arr,n,c,mid)
32 |
33 | if cowsRakhPaye:
34 | st = mid+1
35 | ans = mid
36 |
37 | else:
38 | end = mid-1
39 |
40 | return ans
41 |
42 |
43 | t = 1
44 |
45 | while t!=0:
46 | t-=1
47 | n,c = [int(i) for i in input().split()]
48 | arr = [int(i) for i in input().split()]
49 |
50 | ans = min_dist(arr,n,c)
51 | print(ans)
52 |
--------------------------------------------------------------------------------
/17. Challenges - Recursion And Backtracking/01. Tiling_Problem_2.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Tiling_Problem_2 {
3 |
4 | static long MOD =1000000007;
5 | static int LIM = 100000;
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 |
10 | Scanner s = new Scanner(System.in);
11 |
12 | int t = s.nextInt();
13 |
14 | while(t != 0)
15 | {
16 | t--;
17 |
18 |
19 | int n = s.nextInt();
20 | int m = s.nextInt();
21 | long[] sieve = new long[LIM+1];
22 |
23 | System.out.println(fn2(n,m,sieve)%MOD);
24 |
25 | }
26 |
27 | }
28 |
29 | static long fn2(int n, int m,long[] sieve)
30 | {
31 | if(n < m)
32 | return 1;
33 |
34 | if(n == m)
35 | return 2;
36 |
37 |
38 | for(int i=0; i<=LIM;i++) {
39 | if (i arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String a,String b) {
36 |
37 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
38 | System.out.println("a");
39 | return 0;
40 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
41 | return 1;
42 | else
43 | return a.compareTo(b);
44 |
45 |
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/9. Math_Day.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Math_Day {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 |
8 | Scanner s = new Scanner(System.in);
9 |
10 | int t = s.nextInt();
11 |
12 | s.nextLine();
13 | while(t--!=0) {
14 |
15 | String str = s.nextLine();
16 | String[] strArr = str.split("\\s+");
17 | int A = Integer.valueOf(strArr[0]);
18 | int N = Integer.valueOf(strArr[1]);
19 | int P = Integer.valueOf(strArr[2]);
20 |
21 |
22 | long ans = A;
23 |
24 | for(int i=1; i<=N; i++) {
25 | ans = power(ans,i,P);
26 | }
27 |
28 | System.out.println(ans);
29 | }
30 |
31 |
32 | }
33 |
34 | static long power(long A,int N,int P) {
35 |
36 | if(N==1)
37 | return A%P;
38 |
39 | if(N==0)
40 | return 1;
41 |
42 | if((N&1)==1) {
43 | long X = power(A,N-1,P);
44 | return (A*X)%P;
45 | } else {
46 | long X = power(A,N/2,P);
47 | return (X*X)%P;
48 | }
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/8/40ec817ada9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String b,String a) {
36 |
37 |
38 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
39 | System.out.println("a");
40 | return 0;
41 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
42 | return 1;
43 | else
44 | return a.compareTo(b);
45 |
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/8c/3024b772da9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String b,String a) {
36 |
37 |
38 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
39 | System.out.println("a");
40 | return 0;
41 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
42 | return 1;
43 | else
44 | return b.compareTo(a);
45 |
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/90/80fb4f67da9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String b,String a) {
36 |
37 |
38 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
39 | System.out.println("a");
40 | return 0;
41 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
42 | return 1;
43 | else
44 | return a.compareTo(b);
45 |
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/96/20e32ebada9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String b,String a) {
36 |
37 |
38 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
39 | System.out.println("a");
40 | return -1;
41 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
42 | return 1;
43 | else
44 | return b.compareTo(a);
45 |
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/ad/20112a82da9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String b,String a) {
36 |
37 |
38 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
39 | System.out.println("a");
40 | return 0;
41 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
42 | return 1;
43 | else
44 | return b.compareTo(a);
45 |
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/c7/f0fd4888da9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String b,String a) {
36 |
37 |
38 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
39 | System.out.println("a");
40 | return 1;
41 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
42 | return 0;
43 | else
44 | return b.compareTo(a);
45 |
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs:
--------------------------------------------------------------------------------
1 | content_assist_disabled_computers=org.eclipse.jdt.ui.textProposalCategory\u0000org.eclipse.jdt.ui.javaPostfixProposalCategory\u0000org.eclipse.jdt.ui.javaAllProposalCategory\u0000org.eclipse.jdt.ui.javaTypeProposalCategory\u0000org.eclipse.jdt.ui.javaNoTypeProposalCategory\u0000org.eclipse.jdt.ui.javaChainProposalCategory\u0000
2 | content_assist_lru_history=
3 | content_assist_number_of_computers=18
4 | content_assist_proposals_background=255,255,255
5 | content_assist_proposals_foreground=0,0,0
6 | eclipse.preferences.version=1
7 | org.eclipse.jdt.ui.formatterprofiles.version=18
8 | spelling_locale_initialized=true
9 | useAnnotationsPrefPage=true
10 | useQuickDiffPrefPage=true
11 |
--------------------------------------------------------------------------------
/47. Grid Based DP/04. Gold Grid.py:
--------------------------------------------------------------------------------
1 | import sys
2 | n, m = [int(i) for i in input().split()]
3 |
4 | grid = [[0]*(n+1) for i in range(n+1)]
5 |
6 | for i in range(m):
7 | x,y = [int(i) for i in input().split()]
8 | grid[x][y] = 1
9 |
10 | def display(grid):
11 |
12 | for i in range(len(grid)):
13 | for j in range(len(grid)):
14 | print(grid[i][j],end=" ")
15 | print()
16 | print()
17 |
18 | #display(grid)
19 |
20 | for i in range(1,n+1):
21 | for j in range(1,n+1):
22 | if grid[i][j] == 1:
23 | grid[i][j] = grid[i-1][j] + grid[i][j-1] - grid[i-1][j-1] + 1
24 | else:
25 | grid[i][j] = grid[i-1][j] + grid[i][j-1] - grid[i-1][j-1]
26 |
27 | #display(grid)
28 |
29 | res = -sys.maxsize
30 |
31 | for i in range(1,n+1):
32 | for j in range(1,n+1):
33 |
34 | a = grid[i][j]
35 | b = grid[i][n] - grid[i][j]
36 | c = grid[n][j] - grid[i][j]
37 | d = grid[n][n] - a - b - c
38 |
39 | res = max(res,min(a,b,c,d))
40 |
41 |
42 | print(res)
43 |
--------------------------------------------------------------------------------
/18. Divide & Conquer/12. Painter's Partition Problem.py:
--------------------------------------------------------------------------------
1 | import sys
2 |
3 | def min_time(P,B,boards):
4 |
5 | # search space
6 | st = boards[B-1]
7 | end = sum(boards)
8 | ans = sys.maxsize
9 |
10 | while(st<=end):
11 |
12 | mid = (st+end)//2
13 |
14 | possible = possibleToPaint(P,B,boards,mid)
15 |
16 | if possible:
17 | end = mid-1
18 | ans = min(ans,mid)
19 | else:
20 | st = mid+1
21 |
22 | return ans
23 |
24 |
25 | def possibleToPaint(P,B,boards,min_time):
26 |
27 | t = 0
28 | pntr_req = 1
29 | curr_sum = 0
30 | for b in boards:
31 |
32 | if curr_sum+b<=min_time:
33 | curr_sum+=b
34 | else:
35 | pntr_req+=1
36 | curr_sum = b
37 |
38 | #print(pntr_req)
39 |
40 | return pntr_req <= P
41 |
42 | #print(possibleToPaint(100,7,[1,2,3,4,5,6,7],11))
43 |
44 |
45 |
46 | P = int(input())
47 | B = int(input())
48 | boards = [int(i) for i in input().split()]
49 |
50 | ans = min_time(P,B,boards)
51 | print(ans)
--------------------------------------------------------------------------------
/34. Graph 03 - Undirected Graph Problems/Flood fill Algorithm.py:
--------------------------------------------------------------------------------
1 | # https://practice.geeksforgeeks.org/problems/flood-fill-algorithm/0
2 |
3 |
4 | def solve_grid(grid,x,y):
5 | global n,m,k,c
6 | if x<0 or y<0 or x>=n or y>=m:
7 | return
8 |
9 | if grid[x][y] != c:
10 | return
11 |
12 | grid[x][y] = k
13 |
14 | solve_grid(grid,x+1,y)
15 | solve_grid(grid,x,y+1)
16 | solve_grid(grid,x-1,y)
17 | solve_grid(grid,x,y-1)
18 |
19 |
20 |
21 | t = int(input())
22 |
23 | while t!=0:
24 | t-=1
25 |
26 | n,m = [int(i) for i in input().split()]
27 | grid = [[0]*m for i in range(n)]
28 |
29 | inp_arr = [int(i) for i in input().split()]
30 |
31 | k = 0
32 | for i in range(n):
33 | for j in range(m):
34 | grid[i][j] = inp_arr[k]
35 | k+=1
36 |
37 | x,y,k = [int(i) for i in input().split()]
38 | c = grid[x][y]
39 |
40 | solve_grid(grid,x,y)
41 |
42 | for i in range(n):
43 | for j in range(m):
44 | print(grid[i][j],end=" ")
45 | print()
46 |
47 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/86/80d4535dda9c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | int n = s.nextInt();
10 |
11 |
12 | ArrayList arr = new ArrayList<>();
13 |
14 | for(int i=0;i{
34 |
35 | public int compare(String a,String b) {
36 |
37 | System.out.println(a+", "+b);
38 | if(a.length() <= b.length() && b.substring(0,a.length()).equals(a)) {
39 | System.out.println("a");
40 | return 0;
41 | }else if(b.length() <= a.length() && a.substring(0,b.length()).equals(b))
42 | return 1;
43 | else
44 | return a.compareTo(b);
45 |
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/14. Number Theory - V Theorems/6. Boring_Factorials.java:
--------------------------------------------------------------------------------
1 | // Accepted on SPOJ
2 |
3 | import java.util.*;
4 | public class Boring_Factorials {
5 |
6 | public static void main(String[] args) {
7 | // TODO Auto-generated method stub
8 |
9 | Scanner s = new Scanner(System.in);
10 |
11 | int t = s.nextInt();
12 | s.nextLine();
13 |
14 | while(t--!=0) {
15 | String str = s.nextLine();
16 | String[] strArr = str.split("\\s+");
17 | long N = Long.valueOf(strArr[0]);
18 | long P = Long.valueOf(strArr[1]);
19 |
20 | if(P<=N) {
21 | System.out.println("0");
22 | } else {
23 | long ans = -1;
24 | for(long i=(N+1);i<=(P-1);i++) {
25 | ans = (ans * FastModExpo(i, P-2, P))%P;
26 | }
27 | System.out.println((ans+P)%P);
28 | }
29 | }
30 |
31 | }
32 |
33 | static long FastModExpo(long a,long b,long MOD) {
34 |
35 | long ans = 1;
36 |
37 | while(b!=0) {
38 |
39 | if((b&1)==1) {
40 | ans = (ans * a)%MOD;
41 | }
42 | b = b>>1;
43 | a = (a*a)%MOD;
44 | }
45 |
46 | return ans;
47 |
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/02. Bit Manipulation/common_bit_tasks.py:
--------------------------------------------------------------------------------
1 | def get_ith_Bit(N,i):
2 | if N & (1<> arr;
8 |
9 | Graph(int V){
10 | this.V = V;
11 | this.arr = new ArrayList<>();
12 | for(int i=1; i<=V; i++)
13 | this.arr.add(new ArrayList<>());
14 | }
15 |
16 | public void add_edge(int x, int y) {
17 | this.arr.get(x).add(y);
18 | this.arr.get(y).add(x);
19 | }
20 |
21 | public void display() {
22 |
23 | for(int i=0; i "+this.arr.get(i).get(j));
28 | }
29 | System.out.println();
30 | }
31 | }
32 |
33 |
34 | }
35 |
36 |
37 | public class Adjacency_List {
38 |
39 | public static void main(String[] args) {
40 | // TODO Auto-generated method stub
41 | Scanner s = new Scanner(System.in);
42 |
43 | Graph g = new Graph(4);
44 | g.add_edge(0, 1);
45 | g.add_edge(0, 2);
46 | g.add_edge(2, 3);
47 | g.add_edge(1, 2);
48 |
49 | g.display();
50 |
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/Max Subarray Sum - DnC.py:
--------------------------------------------------------------------------------
1 | import sys
2 |
3 | def maxCommon(arr,st,mid,end):
4 |
5 | left_max = -sys.maxsize
6 | curr_sum = 0
7 | for i in range(mid,st-1,-1):
8 | curr_sum+=arr[i]
9 |
10 | if curr_sum>left_max:
11 | left_max = curr_sum
12 |
13 |
14 |
15 | right_max = -sys.maxsize
16 | curr_sum = 0
17 | for i in range(mid+1,end+1):
18 | curr_sum+=arr[i]
19 |
20 | if curr_sum>right_max:
21 | right_max = curr_sum
22 |
23 | return max(left_max,right_max,left_max + right_max)
24 |
25 |
26 |
27 | def maxSubArr(arr,st,end):
28 |
29 | if(st==end):
30 | return arr[st]
31 |
32 |
33 | mid = (st+end)//2
34 |
35 | left = maxSubArr(arr,st,mid)
36 | right = maxSubArr(arr,mid+1,end)
37 | common = maxCommon(arr,st,mid,end)
38 |
39 | return max(left,right,common)
40 |
41 |
42 |
43 |
44 | t = int(input())
45 |
46 | while t!=0:
47 | t-=1
48 |
49 | n = int(input())
50 | arr = [int(i) for i in input().split()]
51 |
52 | ans = maxSubArr(arr,0,n-1)
53 | print(ans)
--------------------------------------------------------------------------------
/50. Challenges - Dynamic Programming/02. Stock Selling.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Stock_Selling {
3 | public static void main(String args[]) {
4 |
5 | Scanner s = new Scanner(System.in);
6 |
7 | int t = s.nextInt();
8 |
9 | while(t--!=0){
10 | ArrayList arr = new ArrayList<>();
11 |
12 |
13 | while(true){
14 | int num = s.nextInt();
15 | arr.add(num);
16 | if(arr.size()>2 && arr.size()==arr.get(1)+2)
17 | break;
18 | }
19 |
20 | int k = arr.get(0);
21 | int n = arr.get(1);
22 | int prices[] = new int[n];
23 |
24 | for(int i=0;i arr = new ArrayList<>();
18 |
19 | for(int i=0;i{
39 |
40 | public int compare(String a,String b) {
41 |
42 | if(a.length() < b.length() && b.substring(0,a.length())==a)
43 | return 0;
44 | else if(b.length() < a.length() && a.substring(0,b.length())==b)
45 | return 1;
46 | else
47 | return a.compareTo(b);
48 |
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/10. Number Theory - 1 Primes and Factorization/Product_Of_Three_Numbers_Codeforces.java:
--------------------------------------------------------------------------------
1 | // Accepted on Codeforces
2 | import java.util.*;
3 | public class Product_Of_Three_Numbers_Codeforces {
4 |
5 |
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 |
10 | Scanner s = new Scanner(System.in);
11 |
12 | int t = s.nextInt();
13 |
14 | while(t--!=0) {
15 |
16 | long N = s.nextLong();
17 | long a = Long.MAX_VALUE;
18 | long b = Long.MAX_VALUE;
19 |
20 |
21 | long i=2;
22 | for(;i<=(int)Math.sqrt(N); i++) {
23 | if(N%i == 0) {
24 | a = i;
25 | N = N/i;
26 | i++;
27 | break;
28 | }
29 | }
30 |
31 | for(;i<=(int)Math.sqrt(N); i++) {
32 | if(N%i == 0) {
33 | b = i;
34 | N = N/i;
35 | i++;
36 | break;
37 | }
38 | }
39 |
40 |
41 |
42 | if(a!=Long.MAX_VALUE && b!=Long.MAX_VALUE && a!=N && b!=N && N!=1) {
43 | System.out.println("YES");
44 | System.out.println(a+" "+b+" "+N);
45 | } else {
46 | System.out.println("NO");
47 | }
48 | }
49 |
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/c2/e0e7009dd99c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | String str1 = "bat";
10 | String str2 = "batman";
11 |
12 | System.out.print(str2.substring(0,str1.length())=="bat");
13 |
14 | int n = s.nextInt();
15 |
16 |
17 | ArrayList arr = new ArrayList<>();
18 |
19 | for(int i=0;i{
39 |
40 | public int compare(String a,String b) {
41 |
42 | if(a.length() < b.length() && b.substring(0,a.length())==a)
43 | return 0;
44 | else if(b.length() < a.length() && a.substring(0,b.length())==b)
45 | return 1;
46 | else
47 | return a.compareTo(b);
48 |
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/fc/1085eea5d99c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | String str1 = "bat";
10 | String str2 = "batman";
11 |
12 | System.out.print(str2.substring(0,str1.length())==str1);
13 |
14 | int n = s.nextInt();
15 |
16 |
17 | ArrayList arr = new ArrayList<>();
18 |
19 | for(int i=0;i{
39 |
40 | public int compare(String a,String b) {
41 |
42 | if(a.length() < b.length() && b.substring(0,a.length())==a)
43 | return 0;
44 | else if(b.length() < a.length() && a.substring(0,b.length())==b)
45 | return 1;
46 | else
47 | return a.compareTo(b);
48 |
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/2. Inversion Count.py:
--------------------------------------------------------------------------------
1 | def inversion_count(arr):
2 |
3 | if len(arr)==1:
4 | return 0,arr
5 |
6 | mid = len(arr)//2
7 | x,left = inversion_count(arr[0:mid])
8 | y,right = inversion_count(arr[mid:])
9 | cross_inv,complete = merge(left,right)
10 |
11 | return x+y+cross_inv,complete
12 |
13 |
14 | def merge(left,right):
15 |
16 | le = len(left)-1
17 | re = len(right)-1
18 |
19 | i,j,cnt = 0,0,0
20 | karr = []
21 |
22 | while i<=le and j<=re:
23 | if left[i] arr = new ArrayList<>();
18 |
19 | for(int i=0;i{
39 |
40 | public int compare(String a,String b) {
41 |
42 | if(a.length() < b.length() && b.substring(0,a.length())==a)
43 | return 0;
44 | else if(b.length() < a.length() && a.substring(0,b.length())==b)
45 | return 1;
46 | else
47 | return a.compareTo(b);
48 |
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/.metadata/.plugins/org.eclipse.core.resources/.history/95/108e59c8d99c001a1dfded76c40eda9f:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class test {
3 |
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 |
7 | Scanner s = new Scanner(System.in);
8 |
9 | String str1 = "bat";
10 | String str2 = "batman";
11 |
12 | System.out.print(str2.substring(0,str1.length()).compareTo(str1));
13 |
14 | int n = s.nextInt();
15 |
16 |
17 | ArrayList arr = new ArrayList<>();
18 |
19 | for(int i=0;i{
39 |
40 | public int compare(String a,String b) {
41 |
42 | if(a.length() < b.length() && b.substring(0,a.length())==a)
43 | return 0;
44 | else if(b.length() < a.length() && a.substring(0,b.length())==b)
45 | return 1;
46 | else
47 | return a.compareTo(b);
48 |
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/18. Divide & Conquer/2. Inversion Count.py:
--------------------------------------------------------------------------------
1 | def inversion_count(arr):
2 |
3 | if len(arr)==1:
4 | return 0,arr
5 |
6 | mid = len(arr)//2
7 | x,left = inversion_count(arr[0:mid])
8 | y,right = inversion_count(arr[mid:])
9 | cross_inv,complete = merge(left,right)
10 |
11 | return x+y+cross_inv,complete
12 |
13 |
14 | def merge(left,right):
15 |
16 | le = len(left)-1
17 | re = len(right)-1
18 |
19 | i,j,cnt = 0,0,0
20 | karr = []
21 |
22 | while i<=le and j<=re:
23 | if left[i]>1
14 | return res
15 |
16 | def generate_subsets(arr):
17 |
18 | n = len(arr)
19 | res = []
20 | for i in range(1,1< n:
48 | print("-1")
49 |
50 |
51 | elif x<0:
52 | print("-1")
53 |
54 | else:
55 | z = n-(x+y)
56 | print(x,y,z)
--------------------------------------------------------------------------------
/15. Number Theory Challenges/13. Linear Congerence.py:
--------------------------------------------------------------------------------
1 | import sys
2 | x = sys.maxsize
3 | y = sys.maxsize
4 | MAX_GCD = sys.maxsize
5 |
6 | n = int(input())
7 | num = [int(i) for i in input().split()]
8 | rem = [int(i) for i in input().split()]
9 | n = len(num)
10 | prod = 1
11 | for i in num:
12 | prod*=i
13 |
14 | pp = [ 0 for i in range(n)]
15 | for i in range(n):
16 | pp[i] = prod//num[i]
17 |
18 | inv = [ 0 for i in range(n)]
19 |
20 |
21 | def GCD(a,b):
22 | return a if b==0 else GCD(b,a%b)
23 |
24 | def ExtendedEuclid(a,b):
25 | global x, y, MAX_GCD
26 |
27 | if b==0:
28 | x=1
29 | y=0
30 | MAX_GCD = a
31 | return
32 |
33 | ExtendedEuclid(b,a%b)
34 | cX = y
35 | cY = x - (a//b)*y
36 |
37 | x = cX
38 | y = cY
39 |
40 | def invModulo(a,m):
41 | if GCD(a,m) != 1:
42 | raise Exception('No inverse modulo exist for given a and m')
43 | else:
44 | ExtendedEuclid(a,m)
45 | return (x+m)%m
46 |
47 |
48 | for i in range(n):
49 | inv[i] = invModulo(pp[i],num[i])
50 |
51 | res = 0
52 |
53 | for i in range(n):
54 | res+=(rem[i] * pp[i] * inv[i])%prod
55 |
56 | print(res%prod)
57 |
--------------------------------------------------------------------------------
/10. Number Theory - 1 Primes and Factorization/Fermat_Little_Primality_Testing.py:
--------------------------------------------------------------------------------
1 | # Checking Primality using Fermat Little Theorem
2 | # Accpeted on GFG, Link- https://practice.geeksforgeeks.org/problems/prime-number/0
3 |
4 |
5 | import random
6 |
7 |
8 | def GCD(a,b):
9 | return a if b==0 else GCD(b,a%b)
10 |
11 | def BitPowMod(a,b,MOD):
12 |
13 | res = 1
14 | while b!=0:
15 |
16 | if b&1 == 1:
17 | res = (res * a)%MOD
18 |
19 | a = (a*a)%MOD
20 | b = b>>1
21 |
22 | return res
23 |
24 | T = int(input())
25 |
26 | while(T!=0):
27 | T-=1
28 | N = int(input())
29 |
30 | if N<=1 or N&1==0:
31 | print("No")
32 | elif N==2:
33 | print("Yes")
34 | else:
35 | k = 5
36 | prime = True
37 | for i in range(k):
38 | a = random.randint(2,N-1)
39 | if GCD(a,N)!=1:
40 | prime=False
41 | break
42 |
43 | if BitPowMod(a,N-1,N)!=1:
44 | prime = False
45 | break
46 |
47 | if prime:
48 | print("Yes")
49 | else:
50 | print("No")
51 |
52 |
53 |
--------------------------------------------------------------------------------
/07. Maths - IV Pigeonhole Principle/Divisible_Subset.py:
--------------------------------------------------------------------------------
1 | # Accepted on Codechef :D
2 | t = int(input())
3 |
4 | while(t!=0):
5 | t-=1
6 | n = int(input())
7 | a = [int(i) for i in input().split()]
8 |
9 | cumm_sum = []
10 |
11 | curr_sum = 0
12 | for i in a:
13 | curr_sum+=i
14 | cumm_sum.append((curr_sum+n) % n)
15 |
16 | print(cumm_sum)
17 |
18 | index_arr = [[] for i in range(n)]
19 |
20 | #count_arr[0] = 1 # bcz of null subarr in beginning
21 | #print(index_arr)
22 |
23 | for i in range(n):
24 | index_arr[cumm_sum[i]].append(i)
25 |
26 | print(index_arr)
27 |
28 | if len(index_arr[0])>=1:
29 | print(index_arr[0][0]+1)
30 | for i in range(index_arr[0][0]+1):
31 | print(i+1,end=" ")
32 |
33 | else:
34 | for i in range(1,n):
35 | if len(index_arr[i])>1:
36 | st_idx = index_arr[i][0]
37 | end_idx = index_arr[i][1]
38 |
39 | size = end_idx-st_idx
40 | print(size)
41 | for j in range(st_idx+1,end_idx+1):
42 | print(j+1,end=" ")
43 | break
44 |
45 |
46 |
--------------------------------------------------------------------------------
/16. Recursion and backtracking/11. Bubble Sort.py:
--------------------------------------------------------------------------------
1 | def bubbleLinear(arr):
2 |
3 | n = len(arr)
4 |
5 | for i in range(0,n-1):
6 | for j in range(0,n-i-1):
7 |
8 | if arr[j]>arr[j+1]:
9 | arr[j],arr[j+1] = arr[j+1],arr[j]
10 |
11 | return arr
12 |
13 |
14 | def bubble_partial_recc(arr,i):
15 |
16 | # Base case
17 | if i==len(arr)-1:
18 | return arr
19 |
20 | # Mimic Inner loop
21 | for j in range(0,len(arr)-i-1):
22 | if arr[j]>arr[j+1]:
23 | arr[j],arr[j+1] = arr[j+1],arr[j]
24 |
25 | # Mimic outer Loop
26 | return bubble_partial_recc(arr,i+1)
27 |
28 |
29 | def bubble_complete_recc(arr,i,j):
30 |
31 | n = len(arr)
32 | # Mimic outer loop base condition
33 | if i==n-1:
34 | return arr
35 |
36 | # Mimic inner loop base condition
37 | if j==n-i-1:
38 | # Mimic outer loop
39 | return bubble_complete_recc(arr,i+1,0)
40 |
41 | if arr[j]>arr[j+1]:
42 | arr[j],arr[j+1] = arr[j+1],arr[j]
43 |
44 | # Mimic inner loop
45 | return bubble_complete_recc(arr,i,j+1)
46 |
47 |
48 | arr = [5,4,3,2,1,6,9]
49 | print(bubble_complete_recc(arr,0,0))
50 |
51 |
52 |
--------------------------------------------------------------------------------
/10. Number Theory - 1 Primes and Factorization/Large_Prime.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Large_Prime {
3 |
4 | static int LIM = 1000000;
5 |
6 | public static void main(String[] args) {
7 | // TODO Auto-generated method stub
8 |
9 | Scanner s = new Scanner(System.in);
10 |
11 | int[] primes = new int[LIM];
12 | for(int i=1; i res = new ArrayList<>();
8 |
9 | public static void main(String[] args) {
10 | // TODO Auto-generated method stub
11 |
12 | Scanner s = new Scanner(System.in);
13 |
14 | String str = s.next();
15 |
16 | solve(str,0,"");
17 |
18 | String output = "[";
19 |
20 | for(int i=0;i=2 && Integer.valueOf(str.substring(i,i+2)) <=26)
40 | solve(str, i+2, out+map[Integer.valueOf(str.substring(i,i+2))]);
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/18. Divide & Conquer/8. Aggressive Cows.py:
--------------------------------------------------------------------------------
1 | # Accepted on SPOJ
2 |
3 | def canPlaceCow(arr,n,c,min_sep):
4 |
5 | cnt = 1 # Placed cow in first stall
6 | lastCowBarn = arr[0]
7 |
8 | for i in range(1,n):
9 | if arr[i] - lastCowBarn >= min_sep: # Cow can be placed at this stall
10 | cnt+=1
11 | lastCowBarn = arr[i] # updating last barn pos
12 |
13 | if cnt == c: # Successfully places all cows at given min sep
14 | return True
15 |
16 | return False
17 |
18 |
19 | def min_dist(arr,n,c):
20 |
21 | arr.sort()
22 |
23 | # Search space
24 | st = 0
25 | end = arr[n-1] - arr[0]
26 | ans = 0
27 |
28 | while(st<=end):
29 |
30 | mid = (st+end)//2
31 |
32 | cowsRakhPaye = canPlaceCow(arr,n,c,mid)
33 |
34 | if cowsRakhPaye:
35 | st = mid+1
36 | ans = max(mid,ans)
37 |
38 | else:
39 | end = mid-1
40 |
41 | return ans
42 |
43 |
44 | t = int(input())
45 |
46 | while t!=0:
47 | t-=1
48 | n,c = [int(i) for i in input().split()]
49 | arr = []
50 | for i in range(n):
51 | arr.append(int(input()))
52 |
53 | ans = min_dist(arr,n,c)
54 | print(ans)
55 |
--------------------------------------------------------------------------------
/01. STL Challenges/binary_search.py:
--------------------------------------------------------------------------------
1 | def binary_search(arr,k):
2 | left = 0
3 | right = len(arr)-1
4 |
5 | while(left<=right):
6 | mid = ((left+right)//2)
7 |
8 | if arr[mid] == k:
9 | return mid
10 | elif k>arr[mid]:
11 | left = mid+1
12 | else:
13 | right = mid-1
14 |
15 |
16 | return mid
17 |
18 |
19 |
20 | def lower_bound(arr,k):
21 | left = 0
22 | right = len(arr)-1
23 | res = -1
24 |
25 | while(left<=right):
26 | mid = ((left+right)//2)
27 |
28 | if arr[mid] == k:
29 | res = mid
30 | right = mid-1
31 | elif k>arr[mid]:
32 | left = mid+1
33 | else:
34 | right = mid-1
35 | return res
36 |
37 | def upper_bound(arr,k):
38 | left = 0
39 | right = len(arr)-1
40 | res = -1
41 |
42 | while(left<=right):
43 | mid = ((left+right)//2)
44 |
45 | if arr[mid] == k:
46 | res = mid
47 | left = mid+1
48 | elif k>arr[mid]:
49 | left = mid+1
50 | else:
51 | right = mid-1
52 | return res
53 |
54 |
55 | print(lower_bound([1,2,5,9,12,12,12,14,18],12))
56 | print(upper_bound([1,2,5,9,12,12,12,14,18],12))
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/Alpha Score.py:
--------------------------------------------------------------------------------
1 | MOD = 1000000007
2 |
3 | def cross_alpha_score(left,right):
4 |
5 | le = len(left)-1
6 | re = len(right)-1
7 |
8 | i,j,cnt = 0,0,0
9 | karr = []
10 | score = 0
11 |
12 | while i<=le and j<=re:
13 |
14 | if left[i]=end):
43 | return 0,[arr[st]]
44 |
45 | mid = (st+end)//2
46 |
47 | x,left = alpha_score(arr,st,mid)
48 | y,right = alpha_score(arr,mid+1,end)
49 | z,complete = cross_alpha_score(left,right)
50 |
51 | return (x+y+z)%MOD,complete
52 |
53 |
54 |
55 |
56 | n = int(input())
57 | arr = [int(i) for i in input().split()]
58 |
59 | score,sort_arr = alpha_score(arr,0,n-1)
60 | print(score)
61 | #print("Arr: ",sort_arr)
--------------------------------------------------------------------------------
/09. Challenges - Mathematics/8. XOR-4.py:
--------------------------------------------------------------------------------
1 |
2 | def binary_search(a,x):
3 |
4 | left = 0
5 | right = len(a)-1
6 |
7 | while(left<=right):
8 | mid = (left+right)//2
9 |
10 | if a[mid] == x:
11 | return True
12 |
13 | elif a[mid] > x:
14 | right = mid-1
15 | else:
16 | left = mid+1
17 |
18 | return False
19 |
20 | def search(a,x,i,j,k):
21 |
22 | n = len(a)
23 | for ni in range(n):
24 |
25 | if ni==i or ni==j or ni==k:
26 | continue
27 |
28 | elif a[ni] == x:
29 | return True
30 |
31 |
32 | return False
33 |
34 |
35 | def compute(a):
36 | n = len(a)
37 | res = "No"
38 | for i in range(n):
39 | for j in range(i+1,n):
40 | for k in range(j+1,n):
41 | temp = (a[i]^a[j]^a[k])
42 | #print(temp)
43 |
44 | if search(a,temp,i,j,k) == True:
45 | res = "Yes"
46 | #print(res)
47 | return res
48 | return res
49 |
50 |
51 |
52 | n = int(input())
53 | a = [int(i) for i in input().split()]
54 |
55 | if n >= 130:
56 | print("Yes")
57 | else:
58 | a.sort()
59 | #print(a)
60 | print(compute(a))
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/19. Challenges - Divide And Conquer/It's Murder SPOJ.py:
--------------------------------------------------------------------------------
1 | # AC on SPOJ
2 |
3 | def cross_alpha_score(left,right):
4 |
5 | le = len(left)-1
6 | re = len(right)-1
7 |
8 | i,j,cnt = 0,0,0
9 | karr = []
10 | score = 0
11 |
12 | while i<=le and j<=re:
13 |
14 | if left[i]=end):
43 | return 0,[arr[st]]
44 |
45 | mid = (st+end)//2
46 |
47 | x,left = alpha_score(arr,st,mid)
48 | y,right = alpha_score(arr,mid+1,end)
49 | z,complete = cross_alpha_score(left,right)
50 |
51 | return (x+y+z),complete
52 |
53 |
54 |
55 | t = int(input())
56 | while t!=0:
57 | t-=1
58 | n = int(input())
59 | arr = [int(i) for i in input().split()]
60 |
61 | score,sort_arr = alpha_score(arr,0,n-1)
62 | print(score)
63 | #print("Arr: ",sort_arr)
--------------------------------------------------------------------------------
/15. Number Theory Challenges/18. Primality Testing.py:
--------------------------------------------------------------------------------
1 | # Accepted
2 | # No Modular Multiplicationn required in Python. Works well for 2^63-1
3 |
4 | import random
5 |
6 | def miller_rabin(N,d):
7 |
8 | a = 2+int(random.random()*((N-2 -2)+1))
9 |
10 | x = modular_exponentiation(a,d,N)
11 |
12 | if x%N==1 or x == N-1:
13 | return True
14 |
15 | while d!=N-1:
16 | x = (x*x)%N
17 | d = d*2
18 |
19 | if x==1:
20 | return True
21 | if x==N-1:
22 | return True
23 |
24 | return False
25 |
26 |
27 | def modular_exponentiation(a,d,n):
28 | res = 1
29 |
30 | while d!=0:
31 | if(d&1==1):
32 | res = (a*res)%n
33 | a = (a*a)%n
34 | d = d>>1
35 |
36 | return res
37 |
38 |
39 |
40 |
41 |
42 |
43 | def isPrime(N,k):
44 |
45 | if N==2:
46 | return True
47 |
48 | if N<=1 or N%2==0:
49 | return False
50 |
51 | d = N-1
52 | while d%2==0:
53 | d//=2
54 |
55 | for i in range(0,k):
56 | if(not miller_rabin(N,d)):
57 | return False
58 | return True
59 |
60 |
61 |
62 | t = int(input())
63 |
64 | while t!=0:
65 | t-=1
66 |
67 | N = int(input())
68 |
69 | if isPrime(N,5):
70 | print("YES")
71 | else:
72 | print("NO")
73 |
--------------------------------------------------------------------------------
/56. Advanced DS - Trie Problems/01. Trie Implmentation.py:
--------------------------------------------------------------------------------
1 | class Node:
2 |
3 | data : int
4 | childrens : dict
5 | terminal : bool
6 |
7 | def __init__(self,data):
8 | self.data = data
9 | self.terminal = False
10 | self.childrens = {}
11 |
12 | class Trie:
13 |
14 | root : Node
15 | size : int
16 |
17 | def __init__(self):
18 | self.root = Node("\0")
19 | self.size = 0
20 |
21 | def addWord(self,word):
22 |
23 | temp = self.root
24 |
25 | for ch in word:
26 |
27 | if ch in temp.childrens:
28 | temp = temp.childrens[ch]
29 | else:
30 | newNode = Node(ch)
31 | temp.childrens[ch] = newNode
32 | temp = newNode
33 |
34 | temp.terminal = True
35 |
36 | def findWord(self,word) -> bool:
37 |
38 | temp = self.root
39 |
40 | for ch in word:
41 |
42 | if ch not in temp.childrens:
43 | return False
44 | else:
45 | temp = temp.childrens[ch]
46 |
47 | return temp.terminal
48 |
49 |
50 | #words = ["a","hello","not","news","apple"]
51 | #trie = Trie()
52 |
53 | #for w in words:
54 | #trie.addWord(w)
55 |
56 | #myWord = "appl"
57 | #print(trie.findWord(myWord))
58 |
59 |
60 |
--------------------------------------------------------------------------------
/07. Maths - IV Pigeonhole Principle/Gray_Similar_Code.py:
--------------------------------------------------------------------------------
1 | # accepted on codechef
2 | def binary_search(a,x):
3 |
4 | left = 0
5 | right = len(a)-1
6 |
7 | while(left<=right):
8 | mid = (left+right)//2
9 |
10 | if a[mid] == x:
11 | return True
12 |
13 | elif a[mid] > x:
14 | right = mid-1
15 | else:
16 | left = mid+1
17 |
18 | return False
19 |
20 | def search(a,x,i,j,k):
21 |
22 | n = len(a)
23 | for ni in range(n):
24 |
25 | if ni==i or ni==j or ni==k:
26 | continue
27 |
28 | elif a[ni] == x:
29 | return True
30 |
31 |
32 | return False
33 |
34 |
35 | def compute(a):
36 | n = len(a)
37 | res = "No"
38 | for i in range(n):
39 | for j in range(i+1,n):
40 | for k in range(j+1,n):
41 | temp = (a[i]^a[j]^a[k])
42 | #print(temp)
43 |
44 | if search(a,temp,i,j,k) == True:
45 | res = "Yes"
46 | #print(res)
47 | return res
48 | return res
49 |
50 |
51 |
52 | n = int(input())
53 | a = [int(i) for i in input().split()]
54 |
55 | if n >= 130:
56 | print("Yes")
57 | else:
58 | a.sort()
59 | #print(a)
60 | print(compute(a))
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/50. String Pattern Matching/2. Rabin karp.py:
--------------------------------------------------------------------------------
1 | PRIME = 3
2 |
3 |
4 | def cal_hash(inp_str):
5 | res = 0
6 | n = len(inp_str)
7 |
8 | for i in range(n):
9 | res += (ord(inp_str[i])*(PRIME**i))
10 |
11 | return res
12 |
13 | def rolling_hash(inp_str,old_hash,i,m):
14 |
15 |
16 | old_hash -= ord(inp_str[i-1])
17 | old_hash = old_hash//PRIME
18 | old_hash += (ord(inp_str[i+m-1])*(PRIME**(m-1)))
19 |
20 | return old_hash
21 |
22 |
23 | def count_occurence(inp_str, pattern):
24 |
25 | n = len(inp_str)
26 | m = len(pattern)
27 |
28 | pattern_hash = cal_hash(pattern)
29 | input_hash = cal_hash(inp_str[0:m])
30 |
31 | cnt = 0
32 | if pattern_hash == input_hash and check(inp_str,pattern,0):
33 | cnt+=1
34 |
35 | for idx in range(1,n-m+1):
36 | input_hash = rolling_hash(inp_str,input_hash,idx,m)
37 |
38 | if input_hash == pattern_hash and check(inp_str,pattern,idx):
39 | cnt+=1
40 |
41 | return cnt
42 |
43 |
44 |
45 | def check(inp_str,pattern,idx):
46 | m = len(pattern)
47 | for i in range(m):
48 | if inp_str[idx+i] != pattern[i]:
49 | return False
50 | return True
51 |
52 |
53 | inp_str = input()
54 | pattern = input()
55 |
56 | #x = cal_hash("abc")
57 | #print(rolling_hash("abcd",x,1,3))
58 | print(count_occurence(inp_str,pattern))
--------------------------------------------------------------------------------
/55. Advanced DS - Hashing Problems/07. Unique Words Pair.py:
--------------------------------------------------------------------------------
1 | class Solution:
2 |
3 | def count_pairs(self,arr : [])->int:
4 |
5 | mySet = set()
6 | for i in arr:
7 | mySet.add(i[::-1])
8 |
9 | print(mySet)
10 |
11 | cnt = 0
12 | for i in arr:
13 | if(len(i)==1):
14 | continue
15 |
16 | for j in range(1,len(i)+1):
17 | if i[:j] in mySet and (j+1==len(i) or self.isPallindrome(i[j+1:])):
18 | print(i[:j])
19 | cnt+=1
20 |
21 | for i in arr:
22 | if(len(i)==1):
23 | continue
24 |
25 | for j in range(len(i)-1,-1,-1):
26 | if i[j:len(i)] in mySet and ( self.isPallindrome(i[0:j])):
27 | cnt+=1
28 | return cnt
29 |
30 |
31 |
32 |
33 | def isPallindrome(self,s : str) -> bool:
34 |
35 | if len(s)==0:
36 | return True
37 |
38 | left = 0
39 | right = len(s)-1
40 |
41 | while left arr = new ArrayList<>();
19 | String[] strarr = br.readLine().trim().split("\\s+");
20 | for(int i=0; i arr, long[] cumm, int n,long sum) {
48 | long ans = 0;
49 | long each = sum/n;
50 |
51 | for(int i=1;i
16 |
17 | ### 2. SPOJ -
18 | - [Julka](https://www.spoj.com/problems/JULKA/)
19 | - [SEQ](https://www.spoj.com/problems/SEQ/)
20 | - [Favourite Dice](https://www.spoj.com/problems/FAVDICE/)
21 | - [Primes1](https://www.spoj.com/problems/PRIME1/)
22 | - [Aggressive Cows](https://www.spoj.com/problems/AGGRCOW/)
23 |
24 |
25 |
26 | ### 3. Codechef -
27 | - [Gray Similar Code](https://www.codechef.com/problems/GRAYSC)
28 | - [Divisible Subset](https://www.codechef.com/problems/DIVSUBS)
29 | - [Marbles](https://www.codechef.com/problems/MARBLES)
30 |
--------------------------------------------------------------------------------
/29. Greedy Algorithms/02. Activity_Selection.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | class Activity{
3 | int st;
4 | int end;
5 |
6 | Activity(int st, int end){
7 | this.st = st;
8 | this.end = end;
9 | }
10 |
11 | @Override
12 | public String toString() {
13 | // TODO Auto-generated method stub
14 | return "("+this.st+","+this.end+")";
15 | }
16 | }
17 |
18 | public class Activity_Selection {
19 |
20 | public static void main(String[] args) {
21 | // TODO Auto-generated method stub
22 |
23 | Scanner s = new Scanner(System.in);
24 |
25 | int t = s.nextInt();
26 |
27 | while(t--!=0) {
28 | int n = s.nextInt();
29 | ArrayList list = new ArrayList<>();
30 | s.nextLine();
31 | for(int i=0; i=last.end) {
46 | last = list.get(i);
47 | cnt++;
48 | }
49 | }
50 |
51 | System.out.println(cnt);
52 | }
53 | }
54 | }
55 |
56 | class EndSort implements Comparator{
57 |
58 | public int compare(Activity a1, Activity a2) {
59 | if(a1.end>=a2.end)
60 | return 1;
61 | else
62 | return -1;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/21. Segment Trees II - Lazy Propagation/1. Lazy Propogation.py:
--------------------------------------------------------------------------------
1 | import sys
2 |
3 | def build_tree(tree,arr,ss,se,index):
4 |
5 | if ss==se:
6 | tree[index] = arr[ss]
7 | return
8 |
9 | mid = (ss+se)//2
10 | build_tree(tree,arr,ss,mid,2*index)
11 | build_tree(tree,arr,mid+1,se,2*index+1)
12 | tree[index] = min(tree[2*index],tree[2*index+1])
13 |
14 | def lazy_update(tree,arr,ss,se,qs,qe,inc,index):
15 |
16 | # resolve lazy value if exist
17 | if lazy[index]!=0:
18 | tree[index]+=lazy[index]
19 |
20 | # check if child exists, i.e it is not leaf node
21 | if ss!=se:
22 | lazy[2*index]+=lazy[index]
23 | lazy[2*index+1]+=lazy[index]
24 |
25 | lazy[index] = 0
26 |
27 | # No Overlap
28 | if se=qs or qe>=se:
33 | tree[index]+=inc
34 |
35 | if ss!=se:
36 | lazy[2*index]+=inc
37 | lazy[2*index+1]+=inc
38 | return
39 |
40 | # Partial Overlap
41 | mid = (ss+se)//2
42 |
43 | lazy_tree(tree,arr,ss,mid,qs,qe,inc,2*index)
44 | lazy_tree(tree,arr,mid+1,se,qs,qe,inc,2*index+1)
45 |
46 | tree[index] = min(tree[2*index],tree[2*index+1])
47 |
48 |
49 |
50 |
51 | arr = [int(i) for i in input().split()]
52 | n = len(arr)
53 | tree = [sys.maxsize for i in range(4*n+1)]
54 | lazy = [0 for i in range(4*n+1)]
55 | build_tree(tree,arr,0,n-1,1)
56 | print(tree)
57 |
58 |
--------------------------------------------------------------------------------
/44. Dynamic Programming/13. Longest Common Subsequence.py:
--------------------------------------------------------------------------------
1 | a = "AGGTAB"
2 | b = "GXTXAYB"
3 | op = ""
4 | MAX = 3000
5 | dp = [[-1]*MAX for i in range(MAX)]
6 |
7 | def solve_topdown(a,b,i,j):
8 |
9 | if j>=len(a) or i>=len(b):
10 | return 0
11 |
12 | if dp[i][j] != -1:
13 | return dp[i][j]
14 |
15 | ans = -1
16 | if a[j] == b[i]:
17 | ans = 1+solve_topdown(a,b,i+1,j+1)
18 | else:
19 | ans = max(solve_topdown(a,b,i+1,j),solve_topdown(a,b,i,j+1))
20 |
21 | dp[i][j] = ans
22 |
23 | return ans
24 |
25 |
26 |
27 |
28 | def solve(a,b):
29 | global op
30 |
31 | la = len(a)
32 | lb = len(b)
33 |
34 | dp = [[0]*(la+1) for i in range(lb+1)]
35 |
36 | for i in range(1,lb+1):
37 | for j in range(1,la+1):
38 | if a[j-1]==b[i-1]:
39 | op += a[j-1]
40 | dp[i][j] = dp[i-1][j-1] + 1
41 | else:
42 | dp[i][j] = max(dp[i-1][j],dp[i][j-1])
43 |
44 | return dp[lb][la],dp
45 |
46 | def get_LCS(a,b,l,dp):
47 |
48 | i = len(b)
49 | j = len(a)
50 | res = ""
51 |
52 | while(l>0):
53 | if a[j-1]==b[i-1]:
54 | res = a[j-1] + res
55 | i-=1
56 | j-=1
57 | l-=1
58 | else:
59 | if (dp[i][j-1]>dp[i-1][j]):
60 | j-=1
61 | else:
62 | i-=1
63 |
64 | return res
65 |
66 | l,dp = solve(a,b)
67 | #print(l,dp)
68 | print(get_LCS(a,b,l,dp))
--------------------------------------------------------------------------------