Here given user input array and the position of the array to be reverse.so our task is to reverse this array. We just reverse the sub array from [0 to (n-1)].
Example
Input : A=[3, 7, 6, 4, 1, 2] n=4 Output : [1, 4, 6, 7, 3, 2]
Algorithm
Step 1: reverse list starting from n-1 position. Step 2: split remaining list after n. Step 3: concat both parts and prints
Example code
#Program to reverse an array
#up to a n position
def arrayreverse(A, n):
#generate list starting from n-1 position element till first element in #reverse order
print ("REVERSE OF AN ARRAY UPTO",n,"POSITION",A[n-1::-1] + A[n:])
# Driver program
if __name__ == "__main__":
A=list()
n1=int(input("Enter the size of the List ::"))
print("Enter the Element of List ::")
for i in range(int(n1)):
k=int(input(""))
A.append(k)
n=int(input("Enter the position to be reverse ::"))
arrayreverse(A, n)Output
Enter the size of the List :: 6 Enter the Element of List :: 2 3 4 1 78 23 Enter the position to be reverse : 5 REVERSE OF AN ARRAY UPTO 5 POSITION [78, 1, 4, 3, 2, 23]