mayukh
BAN USER
Comments (4)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
def maxConsecutive(a):
# a = [1, 94, 93, 1000, 2, 92, 1001]
a.sort()
consecutive = 0
prev = a[0]
m = consecutive
for each in a[1::]:
if each == prev + 1:
consecutive += 1
else:
if consecutive != 0:
if m < consecutive + 1:
m = consecutive + 1
consecutive = 0
prev = each
return m
Comment hidden because of low score. Click to expand.
0
of 0 vote
This can only be done for 2..This is because
n & (n - 1)
will always be non-zero for n not being divisible by 2 and zero otherwise.
Now if n is divisible by 2 then
n % 2^k == n & (2^k - 1)
This is why to check if a number is even or not we can do
n & (2 - 1) == n & 1
instead of
n % 2
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
For c++ code...complexity O(n)
For python code...complexity O(n)
Update even better solution without vector in c++...complexity O(n)
Update:: Even better answer without vector
- mayukh July 27, 2016