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.
Question
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.
Solution
Here is a Python solution for the problem:
def ElementsAndIndices(arr, n):
# Check if array is empty
if not arr:
return -1
# Initialize count to 0
count = 0
# Iterate over the array
for i in range(n):
# If element is equal to its index, increment count
if arr[i] == i:
count += 1
# Return the count
return count
This function works by iterating over each element in the array. If the element is equal to its index, it increments a counter. At the end of the function, it returns this count. If the array is empty, it returns -1.
Similar Questions
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.
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 ).
Given a one dimensional array arr, what is the correct way of getting the number of elements in arr is
You are given an integer array and an integer K. You have to tell if there exists a pair of integers in the given array such that ar[i]-ar[j]=K and i≠j.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N and K - the size of the array and the number K. The second line contains the elements of the array.Output FormatFor each test case, print "true" if the arrays contains such elements, "false" otherwise, separated by new line.Constraints40 points2 <= N <= 100060 points2 <= N <= 100000General Constraints1 <= T <= 100-105 <= ar[i], K <= 105ExampleInput25 601 20 40 100 8010 1112 45 52 65 21 645 234 14 575 112Outputtruefalse
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.
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.