# Given an array of positive integers arr[], return the second largest element from the array. # If the second largest element doesn't exist then return -1.# Note: The second largest element should not be equal to the largest element.classSecondLargest:defgetSecondLargest(self,arr):n=len(arr)largest=-1secondLargest=-1foriinrange(n):ifarr[i]>largest:largest=arr[i]foriinrange(n):ifarr[i]>secondLargestandarr[i]!=largest:secondLargest=arr[i]returnsecondLargestif__name__=="__main__":arr=[12,35,1,10,34,1]solution=SecondLargest()
# Given an array arr[]. Push all the zeros of the given array to the right end of the array while maintaining the order of non-zero elements. # Do the mentioned change in the array in place.classZerosEnd:defpushZerosToEnd(self,arr):count=0foriinrange(len(arr)):ifarr[i]!=0:arr[count]=arr[i]count+=1foriinrange(count,len(arr)):arr[i]=0returnarrif__name__=='__main__':arr=[1,2,0,4,3,0,5,0]solution=ZerosEnd()
# You are given an array of integers arr[]. # Your task is to reverse the given array.classReverseArray:defreverseArray(self,arr):left=0right=len(arr)-1whileleft<right:arr[left],arr[right]=arr[right],arr[left]left+=1right-=1if__name__=='__main__':arr=[1,4,3,2,6,5]solution=ReverseArray()
# Given an array arr[]. Rotate the array to the left (counter-clockwise direction) by d steps, where d is a positive integer. # Do the mentioned change in the array in place.# Note: Consider the array as circular.classRotate:defrotateArr(self,arr,d):n=len(arr)d%=ntemp=[0]*nforiinrange(n-d):temp[i]=arr[d+i]foriinrange(d):temp[n-d+i]=arr[i]foriinrange(n):arr[i]=temp[i]if__name__=="__main__":arr=[1,2,3,4,5,6]d=2solution=Rotate()