Skip to content

Commit 4f77335

Browse files
author
Мето Трајковски
committed
Added new solutions
1 parent 665f9a1 commit 4f77335

3 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
Find an Element Which is Smaller or Equal to Exactly K Numbers
3+
4+
You have to find some number X greater than 0 where exactly K elements in that list are greater than or equal to the number X.
5+
If there are multiple such values return the smallest possible.
6+
If there is no such X, return (-1).
7+
8+
Input: [3,8,5,1,10,3,20,24], 2
9+
Output: 11
10+
Output explanation: Only 20 and 24 are greater or smaller from 11 (11 is the smallest solution, also 12, 13...20 are solutions).
11+
12+
=========================================
13+
Sort the array and check the Kth element from the end.
14+
Time Complexity: O(NLogN)
15+
Space Complexity: O(1)
16+
'''
17+
18+
19+
############
20+
# Solution #
21+
############
22+
23+
def get_minimum_X(arr, k):
24+
n = len(arr)
25+
26+
if n == 0 or k > n:
27+
return -1
28+
29+
if k == n:
30+
return 1
31+
32+
arr.sort()
33+
34+
if k == 0:
35+
return arr[-1] + 1
36+
37+
if arr[-k] == arr[-(k + 1)]:
38+
return -1
39+
40+
return arr[-(k + 1)] + 1
41+
42+
43+
###########
44+
# Testing #
45+
###########
46+
47+
# Test 1
48+
# Correct result => 11
49+
print(get_minimum_X([3, 8, 5, 1, 10, 3, 20, 24], 2))

Math/odd_sum.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
Odd Sum
3+
4+
For a given range [а,b], find the sum of all odd numbers between a and b.
5+
6+
Input: 3, 9
7+
Output: 24
8+
Output explanation: 3+5+7+9=24
9+
10+
=========================================
11+
Several different O(1) approaches exist. This is the explanation of my solution/formula.
12+
3 + 5 + 7 + 9 can be written like (3 + 0) + (3 + 2) + (3 + 4) + (3 + 6)
13+
that's (3 * 4) + (2 + 4 + 6), also this can be written like
14+
(3 * 4) + ((2 * 1) + (2 * 2) + (2 * 3)) = 3 * 4 + 2 * (1 + 2 + 3)
15+
And the formula is: Min_Odd * Num_Odds + 2 * Sum(Num_Odds)
16+
Sum formula is N*(N-1)/2. (for all numbers smaller than N)
17+
This is the simplest formula:
18+
Min_Odd * Num_Odds + 2 * Num_Odds * (Num_Odds - 1) / 2 =
19+
Num_Odds * (Min_Odd + Num_Odds - 1)
20+
Time Complexity: O(1)
21+
Space Complexity: O(1)
22+
'''
23+
24+
25+
############
26+
# Solution #
27+
############
28+
29+
def odd_sum(a, b):
30+
# find first odd number
31+
if a % 2 == 0:
32+
a += 1
33+
# to avoid rounding (math.ceil) find the biggest even number
34+
if b % 2 == 1:
35+
b += 1
36+
# count of odd numbers
37+
n = (b - a + 1) // 2
38+
# use the formula from the description
39+
return n * (a + n - 1)
40+
41+
42+
###########
43+
# Testing #
44+
###########
45+
46+
# Test 1
47+
# Correct result => 24
48+
print(odd_sum(3, 9))

Math/total_divisible_numbers.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
Total Divisible Numbers
3+
4+
Given an array A of N numbers,
5+
your task is to find how many numbers from 1 to S are divisible by all of the elements in the array.
6+
7+
Input: [2, 4, 5], 45
8+
Output: 2
9+
Output explanation: 20 and 40 are divisible by all numbers in the array.
10+
11+
=========================================
12+
Find least common multiple of all numbers in the array (lcm can be found using gcd, (a * b)/gcd(a, b)).
13+
And in the end check how many numbers are divisble by the lcm number (smaller or equal to S).
14+
Time Complexity: O(N)
15+
Space Complexity: O(1)
16+
'''
17+
18+
19+
############
20+
# Solution #
21+
############
22+
23+
def total_divisible_numbers(arr, S):
24+
# find lcm for all numbers in the array
25+
lcm = 1
26+
for a in arr:
27+
lcm = (a * lcm) // gcd(a, lcm)
28+
29+
# return the count of numbers divisble by the lcm number (smaller or equal to S)
30+
return S // lcm
31+
32+
def gcd(a, b):
33+
while a != 0:
34+
a, b = b % a, a
35+
return b
36+
37+
38+
###########
39+
# Testing #
40+
###########
41+
42+
# Test 1
43+
# Correct result => 4
44+
print(total_divisible_numbers([3, 5, 6], 146))
45+
46+
# Test 2
47+
# Correct result => 52
48+
print(total_divisible_numbers([3, 3, 2], 317))
49+
50+
# Test 3
51+
# Correct result => 30
52+
print(total_divisible_numbers([2, 3], 30))

0 commit comments

Comments
 (0)