📌  相关文章
📜  在给定数组中找到一个不动点(值等于索引)|允许重复

📅  最后修改于: 2022-05-13 01:57:47.880000             🧑  作者: Mango

在给定数组中找到一个不动点(值等于索引)|允许重复

给定一个按升序排序的 n 个整数数组,编写一个函数,返回数组中的定点,如果数组中存在定点,则返回 -1。数组中的定点是索引 i 使得 arr[i] 等于 i。请注意,数组中的整数可以是负数。
例子:

Input: arr[] = {-10, -5, 0, 3, 7}
  Output: 3  // arr[3] == 3 

  Input: arr[] = {-10, -5, 2, 2, 2, 3, 4, 7, 9, 12, 13}
  Output: 2  // arr[2] == 2 

  Input: arr[] = {-10, -5, 3, 4, 7, 9}
  Output: -1  // No Fixed Point

我们有一个解决方案可以在不同元素的数组中找到固定点。在这篇文章中,讨论了具有重复值的数组的解决方案。
考虑 arr[] = {-10, -5, 2, 2, 2, 3, 4, 7, 9, 12, 13}, arr[mid] = 3
如果元素不是不同的,那么我们看到 arr[mid] < mid,我们无法断定固定值在哪一侧。它可以在左侧或右侧。
我们肯定知道,因为 arr[5] = 3,所以 arr[4] 不可能是魔术索引,因为 arr[4] 必须小于或等于 arr[5](数组是有序的)。
因此,我们搜索的一般模式是:

  • 左侧:start = start, end = min(arr[midIndex], midIndex-1)
  • 右侧:start = max(arr[midIndex], midIndex+1), end = end

以下是上述算法的代码。

C++
// CPP Program to find magic index.
#include 
using namespace std;
 
int magicIndex(int* arr, int start, int end)
{
    // If No Magic Index return -1;
    if (start > end)
        return -1;
 
    int midIndex = (start + end) / 2;
    int midValue = arr[midIndex];
 
    // Magic Index Found, return it.
    if (midIndex == midValue)
        return midIndex;
 
    // Search on Left side
    int left = magicIndex(arr, start, min(midValue,
                                     midIndex - 1));
 
    // If Found on left side, return.
    if (left >= 0)
        return left;
 
    // Return ans from right side.
    return magicIndex(arr, max(midValue, midIndex + 1),
                                                  end);
}
 
// Driver program
int main()
{
    int arr[] = { -10, -5, 2, 2, 2, 3, 4, 7,
                                 9, 12, 13 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int index = magicIndex(arr, 0, n - 1);
    if (index == -1)
        cout << "No Magic Index";
    else
        cout << "Magic Index is : " << index;
    return 0;
}


Java
// Java Program to find magic index.
 
class GFG {
     
    static int magicIndex(int arr[], int start, int end)
    {
        // If No Magic Index return -1;
        if (start > end)
            return -1;
     
        int midIndex = (start + end) / 2;
        int midValue = arr[midIndex];
     
        // Magic Index Found, return it.
        if (midIndex == midValue)
            return midIndex;
     
        // Search on Left side
        int left = magicIndex(arr, start, Math.min(midValue,
                                            midIndex - 1));
     
        // If Found on left side, return.
        if (left >= 0)
            return left;
     
        // Return ans from right side.
        return magicIndex(arr, Math.max(midValue,
                                midIndex + 1),end);
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { -10, -5, 2, 2, 2, 3, 4, 7,
                    9, 12, 13 };
        int n = arr.length;
        int index = magicIndex(arr, 0, n - 1);
        if (index == -1)
            System.out.print("No Magic Index");
        else
            System.out.print("Magic Index is : "+index);
    }
}
 
// This code is contributed by Anant Agarwal.


Python 3
# Python 3 Program to find
# magic index.
 
def magicIndex(arr, start, end):
 
    # If No Magic Index return -1
    if (start > end):
        return -1
 
    midIndex = int((start + end) / 2)
    midValue = arr[midIndex]
 
    # Magic Index Found, return it.
    if (midIndex == midValue):
        return midIndex
 
    # Search on Left side
    left = magicIndex(arr, start, min(midValue,
                                midIndex - 1))
 
    # If Found on left side, return.
    if (left >= 0):
        return left
 
    # Return ans from right side.
    return magicIndex(arr, max(midValue,
                        midIndex + 1),
                                    end)
 
# Driver program
arr = [-10, -5, 2, 2, 2, 3, 4, 7, 9, 12, 13]
n = len(arr)
 
index = magicIndex(arr, 0, n - 1)
 
if (index == -1):
    print("No Magic Index")
else:
    print("Magic Index is :", index)
 
# This code is contributed by Smitha Dinesh Semwal


C#
// C# Program to find magic index.
using System;
 
class GFG {
     
    static int magicIndex(int []arr, int start,
                                    int end)
    {
        // If No Magic Index return -1;
        if (start > end)
            return -1;
     
        int midIndex = (start + end) / 2;
        int midValue = arr[midIndex];
     
        // Magic Index Found, return it.
        if (midIndex == midValue)
            return midIndex;
     
        // Search on Left side
        int left = magicIndex(arr, start, Math.Min(midValue,
                                            midIndex - 1));
     
        // If Found on left side, return.
        if (left >= 0)
            return left;
     
        // Return ans from right side.
        return magicIndex(arr, Math.Max(midValue,
                                midIndex + 1),end);
    }
 
    // Driver code
    public static void Main ()
    {
        int []arr = { -10, -5, 2, 2, 2, 3,
                        4, 7, 9, 12, 13 };
         
        int n = arr.Length;
         
        int index = magicIndex(arr, 0, n - 1);
         
        if (index == -1)
            Console.WriteLine("No Magic Index");
        else
            Console.WriteLine("Magic Index is : " +
                                            index);
    }
}
 
// This code is contributed by vt_m.


PHP
 $end)
        return -1;
 
    $midIndex = floor(($start + $end) / 2);
    $midValue = $arr[$midIndex];
 
    // Magic Index Found, return it.
    if ($midIndex == $midValue)
        return $midIndex;
 
    // Search on Left side
    $left = magicIndex($arr, $start,
            min($midValue, $midIndex - 1));
 
    // If Found on left side, return.
    if ($left >= 0)
        return $left;
 
    // Return ans from right side.
    return magicIndex($arr, max($midValue,
                     $midIndex + 1), $end);
}
     
    // Driver Code
    $arr = array(-10, -5, 2, 2, 2, 3,
                     4, 7, 9, 12, 13);
    $n = sizeof($arr);
    $index = magicIndex($arr, 0, $n - 1);
    if ($index == -1)
        echo "No Magic Index";
    else
        echo "Magic Index is : " , $index;
     
// This code is contributed by nitin mittal
?>


Javascript


输出:

Magic Index is : 2

时间复杂度: O(N)

辅助空间: O(1)