0% found this document useful (0 votes)
2 views13 pages

vertopal.com_dsa (1)

The document contains various Python code snippets demonstrating array manipulations, including taking inputs, reversing arrays, finding maximum and minimum elements, and identifying duplicates. It also covers algorithms for subarrays, stock buying and selling strategies, and calculating maximum subarray products. Each section includes example inputs and outputs, showcasing different methods for achieving the desired results.

Uploaded by

sk9340466486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views13 pages

vertopal.com_dsa (1)

The document contains various Python code snippets demonstrating array manipulations, including taking inputs, reversing arrays, finding maximum and minimum elements, and identifying duplicates. It also covers algorithms for subarrays, stock buying and selling strategies, and calculating maximum subarray products. Each section includes example inputs and outputs, showcasing different methods for achieving the desired results.

Uploaded by

sk9340466486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

TAKING INPUTS

n = int(input("enter n"))
arr = [int(input(f"Enter element:{i+1}")) for i in range(n)]
print(arr)

[1, 2, 3, 4]

#second method
n = int(input())

list = []

for i in range(n):
list.append(int(input()))

print(list)

[1, 2, 3, 4, 5]

sum of all elements

n = int(input())

arr = []

for i in range(n):
arr.append(int(input()))

sum = 0
for i in arr[i]:
sum = sum + arr[i]

print(sum)

15

# reverse a array
n = int(input())

arr = []

for i in range(n):
arr.append(int(input()))

l = arr[ : : -1]

print(l)

[5, 4, 3, 2, 1]

# reverse a array by functions


n = int(input())

arr = []

for i in range(n):
arr.append(int(input()))

reverse = arr
reverse.reverse()
print(reverse)

[3, 2, 1]

reverse a string ya array method 1

n = str(input("enter string"))
def reverse(n):
k=n[::-1]
return k
reverse(n)

'dgdfd'

reverse a string ya array method 2


n = str(input("enetr string"))
def reverse(n):
l = list(n)
l.reverse()
return "".join(l)

reverse(n)

arr max min

sum of max and min element of an array method 1

n = int(input("enter n"))
arr = list(int(input(f"enter elements{i+1}:"))for i in range(n))

def sum(arr):
if n==1:
return arr[0]

if arr[0]>arr[1]:
maxi = arr[0]
mini = arr[1]

else :
maxi = arr[1]
mini = arr[0]

for i in range(2,n):
if arr[i] > maxi
maxi = arr[i]

else :

sum(arr)
1

sum of max and min element of an array method 2

# arr = list(map(int,input().split())) #input seperated by spaces


n = list(map(int,input().split())) #input as 1234 (not spaces)

print(n)

def sum(n):
return max(n) + min(n) #[1 2 3 45]

sum(n)

[1, 2, 3, 45]

46

find Kth smallest element in an array

always remember the smallest cases

n= int(input("enter n"))
arr = [int(input(f"enter elements:{i+1}"))for i in range(n)]
# arr = list(int(input(f"enter elements:{i+1}"))for i in range(n))

k = int(input("enter k"))
def kthsmallest(arr,k): # [1 2 3 4 5] k=
3
# 0 1 2 3 4
if len(arr)==1:
return arr[0]
arr.sort()
return arr[k-1]

kthsmallest(arr,k)

sort an array of 0 1 2 without functions


n = int(input("enter n"))

arr = [int(input(f"Enter element:{i+1}")) for i in range(n)]

cnt0 = 0 # 3
cnt1 = 0 #2
cnt2 = 0 #2

for i in range(n):
if arr[i]==0:
cnt0+=1
if arr[i]==1:
cnt1+=1
if arr[i]==2:
cnt2+=1

i=0 #2 #3 #4 #6 #7
while(cnt0>0):
arr[i]=0 # [0 0 0 1 1 2 2]
i+=1
cnt0-=1

while(cnt1>0):
arr[i]=1
i+=1
cnt1-=1

while(cnt2>0):
arr[i]=2
i+=1
cnt2-=1

print(arr)

[4, 5, 6]

all negative in one side and all positive in one side (no sorting )

n = int(input("enter n"))
arr = list(int(input(f"enter no:{i+1}")) for i in range(n))

print(arr)

[1, 2, 3, 4, 5]

reverse = arr[::-1]
reverse
[5, 4, 3, 2, 1]

Shiting all negative elements on one sidee

n =5
arr = list(map(int,input()))

# [0 1 2 3 4]

j= 0
for i in range(n):
if(arr[i]<0):
temp = arr[i]
arr[i] = arr[j]
arr[j]=temp
j= j+1

print(arr)

[5, 4, 5, 4, 5]

#method 1
a = [1,2,3,4]
b = [1,2,4,7]

a = set(a)
b = set(b)
c = a.union(b)

print(c)

{1, 2, 3, 4, 7}

#method 2
n = 4
m = 4
arr = [1,2,3,4]
brr = [1,2,3,5]

s =set()
for i in range(n):
s.add(arr[i])
for i in range(m):
s.add(brr[i])

print(s)

{1, 2, 3, 4, 5}
cyclic rotation of an array by 1

# method 1
a = list(map(int,input()))

k= int(input())

a[:] = a[-k: ] + a[ :-k] # 3 4 1 2

print(a)

[1, 2, 3, 4]

# method 2
n =4
a = [12, 14 , 67 ,89]

#[ 0 1 2 3]

last= a[-1] #4

for i in range(n-1,0,-1):
a[i]= a[i-1]

a[0]=l # first position mai rakh diya 4 = l

print(a)

[4, 1, 2, 3]

# duplicate element in an array [ 1 3 57 64 64


]
n = int(input("enter n"))
arr=list(int(input(f"enter:{i+1}"))for i in range(n))

arr.sort()
for i in range(1,n):
if arr[i-1] ==arr[i]:
print(" yhi duplicate ",arr[i])

yhi duplicate 4
# find the elements that appear more than N/K times and fill in array
:

# with function

from collections import Counter


n = int(input())
arr = list(int(input(f"enter no :{i+1}"))for i in range(n))
k = int(input("enter k "))

x = n//k

mp = Counter(arr)
a =[]

for i in mp:
if(mp[i]>x):
a.append(i)

print(arr)

----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
Cell In[5], line 11
7 k = int(input("enter k "))
9 x = n//k
---> 11 mp = Counter(arr)
12 a =[]
14 for i in mp:

NameError: name 'Counter' is not defined

'yes'

arr=[1,2,3,4,5]
arr[:] = arr[-3:] + arr[:-3]

print(arr)

[3, 4, 5, 1, 2]

find all pairs whose sum is equal to a hiven no

# method 1
arr =[3,1,5,1,3]
k = 6
n= 5

count =0
dic ={}

for i in range(n):
dif = k-arr[i]

if dif in dic:
count = count + dic[dif]

if arr[i] in dic:
dic[arr[i]]+=1
else:
dic[arr[i]]=1

print(count)

# method 2

arr = list(map(int,input()))
print(arr)
k = 6
n= len(arr)

count =0

for i in range(n):
for j in range(i+1,n):
if arr[i]+arr[j]==k:
count+=1
print(count)

[3, 1, 5, 4, 5, 6, 7, 8, 9, 4, 5, 3, 3, 3, 3]
13

SUBARRAYS

Subarray with sum 0

arr=[4, 2, -3, 1, 6]

n = len(arr) # [4 5 2 0 1

s=set()
def subarray_zero(arr,n):

sum =0
for i in range(n):

if arr[i]==0:
return True
sum = sum + arr[i] #sum = 4
if sum ==0 or sum in s:
return True

else:
s.add(sum) #s (4 6 3)

return False

subarray_zero(arr,n)

True

find array is subarray of array or not

a1 = [1,2,3,4,5]
a2 = [ 1 ,2,6]

def subarray(a1,a2):
for i in a2:
if i in a1:
continue
else:
return "no"
return "yes"

subarray(a1,a2)

'no'

maximum sum of subarray

# kadane's algorithm #[ 1 2 3 6 -7 5]
n = int(input())
arr = list(int(input(f"enter no:{i+1}")) for i in range(n))

#[-1 -4 3 -3 2]
#
max_abhi_tak = arr[0] #=1
curr_max = arr[0] #=1

for i in range(1,n):
curr_max = max(arr[i],curr_max+arr[i])

max_abhi_tak = max(max_abhi_tak,curr_max)

print(max_abhi_tak)

10

BUY SELL STOCK

# n = int(input("enter no "))
# arr= list(int(input(f"enter no :{i+1}")) for i in range(n))

arr = list(map(int(input())))
n = len(arr)

#[7 1 5 4 6] #5

buy_at_mini= arr[0]
profit =0
for i in range(n):
diff = arr[i]-buy_at_mini # 5
profit = max(diff,profit) #( 5 ,4 ) 5
buy_at_mini = min(buy_at_mini,arr[i])
#1

print(profit)
print(buy_at_mini)

----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[7], line 4
1 # n = int(input("enter no "))
2 # arr= list(int(input(f"enter no :{i+1}")) for i in range(n))
----> 4 arr = list(map(int(input())))
10 #[7 1 5 4 6] #5
12 buy_at_mini= arr[0]

TypeError: map() must have at least two arguments.

which day sell which day buy ,profit

n = int(input("enter no "))
arr= list(int(input(f"enter no :{i+1}")) for i in range(n))
#[7 1 5 4 6 ]
buy_at_mini =arr[0]
sell=0
profit = 0
for i in range(1,n):
diff = arr[i]-buy_at_mini
if diff>profit:
profit=diff
sell = i

if arr[i]<buy_at_mini:
buy_at_mini=arr[i]

print(profit)
print(buy_at_mini)
print("day in which he sell=",sell+1)

5
1
day in which he sell= 5

maximum subarray product


n = int(input("enter no "))
arr= list(int(input(f"enter no :{i+1}")) for i in range(n))
maxin =arr[0]
minin =arr[0]
product = arr[0]

for i in range(1,n):
if arr[i]<0:
maxin,minin = minin,maxin

maxin = max(arr[i],maxin*arr[i])
minin = min(arr[i],minin*arr[i])
product = max(product,maxin)

print(product)

180

You might also like