# Minimum index i such that all the elements from index i to given index are equal

Given an array **arr[]** of integers and an integer **pos**, the task is to find the minimum index **i** such that all the elements from index **i** to index **pos** are equal.

**Examples:**

Input:arr[] = {2, 1, 1, 1, 5, 2}, pos = 3

Output:1

Elements in index range [1, 3] are all equal to 1.

Input:arr[] = {2, 1, 1, 1, 5, 2}, pos = 5

Output:5

**Approach:** Starting from index **pos – 1**, traverse the array in reverse and for the first index **i** such that **arr[i] != arr[pos]** print **i + 1** which is the required index.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum required index ` `int` `minIndex(` `int` `arr[], ` `int` `n, ` `int` `pos) ` `{ ` ` ` `int` `num = arr[pos]; ` ` ` ` ` `// Start from arr[pos - 1] ` ` ` `int` `i = pos - 1; ` ` ` `while` `(i >= 0) { ` ` ` `if` `(arr[i] != num) ` ` ` `break` `; ` ` ` `i--; ` ` ` `} ` ` ` ` ` `// All elements are equal ` ` ` `// from arr[i + 1] to arr[pos] ` ` ` `return` `i + 1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 1, 1, 1, 5, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `pos = 4; ` ` ` ` ` `cout << minIndex(arr, n, pos); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum required index ` `static` `int` `minIndex(` `int` `arr[], ` `int` `n, ` `int` `pos) ` `{ ` ` ` `int` `num = arr[pos]; ` ` ` ` ` `// Start from arr[pos - 1] ` ` ` `int` `i = pos - ` `1` `; ` ` ` `while` `(i >= ` `0` `) ` ` ` `{ ` ` ` `if` `(arr[i] != num) ` ` ` `break` `; ` ` ` `i--; ` ` ` `} ` ` ` ` ` `// All elements are equal ` ` ` `// from arr[i + 1] to arr[pos] ` ` ` `return` `i + ` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `1` `, ` `1` `, ` `1` `, ` `5` `, ` `2` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `pos = ` `4` `; ` ` ` ` ` `System.out.println(minIndex(arr, n, pos)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum ` `# required index ` `def` `minIndex(arr, n, pos): ` ` ` ` ` `num ` `=` `arr[pos] ` ` ` ` ` `# Start from arr[pos - 1] ` ` ` `i ` `=` `pos ` `-` `1` ` ` `while` `(i >` `=` `0` `): ` ` ` `if` `(arr[i] !` `=` `num): ` ` ` `break` ` ` `i ` `-` `=` `1` ` ` ` ` `# All elements are equal ` ` ` `# from arr[i + 1] to arr[pos] ` ` ` `return` `i ` `+` `1` ` ` `# Driver code ` `arr ` `=` `[` `2` `, ` `1` `, ` `1` `, ` `1` `, ` `5` `, ` `2` `] ` `n ` `=` `len` `(arr) ` `pos ` `=` `4` ` ` `print` `(minIndex(arr, n, pos)) ` ` ` `# This code is contributed by ` `# Mohit Kumar 29 ` |

*chevron_right*

*filter_none*

## C#

// C# implementation of the approach

using System;

class GFG

{

// Function to return the minimum required index

static int minIndex(int []arr, int n, int pos)

{

int num = arr[pos];

// Start from arr[pos – 1]

int i = pos – 1;

while (i >= 0)

{

if (arr[i] != num)

break;

i–;

}

// All elements are equal

// from arr[i + 1] to arr[pos]

return i + 1;

}

// Driver code

public static void Main()

{

int []arr = { 2, 1, 1, 1, 5, 2 };

int n = arr.Length;

int pos = 4;

Console.WriteLine(minIndex(arr, n, pos));

}

}

// This code is contributed

// by Akanksha Rai

**Output:**

4

## Recommended Posts:

- Count of index pairs with equal elements in an array
- Find a Fixed Point (Value equal to index) in a given array
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Index with Minimum sum of prefix and suffix sums in an Array
- Find last index of a character in a string
- Delete array element in given index range [L - R]
- Find the index of an array element in Java
- Maximum absolute difference of value and index sums
- Find the index of the left pointer after possible moves in the array
- Value of k-th index of a series formed by append and insert MEX in middle
- Find index of first occurrence when an unsorted array is sorted
- Find index of an extra element present in one sorted array
- Minimum increment by k operations to make all elements equal
- Make all array elements equal with minimum cost

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.