Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Program for Find remainder of array multiplication divided by n

from functools import reduce

def find_remainder(arr,n):

   sum_1=reduce(lambda x,y: x*y,arr)

   remainder=sum_1%n

   print(remainder)

arr=[100,10,5,25,35,14]

n=11

find_remainder(arr,n)

Reconstruct the array by replacing arr[i] with (arr[i-1]+1) % M

def construct(n, m, a):

ind = 0

for i in range(n):

if (a[i]!=-1):

ind = i

break

for i in range(ind-1, -1, -1):

if (a[i]==-1):

a[i]=(a[i + 1]-1 + m)% m

for i in range(ind + 1, n):

if(a[i]==-1):

a[i]=(a[i-1]+1)% m

print(*a)

n, m = 6, 7

a =[5, -1, -1, 1, 2, 3]

construct(n, m, a)

Program for Reversal algorithm for array rotation

def rverseArray(arr, start, end):

while (start < end):

temp = arr[start]

arr[start] = arr[end]

arr[end] = temp

start += 1

end = end-1

def leftRotate(arr, d):

n = len(arr)

rverseArray(arr, 0, d-1)

rverseArray(arr, d, n-1)

rverseArray(arr, 0, n-1)

def printArray(arr):

for i in range(0, len(arr)):

print (arr[i])

arr = [1, 2, 3, 4, 5, 6, 7]

leftRotate(arr, 2)

printArray(arr)


Program to Split the array and add the first part to the end

def splitArr(arr, n, k):

for i in range(0, k):

x = arr[0]

for j in range(0, n-1):

arr[j] = arr[j + 1]

arr[n-1] = x

arr = [12, 8, 10, 11, 52, 36]

n = len(arr)

position = 2

splitArr(arr, n, position)

for i in range(0, n):

print(arr[i], end = ' ')