`{"id":"fff7eb05-6098-4480-b69d-5353bc247ac4","name":"Last Index","description":"1. You are given a number n, representing the count of elements.\r\n2. You are given n numbers.\r\n3. You are given a number x. \r\n4. You are required to find the last index at which x occurs in array a.\r\n5. If x exists in array, print the last index where it is found otherwise print -1.\r\n\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number x","outputFormat":"A number representing last index of occurence of x in array a or -1 if not found at all.","constraints":"1 &lt;= n &lt;= 10^4\r\n0 &lt;= n1, n2, .. n elements &lt;= 10 ^ 3\r\n0 &lt;= x &lt;= 10 ^ 3","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nint lastIndex(int arr[], int idx, int x, int n){\r\n // write your code here\r\n\r\n}\r\n\r\nint main(){\r\n int n;\r\n cin >> n;\r\n int d;\r\n int arr[n];\r\n for (int i = 0; i < n; i++)\r\n cin >> arr[i];\r\n cin >> d;\r\n int p = lastIndex(arr, 0, d, n);\r\n cout << p << endl;\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public static int lastIndex(int[] arr, int idx, int x){\r\n return 0;\r\n }\r\n\r\n}"},"python":{"code":"import sys\nsys.setrecursionlimit (10000)\ndef Lastindex(arr,idx,x,n):\n # Write your code here\n\ndef main():\n n=int(input())\n arr=[]\n for i in range(n):\n arr.append(int(input()))\n x=int(input())\n ans=Lastindex(arr,0,x,n)\n print(ans)\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n15\r\n11\r\n40\r\n4\r\n4\r\n9\r\n4","sampleOutput":"4","questionVideo":"https://www.youtube.com/embed/qr984qKJf4Y","hints":[],"associated":[{"id":"2f459c9c-b834-4dbb-b773-b35d87fe4c2d","name":"(Last Index) The given element exists at indices 2,4 and 5. What should be the output of the code?","slug":"last-index-the-given-element-exists-at-indices-2-4-and-5-what-should-be-the-output-of-the-code","type":4},{"id":"3a4a06a0-0644-4e6c-8a9a-b38e108264cc","name":"(Last Index) What is the time complexity for this question?","slug":"last-index-what-is-the-time-complexity-for-this-question","type":4},{"id":"7f7e4394-eefe-4289-ab72-c1fee407f525","name":"(Last Index) Which of the following cannot be one of the base conditions for the recursive function?","slug":"last-index-which-of-the-following-cannot-be-one-of-the-base-conditions-for-the-recursive-function","type":4},{"id":"f09969ad-15d1-480e-bca2-71dd5570bc6a","name":"(Last Index) What is the starting index for recursion in this question?","slug":"last-index-what-is-the-starting-index-for-recursion-in-this-question","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"78b2ac55-c3d0-442e-8360-767dd94c4076","name":"Last Index","slug":"last-index","type":1}],"next":{"id":"d164c462-19e3-4756-93ea-b5a5f4852f89","name":"Last Index","type":3,"slug":"last-index"},"prev":{"id":"66088e9f-f081-495f-9a6a-4348b1d455b5","name":"First Index","type":3,"slug":"first-index"}}}`

# Last Index

1. You are given a number n, representing the count of elements. 2. You are given n numbers. 3. You are given a number x. 4. You are required to find the last index at which x occurs in array a. 5. If x exists in array, print the last index where it is found otherwise print -1. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

`{"id":"fff7eb05-6098-4480-b69d-5353bc247ac4","name":"Last Index","description":"1. You are given a number n, representing the count of elements.\r\n2. You are given n numbers.\r\n3. You are given a number x. \r\n4. You are required to find the last index at which x occurs in array a.\r\n5. If x exists in array, print the last index where it is found otherwise print -1.\r\n\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number x","outputFormat":"A number representing last index of occurence of x in array a or -1 if not found at all.","constraints":"1 &lt;= n &lt;= 10^4\r\n0 &lt;= n1, n2, .. n elements &lt;= 10 ^ 3\r\n0 &lt;= x &lt;= 10 ^ 3","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nint lastIndex(int arr[], int idx, int x, int n){\r\n // write your code here\r\n\r\n}\r\n\r\nint main(){\r\n int n;\r\n cin >> n;\r\n int d;\r\n int arr[n];\r\n for (int i = 0; i < n; i++)\r\n cin >> arr[i];\r\n cin >> d;\r\n int p = lastIndex(arr, 0, d, n);\r\n cout << p << endl;\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public static int lastIndex(int[] arr, int idx, int x){\r\n return 0;\r\n }\r\n\r\n}"},"python":{"code":"import sys\nsys.setrecursionlimit (10000)\ndef Lastindex(arr,idx,x,n):\n # Write your code here\n\ndef main():\n n=int(input())\n arr=[]\n for i in range(n):\n arr.append(int(input()))\n x=int(input())\n ans=Lastindex(arr,0,x,n)\n print(ans)\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n15\r\n11\r\n40\r\n4\r\n4\r\n9\r\n4","sampleOutput":"4","questionVideo":"https://www.youtube.com/embed/qr984qKJf4Y","hints":[],"associated":[{"id":"2f459c9c-b834-4dbb-b773-b35d87fe4c2d","name":"(Last Index) The given element exists at indices 2,4 and 5. What should be the output of the code?","slug":"last-index-the-given-element-exists-at-indices-2-4-and-5-what-should-be-the-output-of-the-code","type":4},{"id":"3a4a06a0-0644-4e6c-8a9a-b38e108264cc","name":"(Last Index) What is the time complexity for this question?","slug":"last-index-what-is-the-time-complexity-for-this-question","type":4},{"id":"7f7e4394-eefe-4289-ab72-c1fee407f525","name":"(Last Index) Which of the following cannot be one of the base conditions for the recursive function?","slug":"last-index-which-of-the-following-cannot-be-one-of-the-base-conditions-for-the-recursive-function","type":4},{"id":"f09969ad-15d1-480e-bca2-71dd5570bc6a","name":"(Last Index) What is the starting index for recursion in this question?","slug":"last-index-what-is-the-starting-index-for-recursion-in-this-question","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"78b2ac55-c3d0-442e-8360-767dd94c4076","name":"Last Index","slug":"last-index","type":1}],"next":{"id":"d164c462-19e3-4756-93ea-b5a5f4852f89","name":"Last Index","type":3,"slug":"last-index"},"prev":{"id":"66088e9f-f081-495f-9a6a-4348b1d455b5","name":"First Index","type":3,"slug":"first-index"}}}` Editor

# Last Index

easy

1. You are given a number n, representing the count of elements. 2. You are given n numbers. 3. You are given a number x. 4. You are required to find the last index at which x occurs in array a. 5. If x exists in array, print the last index where it is found otherwise print -1. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

## Constraints

1 <= n <= 10^4 0 <= n1, n2, .. n elements <= 10 ^ 3 0 <= x <= 10 ^ 3

## Format

### Input

A number n n1 n2 .. n number of elements A number x

### Output

A number representing last index of occurence of x in array a or -1 if not found at all.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 15 11 40 4 4 9 4```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}4`

Question Video

Discussions

Show Discussion

Related Resources 