cappuch commited on
Commit
f497ffd
·
verified ·
1 Parent(s): 5467208

2180adbf072beeac5a609527434db063adc375c2097e1bd148e9e0e7876f938b

Browse files
Files changed (50) hide show
  1. python_code_instructions_filtered_55.txt +0 -0
  2. python_code_instructions_filtered_550.txt +0 -0
  3. python_code_instructions_filtered_551.txt +0 -0
  4. python_code_instructions_filtered_552.txt +0 -0
  5. python_code_instructions_filtered_553.txt +0 -0
  6. python_code_instructions_filtered_554.txt +0 -0
  7. python_code_instructions_filtered_555.txt +0 -0
  8. python_code_instructions_filtered_556.txt +0 -0
  9. python_code_instructions_filtered_557.txt +0 -0
  10. python_code_instructions_filtered_558.txt +0 -0
  11. python_code_instructions_filtered_559.txt +0 -0
  12. python_code_instructions_filtered_56.txt +0 -0
  13. python_code_instructions_filtered_560.txt +0 -0
  14. python_code_instructions_filtered_561.txt +0 -0
  15. python_code_instructions_filtered_562.txt +0 -0
  16. python_code_instructions_filtered_563.txt +0 -0
  17. python_code_instructions_filtered_564.txt +0 -0
  18. python_code_instructions_filtered_565.txt +0 -0
  19. python_code_instructions_filtered_566.txt +0 -0
  20. python_code_instructions_filtered_567.txt +0 -0
  21. python_code_instructions_filtered_568.txt +0 -0
  22. python_code_instructions_filtered_569.txt +3086 -0
  23. python_code_instructions_filtered_57.txt +0 -0
  24. python_code_instructions_filtered_58.txt +0 -0
  25. python_code_instructions_filtered_59.txt +0 -0
  26. python_code_instructions_filtered_6.txt +0 -0
  27. python_code_instructions_filtered_60.txt +0 -0
  28. python_code_instructions_filtered_61.txt +0 -0
  29. python_code_instructions_filtered_62.txt +0 -0
  30. python_code_instructions_filtered_63.txt +0 -0
  31. python_code_instructions_filtered_64.txt +0 -0
  32. python_code_instructions_filtered_65.txt +0 -0
  33. python_code_instructions_filtered_66.txt +0 -0
  34. python_code_instructions_filtered_67.txt +0 -0
  35. python_code_instructions_filtered_68.txt +0 -0
  36. python_code_instructions_filtered_69.txt +0 -0
  37. python_code_instructions_filtered_7.txt +0 -0
  38. python_code_instructions_filtered_70.txt +0 -0
  39. python_code_instructions_filtered_71.txt +0 -0
  40. python_code_instructions_filtered_72.txt +0 -0
  41. python_code_instructions_filtered_73.txt +0 -0
  42. python_code_instructions_filtered_74.txt +0 -0
  43. python_code_instructions_filtered_75.txt +0 -0
  44. python_code_instructions_filtered_76.txt +0 -0
  45. python_code_instructions_filtered_77.txt +0 -0
  46. python_code_instructions_filtered_78.txt +0 -0
  47. python_code_instructions_filtered_79.txt +0 -0
  48. python_code_instructions_filtered_8.txt +0 -0
  49. python_code_instructions_filtered_80.txt +0 -0
  50. python_code_instructions_filtered_81.txt +0 -0
python_code_instructions_filtered_55.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_550.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_551.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_552.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_553.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_554.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_555.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_556.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_557.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_558.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_559.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_56.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_560.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_561.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_562.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_563.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_564.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_565.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_566.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_567.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_568.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_569.txt ADDED
@@ -0,0 +1,3086 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ return prod
2
+
3
+
4
+ n = 7
5
+ print(multiply_by_fifteen(n))
6
+
7
+ def check(a, n):
8
+ """
9
+ Modify a binary array to Bitwise AND of all elements as 1
10
+ """
11
+ for i in range(n):
12
+ if (a[i]):
13
+ return True
14
+ return False
15
+
16
+
17
+ if __name__ == '__main__':
18
+ a = [0, 1, 0, 1]
19
+ n = len(a)
20
+ if (check(a, n)):
21
+ print("YES")
22
+ else:
23
+ print("NO")
24
+
25
+ def is_equal_block(n):
26
+ """
27
+ Check if the binary representation of a number has equal number of 0 s and 1 s in blocks
28
+ """
29
+ first_bit = n % 2
30
+ first_count = 1
31
+ n = n // 2
32
+ while n % 2 == first_bit and n > 0:
33
+ n = n // 2
34
+ first_count += 1
35
+ if n == 0:
36
+ return False
37
+ while n > 0:
38
+ first_bit = n % 2
39
+ curr_count = 1
40
+ n = n // 2
41
+ while n % 2 == first_bit:
42
+ n = n // 2
43
+ curr_count += 1
44
+ if curr_count != first_count:
45
+ return False
46
+ return True
47
+
48
+
49
+ if __name__ == "__main__":
50
+ n = 51
51
+ if is_equal_block(n):
52
+ print("Yes")
53
+ else:
54
+ print("No")
55
+
56
+ def calculate(X):
57
+ """
58
+ Find a value whose XOR with given number is maximum
59
+ """
60
+ number_of_bits = 8
61
+ return ((1 << number_of_bits) - 1) ^ X
62
+
63
+
64
+ if __name__ == "__main__":
65
+ X = 4
66
+ print("Required Number is:", calculate(X))
67
+
68
+ def count_zeros(x):
69
+ """
70
+ Number of leading zeros in binary representation of a given number
71
+ """
72
+ total_bits = 32
73
+ res = 0
74
+ while ((x & (1 << (total_bits - 1))) == 0):
75
+ x = (x << 1)
76
+ res += 1
77
+ return res
78
+
79
+
80
+ x = 101
81
+ print(count_zeros(x))
82
+
83
+ def set_rightmost_unset_bit(n):
84
+ """
85
+ Set the rightmost off bit
86
+ """
87
+ if n & (n + 1) == 0:
88
+ return n
89
+ return n | (n + 1)
90
+
91
+
92
+ if __name__ == "__main__":
93
+ n = 21
94
+ print(set_rightmost_unset_bit(n))
95
+
96
+ def increment(i):
97
+ """
98
+ Increment a number without using ++ or +
99
+ """
100
+ i = -(~i)
101
+ return i
102
+
103
+
104
+ if __name__ == "__main__":
105
+ n = 3
106
+ print(increment(n))
107
+
108
+ def xor_pair_sum(ar, n):
109
+ """
110
+ Sum of XOR of sum of all pairs in an array
111
+ """
112
+ total = 0
113
+ for i in range(n):
114
+ total = total ^ ar[i]
115
+ return 2 * total
116
+
117
+
118
+ if __name__ == "__main__":
119
+ data = [1, 2, 3]
120
+ print(xor_pair_sum(data, len(data)))
121
+
122
+ def kth_character(m, n, k):
123
+ """
124
+ Find iâ €™ th index character in a binary string obtained after n iterations
125
+ """
126
+ distance = pow(2, n)
127
+ Block_number = int(k / distance)
128
+ remaining = k % distance
129
+ s = [0] * 32
130
+ x = 0
131
+ while (m > 0):
132
+ s[x] = m % 2
133
+ m = int(m / 2)
134
+ x += 1
135
+ root = s[x - 1 - Block_number]
136
+ if (remaining == 0):
137
+ print(root)
138
+ return
139
+ flip = True
140
+ while (remaining > 1):
141
+ if (remaining & 1):
142
+ flip = not (flip)
143
+ remaining = remaining >> 1
144
+ if (flip):
145
+ print(not (root))
146
+ else:
147
+ print(root)
148
+
149
+
150
+ m = 5
151
+ k = 5
152
+ n = 3
153
+ kth_character(m, n, k)
154
+
155
+ from math import pow
156
+
157
+
158
+ def get_integer(L, R):
159
+ """
160
+ Number with set bits only between L
161
+ """
162
+ number = 0
163
+ for i in range(L, R + 1, 1):
164
+ number += pow(2, i)
165
+ return number
166
+
167
+
168
+ if __name__ == '__main__':
169
+ L = 2
170
+ R = 5
171
+ print(int(get_integer(L, R)))
172
+
173
+ def find_eletobe_inserted(A, n, k):
174
+ """
175
+ Number whose XOR sum with given array is a given number k
176
+ """
177
+ ans = k
178
+ for i in range(n):
179
+ return ans
180
+
181
+
182
+ if __name__ == '__main__':
183
+ A = [1, 2, 3, 4, 5]
184
+ n = len(A)
185
+ k = 10
186
+ print(
187
+ find_eletobe_inserted(
188
+ A,
189
+ n,
190
+ k),
191
+ "has to be inserted in the given",
192
+ "array to make xor sum of",
193
+ k)
194
+
195
+ def minimize(a):
196
+ """
197
+ Minimum number using set bits of a given number
198
+ """
199
+ n = bin(a).count("1")
200
+ return (pow(2, n) - 1)
201
+
202
+
203
+ a = 11
204
+ print(minimize(a))
205
+
206
+ def count_set_bits(n):
207
+ """
208
+ Maximum steps to transform 0 to X with bitwise AND
209
+ """
210
+ count = 0
211
+ while (n):
212
+ count += n & 1
213
+ n >>= 1
214
+ return count
215
+
216
+
217
+ i = 3
218
+ print(count_set_bits(i))
219
+
220
+ def is_even(n):
221
+ """
222
+ Check a number is odd or even without modulus operator
223
+ """
224
+ return (int(n / 2) * 2 == n)
225
+
226
+
227
+ n = 101
228
+ if (is_even(n)):
229
+ print("Even")
230
+ else:
231
+ print("Odd")
232
+
233
+ def add(x, y):
234
+ """
235
+ Bitwise recursive addition of two integers
236
+ """
237
+ keep = (x & y) << 1
238
+ res = x ^ y
239
+ if (keep == 0):
240
+ return res
241
+ return add(keep, res)
242
+
243
+
244
+ print(add(15, 38))
245
+
246
+ def divide(dividend, divisor):
247
+ """
248
+ Divide two integers without using multiplication , division and mod operator
249
+ """
250
+ sign = (-1 if ((dividend < 0) ^ (divisor < 0))else 1)
251
+ dividend = abs(dividend)
252
+ divisor = abs(divisor)
253
+ quotient = 0
254
+ temp = 0
255
+ for i in range(31, -1, -1):
256
+ if (temp + (divisor << i) <= dividend):
257
+ temp += divisor << i
258
+ quotient |= 1 << i
259
+ if sign == -1:
260
+ quotient = -quotient
261
+ return quotient
262
+
263
+
264
+ a = 10
265
+ b = 3
266
+ print(divide(a, b))
267
+ a = 43
268
+ b = -8
269
+ print(divide(a, b))
270
+
271
+ def toggle_bits(n1, n2):
272
+ """
273
+ For every set bit of a number toggle bits of other
274
+ """
275
+ return (n1 ^ n2)
276
+
277
+
278
+ n1 = 2
279
+ n2 = 5
280
+ print(toggle_bits(n1, n2))
281
+
282
+ def max_xor_sum(n, k):
283
+ """
284
+ Maximum XOR using K numbers from 1 to n
285
+ """
286
+ if k == 1:
287
+ return n
288
+ res = 1
289
+ while res <= n:
290
+ res <<= 1
291
+ return res - 1
292
+
293
+
294
+ n = 4
295
+ k = 3
296
+ print(max_xor_sum(n, k))
297
+
298
+ def maximum_sum(a, b, n):
299
+ """
300
+ Maximum OR sum of sub
301
+ """
302
+ sum1 = 0
303
+ sum2 = 0
304
+ for i in range(0, n):
305
+ sum1 |= a[i]
306
+ sum2 |= b[i]
307
+ print(sum1 + sum2)
308
+
309
+
310
+ A = [1, 2, 4, 3, 2]
311
+ B = [2, 3, 3, 12, 1]
312
+ n = len(A)
313
+ maximum_sum(A, B, n)
314
+
315
+ def multiply(x, n):
316
+ """
317
+ Multiplication with a power of 2
318
+ """
319
+ return x << n
320
+
321
+
322
+ x = 70
323
+ n = 2
324
+ print(multiply(x, n))
325
+
326
+ def adjacent_set(n):
327
+ """
328
+ Check if a number has two adjacent set bits
329
+ """
330
+ return (n & (n >> 1))
331
+
332
+
333
+ if __name__ == '__main__':
334
+ n = 3
335
+ if (adjacent_set(n)):
336
+ print("Yes")
337
+ else:
338
+ print("No")
339
+
340
+ def multiply(n, m):
341
+ """
342
+ Multiplication of two numbers with shift operator
343
+ """
344
+ ans = 0
345
+ count = 0
346
+ while (m):
347
+ if (m % 2 == 1):
348
+ ans += n << count
349
+ count += 1
350
+ m = int(m / 2)
351
+ return ans
352
+
353
+
354
+ if __name__ == '__main__':
355
+ n = 20
356
+ m = 13
357
+ print(multiply(n, m))
358
+
359
+ def equal_number(A, B):
360
+ """
361
+ Compare two integers without using any Comparison operator
362
+ """
363
+ return (A ^ B)
364
+
365
+
366
+ A = 5
367
+ B = 6
368
+ print(int(not (equal_number(A, B))))
369
+
370
+ def multiply_ten(n):
371
+ """
372
+ Multiply a number with 10 without using multiplication operator
373
+ """
374
+ return (n << 1) + (n << 3)
375
+
376
+
377
+ n = 50
378
+ print(multiply_ten(n))
379
+
380
+ def count_values(n):
381
+ """
382
+ Equal Sum and XOR
383
+ """
384
+ unset_bits = 0
385
+ while (n):
386
+ if n & 1 == 0:
387
+ unset_bits += 1
388
+ n = n >> 1
389
+ return 1 << unset_bits
390
+
391
+
392
+ if __name__ == '__main__':
393
+ n = 12
394
+ print(count_values(n))
395
+
396
+ def swap_bits(n, p1, p2):
397
+ """
398
+ How to swap two bits in a given integer ?
399
+ """
400
+ bit1 = (n >> p1) & 1
401
+ bit2 = (n >> p2) & 1
402
+ x = (bit1 ^ bit2)
403
+ x = (x << p1) | (x << p2)
404
+ result = n ^ x
405
+ return result
406
+
407
+
408
+ if __name__ == '__main__':
409
+ res = swap_bits(28, 0, 3)
410
+ print("Result = ", res)
411
+
412
+ def invert(x):
413
+ """
414
+ Write a function that returns 2 for input 1 and returns 1 for 2 |
415
+ """
416
+ if (x == 1):
417
+ return 2
418
+ else:
419
+ return 1
420
+
421
+ def invert_sub(x):
422
+ """
423
+ Write a function that returns 2 for input 1 and returns 1 for 2 |
424
+ """
425
+ return (3 - x)
426
+
427
+ def calc__expectation(a, n):
428
+ """
429
+ Expectation or expected value of an array
430
+ """
431
+ prb = 1 / n
432
+ sum = 0
433
+ for i in range(0, n):
434
+ sum += (a[i] * prb)
435
+ return float(sum)
436
+
437
+
438
+ n = 6
439
+ a = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
440
+ expect = calc__expectation(a, n)
441
+ print("Expectation of array E(X) is : ", expect)
442
+
443
+ def maximum_updates(arr):
444
+ """
445
+ Number of times Maximum and minimum value updated during traversal of array
446
+ """
447
+ min = arr[0]
448
+ max = arr[0]
449
+ minCount, maxCount = 1, 1
450
+ for arr in arr:
451
+ if arr > max:
452
+ maxCount += 1
453
+ max = arr
454
+ if arr < min:
455
+ minCount += 1
456
+ min = arr
457
+ print("Number of times maximum ", end="")
458
+ print("value updated = ", maxCount)
459
+ print("Number of times minimum ", end="")
460
+ print("value updated = ", minCount)
461
+
462
+
463
+ if __name__ == "__main__":
464
+ arr = [10, 5, 20, 22]
465
+ maximum_updates(arr)
466
+
467
+ def max_sum(a, n):
468
+ """
469
+ Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1
470
+ """
471
+ count = [0] * (n + 1)
472
+ for i in range(0, n):
473
+ count[min(a[i], n)] += 1
474
+ size = 0
475
+ ans = 0
476
+ for k in range(1, n + 1):
477
+ while (count[k] > 0 and size < k):
478
+ size += 1
479
+ ans += size
480
+ count[k] -= 1
481
+ ans += k * count[k]
482
+ return ans
483
+
484
+
485
+ if __name__ == '__main__':
486
+ arr = [3, 5, 1]
487
+ n = len(arr)
488
+ print(max_sum(arr, n))
489
+
490
+ def count_triplets(N, K):
491
+ """
492
+ Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K
493
+ """
494
+ if (K % 2 == 0):
495
+ x = N // K
496
+ y = (N + (K // 2)) // K
497
+ return x * x * x + y * y * y
498
+ else:
499
+ x = N // K
500
+ return x * x * x
501
+
502
+
503
+ if __name__ == "__main__":
504
+ N = 2
505
+ K = 2
506
+ print(count_triplets(N, K))
507
+
508
+ def maximum_result(a, b, c):
509
+ """
510
+ Maximize the value of the given expression
511
+ """
512
+ countOfNegative = 0
513
+ Sum = a + b + c
514
+ product = a * b * c
515
+ largest = max(a, b, c)
516
+ smallest = min(a, b, c)
517
+ if a < 0:
518
+ countOfNegative += 1
519
+ if b < 0:
520
+ countOfNegative += 1
521
+ if c < 0:
522
+ countOfNegative += 1
523
+ if countOfNegative == 0:
524
+ return (Sum - largest) * largest
525
+ elif countOfNegative == 1:
526
+ return (product // smallest) + smallest
527
+ elif countOfNegative == 2:
528
+ return (product // largest) + largest
529
+ elif countOfNegative == 3:
530
+ return (Sum - smallest) * smallest
531
+
532
+
533
+ if __name__ == "__main__":
534
+ a, b, c = -2, -1, -4
535
+ print(maximum_result(a, b, c))
536
+
537
+ '
538
+
539
+
540
+ def solve(n, lookup={}):
541
+ """
542
+ How to solve a Dynamic Programming Problem ?
543
+ """
544
+ if n < 0:
545
+ return 0
546
+ if n == 0:
547
+ return 1
548
+ if n not in lookup:
549
+ lookup[n] = (solve(n - 1) + solve(n - 3) + solve(n - 5))
550
+ return lookup[n]
551
+
552
+ def number_of_paths(p, q):
553
+ """
554
+ Count all possible paths from top left to bottom right of a mXn matrix
555
+ """
556
+ dp = [1 for i in range(q)]
557
+ for i in range(p - 1):
558
+ for j in range(1, q):
559
+ dp[j] += dp[j - 1]
560
+ return dp[q - 1]
561
+
562
+
563
+ print(number_of_paths(3, 3))
564
+
565
+ class Solution:
566
+
567
+ """
568
+ Longest Arithmetic Progression
569
+ """
570
+
571
+ def solve(self, A):
572
+ ans = 2
573
+ n = len(A)
574
+ if n <= 2:
575
+ return n
576
+ llap = [2] * n
577
+ A.sort()
578
+ for j in range(n - 2, -1, -1):
579
+ i = j - 1
580
+ k = j + 1
581
+ while (i >= 0 and k < n):
582
+ if A[i] + A[k] == 2 * A[j]:
583
+ llap[j] = max(llap[k] + 1, llap[j])
584
+ ans = max(ans, llap[j])
585
+ i -= 1
586
+ k += 1
587
+ elif A[i] + A[k] < 2 * A[j]:
588
+ k += 1
589
+ else:
590
+ i -= 1
591
+ return ans
592
+
593
+
594
+ obj = Solution()
595
+ a = [9, 4, 7, 2, 10]
596
+ print(obj.solve(a))
597
+
598
+ def ending_with(str, suff):
599
+ """
600
+ Count of words ending at the given suffix in Java
601
+ """
602
+ c = 0
603
+ wrd = str.split(" ")
604
+ for l in wrd:
605
+ if l.endswith(suff):
606
+ c += 1
607
+ return c
608
+
609
+
610
+ str = "GeeksForGeeks is a computer science portal for geeks"
611
+ suff = "ks"
612
+ print(ending_with(str, suff))
613
+
614
+ def is_frequency_equal(string, length):
615
+ """
616
+ Character whose frequency is equal to the sum of frequencies of other characters of the given string
617
+ """
618
+ if length % 2 == 1:
619
+ return False
620
+ freq = [0] * 26
621
+ for i in range(0, length):
622
+ freq[ord(string[i]) - ord('a')] += 1
623
+ for i in range(0, 26):
624
+ if freq[i] == length // 2:
625
+ return True
626
+ return False
627
+
628
+
629
+ if __name__ == "__main__":
630
+ string = "geeksforgeeks"
631
+ length = len(string)
632
+ if is_frequency_equal(string, length):
633
+ print("Yes")
634
+ else:
635
+ print("No")
636
+
637
+ def round(n):
638
+ """
639
+ Round the given number to nearest multiple of 10
640
+ """
641
+ a = (n // 10) * 10
642
+ b = a + 10
643
+ return (b if n - a > b - n else a)
644
+
645
+
646
+ n = 4722
647
+ print(round(n))
648
+
649
+ def decode(string):
650
+ """
651
+ Null Cipher
652
+ """
653
+ res = ""
654
+ found = False
655
+ for character in string:
656
+ if character == ' ':
657
+ found = False
658
+ continue
659
+ if not found:
660
+ if character >= 'A' and character <= 'Z' or character >= 'a' and character <= 'z':
661
+ res += character
662
+ found = True
663
+ return res.lower()
664
+
665
+
666
+ if __name__ == "__main__":
667
+ input = "A Step by Step Guide for Placement Preparation by GeeksforGeeks"
668
+ print("Enciphered Message:", decode(input))
669
+
670
+ def count9s(number):
671
+ """
672
+ Given a number as a string , find the number of contiguous subsequences which recursively add up to 9
673
+ """
674
+ n = len(number)
675
+ for i in range(n):
676
+ sum = ord(number[i]) - ord('0')
677
+ if (number[i] == '9'):
678
+ count += 1
679
+ for j in range(i + 1, n):
680
+ sum = (sum + ord(number[j]) - ord('0')) % 9
681
+ if (sum == 0):
682
+ count += 1
683
+ return count
684
+
685
+
686
+ if __name__ == "__main__":
687
+ print(count9s("4189"))
688
+ print(count9s("1809"))
689
+
690
+ def count_direct_path(N):
691
+ """
692
+ Count of Unique Direct Path Between N Points On a Plane
693
+ """
694
+ return N + (N * (N - 3)) // 2
695
+
696
+
697
+ if __name__ == "__main__":
698
+ N = 5
699
+ print(count_direct_path(N))
700
+
701
+ def triacontagonal_num(n):
702
+ """
703
+ Triacontagon Number
704
+ """
705
+ return (28 * n * n - 26 * n) // 2
706
+
707
+
708
+ n = 3
709
+ print("3rd triacontagonal Number is = ", triacontagonal_num(n))
710
+
711
+ def hexacontagon_num(n):
712
+ """
713
+ Hexacontagon Number
714
+ """
715
+ return (58 * n * n - 56 * n) // 2
716
+
717
+
718
+ n = 3
719
+ print("3rd hexacontagon Number is = ", hexacontagon_num(n))
720
+
721
+ def icosihexagonal_num(n):
722
+ """
723
+ Icosihexagonal Number
724
+ """
725
+ return (24 * n * n - 22 * n) // 2
726
+
727
+
728
+ n = 3
729
+ print("3rd Icosihexagonal Number is = ", icosihexagonal_num(n))
730
+
731
+ def icosikaioctagonal_num(n):
732
+ """
733
+ Icosikaioctagon or Icosioctagon Number
734
+ """
735
+ return (26 * n * n - 24 * n) // 2
736
+
737
+
738
+ n = 3
739
+ print("3rd icosikaioctagonal Number is = ", icosikaioctagonal_num(n))
740
+
741
+ def octacontagon_num(n):
742
+ """
743
+ Octacontagon Number
744
+ """
745
+ return (78 * n * n - 76 * n) // 2
746
+
747
+
748
+ n = 3
749
+ print("3rd octacontagon Number is = ", octacontagon_num(n))
750
+
751
+ from math import sqrt
752
+
753
+
754
+ def perimeter(a, b):
755
+ """
756
+ perimeter of an Ellipse
757
+ """
758
+ perimeter = 0
759
+ perimeter = (2 * 3.14 * sqrt((a * a + b * b) / (2 * 1.0)))
760
+ print(perimeter)
761
+
762
+
763
+ a = 3
764
+ b = 2
765
+ perimeter(a, b)
766
+
767
+ def reuleaux_area(a):
768
+ """
769
+ Biggest Reuleaux Triangle within A Square
770
+ """
771
+ if (a < 0):
772
+ return -1
773
+ A = 0.70477 * pow(a, 2)
774
+ return A
775
+
776
+
777
+ if __name__ == "__main__":
778
+ a = 6
779
+ print(reuleaux_area(a))
780
+
781
+ def hexagonside(a):
782
+ """
783
+ Largest hexagon that can be inscribed within a square
784
+ """
785
+ if (a < 0):
786
+ return -1
787
+ x = 0.5176 * a
788
+ return x
789
+
790
+
791
+ a = 6
792
+ print(hexagonside(a))
793
+
794
+ def hexagonside(a):
795
+ """
796
+ Largest hexagon that can be inscribed within an equilateral triangle
797
+ """
798
+ if a < 0:
799
+ return -1
800
+ x = a // 3
801
+ return x
802
+
803
+
804
+ a = 6
805
+ print(hexagonside(a))
806
+
807
+ def find_segment(n, m, segment_length):
808
+ """
809
+ Find middle point segment from given segment lengths
810
+ """
811
+ meet_point = (1.0 * n) / 2.0
812
+ sum = 0
813
+ segment_number = 0
814
+ for i in range(0, m, 1):
815
+ sum += segment_length[i]
816
+ if (sum == meet_point):
817
+ segment_number = -1
818
+ break
819
+ if (sum > meet_point):
820
+ segment_number = i + 1
821
+ break
822
+ return segment_number
823
+
824
+
825
+ if __name__ == '__main__':
826
+ n = 13
827
+ m = 3
828
+ segment_length = [3, 2, 8]
829
+ ans = find_segment(n, m, segment_length)
830
+ print(ans)
831
+
832
+ def count_max_intersect(n):
833
+ """
834
+ Maximum points of intersection n lines
835
+ """
836
+ return int(n * (n - 1) / 2)
837
+
838
+
839
+ if __name__ == '__main__':
840
+ n = 8
841
+ print(count_max_intersect(n))
842
+
843
+ def checkpoint(h, k, x, y, a):
844
+ """
845
+ Check if a point is inside , outside or on the parabola
846
+ """
847
+ p = pow((y - k), 2) - 4 * a * (x - h)
848
+ return p
849
+
850
+
851
+ if __name__ == "__main__":
852
+ h = 0
853
+ k = 0
854
+ x = 2
855
+ y = 1
856
+ a = 4
857
+ if checkpoint(h, k, x, y, a) > 0:
858
+ print("Outside")
859
+ elif checkpoint(h, k, x, y, a) == 0:
860
+ print("Ontheparabola")
861
+ else:
862
+ print("Inside")
863
+
864
+ import math
865
+
866
+
867
+ def checkpoint(h, k, x, y, a, b):
868
+ """
869
+ Check if a point is inside , outside or on the ellipse
870
+ """
871
+ p = ((math.pow((x - h), 2) // math.pow(a, 2)) +
872
+ (math.pow((y - k), 2) // math.pow(b, 2)))
873
+ return p
874
+
875
+
876
+ if __name__ == "__main__":
877
+ h = 0
878
+ k = 0
879
+ x = 2
880
+ y = 1
881
+ a = 4
882
+ b = 5
883
+ if (checkpoint(h, k, x, y, a, b) > 1):
884
+ print("Outside")
885
+ elif (checkpoint(h, k, x, y, a, b) == 1):
886
+ print("On the ellipse")
887
+ else:
888
+ print("Inside")
889
+
890
+ def circlearea(a, b):
891
+ """
892
+ Area of circle inscribed within rhombus
893
+ """
894
+ if (a < 0 or b < 0):
895
+ return -1
896
+ A = ((3.14 * pow(a, 2) * pow(b, 2)) / (4 * (pow(a, 2) + pow(b, 2))))
897
+ return A
898
+
899
+
900
+ if __name__ == "__main__":
901
+ a = 8
902
+ b = 10
903
+ print(circlearea(a, b))
904
+
905
+ def circlearea(l, b):
906
+ """
907
+ The biggest possible circle that can be inscribed in a rectangle
908
+ """
909
+ if (l < 0 or b < 0):
910
+ return -1
911
+ if (l < b):
912
+ return 3.14 * pow(l // 2, 2)
913
+ else:
914
+ return 3.14 * pow(b // 2, 2)
915
+
916
+
917
+ if __name__ == "__main__":
918
+ l = 4
919
+ b = 8
920
+ print(circlearea(l, b))
921
+
922
+ def centered_cube(n):
923
+ """
924
+ Centered cube number
925
+ """
926
+ return (2 * n + 1) * (n * n + n + 1)
927
+
928
+
929
+ if __name__ == '__main__':
930
+ n = 3
931
+ print(n, "th Centered cube " + "number : ", centered_cube(n))
932
+ n = 10
933
+ print(n, "th Centered cube " + "number : ", centered_cube(n))
934
+
935
+ def center(x1, x2, y1, y2):
936
+ """
937
+ Find the center of the circle using endpoints of diameter
938
+ """
939
+ print(int((x1 + x2) / 2), end="")
940
+ print(",", int((y1 + y2) / 2))
941
+
942
+
943
+ x1 = -9
944
+ y1 = 3
945
+ x2 = 5
946
+ y2 = -7
947
+ center(x1, x2, y1, y2)
948
+
949
+ import math
950
+
951
+
952
+ def vol_of_octahedron(side):
953
+ """
954
+ Program to calculate volume of Octahedron
955
+ """
956
+ return ((side * side * side) * (math.sqrt(2) / 3))
957
+
958
+
959
+ side = 3
960
+ print("Volume of octahedron =", round(vol_of_octahedron(side), 4))
961
+
962
+ def count(N):
963
+ """
964
+ Minimum count of consecutive integers till N whose bitwise AND is 0 with N
965
+ """
966
+ a = bin(N)
967
+ a = a[2:]
968
+ m = len(a) - 1
969
+ res = N - (2 ** m - 1)
970
+ return res
971
+
972
+
973
+ N = 18
974
+ print(count(N))
975
+
976
+ def find_cycles(N):
977
+ """
978
+ Number of cycles formed by joining vertices of n sided polygon at the center
979
+ """
980
+ res = 0
981
+ finalResult = 0
982
+ val = 2 * N - 1
983
+ s = val
984
+ res = (N - 1) * (N - 2)
985
+ finalResult = res + s
986
+ return finalResult
987
+
988
+
989
+ if __name__ == '__main__':
990
+ N = 5
991
+ print(find_cycles(N))
992
+
993
+ def find(a, n, k):
994
+ """
995
+ Split a given array into K subarrays minimizing the difference between their maximum and minimum
996
+ """
997
+ v = []
998
+ for i in range(1, n):
999
+ v.append(a[i - 1] - a[i])
1000
+ v.sort()
1001
+ res = a[n - 1] - a[0]
1002
+ for i in range(k - 1):
1003
+ res += v[i]
1004
+ return res
1005
+
1006
+
1007
+ arr = [4, 8, 15, 16, 23, 42]
1008
+ N = len(arr)
1009
+ K = 3
1010
+ print(find(arr, N, K))
1011
+
1012
+ def smallest_div(n):
1013
+ """
1014
+ Find the Smallest number that divides X ^ X
1015
+ """
1016
+ i = 2
1017
+ while i * i <= n:
1018
+ if (n % i == 0):
1019
+ return i
1020
+ i += 1
1021
+ return n
1022
+
1023
+
1024
+ if __name__ == "__main__":
1025
+ X = 385
1026
+ ans = smallest_div(X)
1027
+ print(ans)
1028
+
1029
+ def power(x, y, p):
1030
+ """
1031
+ Find number of magical pairs of string of length L
1032
+ """
1033
+ res = 1
1034
+ x = x % p
1035
+ while (y > 0):
1036
+ if (y % 2 == 1):
1037
+ res = (res * x) % p
1038
+ x = (x * x) % p
1039
+ return res
1040
+
1041
+
1042
+ L = 2
1043
+ P = pow(10, 9)
1044
+ ans = power(325, L, P)
1045
+ print(ans)
1046
+
1047
+ MAXN = 30
1048
+
1049
+
1050
+ def count_max_length(N):
1051
+ """
1052
+ Longest substring of only 4 's from the first N characters of the infinite string
1053
+ """
1054
+ pre = [0 for i in range(MAXN)]
1055
+ p = 1
1056
+ pre[0] = 0
1057
+ for i in range(1, MAXN, 1):
1058
+ p *= 2
1059
+ pre[i] = pre[i - 1] + i * p
1060
+ for i in range(1, MAXN, 1):
1061
+ if (pre[i] >= N):
1062
+ ind = i
1063
+ break
1064
+ x = N - pre[ind - 1]
1065
+ y = 2 * ind - 1
1066
+ if (x >= y):
1067
+ res = min(x, y)
1068
+ else:
1069
+ res = max(x, 2 * (ind - 2) + 1)
1070
+ return res
1071
+
1072
+
1073
+ if __name__ == '__main__':
1074
+ N = 25
1075
+ print(count_max_length(N))
1076
+
1077
+ def minimum(N, K):
1078
+ """
1079
+ minimum count of numbers needed from 1 to N that yields the sum as K
1080
+ """
1081
+ sum = N * (N + 1) // 2
1082
+ if (K > sum):
1083
+ return -1
1084
+ if (K <= N):
1085
+ return 1
1086
+ sum = 0
1087
+ count = 0
1088
+ while (N >= 1 and sum < K):
1089
+ count += 1
1090
+ sum += N
1091
+ N -= 1
1092
+ return count
1093
+
1094
+
1095
+ if __name__ == '__main__':
1096
+ N = 5
1097
+ K = 10
1098
+ print(minimum(N, K))
1099
+
1100
+ def count(N, K):
1101
+ """
1102
+ Maximize X such that sum of numbers in range [ 1 , X ] is at most K
1103
+ """
1104
+ if (K == 0):
1105
+ return 0
1106
+ res = 0
1107
+ low = 1
1108
+ high = N
1109
+ while (low <= high):
1110
+ mid = (low + high) // 2
1111
+ sum = (mid * mid + mid) // 2
1112
+ if (sum <= K):
1113
+ res = max(res, mid)
1114
+ low = mid + 1
1115
+ else:
1116
+ high = mid - 1
1117
+ return res
1118
+
1119
+
1120
+ if __name__ == '__main__':
1121
+ N = 6
1122
+ K = 14
1123
+ print(count(N, K))
1124
+
1125
+ def find_element(A, N, X):
1126
+ """
1127
+ Check if an element is present in an array using at most floor ( N / 2 ) + 2 comparisons
1128
+ """
1129
+ i = 0
1130
+ Comparisons = 0
1131
+ T = 1
1132
+ Found = "No"
1133
+ Comparisons += 1
1134
+ if (N % 2 == 1):
1135
+ i = 1
1136
+ T *= (A[0] - X)
1137
+ while (i < N):
1138
+ Comparisons += 1
1139
+ T *= (A[i] - X)
1140
+ T *= (A[i + 1] - X)
1141
+ i += 2
1142
+ Comparisons += 1
1143
+ if (T == 0):
1144
+ print("Yes", Comparisons)
1145
+ else:
1146
+ print("No")
1147
+
1148
+
1149
+ if __name__ == '__main__':
1150
+ A = [-3, 5, 11, 3, 100, 2, 88, 22, 7, 900, 23, 4, 1]
1151
+ N = len(A)
1152
+ X = 1
1153
+ find_element(A, N, X)
1154
+
1155
+ def find(arr, N, key):
1156
+ """
1157
+ Search an element in a sorted array formed by reversing subarrays from a random index
1158
+ """
1159
+ l = 0
1160
+ h = N - 1
1161
+ while l <= h:
1162
+ mid = (l + h) // 2
1163
+ if arr[mid] == key:
1164
+ return mid
1165
+ if arr[l] >= arr[mid]:
1166
+ if arr[l] >= key >= arr[mid]:
1167
+ h = mid - 1
1168
+ else:
1169
+ l = mid + 1
1170
+ else:
1171
+ if arr[mid] >= key >= arr[h]:
1172
+ l = mid + 1
1173
+ else:
1174
+ h = mid - 1
1175
+ return -1
1176
+
1177
+
1178
+ if __name__ == "__main__":
1179
+ arr = [10, 8, 6, 5, 2, 1, 13, 12]
1180
+ N = len(arr)
1181
+ key = 8
1182
+ ans = find(arr, N, key)
1183
+ print(ans)
1184
+
1185
+ def max_items(n, m, a, b, K):
1186
+ """
1187
+ Maximum elements that can be removed from front of two arrays such that their sum is at most K
1188
+ """
1189
+ count = 0
1190
+ A = [0 for i in range(n + 1)]
1191
+ B = [0 for i in range(m + 1)]
1192
+ for i in range(1, n + 1, 1):
1193
+ A[i] = a[i - 1] + A[i - 1]
1194
+ for i in range(1, m + 1, 1):
1195
+ B[i] = b[i - 1] + B[i - 1]
1196
+ for i in range(n + 1):
1197
+ if (A[i] > K):
1198
+ break
1199
+ rem = K - A[i]
1200
+ j = 0
1201
+ lo = 0
1202
+ hi = m
1203
+ while (lo <= hi):
1204
+ mid = (lo + hi) // 2
1205
+ if (B[mid] <= rem):
1206
+ j = mid
1207
+ lo = mid + 1
1208
+ else:
1209
+ hi = mid - 1
1210
+ count = max(j + i, count)
1211
+ print(count)
1212
+
1213
+
1214
+ if __name__ == '__main__':
1215
+ n = 4
1216
+ m = 5
1217
+ K = 7
1218
+ A = [2, 4, 7, 3]
1219
+ B = [1, 9, 3, 4, 5]
1220
+ max_items(n, m, A, B, K)
1221
+
1222
+ def minimum_swaps(arr, n):
1223
+ """
1224
+ Minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively
1225
+ """
1226
+ count = 0
1227
+ max_el = max(arr)
1228
+ min_el = min(arr)
1229
+ if (min_el == max_el):
1230
+ return 0
1231
+ index_max = -1
1232
+ index_min = -1
1233
+ for i in range(n):
1234
+ if (arr[i] == max_el and index_max == -1):
1235
+ index_max = i
1236
+ if (arr[i] == min_el):
1237
+ index_min = i
1238
+ count += index_max
1239
+ count += (n - 1 - index_min)
1240
+ if (index_min < index_max):
1241
+ count -= 1
1242
+ return count
1243
+
1244
+
1245
+ if __name__ == '__main__':
1246
+ arr = [2, 4, 1, 6, 5]
1247
+ N = len(arr)
1248
+ print(minimum_swaps(arr, N))
1249
+
1250
+ def least_weighted_sum_path(n, edges, src, dst, K):
1251
+ """
1252
+ Minimum cost path from source node to destination node via K intermediate nodes
1253
+ """
1254
+ graph = [[]for i in range(3)]
1255
+ for edge in edges:
1256
+ graph[edge[0]].append([edge[1], edge[2]])
1257
+ pq = []
1258
+ costs = [[10 ** 9 for i in range(K + 2)]for i in range(n)]
1259
+ costs[src][K + 1] = 0
1260
+ pq.append([0, src, K + 1])
1261
+ pq = sorted(pq)[::-1]
1262
+ while (len(pq) > 0):
1263
+ top = pq[-1]
1264
+ del pq[-1]
1265
+ if (top[1] == dst):
1266
+ return top[0]
1267
+ if (top[2] == 0):
1268
+ continue
1269
+ for neighbor in graph[top[1]]:
1270
+ if (costs[neighbor[0]][top[2] - 1] < neighbor[1] + top[0]):
1271
+ continue
1272
+ costs[neighbor[0]][top[2] - 1] = neighbor[1] + top[0]
1273
+ pq.append([neighbor[1] + top[0], neighbor[0], top[2] - 1])
1274
+ pq = sorted(pq)[::-1]
1275
+ return -1
1276
+
1277
+
1278
+ if __name__ == '__main__':
1279
+ n, src, dst, k = 3, 0, 2, 1
1280
+ edges = [[0, 1, 100], [1, 2, 100], [0, 2, 500]]
1281
+ print(least_weighted_sum_path(n, edges, src, dst, k))
1282
+
1283
+ def kth_character(S, N, K):
1284
+ """
1285
+ Frequency of lexicographically Kth smallest character in the a string
1286
+ """
1287
+ strarray = [char for char in S]
1288
+ strarray.sort()
1289
+ ch = strarray[K - 1]
1290
+ count = 0
1291
+ for c in strarray:
1292
+ if (c == ch):
1293
+ count += 1
1294
+ print(count)
1295
+
1296
+
1297
+ if __name__ == '__main__':
1298
+ S = "geeksforgeeks"
1299
+ N = len(S)
1300
+ K = 3
1301
+ kth_character(S, N, K)
1302
+
1303
+ def max_non_emp_sub_seq(a, n):
1304
+ """
1305
+ Maximum Sum Subsequence
1306
+ """
1307
+ sum = 0
1308
+ maxm = max(a)
1309
+ if (maxm <= 0):
1310
+ return maxm
1311
+ for i in range(n):
1312
+ if (a[i] > 0):
1313
+ sum += a[i]
1314
+ return sum
1315
+
1316
+
1317
+ if __name__ == '__main__':
1318
+ arr = [-2, 11, -4, 2, -3, -10]
1319
+ N = len(arr)
1320
+ print(max_non_emp_sub_seq(arr, N))
1321
+
1322
+ def find_intersecting_range(tup, N, ranges):
1323
+ """
1324
+ Find a pair of intersecting ranges from a given array
1325
+ """
1326
+ curr = 0
1327
+ currPos = 0
1328
+ for i in range(N):
1329
+ x = ranges[i][0]
1330
+ y = ranges[i][1]
1331
+ tup.append([[x, y], i + 1])
1332
+ tup = sorted(tup)
1333
+ curr = tup[0][0][1]
1334
+ currPos = tup[0][1]
1335
+ for i in range(1, N):
1336
+ Q = tup[i - 1][0][0]
1337
+ R = tup[i][0][0]
1338
+ if (Q == R):
1339
+ if (tup[i - 1][0][1] < tup[i][0][1]):
1340
+ print(tup[i - 1][1], tup[i][1])
1341
+ else:
1342
+ print(tup[i][1], tup[i - 1][1])
1343
+ return
1344
+ T = tup[i][0][1]
1345
+ if (T <= curr):
1346
+ print(tup[i][1], currPos)
1347
+ return
1348
+ else:
1349
+ curr = T
1350
+ currPos = tup[i][1]
1351
+ print("-1 -1")
1352
+
1353
+
1354
+ if __name__ == '__main__':
1355
+ N = 5
1356
+ ranges = [[1, 5], [2, 10], [3, 10], [2, 2], [2, 15]]
1357
+ find_intersecting_range([], N, ranges)
1358
+
1359
+ def find_index(arr, n, B):
1360
+ """
1361
+ Search insert position of K in a sorted array
1362
+ """
1363
+ start = 0
1364
+ end = n - 1
1365
+ while start <= end:
1366
+ mid = (start + end) // 2
1367
+ if arr[mid] == K:
1368
+ return mid
1369
+ elif arr[mid] < K:
1370
+ start = mid + 1
1371
+ else:
1372
+ end = mid - 1
1373
+ return end + 1
1374
+
1375
+
1376
+ arr = [1, 3, 5, 6]
1377
+ n = len(arr)
1378
+ K = 2
1379
+ print(find_index(arr, n, K))
1380
+
1381
+ def binary_search(arr, N, X):
1382
+ """
1383
+ Search an element in a reverse sorted array
1384
+ """
1385
+ start = 0
1386
+ end = N
1387
+ while (start <= end):
1388
+ mid = start + (end - start) // 2
1389
+ if (X == arr[mid]):
1390
+ return mid
1391
+ elif (X < arr[mid]):
1392
+ start = mid + 1
1393
+ else:
1394
+ end = mid - 1
1395
+ return -1
1396
+
1397
+
1398
+ if __name__ == '__main__':
1399
+ arr = [5, 4, 3, 2, 1]
1400
+ N = len(arr)
1401
+ X = 5
1402
+ print(binary_search(arr, N, X))
1403
+
1404
+ def check_hex(s):
1405
+ """
1406
+ Check if a string represents a hexadecimal number or not
1407
+ """
1408
+ for ch in s:
1409
+ if ((ch < '0' or ch > '9') and (ch < 'A' or ch > 'F')):
1410
+ print("No")
1411
+ return
1412
+ print("Yes")
1413
+
1414
+
1415
+ s = "BF57C"
1416
+ check_hex(s)
1417
+
1418
+ def check_same_diag(x, y):
1419
+ """
1420
+ Check if two elements of a matrix are on the same diagonal or not
1421
+ """
1422
+ for i in range(m):
1423
+ for j in range(n):
1424
+ if li[i][j] == x:
1425
+ I, J = i, j
1426
+ if li[i][j] == y:
1427
+ P, Q = i, j
1428
+ if P - Q == I - J or P + Q == I + J:
1429
+ print("YES")
1430
+ else:
1431
+ print("NO")
1432
+
1433
+
1434
+ if __name__ == "__main__":
1435
+ m, n = 6, 5
1436
+ li = [[32, 94, 99, 26, 82], [51, 69, 52, 63, 17], [90, 36, 88, 55, 33], [
1437
+ 93, 42, 73, 39, 28], [81, 31, 83, 53, 10], [12, 29, 85, 80, 87]]
1438
+ x, y = 42, 80
1439
+ check_same_diag(x, y)
1440
+
1441
+ def calc_distance(A, B, n):
1442
+ """
1443
+ Distance Traveled by Two Trains together in the same Direction
1444
+ """
1445
+ distance_traveled_A = 0
1446
+ distance_traveled_B = 0
1447
+ answer = 0
1448
+ for i in range(5):
1449
+ distance_traveled_A += A[i]
1450
+ distance_traveled_B += B[i]
1451
+ if ((distance_traveled_A == distance_traveled_B) and (A[i] == B[i])):
1452
+ answer += A[i]
1453
+ return answer
1454
+
1455
+
1456
+ A = [1, 2, 3, 2, 4]
1457
+ B = [2, 1, 3, 1, 4]
1458
+ N = len(A)
1459
+ print(calc_distance(A, B, N))
1460
+
1461
+ from collections import defaultdict
1462
+
1463
+
1464
+ def get_count(N, s):
1465
+ """
1466
+ Count of pairs of strings whose concatenation forms a palindromic string
1467
+ """
1468
+ mp = defaultdict(lambda: 0)
1469
+ ans = 0
1470
+ for i in range(N):
1471
+ a = [0] * 26
1472
+ for j in range(len(s[i])):
1473
+ a[ord(s[i][j]) - ord('a')] += 1
1474
+ for j in range(26):
1475
+ a[j] = a[j] % 2
1476
+ ans += mp[tuple(a)]
1477
+ for j in range(26):
1478
+ changedCount = a[:]
1479
+ if (a[j] == 0):
1480
+ changedCount[j] = 1
1481
+ else:
1482
+ changedCount[j] = 0
1483
+ ans += mp[tuple(changedCount)]
1484
+ mp[tuple(a)] += 1
1485
+ return ans
1486
+
1487
+
1488
+ if __name__ == '__main__':
1489
+ N = 6
1490
+ A = ["aab", "abcac", "dffe", "ed", "aa", "aade"]
1491
+ print(get_count(N, A))
1492
+
1493
+ def checkrules(s):
1494
+ """
1495
+ Check if given Binary string follows then given condition or not
1496
+ """
1497
+ if len(s) == 0:
1498
+ return True
1499
+ if s[0] != '1':
1500
+ return False
1501
+ if len(s) > 2:
1502
+ if s[1] == '0' and s[2] == '0':
1503
+ return checkrules(s[3:])
1504
+ return checkrules(s[1:])
1505
+
1506
+
1507
+ if __name__ == '__main__':
1508
+ s = '1111'
1509
+ if checkrules(s):
1510
+ print('valid string')
1511
+ else:
1512
+ print('invalid string')
1513
+
1514
+ def split_array(arr, N):
1515
+ """
1516
+ Partition a set into two subsets such that difference between max of one and min of other is minimized
1517
+ """
1518
+ arr = sorted(arr)
1519
+ result = 10 ** 9
1520
+ for i in range(1, N):
1521
+ result = min(result, arr[i] - arr[i - 1])
1522
+ return result
1523
+
1524
+
1525
+ if __name__ == '__main__':
1526
+ arr = [3, 1, 2, 6, 4]
1527
+ N = len(arr)
1528
+ print(split_array(arr, N))
1529
+
1530
+ def find_value(R, C):
1531
+ """
1532
+ Find the element at R ' th ▁ row ▁ and ▁ C ' th column in given a 2D pattern
1533
+ """
1534
+ k = (R * (R - 1)) // 2 + 1
1535
+ diff = R + 1
1536
+ for i in range(1, C):
1537
+ k = (k + diff)
1538
+ diff += 1
1539
+ return k
1540
+
1541
+
1542
+ if __name__ == "__main__":
1543
+ R = 4
1544
+ C = 4
1545
+ k = find_value(R, C)
1546
+ print(k)
1547
+
1548
+ def find_minimum_k(a, n, s):
1549
+ """
1550
+ Minimum K such that sum of array elements after division by K does not exceed S
1551
+ """
1552
+ maximum = a[0]
1553
+ for i in range(n):
1554
+ maximum = max(maximum, a[i])
1555
+ low = 1
1556
+ high = maximum + 1
1557
+ ans = high
1558
+ while (low <= high):
1559
+ mid = (low + high) // 2
1560
+ sum = 0
1561
+ for i in range(n):
1562
+ sum += (a[i] // mid)
1563
+ if (sum > s):
1564
+ low = mid + 1
1565
+ else:
1566
+ ans = min(ans, mid)
1567
+ high = mid - 1
1568
+ return ans
1569
+
1570
+
1571
+ a = [10, 7, 8, 10, 12, 19]
1572
+ n = len(a)
1573
+ s = 27
1574
+ print(find_minimum_k(a, n, s))
1575
+
1576
+ def max_sum(arr, n, K):
1577
+ """
1578
+ Find maximum sum taking every Kth element in the array
1579
+ """
1580
+ maximum = -2 ** 32
1581
+ sum = [0] * n
1582
+ for i in range(n - 1, -1, -1):
1583
+ if (i + K < n):
1584
+ sum[i] = sum[i + K] + arr[i]
1585
+ else:
1586
+ sum[i] = arr[i]
1587
+ maximum = max(maximum, sum[i])
1588
+ return maximum
1589
+
1590
+
1591
+ arr = [3, 6, 4, 7, 2]
1592
+ n = len(arr)
1593
+ K = 2
1594
+ print(max_sum(arr, n, K))
1595
+
1596
+ def find_min_difference(arr, n):
1597
+ """
1598
+ Minimize the maximum minimum difference after one removal from array
1599
+ """
1600
+ arr.sort()
1601
+ diff1 = arr[n - 1] - arr[1]
1602
+ diff2 = arr[n - 2] - arr[0]
1603
+ return min(diff1, diff2)
1604
+
1605
+
1606
+ if __name__ == "__main__":
1607
+ arr = [1, 2, 4, 3, 4]
1608
+ n = len(arr)
1609
+ print(find_min_difference(arr, n))
1610
+
1611
+ def col_max_diff(mat):
1612
+ """
1613
+ Find pair with maximum difference in any column of a Matrix
1614
+ """
1615
+ max_diff = 0
1616
+ for i in range(N):
1617
+ max_val = mat[0][i]
1618
+ min_val = mat[0][i]
1619
+ for j in range(1, N):
1620
+ max_val = max(max_val, mat[j][i])
1621
+ min_val = min(min_val, mat[j][i])
1622
+ max_diff = max(max_diff, max_val - min_val)
1623
+ return max_diff
1624
+
1625
+
1626
+ if __name__ == "__main__":
1627
+ mat = [[1, 2, 3, 4, 5], [5, 3, 5, 4, 0], [
1628
+ 5, 6, 7, 8, 9], [0, 6, 3, 4, 12], [9, 7, 12, 4, 3]]
1629
+ print("Max difference :", col_max_diff(mat))
1630
+
1631
+ def search(ar, size):
1632
+ """
1633
+ Find the Missing Number in a sorted array
1634
+ """
1635
+ a = 0
1636
+ b = size - 1
1637
+ mid = 0
1638
+ while b > a + 1:
1639
+ mid = (a + b) // 2
1640
+ if (ar[a] - a) != (ar[mid] - mid):
1641
+ b = mid
1642
+ elif (ar[b] - b) != (ar[mid] - mid):
1643
+ a = mid
1644
+ return ar[a] + 1
1645
+
1646
+
1647
+ a = [1, 2, 3, 4, 5, 6, 8]
1648
+ n = len(a)
1649
+ print("Missing number:", search(a, n))
1650
+
1651
+ def count_triplets_less_than_l(n, L, arr):
1652
+ """
1653
+ Ways to choose three points with distance between the most distant points <= L
1654
+ """
1655
+ arr.sort()
1656
+ ways = 0
1657
+ for i in range(n):
1658
+ for j in range(i + 1, n):
1659
+ for k in range(j + 1, n):
1660
+ mostDistantDistance = arr[k] - arr[i]
1661
+ if (mostDistantDistance <= L):
1662
+ ways += 1
1663
+ return ways
1664
+
1665
+
1666
+ if __name__ == "__main__":
1667
+ arr = [1, 2, 3, 4]
1668
+ n = len(arr)
1669
+ L = 3
1670
+ ans = count_triplets_less_than_l(n, L, arr)
1671
+ print("Total Number of ways =", ans)
1672
+
1673
+ def count_max_set_bits(left, right):
1674
+ """
1675
+ Smallest number whose set bits are maximum in a given range
1676
+ """
1677
+ while (left | (left + 1)) <= right:
1678
+ left |= left + 1
1679
+ return left
1680
+
1681
+
1682
+ l = 1
1683
+ r = 5
1684
+ print(count_max_set_bits(l, r))
1685
+ l = 1
1686
+ r = 10
1687
+ print(count_max_set_bits(l, r))
1688
+
1689
+ def maximum_mex(arr, N):
1690
+ """
1691
+ Rearrange array elements to maximize the sum of MEX of all prefix arrays
1692
+ """
1693
+ ans = []
1694
+ arr = sorted(arr)
1695
+ for i in range(N):
1696
+ if (i == 0 or arr[i] != arr[i - 1]):
1697
+ ans.append(arr[i])
1698
+ for i in range(N):
1699
+ if (i > 0 and arr[i] == arr[i - 1]):
1700
+ ans.append(arr[i])
1701
+ for i in range(N):
1702
+ print(ans[i], end=" ")
1703
+
1704
+
1705
+ if __name__ == '__main__':
1706
+ arr = [1, 0, 0]
1707
+ N = len(arr)
1708
+ maximum_mex(arr, N)
1709
+
1710
+ def getcount(arr, N):
1711
+ """
1712
+ Count triplets from an array which can form quadratic equations with real roots
1713
+ """
1714
+ count = 0
1715
+ if (N < 3):
1716
+ return 0
1717
+ for b in range(0, N):
1718
+ for a in range(0, N):
1719
+ if (a == b):
1720
+ continue
1721
+ for c in range(0, N):
1722
+ if (c == a or c == b):
1723
+ continue
1724
+ d = arr[b] * arr[b] // 4
1725
+ if (arr[a] * arr) <= d:
1726
+ count += 1
1727
+ return count
1728
+
1729
+
1730
+ arr = [1, 2, 3, 4, 5]
1731
+ N = len(arr)
1732
+ print(getcount(arr, N))
1733
+
1734
+ def check(a, b, Na, Nb, k, m):
1735
+ """
1736
+ Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y
1737
+ """
1738
+ if (Na < k or Nb < m):
1739
+ return "No"
1740
+ a.sort()
1741
+ a.sort()
1742
+ if (a[k - 1] < b[Nb - m]):
1743
+ return "Yes"
1744
+ return "No"
1745
+
1746
+
1747
+ arr1 = [1, 2, 3]
1748
+ arr2 = [3, 4, 5]
1749
+ N = len(arr1)
1750
+ M = len(arr2)
1751
+ X = 2
1752
+ Y = 1
1753
+ print(check(arr1, arr2, N, M, X, Y))
1754
+
1755
+ def split_array(arr, N):
1756
+ """
1757
+ Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum
1758
+ """
1759
+ arr = sorted(arr)
1760
+ result = 10 ** 9
1761
+ for i in range(1, N):
1762
+ result = min(result, arr[i] ^ arr[i - 1])
1763
+ return result
1764
+
1765
+
1766
+ if __name__ == '__main__':
1767
+ arr = [3, 1, 2, 6, 4]
1768
+ N = len(arr)
1769
+ print(split_array(arr, N))
1770
+
1771
+ from bisect import bisect_left
1772
+
1773
+
1774
+ def maximum_shops(opening, closing, n, k):
1775
+ """
1776
+ Activity selection problem with K persons
1777
+ """
1778
+ a = [[0, 0]for i in range(n)]
1779
+ for i in range(n):
1780
+ a[i][0] = opening[i]
1781
+ a[i][1] = closing[i]
1782
+ a = sorted(a)
1783
+ count = 1
1784
+ st = {}
1785
+ for i in range(n):
1786
+ flag = False
1787
+ if (len(st) == 0):
1788
+ ar = list(st.keys())
1789
+ it = bisect_left(ar, a[i][0])
1790
+ if (it != 0):
1791
+ it -= 1
1792
+ if (ar[it] <= a[i][0]):
1793
+ del st[it]
1794
+ st[a[i][1]] = 1
1795
+ count += 1
1796
+ flag = True
1797
+ if (len(st) < k and flag == False):
1798
+ st[a[i][1]] = 1
1799
+ count += 1
1800
+ return count
1801
+
1802
+
1803
+ if __name__ == '__main__':
1804
+ S = [1, 8, 3, 2, 6]
1805
+ E = [5, 10, 6, 5, 9]
1806
+ K, N = 2, len(S)
1807
+ print(maximum_shops(S, E, N, K))
1808
+
1809
+ def count_pairs(A, N):
1810
+ """
1811
+ Count pairs with Bitwise XOR greater than both the elements of the pair
1812
+ """
1813
+ count = 0
1814
+ for i in range(0, N):
1815
+ for j in range(i + 1, N):
1816
+ xo = (A[i] ^ A[j])
1817
+ mx = max(A[i], A[j])
1818
+ if (xo > mx):
1819
+ count += 1
1820
+ print(count)
1821
+
1822
+
1823
+ if __name__ == '__main__':
1824
+ arr = [2, 4, 3]
1825
+ N = len(arr)
1826
+ count_pairs(arr, N)
1827
+
1828
+ MOD = 1000000007
1829
+
1830
+
1831
+ def solve(values, salary):
1832
+ """
1833
+ Count ways to distribute exactly one coin to each worker
1834
+ """
1835
+ ret = 1
1836
+ amt = 0
1837
+ values = sorted(values)
1838
+ salary = sorted(salary)
1839
+ while (len(salary) > 0):
1840
+ while ((len(values) and values[-1] >= salary[-1])):
1841
+ amt += 1
1842
+ del values[-1]
1843
+ if (amt == 0):
1844
+ return 0
1845
+ ret *= amt
1846
+ amt -= 1
1847
+ ret %= MOD
1848
+ del salary[-1]
1849
+ return ret
1850
+
1851
+
1852
+ if __name__ == '__main__':
1853
+ values = [1, 2]
1854
+ salary = [2]
1855
+ print(solve(values, salary))
1856
+
1857
+ def count_pairs(arr, n):
1858
+ """
1859
+ Count of index pairs with equal elements in an array
1860
+ """
1861
+ ans = 0
1862
+ arr.sort()
1863
+ left = 0
1864
+ right = 1
1865
+ while (right < n):
1866
+ if (arr[left] == arr[right]):
1867
+ ans += right - left
1868
+ else:
1869
+ left = right
1870
+ right += 1
1871
+ return ans
1872
+
1873
+
1874
+ if __name__ == "__main__":
1875
+ arr = [2, 2, 3, 2, 3]
1876
+ N = len(arr)
1877
+ print(count_pairs(arr, N))
1878
+
1879
+ def calculate_minimum_split(a, k):
1880
+ """
1881
+ Divide a sorted array in K parts with sum of difference of max and min minimized in each part
1882
+ """
1883
+ p = []
1884
+ n = len(a)
1885
+ for i in range(1, n):
1886
+ p.append(a[i] - a[i - 1])
1887
+ p.sort(reverse=True)
1888
+ min_sum = sum(p[:k - 1])
1889
+ res = a[n - 1] - a[0] - min_sum
1890
+ return res
1891
+
1892
+
1893
+ /*Driver code * /
1894
+ if __name__ == "__main__":
1895
+ arr = [4, 8, 15, 16, 23, 42]
1896
+ K = 3
1897
+ print(calculate_minimum_split(arr, K))
1898
+
1899
+ import numpy as np
1900
+ N = 100
1901
+ INF = 1000000
1902
+ dp = np.zeros((N, N))
1903
+ vis = np.zeros((N, N))
1904
+
1905
+
1906
+ def find_sum(arr, n, k, l, r):
1907
+ """
1908
+ Minimize the sum of differences of consecutive elements after removing exactly K elements
1909
+ """
1910
+ if ((l) + (n - 1 - r) == k):
1911
+ return arr[r] - arr[l]
1912
+ if (vis[l][r]):
1913
+ return dp[l][r]
1914
+ vis[l][r] = 1
1915
+ dp[l][r] = min(find_sum(arr, n, k, l, r - 1), find_sum(arr, n, k, l + 1, r))
1916
+ return dp[l][r]
1917
+
1918
+
1919
+ if __name__ == "__main__":
1920
+ arr = [1, 2, 100, 120, 140]
1921
+ k = 2
1922
+ n = len(arr)
1923
+ print(find_sum(arr, n, k, 0, n - 1))
1924
+
1925
+ def get_number(n, k):
1926
+ """
1927
+ Find Kth element in an array containing odd elements first and then even elements
1928
+ """
1929
+ if (n % 2 == 0):
1930
+ pos = n // 2
1931
+ else:
1932
+ pos = (n // 2) + 1
1933
+ if (k <= pos):
1934
+ return (k * 2 - 1)
1935
+ else:
1936
+ return ((k - pos) * 2)
1937
+
1938
+
1939
+ if __name__ == "__main__":
1940
+ n = 8
1941
+ k = 5
1942
+ print(get_number(n, k))
1943
+
1944
+ def count_points(n, m, a, b, x, y):
1945
+ """
1946
+ Maximum number of segments that can contain the given points
1947
+ """
1948
+ a.sort()
1949
+ b.sort()
1950
+ j, count = 0, 0
1951
+ for i in range(0, n):
1952
+ while j < m:
1953
+ if a[i] + y < b[j]:
1954
+ break
1955
+ if (b[j] >= a[i] - x and b[j] <= a[i] + y):
1956
+ count += 1
1957
+ j += 1
1958
+ break
1959
+ else:
1960
+ j += 1
1961
+ return count
1962
+
1963
+
1964
+ if __name__ == "__main__":
1965
+ x, y = 1, 4
1966
+ a = [1, 5]
1967
+ n = len(a)
1968
+ b = [1, 1, 2]
1969
+ m = len(b)
1970
+ print(count_points(n, m, a, b, x, y))
1971
+
1972
+ def min_operations(n, m, k, matrix):
1973
+ """
1974
+ Minimum operations of given type to make all elements of a matrix equal
1975
+ """
1976
+ arr = []
1977
+ if (matrix[0][0] < 0):
1978
+ mod = k - (abs(matrix[0][0]) % k)
1979
+ else:
1980
+ mod = matrix[0][0] % k
1981
+ for i in range(n):
1982
+ for j in range(m):
1983
+ arr.append(matrix[i][j])
1984
+ val = matrix[i][j]
1985
+ if (val < 0):
1986
+ res = k - (abs(val) % k)
1987
+ if (res != mod):
1988
+ return -1
1989
+ else:
1990
+ foo = matrix[i][j]
1991
+ if (foo % k != mod):
1992
+ return -1
1993
+ arr.sort()
1994
+ median = arr[(n * m) // 2]
1995
+ min_operations = 0
1996
+ for i in range(n * m):
1997
+ min_operations += abs(arr[i] - median) // k
1998
+ if ((n * m) % 2 == 0):
1999
+ median2 = arr[((n * m) // 2) - 1]
2000
+ min_operations2 = 0
2001
+ for i in range(n * m):
2002
+ min_operations2 += abs(arr[i] - median2) / k
2003
+ min_operations = min(min_operations, min_operations2)
2004
+ return min_operations
2005
+
2006
+
2007
+ if __name__ == "__main__":
2008
+ matrix = [[2, 4, 6], [8, 10, 12], [14, 16, 18], [20, 22, 24]]
2009
+ n = len(matrix)
2010
+ m = len(matrix[0])
2011
+ k = 2
2012
+ print(min_operations(n, m, k, matrix))
2013
+
2014
+ def count_sequences(arr, n):
2015
+ """
2016
+ Minimum number of consecutive sequences that can be formed in an array
2017
+ """
2018
+ count = 1
2019
+ arr.sort()
2020
+ for i in range(n - 1):
2021
+ if (arr[i] + 1 != arr[i + 1]):
2022
+ count += 1
2023
+ return count
2024
+
2025
+
2026
+ if __name__ == "__main__":
2027
+ arr = [1, 7, 3, 5, 10]
2028
+ n = len(arr)
2029
+ print(count_sequences(arr, n))
2030
+
2031
+ def minimum_moves(a, n):
2032
+ """
2033
+ Minimum number of increment / decrement operations such that array contains all elements from 1 to N
2034
+ """
2035
+ operations = 0
2036
+ a.sort(reverse=False)
2037
+ for i in range(0, n, 1):
2038
+ operations = operations + abs(a[i] - (i + 1))
2039
+ return operations
2040
+
2041
+
2042
+ if __name__ == '__main__':
2043
+ arr = [5, 3, 2]
2044
+ n = len(arr)
2045
+ print(minimum_moves(arr, n))
2046
+
2047
+ def max_array_cover(a, n, x):
2048
+ """
2049
+ Covering maximum array elements with given value
2050
+ """
2051
+ a.sort()
2052
+ cc = 0
2053
+ s = 0
2054
+ for i in range(n):
2055
+ s += a[i]
2056
+ if (s > x):
2057
+ break
2058
+ cc += 1
2059
+ if (sum(a) == x):
2060
+ return n
2061
+ else:
2062
+ if (cc == n):
2063
+ return n - 1
2064
+ else:
2065
+ return cc
2066
+
2067
+
2068
+ if __name__ == '__main__':
2069
+ n, x = 3, 70
2070
+ a = [10, 20, 30]
2071
+ print(max_array_cover(a, n, x))
2072
+
2073
+ def maximum_sop(a, b):
2074
+ """
2075
+ Maximum Sum of Products of Two Arrays
2076
+ """
2077
+ sop = 0
2078
+ n = len(a)
2079
+ a.sort()
2080
+ b.sort()
2081
+ for i in range(n):
2082
+ sop += a[i] * b[i]
2083
+ return sop
2084
+
2085
+
2086
+ if __name__ == "__main__":
2087
+ A = [1, 2, 3]
2088
+ B = [4, 5, 1]
2089
+ print(maximum_sop(A, B))
2090
+
2091
+ def count_triplets(arr, n, m):
2092
+ """
2093
+ Count number of triplets with product equal to given number
2094
+ """
2095
+ count = 0
2096
+ arr.sort()
2097
+ for end in range(n - 1, 1, -1):
2098
+ start = 0
2099
+ mid = end - 1
2100
+ while (start < mid):
2101
+ prod = (arr[end] * arr[start] * arr[mid])
2102
+ if (prod > m):
2103
+ mid -= 1
2104
+ elif (prod < m):
2105
+ start += 1
2106
+ elif (prod == m):
2107
+ count += 1
2108
+ mid -= 1
2109
+ start += 1
2110
+ return count
2111
+
2112
+
2113
+ if __name__ == "__main__":
2114
+ arr = [1, 1, 1, 1, 1, 1]
2115
+ n = len(arr)
2116
+ m = 1
2117
+ print(count_triplets(arr, n, m))
2118
+
2119
+ def distribution(arr, n):
2120
+ """
2121
+ Equally divide into two sets such that one set has maximum distinct elements
2122
+ """
2123
+ resources = set()
2124
+ for i in range(n):
2125
+ resources.add(arr[i])
2126
+ return min(len(resources), n // 2)
2127
+
2128
+
2129
+ if __name__ == '__main__':
2130
+ arr = [1, 1, 2, 1, 3, 4]
2131
+ n = len(arr)
2132
+ print(distribution(arr, n), "")
2133
+
2134
+ def check_fitting_arrays(A, B, N):
2135
+ """
2136
+ Check whether an array can be fit into another array rearranging the elements in the array
2137
+ """
2138
+ A = sorted(A)
2139
+ B = sorted(B)
2140
+ for i in range(N):
2141
+ if (A[i] > B[i]):
2142
+ return False
2143
+ return True
2144
+
2145
+
2146
+ A = [7, 5, 3, 2]
2147
+ B = [5, 4, 8, 7]
2148
+ N = len(A)
2149
+ if (check_fitting_arrays(A, B, N)):
2150
+ print("YES")
2151
+ else:
2152
+ print("NO")
2153
+
2154
+ def maximum_toys(cost, N, K):
2155
+ """
2156
+ Maximise the number of toys that can be purchased with amount K
2157
+ """
2158
+ count = 0
2159
+ sum = 0
2160
+ cost.sort(reverse=False)
2161
+ for i in range(0, N, 1):
2162
+ if (sum + cost[i] <= K):
2163
+ sum = sum + cost[i]
2164
+ count += 1
2165
+ return count
2166
+
2167
+
2168
+ if __name__ == '__main__':
2169
+ K = 50
2170
+ cost = [1, 12, 5, 111, 200, 1000, 10, 9, 12, 15]
2171
+ N = len(cost)
2172
+ print(maximum_toys(cost, N, K))
2173
+
2174
+ def almost_sort(A, n):
2175
+ """
2176
+ Check if given array is almost sorted ( elements are at
2177
+ """
2178
+ i = 0
2179
+ while i < n - 1:
2180
+ if A[i] > A[i + 1]:
2181
+ A[i], A[i + 1] = A[i + 1], A[i]
2182
+ i += 1
2183
+ i += 1
2184
+ for i in range(0, n - 1):
2185
+ if A[i] > A[i + 1]:
2186
+ return False
2187
+ return True
2188
+
2189
+
2190
+ if __name__ == "__main__":
2191
+ A = [1, 3, 2, 4, 6, 5]
2192
+ n = len(A)
2193
+ if almost_sort(A, n):
2194
+ print("Yes")
2195
+ else:
2196
+ print("No")
2197
+
2198
+ def distinct_count(arr, n):
2199
+ """
2200
+ Absolute distinct count in a sorted array
2201
+ """
2202
+ s = set()
2203
+ for i in range(n):
2204
+ s.add(abs(arr[i]))
2205
+ return len(s)
2206
+
2207
+
2208
+ arr = [-2, -1, 0, 1, 1]
2209
+ n = len(arr)
2210
+ print("Count of absolute distinct values:", distinct_count(arr, n))
2211
+
2212
+ def gen_string(N):
2213
+ """
2214
+ Lexicographically smallest numeric string having odd digit counts
2215
+ """
2216
+ ans = ""
2217
+ if (N % 2 == 0):
2218
+ ans = "".join("1"for i in range(N - 1))
2219
+ ans = ans + "2"
2220
+ else:
2221
+ ans = "".join("1"for i in range(N))
2222
+ return ans
2223
+
2224
+
2225
+ if __name__ == "__main__":
2226
+ N = 5
2227
+ print(gen_string(N))
2228
+
2229
+ def find(arr, N):
2230
+ """
2231
+ Minimize moves required to make array elements equal by incrementing and decrementing pairs
2232
+ """
2233
+ Sum = sum(arr)
2234
+ if Sum % N:
2235
+ return -1
2236
+ else:
2237
+ k = Sum // N
2238
+ ans = 0
2239
+ i = 0
2240
+ while i < N:
2241
+ ans = ans + abs(k - arr[i])
2242
+ i += 1
2243
+ return ans // 2
2244
+
2245
+
2246
+ if __name__ == '__main__':
2247
+ arr = [5, 4, 1, 10]
2248
+ N = len(arr)
2249
+ print(find(arr, N))
2250
+
2251
+ def minimum_steps(a, b):
2252
+ """
2253
+ Convert A into B by incrementing or decrementing 1 , 2 , or 5 any number of times
2254
+ """
2255
+ cnt = 0
2256
+ a = abs(a - b)
2257
+ cnt = (a // 5) + (a % 5) // 2 + (a % 5) % 2
2258
+ return cnt
2259
+
2260
+
2261
+ A = 3
2262
+ B = 9
2263
+ print(minimum_steps(A, B))
2264
+
2265
+ def smallest_non_subsequence(S, N):
2266
+ """
2267
+ Lexicographically smallest string which is not a subsequence of given string
2268
+ """
2269
+ freq = 0
2270
+ for i in range(N):
2271
+ if (S[i] == 'a'):
2272
+ freq += 1
2273
+ ans = ""
2274
+ for i in range(freq):
2275
+ ans += 'a'
2276
+ if (freq == N):
2277
+ ans = ans.replace(ans[freq - 1], 'b')
2278
+ else:
2279
+ ans += 'a'
2280
+ return ans
2281
+
2282
+
2283
+ if __name__ == '__main__':
2284
+ S = "abcdefghijklmnopqrstuvwxyz"
2285
+ N = len(S)
2286
+ print(smallest_non_subsequence(S, N))
2287
+
2288
+ from collections import defaultdict
2289
+
2290
+
2291
+ def least_bricks(wall):
2292
+ """
2293
+ Minimum number of bricks that can be intersected
2294
+ """
2295
+ map = defaultdict(int)
2296
+ res = 0
2297
+ for list in wall:
2298
+ width = 0
2299
+ for i in range(len(list) - 1):
2300
+ width += list[i]
2301
+ map[width] += 1
2302
+ res = max(res, map[width])
2303
+ print(len(wall) - res)
2304
+
2305
+
2306
+ if __name__ == "__main__":
2307
+ arr = [[1, 2, 2, 1], [3, 1, 2], [1, 3, 2], [2, 4], [3, 1, 2], [1, 3, 1, 1]]
2308
+ least_bricks(arr)
2309
+
2310
+ def number_of_ways(N, X, Y):
2311
+ """
2312
+ Maximize count of distinct profits possible by N transactions
2313
+ """
2314
+ S1 = (N - 1) * X + Y
2315
+ S2 = (N - 1) * Y + X
2316
+ return (S2 - S1 + 1)
2317
+
2318
+
2319
+ if __name__ == '__main__':
2320
+ N = 3
2321
+ X = 13
2322
+ Y = 15
2323
+ print(number_of_ways(N, X, Y))
2324
+
2325
+ def minimum_operations(A, N, K):
2326
+ """
2327
+ Minimum number of operations required to make a permutation of first N natural numbers equal
2328
+ """
2329
+ Count = 0
2330
+ i = 0
2331
+ while (i < N - 1):
2332
+ i = i + K - 1
2333
+ Count += 1
2334
+ return Count
2335
+
2336
+
2337
+ if __name__ == '__main__':
2338
+ A = [5, 4, 3, 1, 2]
2339
+ K = 3
2340
+ N = len(A)
2341
+ print(minimum_operations(A, N, K))
2342
+
2343
+ from collections import Counter
2344
+
2345
+
2346
+ def construct_digits(s):
2347
+ """
2348
+ Reorder characters of a string to valid English representations of digits
2349
+ """
2350
+ k = ["z", "w", "u", "x", "g", "h", "o", "f", "v", "i"]
2351
+ l = [
2352
+ "zero",
2353
+ "two",
2354
+ "four",
2355
+ "six",
2356
+ "eight",
2357
+ "three",
2358
+ "one",
2359
+ "five",
2360
+ "seven",
2361
+ "nine"]
2362
+ c = [0, 2, 4, 6, 8, 3, 1, 5, 7, 9]
2363
+ ans = []
2364
+ d = Counter(s)
2365
+ for i in range(len(k)):
2366
+ x = d.get(k[i], 0)
2367
+ for j in range(len(l[i])):
2368
+ d[l[i][j]] -= x
2369
+ ans.append(str(c[i]) * x)
2370
+ ans.sort()
2371
+ return "".join(ans)
2372
+
2373
+
2374
+ s = "fviefuro"
2375
+ print(construct_digits(s))
2376
+
2377
+ def dist_integers(L, R):
2378
+ """
2379
+ Count distinct sum of pairs possible from a given range
2380
+ """
2381
+ return 2 * R - 2 * L + 1
2382
+
2383
+
2384
+ if __name__ == '__main__':
2385
+ L, R = 3, 8
2386
+ print(dist_integers(L, R))
2387
+
2388
+ from math import sqrt
2389
+
2390
+
2391
+ def count_occurrences(N, X):
2392
+ """
2393
+ Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices
2394
+ """
2395
+ count = 0
2396
+ for i in range(1, int(sqrt(X)) + 1):
2397
+ if X % i == 0:
2398
+ a = i
2399
+ b = X // i
2400
+ if a <= N and b <= N:
2401
+ if a == b:
2402
+ count += 1
2403
+ else:
2404
+ count += 2
2405
+ return count
2406
+
2407
+
2408
+ if __name__ == '__main__':
2409
+ N = 7
2410
+ X = 12
2411
+ print(count_occurrences(N, X))
2412
+
2413
+ def count_pairs(L, R):
2414
+ """
2415
+ Count pairs having distinct sum from a given range
2416
+ """
2417
+ firstNum = 2 * L
2418
+ lastNum = 2 * R
2419
+ Cntpairs = lastNum - firstNum + 1
2420
+ print(Cntpairs)
2421
+
2422
+
2423
+ if __name__ == '__main__':
2424
+ L, R = 2, 3
2425
+ count_pairs(L, R)
2426
+
2427
+ from collections import defaultdict
2428
+
2429
+
2430
+ def count_pairs(A, n):
2431
+ """
2432
+ Count pairs from an array whose Bitwise OR is greater than Bitwise AND
2433
+ """
2434
+ count = (n * (n - 1)) // 2
2435
+ ump = defaultdict(int)
2436
+ for i in range(n):
2437
+ ump[A[i]] += 1
2438
+ for it in ump.keys():
2439
+ c = ump[it]
2440
+ count = count - (c * (c - 1)) // 2
2441
+ print(count)
2442
+
2443
+
2444
+ if __name__ == "__main__":
2445
+ A = [1, 4, 7]
2446
+ N = len(A)
2447
+ count_pairs(A, N)
2448
+
2449
+ def check(current_row, current_col, destination_row, destination_col):
2450
+ """
2451
+ Check if a Rook can reach the given destination in a single move
2452
+ """
2453
+ if (current_row == destination_row):
2454
+ return ("POSSIBLE")
2455
+ elif (current_col == destination_col):
2456
+ return ("POSSIBLE")
2457
+ else:
2458
+ return ("NOT POSSIBLE")
2459
+
2460
+
2461
+ current_row = 8
2462
+ current_col = 8
2463
+ destination_row = 8
2464
+ destination_col = 4
2465
+ output = check(current_row, current_col, destination_row, destination_col)
2466
+ print(output)
2467
+
2468
+ def minimum_moves(arr, N):
2469
+ """
2470
+ Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal
2471
+ """
2472
+ sum = 0
2473
+ maxelement = -1
2474
+ if (N == 2):
2475
+ print(0, end="")
2476
+ for i in range(N):
2477
+ sum += arr[i]
2478
+ maxelement = max(maxelement, arr[i])
2479
+ K = (sum + N - 2) // (N - 1)
2480
+ K = max(maxelement, K)
2481
+ ans = K * (N - 1) - sum
2482
+ print(ans)
2483
+
2484
+
2485
+ if __name__ == '__main__':
2486
+ arr = [2, 3, 7]
2487
+ N = 3
2488
+ minimum_moves(arr, N)
2489
+
2490
+ def max_sum_of_squares(N, S):
2491
+ """
2492
+ Maximum possible sum of squares of stack elements satisfying the given properties
2493
+ """
2494
+ res = 0
2495
+ if (S < N or S > 9 * N):
2496
+ cout << -1
2497
+ return
2498
+ S = S - N
2499
+ c = 0
2500
+ while (S > 0):
2501
+ c += 1
2502
+ if (S // 8 > 0):
2503
+ res += 9 * 9
2504
+ S -= 8
2505
+ else:
2506
+ res += (S + 1) * (S + 1)
2507
+ break
2508
+ res = res + (N - c)
2509
+ print(res)
2510
+
2511
+
2512
+ if __name__ == '__main__':
2513
+ N = 3
2514
+ S = 12
2515
+ max_sum_of_squares(N, S)
2516
+
2517
+ def clear_last_bit(N, K):
2518
+ """
2519
+ Unset least significant K bits of a given number
2520
+ """
2521
+ mask = (-1 << K + 1)
2522
+ N = N & mask
2523
+ return N
2524
+
2525
+
2526
+ N = 730
2527
+ K = 3
2528
+ print(clear_last_bit(N, K))
2529
+
2530
+ def areaof_rectangle(L, W):
2531
+ """
2532
+ Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
2533
+ """
2534
+ area = (W + L) * (W + L) / 2
2535
+ return area
2536
+
2537
+
2538
+ if __name__ == "__main__":
2539
+ L = 18
2540
+ W = 12
2541
+ print(areaof_rectangle(L, W))
2542
+
2543
+ def down_to_zero(n):
2544
+ """
2545
+ Minimum number of operations required to reduce N to 0
2546
+ """
2547
+ if (n <= 3):
2548
+ return n
2549
+ if (n % 2 == 0):
2550
+ return 3
2551
+ else:
2552
+ return 4
2553
+
2554
+
2555
+ if __name__ == '__main__':
2556
+ n = 4
2557
+ print(down_to_zero(n))
2558
+
2559
+ def minimumrequired(A, N):
2560
+ """
2561
+ Minimum count of elements required to obtain the given Array by repeated mirror operations
2562
+ """
2563
+ K = N
2564
+ while (K > 0):
2565
+ if (K % 2) == 1:
2566
+ ans = K
2567
+ break
2568
+ ispalindrome = 1
2569
+ for i in range(0, K // 2):
2570
+ if (A[i] != A[K - 1 - i]):
2571
+ ispalindrome = 0
2572
+ if (ispalindrome == 1):
2573
+ ans = K // 2
2574
+ K = K // 2
2575
+ else:
2576
+ ans = K
2577
+ break
2578
+ return ans
2579
+
2580
+
2581
+ A = [1, 2, 2, 1, 1, 2, 2, 1]
2582
+ N = len(A)
2583
+ print(minimumrequired(A, N))
2584
+
2585
+ def sum_of_factors(N):
2586
+ """
2587
+ Sum of product of all integers upto N with their count of divisors
2588
+ """
2589
+ ans = 0
2590
+ for i in range(1, N + 1):
2591
+ first = i
2592
+ last = (N // i) * i
2593
+ factors = (last - first) // i + 1
2594
+ totalContribution = (((factors * (factors + 1)) // 2) * i)
2595
+ ans += totalContribution
2596
+ return ans
2597
+
2598
+
2599
+ N = 3
2600
+ print(sum_of_factors(N))
2601
+
2602
+ def get_max_difference(N):
2603
+ """
2604
+ Find a number M < N such that difference between their XOR and AND is maximum
2605
+ """
2606
+ M = -1
2607
+ maxDiff = 0
2608
+ for i in range(N):
2609
+ diff = (N ^ i) - (N & i)
2610
+ if (diff >= maxDiff):
2611
+ maxDiff = diff
2612
+ M = i
2613
+ return M
2614
+
2615
+
2616
+ if __name__ == '__main__':
2617
+ N = 6
2618
+ print(get_max_difference(N))
2619
+
2620
+ def count_substrings(s):
2621
+ """
2622
+ Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
2623
+ """
2624
+ n = len(s)
2625
+ answer = (n * (n - 1)) // 2
2626
+ cnt = 1
2627
+ v = []
2628
+ for i in range(1, n):
2629
+ if (s[i] == s[i - 1]):
2630
+ cnt += 1
2631
+ else:
2632
+ v.append(cnt)
2633
+ cnt = 1
2634
+ if (cnt > 0):
2635
+ v.append(cnt)
2636
+ for i in range(len(v) - 1):
2637
+ answer -= (v[i] + v[i + 1] - 1)
2638
+ return answer
2639
+
2640
+
2641
+ if __name__ == '__main__':
2642
+ s = "00111"
2643
+ print(count_substrings(s))
2644
+
2645
+ def max_modulosum(a, n):
2646
+ """
2647
+ Maximize the sum of modulus with every Array element
2648
+ """
2649
+ sum1 = 0
2650
+ for i in range(0, n):
2651
+ sum1 += a[i]
2652
+ return sum1 - n
2653
+
2654
+
2655
+ a = [3, 4, 6]
2656
+ n = len(a)
2657
+ print(max_modulosum(a, n))
2658
+
2659
+ def min_occupied_position(A, n):
2660
+ """
2661
+ Minimize the non
2662
+ """
2663
+ minPos = 0
2664
+ i = 0
2665
+ while i < n:
2666
+ if (A[i] > 0):
2667
+ minPos += 1
2668
+ i += 2
2669
+ i += 1
2670
+ return minPos
2671
+
2672
+
2673
+ if __name__ == '__main__':
2674
+ A = [8, 0, 7, 0, 0, 6]
2675
+ n = len(A)
2676
+ print(min_occupied_position(A, n))
2677
+
2678
+ def largest_num(n):
2679
+ """
2680
+ Find the largest number smaller than integer N with maximum number of set bits
2681
+ """
2682
+ num = 0
2683
+ for i in range(32):
2684
+ x = (1 << i)
2685
+ if ((x - 1) <= n):
2686
+ num = (1 << i) - 1
2687
+ else:
2688
+ break
2689
+ return num
2690
+
2691
+
2692
+ if __name__ == "__main__":
2693
+ N = 345
2694
+ print(largest_num(N))
2695
+
2696
+ def segments(n):
2697
+ """
2698
+ Maximum number on 7
2699
+ """
2700
+ if (n == 1 or n == 0):
2701
+ return
2702
+ if (n % 2 == 0):
2703
+ print("1", end="")
2704
+ segments(n - 2)
2705
+ elif (n % 2 == 1):
2706
+ print("7", end="")
2707
+ segments(n - 3)
2708
+
2709
+
2710
+ if __name__ == "__main__":
2711
+ n = 11
2712
+ segments(n)
2713
+
2714
+ MAX_SIZE = 10
2715
+
2716
+
2717
+ def convolution(x, h, n, m):
2718
+ """
2719
+ Circular Convolution using Matrix Method
2720
+ """
2721
+ row_vec = [0] * MAX_SIZE
2722
+ col_vec = [0] * MAX_SIZE
2723
+ out = [0] * MAX_SIZE
2724
+ circular_shift_mat = [[0 for i in range(MAX_SIZE)]for j in range(MAX_SIZE)]
2725
+ if (n > m):
2726
+ maxSize = n
2727
+ else:
2728
+ maxSize = m
2729
+ for i in range(maxSize):
2730
+ if (i >= n):
2731
+ row_vec[i] = 0
2732
+ else:
2733
+ row_vec[i] = x[i]
2734
+ for i in range(maxSize):
2735
+ if (i >= m):
2736
+ col_vec[i] = 0
2737
+ else:
2738
+ col_vec[i] = h[i]
2739
+ k = 0
2740
+ d = 0
2741
+ for i in range(maxSize):
2742
+ curIndex = k - d
2743
+ for j in range(maxSize):
2744
+ circular_shift_mat[j][i] = row_vec[curIndex % maxSize]
2745
+ curIndex += 1
2746
+ k = maxSize
2747
+ d += 1
2748
+ for i in range(maxSize):
2749
+ for j in range(maxSize):
2750
+ out[i] += circular_shift_mat[i][j] * col_vec[j]
2751
+ print(out[i], end=" ")
2752
+
2753
+
2754
+ if __name__ == '__main__':
2755
+ x = [5, 7, 3, 2]
2756
+ n = len(x)
2757
+ h = [1, 5]
2758
+ m = len(h)
2759
+ convolution(x, h, n, m)
2760
+
2761
+ def get_maximum(s, a):
2762
+ """
2763
+ Maximize the given number by replacing a segment of digits with the alternate digits given
2764
+ """
2765
+ s = list(s)
2766
+ n = len(s)
2767
+ for i in range(n):
2768
+ if (ord(s[i]) - ord('0') < a[ord(s[i]) - ord('0')]):
2769
+ j = i
2770
+ while (j < n and (ord(s[j]) - ord('0')
2771
+ <= a[ord(s[j]) - ord('0')])):
2772
+ s[j] = chr(ord('0') + a[ord(s[j]) - ord('0')])
2773
+ j += 1
2774
+ return "".join(s)
2775
+ return s
2776
+
2777
+
2778
+ if __name__ == "__main__":
2779
+ s = "1337"
2780
+ a = [0, 1, 2, 5, 4, 6, 6, 3, 1, 9]
2781
+ print(get_maximum(s, a))
2782
+
2783
+ from math import sqrt
2784
+
2785
+
2786
+ def count_steps(n):
2787
+ """
2788
+ Number of times the largest perfect square number can be subtracted from N
2789
+ """
2790
+ steps = 0
2791
+ while (n):
2792
+ largest = int(sqrt(n))
2793
+ n -= (largest * largest)
2794
+ steps += 1
2795
+ return steps
2796
+
2797
+
2798
+ if __name__ == "__main__":
2799
+ n = 85
2800
+ print(count_steps(n))
2801
+
2802
+ def maxsum(c1, c2, c3, c4):
2803
+ """
2804
+ Given count of digits 1 , 2 , 3 , 4 , find the maximum sum possible
2805
+ """
2806
+ sum = 0
2807
+ two34 = min(c2, min(c3, c4))
2808
+ sum = two34 * 234
2809
+ c2 -= two34
2810
+ sum += min(c2, c1) * 12
2811
+ return sum
2812
+
2813
+
2814
+ c1 = 5
2815
+ c2 = 2
2816
+ c3 = 3
2817
+ c4 = 4
2818
+ print(maxsum(c1, c2, c3, c4))
2819
+
2820
+ def find_count_of_pairs(a, b, n):
2821
+ """
2822
+ Count of pairs from 1 to a and 1 to b whose sum is divisible by N
2823
+ """
2824
+ ans = 0
2825
+ ans += n * int(a / n) * int(b / n)
2826
+ ans += int(a / n) * (b % n)
2827
+ ans += (a % n) * int(b / n)
2828
+ ans += int(((a % n) + (b % n)) / n)
2829
+ return ans
2830
+
2831
+
2832
+ if __name__ == '__main__':
2833
+ a = 5
2834
+ b = 13
2835
+ n = 3
2836
+ print(find_count_of_pairs(a, b, n))
2837
+
2838
+ def count_minimum_operations(n):
2839
+ """
2840
+ Minimum number of operations required to reduce N to 1
2841
+ """
2842
+ count = 0
2843
+ while (n > 1):
2844
+ if (n % 3 == 0):
2845
+ n //= 3
2846
+ elif (n % 3 == 1):
2847
+ n -= 1
2848
+ else:
2849
+ if (n == 2):
2850
+ n -= 1
2851
+ else:
2852
+ n += 1
2853
+ count += 1
2854
+ return count
2855
+
2856
+
2857
+ if __name__ == "__main__":
2858
+ n = 4
2859
+ ans = count_minimum_operations(n)
2860
+ print(ans)
2861
+
2862
+ def count_minimum_operations(n):
2863
+ """
2864
+ Minimum number of operations required to reduce N to 1
2865
+ """
2866
+ if (n == 2):
2867
+ return 1
2868
+ elif (n == 1):
2869
+ return 0
2870
+ if (n % 3 == 0):
2871
+ return 1 + count_minimum_operations(n / 3)
2872
+ elif (n % 3 == 1):
2873
+ return 1 + count_minimum_operations(n - 1)
2874
+ else:
2875
+ return 1 + count_minimum_operations(n + 1)
2876
+
2877
+
2878
+ n = 4
2879
+ ans = count_minimum_operations(n)
2880
+ print(ans)
2881
+
2882
+ def problems_left(K, P, N):
2883
+ """
2884
+ Problems not solved at the end of Nth day
2885
+ """
2886
+ if (K <= P):
2887
+ return 0
2888
+ else:
2889
+ return ((K - P) * N)
2890
+
2891
+
2892
+ K, P, N = 4, 1, 10
2893
+ print(problems_left(K, P, N))
2894
+
2895
+ def results(n, k):
2896
+ """
2897
+ Number of chocolates left after k iterations
2898
+ """
2899
+ return round(pow(n, (1.0 / pow(2, k))))
2900
+
2901
+
2902
+ k = 3
2903
+ n = 100000000
2904
+ print("Chocolates left after"),
2905
+ print(k),
2906
+ print("iterations are"),
2907
+ print(int(results(n, k)))
2908
+
2909
+ def max_sum_wo3_consec(A, N):
2910
+ """
2911
+ Maximum subsequence sum such that no three are consecutive in O ( 1 ) space
2912
+ """
2913
+ if (N == 1):
2914
+ return A[0]
2915
+ if (N == 2):
2916
+ return A[0] + A[1]
2917
+ third = A[0]
2918
+ second = third + A[1]
2919
+ first = max(second, A[1] + A[2])
2920
+ sum = max(max(third, second), first)
2921
+ for i in range(3, N, 1):
2922
+ sum = max(max(first, second + A[i]), third + A[i] + A[i - 1])
2923
+ third = second
2924
+ second = first
2925
+ first = sum
2926
+ return sum
2927
+
2928
+
2929
+ if __name__ == '__main__':
2930
+ A = [3000, 2000, 1000, 3, 10]
2931
+ N = len(A)
2932
+ print(max_sum_wo3_consec(A, N))
2933
+
2934
+ def find_min_operations(n):
2935
+ """
2936
+ Minimum number of given operations required to reduce a number to 2
2937
+ """
2938
+ dp = [0 for i in range(n + 1)]
2939
+ for i in range(n + 1):
2940
+ dp[i] = 999999
2941
+ dp[2] = 0
2942
+ for i in range(2, n + 1):
2943
+ if (i * 5 <= n):
2944
+ dp[i * 5] = min(dp[i * 5], dp[i] + 1)
2945
+ if (i + 3 <= n):
2946
+ dp[i + 3] = min(dp[i + 3], dp[i] + 1)
2947
+ return dp[n]
2948
+
2949
+
2950
+ if __name__ == '__main__':
2951
+ n = 28
2952
+ m = find_min_operations(n)
2953
+ if (m != 9999):
2954
+ print(m)
2955
+ else:
2956
+ print(-1)
2957
+
2958
+ def get_value(arr, N):
2959
+ """
2960
+ Split array into subarrays such that sum of difference between their maximums and minimums is maximum
2961
+ """
2962
+ dp = [0 for i in range(N)]
2963
+ for i in range(1, N):
2964
+ minn = arr[i]
2965
+ maxx = arr[i]
2966
+ j = i
2967
+ while (j >= 0):
2968
+ minn = min(arr[j], minn)
2969
+ maxx = max(arr[j], maxx)
2970
+ dp[i] = max(dp[i], maxx - minn + (dp[j - 1]if (j >= 1)else 0))
2971
+ j -= 1
2972
+ return dp[N - 1]
2973
+
2974
+
2975
+ if __name__ == '__main__':
2976
+ arr = [8, 1, 7, 9, 2]
2977
+ N = len(arr)
2978
+ print(get_value(arr, N))
2979
+
2980
+ dp = dict()
2981
+
2982
+
2983
+ def max_score(s, a):
2984
+ """
2985
+ Maximum score possible by removing substrings made up of single distinct character
2986
+ """
2987
+ if s in dp:
2988
+ return dp[s]
2989
+ n = len(s)
2990
+ if n == 0:
2991
+ return 0
2992
+ if n == 1:
2993
+ return a[0]
2994
+ head = 0
2995
+ mx = -1
2996
+ while head < n:
2997
+ tail = head
2998
+ while tail < n:
2999
+ if s[tail] != s[head]:
3000
+ head = tail
3001
+ break
3002
+ sub = s[head:tail + 1]
3003
+ mx = max(mx, a[len(sub) - 1] +
3004
+ max_score(s[:head] + s[tail + 1:], a))
3005
+ tail += 1
3006
+ if tail == n:
3007
+ break
3008
+ dp[s] = mx
3009
+ return mx
3010
+
3011
+
3012
+ if __name__ == "__main__":
3013
+ s = "abb"
3014
+ a = [1, 3, 1]
3015
+ print(max_score(s, a))
3016
+
3017
+ def min_cost(costs, N):
3018
+ """
3019
+ Minimize cost of painting N houses such that adjacent houses have different colors
3020
+ """
3021
+ if (N == 0):
3022
+ return 0
3023
+ dp = [[0 for i in range(3)]for j in range(3)]
3024
+ dp[0][0] = costs[0][0]
3025
+ dp[0][1] = costs[0][1]
3026
+ dp[0][2] = costs[0][2]
3027
+ for i in range(1, N, 1):
3028
+ dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]) + costs[i][0]
3029
+ dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + costs[i][1]
3030
+ dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + costs[i][2]
3031
+ print(min(dp[N - 1][0], min(dp[N - 1][1], dp[N - 1][2])))
3032
+
3033
+
3034
+ if __name__ == '__main__':
3035
+ costs = [[14, 2, 11], [11, 14, 5], [14, 3, 10]]
3036
+ N = len(costs)
3037
+ min_cost(costs, N)
3038
+
3039
+ def maximum_sum(A, B, length, X, Y):
3040
+ """
3041
+ Maximum sum by picking elements from two arrays in order
3042
+ """
3043
+ l = length
3044
+ l1 = min(length, X)
3045
+ l2 = min(length, Y)
3046
+ dp = [[0 for i in range(l2 + 1)]for i in range(l1 + 1)]
3047
+ dp[0][0] = 0
3048
+ max_sum = -10 * 9
3049
+ for i in range(1, l1 + 1):
3050
+ dp[i][0] = dp[i - 1][0] + A[i - 1]
3051
+ max_sum = max(max_sum, dp[i][0])
3052
+ for i in range(1, l2 + 1):
3053
+ dp[0][i] = dp[0][i - 1] + B[i - 1]
3054
+ max_sum = max(max_sum, dp[0][i])
3055
+ for i in range(1, l1 + 1):
3056
+ for j in range(1, l2 + 1):
3057
+ if (i + j <= l):
3058
+ dp[i][j] = max(dp[i - 1][j] + A[i + j - 1],
3059
+ dp[i][j - 1] + B[i + j - 1])
3060
+ max_sum = max(dp[i][j], max_sum)
3061
+ return max_sum
3062
+
3063
+
3064
+ if __name__ == '__main__':
3065
+ A = [1, 2, 3, 4, 5]
3066
+ B = [5, 4, 3, 2, 1]
3067
+ X = 3
3068
+ Y = 2
3069
+ N = len(A)
3070
+ print(maximum_sum(A, B, N, X, Y))
3071
+
3072
+ def reduce_zero(N):
3073
+ """
3074
+ Min number of operations to reduce N to 0 by subtracting any digits from N
3075
+ """
3076
+ dp = [1e9 for i in range(N + 1)]
3077
+ dp[0] = 0
3078
+ for i in range(N + 1):
3079
+ for c in str(i):
3080
+ dp[i] = min(dp[i], dp[i - (ord(c) - 48)] + 1)
3081
+ return dp[N]
3082
+
3083
+
3084
+ N = 25
3085
+ print(reduce_zero(N))
3086
+
python_code_instructions_filtered_57.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_58.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_59.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_6.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_60.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_61.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_62.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_63.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_64.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_65.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_66.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_67.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_68.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_69.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_7.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_70.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_71.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_72.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_73.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_74.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_75.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_76.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_77.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_78.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_79.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_8.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_80.txt ADDED
The diff for this file is too large to render. See raw diff
 
python_code_instructions_filtered_81.txt ADDED
The diff for this file is too large to render. See raw diff