给定一个整数数组arr []和一个整数pos ,任务是找到最小索引i ,以使从索引i到索引pos的所有元素都相等。
例子:
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
简单方法:从索引pos – 1开始,反向遍历数组,对于第一个索引i ,以使arr [i]!= arr [pos]打印i + 1 ,这是所需的索引。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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;
// Function Call
cout << minIndex(arr, n, pos);
return 0;
}
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;
// Function Call
System.out.println(minIndex(arr, n, pos));
}
}
// This code is contributed by Code_Mech.
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
# Function Call
print(minIndex(arr, n, pos))
# This code is contributed by
# Mohit Kumar 29
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;
// Function Call
Console.WriteLine(minIndex(arr, n, pos));
}
}
// This code is contributed
// by Akanksha Rai
PHP
= 0)
{
if ($arr[$i] != $num)
break;
$i--;
}
// All elements are equal
// from arr[i + 1] to arr[pos]
return $i + 1;
}
// Driver code
$arr = array(2, 1, 1, 1, 5, 2 );
$n = sizeof($arr);
$pos = 4;
echo minIndex($arr, $n, $pos);
// This code is contributed by Ryuga
?>
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum required index
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]) {
// Short-cicuit more comparisions as found
// the border point
break;
}
}
}
// For cases were high = low + 1 and arr[high] will
// match with
// arr[pos] but not arr[low] or arr[mid]. In such
// iteration the if condition will satisfy and loop will
// break post that low will be updated. Hence i will not
// point to the correct index.
return arr[low] == arr[pos] ? low : i;
}
// Driver code
int main()
{
int arr[] = { 2, 1, 1, 1, 5, 2 };
cout << minIndex(arr, 2) << endl; // Should be 1
cout << minIndex(arr, 3) << endl; // Should be 1
cout << minIndex(arr, 4) << endl; // Should be 4
return 0;
}
// This code is contributed by
// anshbikram
Java
// Java implementation of the approach
class GFG {
// Function to return the minimum required index
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]) {
// Short-cicuit more comparisions as
// found the border point
break;
}
}
}
// For cases were high = low + 1 and arr[high] will
// match with arr[pos] but not arr[low] or arr[mid].
// In such iteration the if condition will satisfy
// and loop will break post that low will be
// updated. Hence i will not point to the correct
// index.
return arr[low] == arr[pos] ? low : i;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 1, 1, 1, 5, 2 };
System.out.println(minIndex(arr, 2)); // Should be 1
System.out.println(minIndex(arr, 3)); // Should be 1
System.out.println(minIndex(arr, 4)); // Should be 4
}
}
// This code is contributed by
// anshbikram
Python3
# Python3 implementation of the approach
# Function to return the minimum
# required index
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]:
# Short-cicuit more comparisions as found the border point
break
# For cases were high = low + 1 and arr[high] will match with
# arr[pos] but not arr[low] or arr[mid]. In such iteration
# the if condition will satisfy and loop will break post that
# low will be updated. Hence i will not point to the correct index.
return low if arr[low] == arr[pos] else i
# Driver code
arr = [2, 1, 1, 1, 5, 2]
print(minIndex(arr, 2)) # Should be 1
print(minIndex(arr, 3)) # Should be 1
print(minIndex(arr, 4)) # Should be 4
# This code is contributed by
# anshbikram
C#
// C# implementation of the approach
using System;
class GFG{
// Function to return the minimum
// required index
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])
{
// Short-cicuit more comparisions as
// found the border point
break;
}
}
}
// For cases were high = low + 1 and arr[high] will
// match with arr[pos] but not arr[low] or arr[mid].
// In such iteration the if condition will satisfy
// and loop will break post that low will be
// updated. Hence i will not point to the correct
// index.
return arr[low] == arr[pos] ? low : i;
}
// Driver code
public static void Main()
{
int []arr = { 2, 1, 1, 1, 5, 2 };
Console.WriteLine(minIndex(arr, 2)); // Should be 1
Console.WriteLine(minIndex(arr, 3)); // Should be 1
Console.WriteLine(minIndex(arr, 4)); // Should be 4
}
}
// This code is contributed by chitranayal
输出
4
时间复杂度: O(N)
空间复杂度: O(1)
高效的方法:
在子数组[0,pos-1]中进行二进制搜索。停止条件为arr [mid] == arr [pos] && arr [mid-1]!= arr [pos]。左移或右移分别取决于arr [mid] == arr [pos]。
执行:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum required index
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]) {
// Short-cicuit more comparisions as found
// the border point
break;
}
}
}
// For cases were high = low + 1 and arr[high] will
// match with
// arr[pos] but not arr[low] or arr[mid]. In such
// iteration the if condition will satisfy and loop will
// break post that low will be updated. Hence i will not
// point to the correct index.
return arr[low] == arr[pos] ? low : i;
}
// Driver code
int main()
{
int arr[] = { 2, 1, 1, 1, 5, 2 };
cout << minIndex(arr, 2) << endl; // Should be 1
cout << minIndex(arr, 3) << endl; // Should be 1
cout << minIndex(arr, 4) << endl; // Should be 4
return 0;
}
// This code is contributed by
// anshbikram
Java
// Java implementation of the approach
class GFG {
// Function to return the minimum required index
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]) {
// Short-cicuit more comparisions as
// found the border point
break;
}
}
}
// For cases were high = low + 1 and arr[high] will
// match with arr[pos] but not arr[low] or arr[mid].
// In such iteration the if condition will satisfy
// and loop will break post that low will be
// updated. Hence i will not point to the correct
// index.
return arr[low] == arr[pos] ? low : i;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 1, 1, 1, 5, 2 };
System.out.println(minIndex(arr, 2)); // Should be 1
System.out.println(minIndex(arr, 3)); // Should be 1
System.out.println(minIndex(arr, 4)); // Should be 4
}
}
// This code is contributed by
// anshbikram
Python3
# Python3 implementation of the approach
# Function to return the minimum
# required index
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]:
# Short-cicuit more comparisions as found the border point
break
# For cases were high = low + 1 and arr[high] will match with
# arr[pos] but not arr[low] or arr[mid]. In such iteration
# the if condition will satisfy and loop will break post that
# low will be updated. Hence i will not point to the correct index.
return low if arr[low] == arr[pos] else i
# Driver code
arr = [2, 1, 1, 1, 5, 2]
print(minIndex(arr, 2)) # Should be 1
print(minIndex(arr, 3)) # Should be 1
print(minIndex(arr, 4)) # Should be 4
# This code is contributed by
# anshbikram
C#
// C# implementation of the approach
using System;
class GFG{
// Function to return the minimum
// required index
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])
{
// Short-cicuit more comparisions as
// found the border point
break;
}
}
}
// For cases were high = low + 1 and arr[high] will
// match with arr[pos] but not arr[low] or arr[mid].
// In such iteration the if condition will satisfy
// and loop will break post that low will be
// updated. Hence i will not point to the correct
// index.
return arr[low] == arr[pos] ? low : i;
}
// Driver code
public static void Main()
{
int []arr = { 2, 1, 1, 1, 5, 2 };
Console.WriteLine(minIndex(arr, 2)); // Should be 1
Console.WriteLine(minIndex(arr, 3)); // Should be 1
Console.WriteLine(minIndex(arr, 4)); // Should be 4
}
}
// This code is contributed by chitranayal
输出
1
1
4
时间复杂度: O(log(n))
空间复杂度: O(1)