Computer Science, asked by Eshaan7017, 4 months ago

You have been given an array of size N consisting of integers. In addition you have been given an element M you need to find and print the index of the last occurrence of this element M in the array if it exists in it, otherwise print -1. Consider this array to be 1 indexed.

Answers

Answered by rohitkhajuria90
0

Please refer the attached image

Attachments:
Answered by vishakasaxenasl
0

Answer:

The following Python program solves the stated problem:

arr = list(map(int,input().split()))

n = int(input())

m = int(input())

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

 if(arr[i]==m):

    return i+1

return -1

Explanation:

Let's understand the code:

  • First, we take the array input and size of the array N by the user.
  • m represents that element that is to search in the given array.
  • Since we want to print the index of the last occurence occurrence of the m element. So we have to start the for loop from the backward of the array.
  • Each time element of the array is compared with the m element. If they match then we return i+1. Since the problem is mentioning the array to be 1 indexed.
  • But if the m element is not found then we return -1.

#SPJ2

Similar questions