HackwithInfydoc
HackwithInfydoc
CODE:
n = 8
array = [1, 2, 3, 4, 5, 6, 7, 7]
duplicates = []
for i in range(len(array)):
if array[i] in array[i+1:]:
if array[i] not in duplicates:
duplicates.append(array[i])
print("duplcate elements",duplicates)
4.JUMPING GAME
you are given an integer array nums.you are initially
positioned at the arrays first index,and each element in the
array represents your maximum jump length at that
positioned
return true if you can reach the last index, or false otherwise
-->output format:True or False
CODE:
def can_jump(nums):
i=0
reach=0
while i< len(nums) and i<=reach:
reach= max(i + nums[i],reach)
i += 1
return i ==len(nums)
N=5
nums = [3,2,1,0,4]
result = can_jump(nums)
print("true"if result else"false")
5.REVERSING ELEMENTS
CODE:
def rotate_array(nums,k):
n = len(nums)
k %= n
nums.reverse()
nums[:k] = reversed(nums[:k])
nums[k:] = reversed(nums[k:])
n = 7
nums = [1,2,3,4,5,6,7]
k = 3
rotate_array(nums,k)
print(*nums)
you are given with the array and in the array only one
unique element just return that unique element in the array
CODE:
arr = [2, 4, 6, 8, 10, 2, 4, 6, 8]
counts = {}
for num in arr:
if num in counts:
counts[num] += 1
else:
counts[num] = 1
for num, count in counts.items():
if count == 1:
print("The unique element in the array is:",
num)
break
7.STORE RAIN WATER
give a non negative integer representing an elevation map
where the width of each bar is 1 .compute how much water
it can trap after raining
output:value showing how much water we can store # store
rain water
CODE:
def trap(height):
n = len(height)
left_max = [0] * n
right_max = [0] * n
left_max[0] = height[0]
for i in range(1, n):
left_max[i] = max(left_max[i-1], height[i])
right_max[n-1] = height[n-1]
for i in range(n-2, -1, -1):
right_max[i] = max(right_max[i+1],
height[i])
trapped_water = 0
for i in range(n):
trapped_water += min(left_max[i],
right_max[i]) - height[i]
return trapped_water
heights = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
result = trap(heights)
print(result)
8. PRINTING THE INDEX POSITION FROM BACKSIDE OF
GIVEN NUMBER
CODE:
n = 8
a = [1,2,3,4,5,6,7,8]
k = 2
a.sort()
print(a[(-1*k)])
9.DISTRIBUTION OF CANDIES
there are n children in a line. Each child is assigned a rating
value given in the integer array ratings
you are giving candies to these children subjected to the
following requirements
.Each child must have at least one candy
.children with a heigher rating get more candies than their
neighbors
.return the numbers of candies you need to have to
distribute the candies
CODE:
def candy(nums):
n = len(nums)
if n == 1:
return 1
# Left to right
L2R = [1] * n
for i in range(1, n):
if nums[i] > nums[i - 1]:
L2R[i] = L2R[i - 1] + 1
# Right to left
R2L = [1] * n
for i in range(n - 2, -1, -1):
if nums[i] > nums[i + 1]:
R2L[i] = R2L[i + 1] + 1
for i in range(n):
L2R[i] = max(L2R[i], R2L[i])
return sum(L2R)
ratings = [1, 0, 2]
print(candy(ratings))
gas = [1, 2, 3, 4, 5]
cost = [3, 4, 5, 1, 2]
print(can_complete_circuit(gas, cost))
11.you are given an nteger number you can swap two digits at most
once to get the maximum value of the number
CODE:
def maximumSwap(num):
num_str = str(num)
max_num = num_str
for i in range(len(num_str)):
for j in range(i + 1, len(num_str)):
swapped_num_str = num_str[:i] +
num_str[j] + num_str[i + 1:j] + num_str[i] +
num_str[j + 1:]
max_num = max(max_num, swapped_num_str)
return int(max_num)
12. you have a long flowerbed in which some of the plots are
planted ,and some are not However flowers cannot be
planted in adjacent plots
given an integer array flowerbed containing 0's and 1's
where 0 means empty 1 means not empty .and an integer n,
return true n new flowers can be planted in the flowerbed
without violating the no-adjacent -flowres rule and false
otherwise.
CODE:
def can_place_flowers(flowerbed, n):
if n == 0:
return True
for i in range(len(flowerbed)):
if flowerbed[i] == 0 and (i == 0 or
flowerbed[i - 1] == 0) and (i == len(flowerbed) - 1
or flowerbed[i + 1] == 0):
flowerbed[i] = 1
n -= 1
if n == 0:
return True
return False
flowerbed = [1,0,0,0,1]
n = 1
print(can_place_flowers(flowerbed,n))