Knowee
Questions
Features
Study Tools

Given an array Arr of N positive integers. Your task is to find the elements whose value is equal to that of its index value ( Consider 1-based indexing ).

Question

Given an array Arr of N positive integers. Your task is to find the elements whose value is equal to that of its index value ( Consider 1-based indexing ).

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step solution in Python:

  1. Initialize an empty list to store the elements whose value is equal to their index value.
result = []
  1. Iterate over the array using a for loop. The enumerate function can be used to get both the index and value of each element. Remember to add 1 to the index during the comparison because the problem uses 1-based indexing.
for i, num in enumerate(Arr, start=1):
    if i == num:
        result.append(num)
  1. Return the result list.
return result

Here is the complete Python function:

def find_elements(Arr):
    result = []
    for i, num in enumerate(Arr, start=1):
        if i == num:
            result.append(num)
    return result

You can call this function with your array as the argument to find the elements whose value is equal to their index value.

This problem has been solved

Similar Questions

Find which element and Indices are equal 3.5 marks Problem Statement You are given a function, int ElementsAndIndices(int arr[], int n); The function takes an integer array 'arr' of size 'n' as its arguments. Implement the function to find and return the number of array elements which are equal to their index value in array i.e. arr[k] = k, 0 <= k < n. Note: Indexing starts from 0. Return -1 if 'arr' is empty or None in case of python Example: Input: 10 1 12 3 5 8 9 7 12 23 Output: 3 Explanation: Index Element 0 10 1 1 2 12 3 3 4 5 5 8 6 9 7 7 8 12 9 23 Elements at index {1, 3, 7} are equal to their index values {1, 3, 7} respectively. Since, these are 3 elements, thus, output is 3. The custom input format for the above case: 10 10 1 12 3 5 8 9 7 12 23 (The first line represents the size of the array, the second line represents the elements of the array) Sample input -3 0 1 3 5 7 Sample Output 1 The custom input format for the above case: 6 -3 0 1 3 5 7 (The first line represents the size of the array, the second line represents the elements of the array) Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time.

Problem Statement You are given a function, static int ElementsAndIndices(int[] arr){} The function takes an integer array 'arr' of size 'n' as its arguments. Implement the function to find and return the number of array elements which are equal to their index value in array i.e. arr[k] = k, 0 <= k < n. Note: Indexing starts from 0. Return -1 if 'arr' is empty or None in case of python Example: Input: 10 1 12 3 5 8 9 7 12 23 Output: 3 Explanation: Index Element 0 10 1 1 2 12 3 3 4 5 5 8 6 9 7 7 8 12 9 23 Elements at index {1, 3, 7} are equal to their index values {1, 3, 7} respectively. Since, these are 3 elements, thus, output is 3. The custom input format for the above case: 10 10 1 12 3 5 8 9 7 12 23 (The first line represents the size of the array, the second line represents the elements of the array) Sample input -3 0 1 3 5 7 Sample Output 1 The custom input format for the above case: 6 -3 0 1 3 5 7 (The first line represents the size of the array, the second line represents the elements of the array) Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time. Now let's start coding : Language: Java 17.0 Day Mode Read-only code below . . . 1 class SolutionClass { 2 public static void main(String[] args) throws java.lang.Exception { 3 //Input read from STDIN 4 int result = ElementsAndIndices(arr); 5 //Value in result printed to STDOUT 6 } 7 } 8 ​ Write your code below . . . 9 static int ElementsAndIndices(int[] arr) throws java.lang.Exception 10 { 11 /* Write your code here. */ 12 } 13 ​ 14 ​ 15 ​

Given an unsorted array Arr[] of N integers and a Key which is present in this array. You need to write a program to find the start index( index where the element is first found from left in the array ) and end index( index where the element is first found from right in the array ).If the key does not exist in the array then return -1 for both start and end index in this case.

You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:abs(i - j) >= indexDifference, andabs(nums[i] - nums[j]) >= valueDifferenceReturn an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.Note: i and j may be equal. Example 1:Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4Output: [0,3]Explanation: In this example, i = 0 and j = 3 can be selected.abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.Hence, a valid answer is [0,3].[3,0] is also a valid answer.Example 2:Input: nums = [2,1], indexDifference = 0, valueDifference = 0Output: [0,0]Explanation: In this example, i = 0 and j = 0 can be selected.abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.Hence, a valid answer is [0,0].Other valid answers are [0,1], [1,0], and [1,1].Example 3:Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4Output: [-1,-1]Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.Hence, [-1,-1] is returned. Constraints:1 <= n == nums.length <= 1050 <= nums[i] <= 1090 <= indexDifference <= 1050 <= valueDifference <= 109

First index of elementSend FeedbackTake an array with n elements with possibly duplicate elements as the input. The task is to find the index of the first occurrences of the element x in the array and, if it is not present, return -1.Input Format:The first line contains an integer N representing the size of the array.The next line contains N space-separated integers representing the elements of the array.The last line contains an integer 'x' whose index has to be found.Output Format:The only line of the output prints the Index or -1.Constraints:1 <= N <= 10^31 <= arr[i] <= 10^91 <= x < NSample Input 1 :87 5 2 11 2 43 1 12Sample Output 1 :2Explanation of Sample Input 1:2 is present twice in the input array and the first time it appears is at index 2.Sample Input 2 :87 5 2 11 2 43 1 110Sample Output 2 :-1Explanation of Sample Input 2:10 is not present in the array so the output is -1.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.