0% found this document useful (0 votes)
3 views

HackwithInfydoc

Hackwithinfy

Uploaded by

mounikadokala21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

HackwithInfydoc

Hackwithinfy

Uploaded by

mounikadokala21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

HackwithInfy

1.RETURN THE INDEXES OF THE SUM OF TWO ARRAYS


CODE:
arr=[2,7,11,15]
tar=9
n = len(arr)
for i in range(n):
pick = arr[i]
flag = 0
for j in range(n):
if tar - pick == arr[j] and i!=j:
print(i,j)
flag=1
break
if flag:
break

2.STOCK AND PROFIT


you are given an array prices where prices[i] is the price of
given stock on the ith day
you want to maximize the profit by choosing a single day one
stock and choosing a different day in the future to sell that
cost
return the maximum profit you can acheive from this
transaction .if you cannot acheive any profit ,return 0
CODE:
n = 6
a = [7, 1, 5, 3, 6, 4]
max = 0
for i in range(n):
for j in range(i+1, n):
if a[i] < a[j]:
if max < a[j] - a[i]:
max = a[j] - a[i]
print(max)

3.FINDIND DUPLICATE ARRAY

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

given an integer array nums,rotate the array to the right by


K steps ,where k is non-negative

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)

6.FINDING UNIQUE ELEMENT

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))

10. GAS STATION


There are n gas stations along a circular route,where the
amount of gas at the ith stationof gas[i] you have a car with
unlimited gas tank and it cost costs[i] of gas to trfavel from
the ith station to its next (i*1)th starion you begin the
journey with an empty tank at one of the gas stations
Give two integer arrays gas and cost.return the stating gas
station index if you can travel around the circuit once in the
clockwise dirction .otherwise retutn -1 .if there exisit a
solution ,it is guaranteed to be unique
CODE:
def can_complete_circuit(gas, cost):
position = 0
sum_ = 0
total = 0
for index in range(len(gas)):
sum_ += gas[index] - cost[index]
if sum_ < 0:
total += sum_
sum_ = 0
position = index + 1
total += sum_
return position if total >= 0 else -1

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

Return the maximum valued number you can get

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)

num = int(input("enter an integer"))


print("maximum valued number:", maximumSwap(num))

output: enter an integer 2736

maximum valued number:7236

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))

13. Swapping a alphabet with another alphabet of the


string.
Code:
str='abacda'
a,r="a","x"
out=''
for i in range(len(str)):
if str[i]==a:
out+=r
else:
out+=str[i]
print(out)
14.Reverse entire string and replace one alphabet with
another alphabet.
Code:
str=input()
words=str.split()
minlen=min(words,key=len)
print(minlen)

15.Check if the string has duplicates then it should print false


else it is true.
Code:
st="leetcode"
out=list(st)
out=set(out)
if len(out)==26:
print("true")
else:
print("false")

You might also like