{"id":"201aa1a2-cefc-45c3-93b3-dea05642814e","name":"First 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 first index at which x occurs in array a.\r\n5. If x exists in array, print the first 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 first 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 firstIndex(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{\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 = firstIndex(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 firstIndex(int[] arr, int idx, int x){\r\n return 0;\r\n }\r\n\r\n}"},"python":{"code":"def Firstindex(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=Firstindex(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":"3","questionVideo":"https://www.youtube.com/embed/OD39jBFjgdI","hints":[],"associated":[{"id":"2922ccc6-0275-407b-934a-4444910b1345","name":"(First Index) What is the time complexity of the solution code ?","slug":"first-index-what-is-the-time-complexity-of-the-solution-code","type":4},{"id":"43a2a0c0-8c16-483c-acd3-af282b3aeada","name":"(First Index) What is the space complexity of the solution code ?","slug":"first-index-what-is-the-space-complexity-of-the-solution-code","type":4},{"id":"66c5095c-69d6-4cb7-918c-fda5ac820c55","name":"(First Index) What should we do when we hit the base case?","slug":"first-index-what-should-we-do-when-we-hit-the-base-case","type":4},{"id":"f0780294-e4b7-434f-80ea-ec2aa4b35c10","name":"(First Index) Which variable are we updating when we didn't find the data while going up on the stack ?","slug":"first-index-which-variable-are-we-updating-when-we-didn-t-find-the-data-while-going-up-on-the-stack","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":"438d872d-2c8e-42e0-9fc2-f3565afedb51","name":"First Index","slug":"first-index","type":1}],"next":{"id":"66088e9f-f081-495f-9a6a-4348b1d455b5","name":"First Index","type":3,"slug":"first-index"},"prev":{"id":"14f4517b-0784-48bb-b607-470917f56da9","name":"Max of an Array","type":3,"slug":"max-of-an-array"}}}

First 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 first index at which x occurs in array a. 5. If x exists in array, print the first 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":"201aa1a2-cefc-45c3-93b3-dea05642814e","name":"First 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 first index at which x occurs in array a.\r\n5. If x exists in array, print the first 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 first 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 firstIndex(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{\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 = firstIndex(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 firstIndex(int[] arr, int idx, int x){\r\n return 0;\r\n }\r\n\r\n}"},"python":{"code":"def Firstindex(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=Firstindex(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":"3","questionVideo":"https://www.youtube.com/embed/OD39jBFjgdI","hints":[],"associated":[{"id":"2922ccc6-0275-407b-934a-4444910b1345","name":"(First Index) What is the time complexity of the solution code ?","slug":"first-index-what-is-the-time-complexity-of-the-solution-code","type":4},{"id":"43a2a0c0-8c16-483c-acd3-af282b3aeada","name":"(First Index) What is the space complexity of the solution code ?","slug":"first-index-what-is-the-space-complexity-of-the-solution-code","type":4},{"id":"66c5095c-69d6-4cb7-918c-fda5ac820c55","name":"(First Index) What should we do when we hit the base case?","slug":"first-index-what-should-we-do-when-we-hit-the-base-case","type":4},{"id":"f0780294-e4b7-434f-80ea-ec2aa4b35c10","name":"(First Index) Which variable are we updating when we didn't find the data while going up on the stack ?","slug":"first-index-which-variable-are-we-updating-when-we-didn-t-find-the-data-while-going-up-on-the-stack","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":"438d872d-2c8e-42e0-9fc2-f3565afedb51","name":"First Index","slug":"first-index","type":1}],"next":{"id":"66088e9f-f081-495f-9a6a-4348b1d455b5","name":"First Index","type":3,"slug":"first-index"},"prev":{"id":"14f4517b-0784-48bb-b607-470917f56da9","name":"Max of an Array","type":3,"slug":"max-of-an-array"}}}
plane

Editor


Loading...

First 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 first index at which x occurs in array a. 5. If x exists in array, print the first 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 first index of occurence of x in array a or -1 if not found at all.

Example

Sample Input

6 15 11 40 4 4 9 4

Sample Output

3

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode