diff --git a/HackerRank/Project Euler/ProjectEuler1-Multiples-of-3-and-5.py b/HackerRank/Project Euler/ProjectEuler1-Multiples-of-3-and-5.py new file mode 100644 index 000000000000..eee1ab614d1c --- /dev/null +++ b/HackerRank/Project Euler/ProjectEuler1-Multiples-of-3-and-5.py @@ -0,0 +1,19 @@ +# Author : Junth Basnet +""" +https://www.hackerrank.com/contests/projecteuler/challenges/euler001/problem +""" +def S(n): + return (n * (n + 1)) // 2 + +for _ in range(int(input())): + + n = int(input()) + #Sum of natural numbers below the given number + n -= 1 + + result = (S(n // 3) * 3) + (S(n // 5) * 5) - (S(n // 15) * 15) + print(result) + + + + diff --git a/sorts/QuickSort_MiddlePivot.py b/sorts/QuickSort_MiddlePivot.py new file mode 100644 index 000000000000..a4f0e20cde5f --- /dev/null +++ b/sorts/QuickSort_MiddlePivot.py @@ -0,0 +1,35 @@ +# Author : Junth Basnet + +""" +Implementation of Quick Sort Algorithm with middle element as pivot element +Time Complexity : O(nlogn) - O(n^2) +""" + +def QuickSortFirst(array): + return QuickSort(array, 0, len(array) - 1) + +def QuickSort(array, left, right): + if left >= right: + return array + pivot = array[(left + right) // 2] + index = Partition(array, left, right, pivot) + QuickSort(array, left, index - 1) + QuickSort(array, index, right) + return array + +def Partition(array, left, right, pivot): + while left <= right: + while array[left] < pivot: + left += 1 + while array[right] > pivot: + right -= 1 + if left <= right: + array[left], array[right] = array[right], array[left] + left += 1 + right -= 1 + return left + +array = [1, 6, 4, 10, 7, 30, 25] +print(array) +sorted_array = QuickSortFirst(array) +print(sorted_array) \ No newline at end of file