Diagonal Difference
Plus Minus
def diagonalDifference(arr):
dp = 0
ds = 0
n = len(arr) - 1
for i in range(0, len(arr)):
dp += arr[i][i]
ds += arr[i][n - i]
difference = abs(dp - ds)
return difference
Staircase
def plusMinus(arr):
cntPoz = 0
cntNeg = 0
cntZero = 0
for x in arr:
if x < 0:
cntNeg += 1;
elif x == 0 :
cntZero += 1;
else:
cntPoz += 1
print(round(cntPoz / len(arr), 6))
print(round(cntNeg / len(arr), 6))
print(round(cntZero / len(arr),6))
Mini-Max Sum
def staircase(n):
for i in range(0, n):
line = ''
for j in range(0, n):
if i + j < (n - 1):
line += ' '
else:
line += '#'
print(line)
Birthday Cake Candles
def miniMaxSum(arr):
minimum = float('inf')
maximum = float('-inf')
sumArr = 0
for x in arr:
if minimum > x:
minimum = x
if maximum < x:
maximum = x
sumArr += x;
print(sumArr - maximum, sumArr - minimum)
Time Conversion
def birthdayCakeCandles(candles):
maximum = float('-inf');
cntMaximum = 0
for x in candles:
if maximum < x:
maximum = x;
for x in candles:
if x == maximum:
cntMaximum += 1
return cntMaximum;
Grading Students
def timeConversion(s):
h = int(s[:2]);
res = ''
if s[8] == 'A':
if h == 12:
res += '0';
res += '0';
else:
res += s[0]
res += s[1]
for i in range(2, 8):
res += s[i]
else:
if h != 12:
h += 12;
res += chr(int(h / 10) % 10 + ord('0'))
res += chr(int(h % 10) + ord('0'))
else:
res += s[0]
res += s[1]
for i in range(2, 8):
res += s[i]
return res
Apple and Orange
def gradingStudents(grades):
for i in range(0, len(grades)):
grade = grades[i]
if grade >= 38:
u = grade % 10;
z = (grade / 10) % 10;
if u == 4 or u == 3:
grade = (grade // 10) * 10 + 5;
if u == 9 or u == 8:
grade = (grade // 10 + 1) * 10;
grades[i] = grade;
return grades;
Number Line Jumps
def countApplesAndOranges(s, t, a, b, apples, oranges):
cnt_apples = 0
cnt_oranges = 0
for apple in apples:
dist = apple + a;
if dist >= s and dist <= t:
cnt_apples += 1
for orange in oranges:
dist = orange + b;
if dist >= s and dist <= t:
cnt_oranges += 1
print(cnt_apples)
print(cnt_oranges)
Between Two Sets
def kangaroo(x1, v1, x2, v2):
while x1 != x2:
if (x1 > x2 and v1 > v2) or (x1 < x2 and v1 < v2):
return 'NO';
x1 += v1;
x2 += v2
return "YES"
Breaking the Records
def getTotalX(a, b):
res = 0
for i in range (1, 101):
isFactor = True
for x in b:
if x % i != 0:
isFactor = False;
if isFactor:
for x in a:
if i % x != 0:
isFactor = False;
if isFactor == True:
res += 1
return res;
Subarray Division
def breakingRecords(scores):
minimum = maximum = scores[0]
cntMin = 0
cntMax = 0
for x in scores:
if maximum < x:
maximum = x;
cntMax += 1;
if minimum > x:
minimum = x;
cntMin += 1;
return [cntMax, cntMin];
Extra Long Factorials
def birthday(s, d, m):
cnt = 0
c_sum = 0
i = 0
j = (i + m - 1)
for x in range(i, j + 1):
c_sum += s[x]
while j < len(s):
if c_sum == d:
cnt += 1
c_sum -= s[i]
i += 1
j += 1
if j < len(s):
c_sum += s[j]
return cnt
Non-Divisible Subset
def extraLongFactorials(n):
bigFactorial = 1
while n >= 2:
bigFactorial *= n
n -= 1
print(bigFactorial)
Divisible Sum Pairs
def nonDivisibleSubset(k, s):
reminder = [0] * k
for i in s:
reminder[i % k] += 1
maxnum = 0
maxnum += min(reminder[0], 1)
if k % 2 == 0:
maxnum += min(reminder[k // 2], 1)
for i in range(1, k // 2 + 1):
if i != k - i:
maxnum += max(reminder[i], reminder[k - i])
return maxnum
def divisibleSumPairs(n, k, ar):
cnt = 0
for i in range(0, n - 1):
for j in range(i + 1, n):
if (ar[i] + ar[j]) % k == 0:
cnt += 1
return cnt