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
Brute Force Approach:
A brute force approach to solve this problem is to use two nested loops. The outer loop iterates through all the indices from 0 to pos, and the inner loop checks if all the elements from the current index to pos are equal. If they are equal, the current index is the minimum required index, and the loops can be terminated.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minIndex( int arr[], int n, int pos)
{
for ( int i = 0; i <= pos; i++) {
int j;
for (j = i + 1; j <= pos; j++) {
if (arr[j] != arr[i]) {
break ;
}
}
if (j == pos + 1) {
return i;
}
}
return -1;
}
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;
}
|
Java
import java.util.*;
public class Main {
static int minIndex( int arr[], int n, int pos) {
for ( int i = 0 ; i <= pos; i++) {
int j;
for (j = i + 1 ; j <= pos; j++) {
if (arr[j] != arr[i]) {
break ;
}
}
if (j == pos + 1 ) {
return i;
}
}
return - 1 ;
}
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));
}
}
|
Javascript
function minIndex(arr, n, pos) {
for (let i = 0; i <= pos; i++) {
let j;
for (j = i + 1; j <= pos; j++) {
if (arr[j] !== arr[i]) {
break ;
}
}
if (j === pos + 1) {
return i;
}
}
return -1;
}
const arr = [2, 1, 1, 1, 5, 2];
const n = arr.length;
const pos = 4;
console.log(minIndex(arr, n, pos));
|
Python3
def minIndex(arr, n, pos):
for i in range (pos + 1 ):
j = i + 1
while j < = pos:
if arr[j] ! = arr[i]:
break
j + = 1
if j = = pos + 1 :
return i
return - 1
arr = [ 2 , 1 , 1 , 1 , 5 , 2 ]
n = len (arr)
pos = 4
print (minIndex(arr, n, pos))
|
C#
using System;
class Program {
static int minIndex( int [] arr, int n, int pos) {
for ( int i = 0; i <= pos; i++) {
int j;
for (j = i + 1; j <= pos; j++) {
if (arr[j] != arr[i]) {
break ;
}
}
if (j == pos + 1) {
return i;
}
}
return -1;
}
static void Main( string [] args) {
int [] arr = { 2, 1, 1, 1, 5, 2 };
int n = arr.Length;
int pos = 4;
Console.WriteLine(minIndex(arr, n, pos));
}
}
|
Output: 4
Time Complexity: O(n^2)
Space Complexity: O(1)
Simple 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++
#include <bits/stdc++.h>
using namespace std;
int minIndex( int arr[], int n, int pos)
{
int num = arr[pos];
int i = pos - 1;
while (i >= 0) {
if (arr[i] != num)
break ;
i--;
}
return i + 1;
}
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;
}
|
Java
import java.io.*;
public class GFG {
static int minIndex( int arr[], int n, int pos)
{
int num = arr[pos];
int i = pos - 1 ;
while (i >= 0 ) {
if (arr[i] != num)
break ;
i--;
}
return i + 1 ;
}
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));
}
}
|
Python3
def minIndex(arr, n, pos):
num = arr[pos]
i = pos - 1
while (i > = 0 ):
if (arr[i] ! = num):
break
i - = 1
return i + 1
arr = [ 2 , 1 , 1 , 1 , 5 , 2 ]
n = len (arr)
pos = 4
print (minIndex(arr, n, pos))
|
C#
using System;
class GFG {
static int minIndex( int [] arr, int n, int pos)
{
int num = arr[pos];
int i = pos - 1;
while (i >= 0) {
if (arr[i] != num)
break ;
i--;
}
return i + 1;
}
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));
}
}
|
PHP
<?php
function minIndex( $arr , $n , $pos )
{
$num = $arr [ $pos ];
$i = $pos - 1;
while ( $i >= 0)
{
if ( $arr [ $i ] != $num )
break ;
$i --;
}
return $i + 1;
}
$arr = array (2, 1, 1, 1, 5, 2 );
$n = sizeof( $arr );
$pos = 4;
echo minIndex( $arr , $n , $pos );
?>
|
Javascript
<script>
function minIndex(arr, n, pos)
{
var num = arr[pos];
var i = pos - 1;
while (i >= 0) {
if (arr[i] != num)
break ;
i--;
}
return i + 1;
}
var arr = [ 2, 1, 1, 1, 5, 2 ];
var n = arr.length;
var pos = 4;
document.write(minIndex(arr, n, pos));
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Efficient Approach :
Do a binary search in the sub-array [0, pos-1]. Stop condition will be if arr[mid] == arr[pos] && arr[mid-1] != arr[pos]. Go-left or Go-right will depend on if arr[mid] == arr[pos] or not respectively.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minIndex( int arr[], int pos)
{
int low = 0;
int high = pos;
int i = pos;
while (low < high) {
int mid = (low + high) / 2;
if (arr[mid] != arr[pos]) {
low = mid + 1;
}
else {
high = mid - 1;
i = mid;
if (mid > 0 && arr[mid - 1] != arr[pos]) {
break ;
}
}
}
return arr[low] == arr[pos] ? low : i;
}
int main()
{
int arr[] = { 2, 1, 1, 1, 5, 2 };
cout << minIndex(arr, 2) << endl;
cout << minIndex(arr, 3) << endl;
cout << minIndex(arr, 4) << endl;
return 0;
}
|
Java
public class GFG {
static int minIndex( int arr[], int pos)
{
int low = 0 ;
int high = pos;
int i = pos;
while (low < high) {
int mid = (low + high) / 2 ;
if (arr[mid] != arr[pos]) {
low = mid + 1 ;
}
else {
high = mid - 1 ;
i = mid;
if (mid > 0 && arr[mid - 1 ] != arr[pos]) {
break ;
}
}
}
return arr[low] == arr[pos] ? low : i;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 1 , 1 , 5 , 2 };
System.out.println(minIndex(arr, 2 ));
System.out.println(minIndex(arr, 3 ));
System.out.println(minIndex(arr, 4 ));
}
}
|
Python3
def minIndex(arr, pos):
low = 0
high = pos
i = pos
while low < high:
mid = (low + high) / / 2
if arr[mid] ! = arr[pos]:
low = mid + 1
else :
high = mid - 1
i = mid
if mid > 0 and arr[mid - 1 ] ! = arr[pos]:
break
return low if arr[low] = = arr[pos] else i
arr = [ 2 , 1 , 1 , 1 , 5 , 2 ]
print (minIndex(arr, 2 ))
print (minIndex(arr, 3 ))
print (minIndex(arr, 4 ))
|
C#
using System;
class GFG{
static int minIndex( int []arr, int pos)
{
int low = 0;
int high = pos;
int i = pos;
while (low < high)
{
int mid = (low + high) / 2;
if (arr[mid] != arr[pos])
{
low = mid + 1;
}
else
{
high = mid - 1;
i = mid;
if (mid > 0 && arr[mid - 1] != arr[pos])
{
break ;
}
}
}
return arr[low] == arr[pos] ? low : i;
}
public static void Main()
{
int []arr = { 2, 1, 1, 1, 5, 2 };
Console.WriteLine(minIndex(arr, 2));
Console.WriteLine(minIndex(arr, 3));
Console.WriteLine(minIndex(arr, 4));
}
}
|
Javascript
<script>
function minIndex(arr , pos) {
var low = 0;
var high = pos;
var i = pos;
while (low < high) {
var mid = parseInt((low + high) / 2);
if (arr[mid] != arr[pos]) {
low = mid + 1;
} else {
high = mid - 1;
i = mid;
if (mid > 0 && arr[mid - 1] != arr[pos]) {
break ;
}
}
}
return arr[low] == arr[pos] ? low : i;
}
var arr = [ 2, 1, 1, 1, 5, 2 ];
document.write(minIndex(arr, 2)+ "<br/>" );
document.write(minIndex(arr, 3)+ "<br/>" );
document.write(minIndex(arr, 4)+ "<br/>" );
</script>
|
Time Complexity: O(log(n))
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Apr, 2023
Like Article
Save Article