File tree Expand file tree Collapse file tree 2 files changed +27
-9
lines changed Expand file tree Collapse file tree 2 files changed +27
-9
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ Given an array of size n, find the majority element. The majority element is the element that appears more than floor(n/2) times.
3
+
4
+ You may assume that the array is non-empty and the majority element always exist in the array.
5
+
6
+ Example :
7
+
8
+ Input : [2, 1, 2]
9
+ Return : 2 which occurs 2 times which is greater than 3/2.
10
+ '''
11
+ import math
12
+ def majority (arr ):
13
+ n = len (arr )
14
+ res = 0
15
+ for i in arr :
16
+ if arr .count (i ) > math .floor (n / 2 ):
17
+ res = i
18
+ break
19
+ return res
20
+
21
+ print majority ([2 ,1 ,2 ])
Original file line number Diff line number Diff line change 4
4
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n.
5
5
Both m and n fit in a 32 bit signed integer.
6
6
'''
7
- def gcd (self , A , B ):
8
-
9
- if B > A :
10
-
11
- A , B = B , A
12
- while B :
13
-
14
- A , B = B , A % B
15
- return A
7
+ def gcd (a ,b ):
8
+ if a < b :
9
+ a , b = b , a
10
+ while b :
11
+ a , b = b , a % b
12
+ return a
You can’t perform that action at this time.
0 commit comments