Maximum Fixed Point (Value equal to index) in a given Array Last Updated : 18 Jul, 2021 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of size N, the task is to find the maximum index i such that arr[i] is equal to i. If there is no such index in the array arr[] then print -1. Examples: Input: arr[ ] = {-10, -5, 0, 3, 7}Output: 3Explanation: Only for i=3, arr[3] = 3 Input: arr[ ] = {0, 2, 5, 8, 4}Output: 4 Approach: Follow the steps below to solve this problem: Iterate in the range [N-1, 0] using the variable i: If the current element is equal to i, then print i and return.If there is no such index, then print -1. Below is the implementation of the above approach: C++ // C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum index // i such that arr[i] is equal to i void findLargestIndex(int arr[], int n) { // Traversing the array from // backwards for (int i = n - 1; i >= 0; i--) { // If arr[i] is equal to i if (arr[i] == i) { cout << i << endl; return; } } // If there is no such index cout << -1 << endl; } // Driver code int main() { // Given Input int arr[] = { -10, -5, 0, 3, 7 }; int n = sizeof(arr) / sizeof(arr[0]); // Function Call findLargestIndex(arr, n); return 0; } Java // Java implementation of the above approach import java.io.*; class GFG { // Function to find the maximum index // i such that arr[i] is equal to i static void findLargestIndex(int arr[], int n) { // Traversing the array from // backwards for (int i = n - 1; i >= 0; i--) { // If arr[i] is equal to i if (arr[i] == i) { System.out.println(i); return; } } // If there is no such index System.out.println(-1); } // Driver code public static void main(String[] args) { // Given Input int arr[] = { -10, -5, 0, 3, 7 }; int n = arr.length; // Function Call findLargestIndex(arr, n); } } // This code is contributed by Potta Lokesh Python # Python implementation of the above approach # Function to find the maximum index # i such that arr[i] is equal to i def findLargestIndex(arr, n): # Traversing the array from # backwards for i in range (n): # If arr[i] is equal to i if (arr[i] == i) : print( i ) return # If there is no such index print( -1 ) # Driver code # Given Input arr = [-10, -5, 0, 3, 7 ] n = len(arr) # Function Call findLargestIndex(arr, n) # This code is contributed by shivanisinghss2110 C# // C# implementation of the above approach using System; class GFG { // Function to find the maximum index // i such that arr[i] is equal to i static void findLargestIndex(int []arr, int n) { // Traversing the array from // backwards for (int i = n - 1; i >= 0; i--) { // If arr[i] is equal to i if (arr[i] == i) { Console.Write(i); return; } } // If there is no such index Console.Write(-1); } // Driver code public static void Main(String[] args) { // Given Input int []arr = { -10, -5, 0, 3, 7 }; int n = arr.Length; // Function Call findLargestIndex(arr, n); } } // This code is contributed by shivanisinghss2110 JavaScript <script> // JavaScript implementation of the above approach // Function to find the maximum index // i such that arr[i] is equal to i function findLargestIndex( arr, n) { // Traversing the array from // backwards for (var i = n - 1; i >= 0; i--) { // If arr[i] is equal to i if (arr[i] == i) { document.write(i); return ; } } // If there is no such index document.write(-1); } // Driver code // Given Input var arr = [ -10, -5, 0, 3, 7 ]; var n = arr.length; // Function Call findLargestIndex(arr, n); // This code is contributed by shivanisinghss2110 </script> Output3 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximum Fixed Point (Value equal to index) in a given Array siddharth_25 Follow Improve Article Tags : Searching Hash Competitive Programming DSA Arrays +1 More Practice Tags : ArraysHashSearching Similar Reads For each Array index find the maximum value among all M operations Given an array arr[] of size N initially filled with 0 and another array Positions[] of size M, the task is to return the maximum value for each index after performing the following M operations: Make the value at index Positions[i] equal to 0All the numbers to the right of Positions[i] will be one 6 min read Find Maximum value of abs(i - j) * min(arr[i], arr[j]) in an array arr[] Given an array of n distinct elements. Find the maximum of product of Minimum of two numbers in the array and absolute difference of their positions, i.e., find maximum value of abs(i - j) * min(arr[i], arr[j]) where i and j vary from 0 to n-1. Examples : Input : arr[] = {3, 2, 1, 4} Output: 9 // ar 6 min read Find the peak index of a given array Given an array arr[] consisting of N(> 2) integers, the task is to find the peak index of the array. If the array doesn't contain any peak index, then print -1. The peak index, say idx, of the given array arr[] consisting of N integers is defined as: 0 < idx < N - 1arr[0] < arr[1] < a 8 min read Find the maximum subarray XOR in a given array Given an array of integers. The task is to find the maximum subarray XOR value in the given array. Examples: Input: arr[] = {1, 2, 3, 4}Output: 7Explanation: The subarray {3, 4} has maximum XOR value Input: arr[] = {8, 1, 2, 12, 7, 6}Output: 15Explanation: The subarray {1, 2, 12} has maximum XOR val 15+ min read Minimum index of element with maximum multiples in Array Given an array arr[] of length n, the task is to calculate the min index of the element which has maximum elements present in the form of {2 * arr[i], 3 * arr[i], 4 * arr[i], 5 * arr[i]}. Examples: Input: n = 4. arr[] = {2, 1, 3, 6}Output: 1Explanation: For 2, {2*2, 3*2, 4*2, 5*2} => {4, 6, 8, 10 5 min read Find the maximum possible value of last element of the Array Given a non-negative array arr of size N and an integer M representing the number of moves such that in one move, the value of any one element in the array decreases by one, and the value of its adjacent element on the right increases by one. The task is to find the maximum possible value of the las 7 min read Maximize value of (a[i]+i)*(a[j]+j) in an array Given an array with input size n, find the maximum value of (a[i] + i) * (a[j] + j) where i is not equal to j. Note that i and j vary from 0 to n-1 . Examples: Input : a[] = [4,5,3,1,10] Output : 84 Explanation: We get the maximum value for i = 4 and j = 1 (10 + 4) * (5 + 1) = 84 Input : a[] = [10,0 12 min read Maximum value of |arr[i] - arr[j]| + |i - j| Given a array of N positive integers. The task is to find the maximum value of |arr[i] - arr[j]| + |i - j|, where 0 <= i, j <= N - 1 and arr[i], arr[j] belong to the array. Examples: Input : N = 4, arr[] = { 1, 2, 3, 1 } Output : 4Explanation:Choose i = 0 and j = 2. This will result in |1-3|+| 15+ min read Maximum element in an array which is equal to its frequency Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequency equals to it's value Examples: Input: arr[] = {3, 2, 2, 3, 4, 3} Output: 3 Frequency of element 2 is 2 Frequency of element 3 is 3 Frequency of element 4 is 1 2 and 3 are elements which h 11 min read Find the element having maximum premutiples in the array Given an array arr[], the task is to find the element which has the maximum number of pre-multiples present in the set. For any index i, pre-multiple is the number that is multiple of i and is present before the ith index of the array. Also, print the count of maximum multiples of that element in th 8 min read Like