Maximum Fixed Point (Value equal to index) in a given Array Last Updated : 18 Jul, 2021 Summarize Comments Improve Suggest changes Share 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 Find the maximum subarray XOR in a given array S siddharth_25 Follow Improve Article Tags : DSA 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 Like