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: 3
Explanation: Only for i=3, arr[3] = 3Input: 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> |
Output
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Recommended Articles