📌  相关文章
📜  查找一个排序数组中存在的额外元素的索引

📅  最后修改于: 2021-04-28 13:59:00             🧑  作者: Mango

给定两个排序的数组。阵列之间只有1个差异。第一个数组之间有一个额外的元素。查找额外元素的索引。

例子:

Input: {2, 4, 6, 8, 9, 10, 12};
       {2, 4, 6, 8, 10, 12};
Output: 4
Explanation: The first array has an extra element 9.
The extra element is present at index 4.

Input: {3, 5, 7, 9, 11, 13}
        {3, 5, 7, 11, 13}
Output: 3
Explanation: The first array has an extra element 9.
The extra element is present at index 3.

方法1:这包括解决此特定问题的基本方法。

方法:基本方法是遍历整个第二个数组,并逐个元素地检查它们是否不同。在对数组进行排序时,检查两个数组的相邻位置应该相似,直到找到丢失的元素为止。

算法:

  1. 从头到尾遍历整个数组。
  2. 检查两个数组的第i个元素的元素是否相似。
  3. 如果元素不相似,则打印索引并中断

执行:

C++
// C++ program to find an extra
// element present in arr1[]
#include 
using namespace std;
 
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
int findExtra(int arr1[],
              int arr2[], int n)
{
for (int i = 0; i < n; i++)
    if (arr1[i] != arr2[i])
        return i;
 
return n;
}
 
// Driver code
int main()
{
    int arr1[] = {2, 4, 6, 8,
                  10, 12, 13};
    int arr2[] = {2, 4, 6,
                  8, 10, 12};
    int n = sizeof(arr2) / sizeof(arr2[0]);
 
    // Solve is passed both arrays
    cout << findExtra(arr1, arr2, n);
    return 0;
}


Java
// Java program to find an extra
// element present in arr1[]
class GFG
{
 
    // Returns index of extra element
    // in arr1[]. n is size of arr2[].
    // Size of arr1[] is n-1.
    static int findExtra(int arr1[],
                         int arr2[], int n)
    {
    for (int i = 0; i < n; i++)
        if (arr1[i] != arr2[i])
            return i;
     
    return n;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr1[] = {2, 4, 6, 8,
                      10, 12, 13};
        int arr2[] = {2, 4, 6,
                      8, 10, 12};
        int n = arr2.length;
     
        // Solve is passed both arrays
        System.out.println(findExtra(arr1,
                                     arr2, n));
    }
}
 
// This code is contributed by Harsh Agarwal


Python3
# Python 3 program to find an
# extra element present in arr1[]
 
 
# Returns index of extra .
# element in arr1[] n is
# size of arr2[]. Size of
# arr1[] is n-1.
def findExtra(arr1, arr2, n) :
    for i in range(0, n) :
        if (arr1[i] != arr2[i]) :
            return i
 
    return n
 
 
# Driver code
arr1 = [2, 4, 6, 8,  10, 12, 13]
arr2 = [2, 4, 6, 8, 10, 12]
n = len(arr2)
 
# Solve is passed both arrays
print(findExtra(arr1, arr2, n))
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to find an extra
// element present in arr1[]
using System;
 
class GfG
{
     
    // Returns index of extra
    // element in arr1[]. n is
    // size of arr2[]. Size of
    // arr1[] is n-1.
    static int findExtra(int []arr1,
                         int []arr2, int n)
    {
        for (int i = 0; i < n; i++)
            if (arr1[i] != arr2[i])
                return i;
         
        return n;
    }
     
    // Driver code
    public static void Main ()
    {
        int []arr1 = {2, 4, 6, 8,
                      10, 12, 13};
        int []arr2 = {2, 4, 6,
                      8, 10, 12};
        int n = arr2.Length;
     
        // Solve is passed both arrays
        Console.Write(findExtra(arr1, arr2, n));
    }
}
 
// This code is contributed by parashar.


PHP


Javascript


C++
// C++ program to find an extra
// element present in arr1[]
#include 
using namespace std;
 
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
int findExtra(int arr1[],
              int arr2[], int n)
{
    // Initialize result
    int index = n;
 
    // left and right are end
    // points denoting the current range.
    int left = 0, right = n - 1;
    while (left <= right)
    {
        int mid = (left + right) / 2;
 
        // If middle element is same
        // of both arrays, it means
        // that extra element is after
        // mid so we update left to mid+1
        if (arr2[mid] == arr1[mid])
            left = mid + 1;
 
        // If middle element is different
        // of the arrays, it means that
        // the index we are searching for
        // is either mid, or before mid.
        // Hence we update right to mid-1.
        else
        {
            index = mid;
            right = mid - 1;
        }
    }
 
    // when right is greater than
    // left our search is complete.
    return index;
}
 
// Driver code
int main()
{
    int arr1[] = {2, 4, 6, 8, 10, 12, 13};
    int arr2[] = {2, 4, 6, 8, 10, 12};
    int n = sizeof(arr2) / sizeof(arr2[0]);
 
    // Solve is passed both arrays
    cout << findExtra(arr1, arr2, n);
    return 0;
}


Java
// Java program to find an extra
// element present in arr1[]
class GFG
{
    // Returns index of extra element
    // in arr1[]. n is size of arr2[].
    // Size of arr1[] is n-1.
    static int findExtra(int arr1[],
                         int arr2[], int n)
    {
        // Initialize result
        int index = n;
     
        // left and right are end
        // points denoting the current range.
        int left = 0, right = n - 1;
        while (left <= right)
        {
            int mid = (left+right) / 2;
     
            // If middle element is same
            // of both arrays, it means
            // that extra element is after
            // mid so we update left to mid+1
            if (arr2[mid] == arr1[mid])
                left = mid + 1;
     
            // If middle element is different
            // of the arrays, it means that
            // the index we are searching for
            // is either mid, or before mid.
            // Hence we update right to mid-1.
            else
            {
                index = mid;
                right = mid - 1;
            }
        }
     
        // when right is greater than
        // left, our search is complete.
        return index;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr1[] = {2, 4, 6, 8, 10, 12,13};
        int arr2[] = {2, 4, 6, 8, 10, 12};
        int n = arr2.length;
     
        // Solve is passed both arrays
        System.out.println(findExtra(arr1, arr2, n));
    }
}
 
// This code is contributed by Harsh Agarwal


Python3
# Python3 program to find an extra
# element present in arr1[]
 
# Returns index of extra element
# in arr1[]. n is size of arr2[].
# Size of arr1[] is n-1.
def findExtra(arr1, arr2, n) :
 
    index = n # Initialize result
 
    # left and right are end points
    # denoting the current range.
    left = 0
    right = n - 1
    while (left <= right) :
        mid = (int)((left + right) / 2)
 
        # If middle element is same
        # of both arrays, it means
        # that extra element is after
        # mid so we update left to
        # mid + 1
        if (arr2[mid] == arr1[mid]) :
            left = mid + 1
 
        # If middle element is different
        # of the arrays, it means that
        # the index we are searching for
        # is either mid, or before mid.
        # Hence we update right to mid-1.
        else :
            index = mid
            right = mid - 1
         
    # when right is greater than left our
    # search is complete.
    return index
 
# Driver code
arr1 = [2, 4, 6, 8, 10, 12, 13]
arr2 = [2, 4, 6, 8, 10, 12]
n = len(arr2)
 
# Solve is passed both arrays
print(findExtra(arr1, arr2, n))
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find an extra
// element present in arr1[]
using System;
 
class GFG {
     
    // Returns index of extra
    // element in arr1[]. n is
    // size of arr2[].
    // Size of arr1[] is
    // n - 1.
    static int findExtra(int []arr1,
                         int []arr2,
                         int n)
    {
         
        // Initialize result
        int index = n;
     
        // left and right are
        // end points denoting
        // the current range.
        int left = 0, right = n - 1;
        while (left <= right)
        {
            int mid = (left+right) / 2;
     
            // If middle element is
            // same of both arrays,
            // it means that extra
            // element is after mid
            // so we update left
            // to mid + 1
            if (arr2[mid] == arr1[mid])
                left = mid + 1;
     
            // If middle element is
            // different of the arrays,
            // it means that the index
            // we are searching for is
            // either mid, or before mid.
            // Hence we update right to mid-1.
            else
            {
                index = mid;
                right = mid - 1;
            }
        }
     
        // when right is greater
        // than left our
        // search is complete.
        return index;
    }
     
    // Driver Code
    public static void Main ()
    {
        int []arr1 = {2, 4, 6, 8, 10, 12,13};
        int []arr2 = {2, 4, 6, 8, 10, 12};
        int n = arr2.Length;
     
        // Solve is passed
        // both arrays
        Console.Write(findExtra(arr1, arr2, n));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// C++ code for above approach
#include
using namespace std;
 
// function return sum of array elements
int sum(int arr[], int n)
{
    int summ = 0;
    for (int i = 0; i < n; i++)
    {
        summ += arr[i];
    }
    return summ;
}
 
// function return index of given element
int indexOf(int arr[], int element, int n)
{
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == element)
        {
            return i;
        }
    }
    return -1;
}
 
// Function to find Index
int find_extra_element_index(int arrA[],
                             int arrB[],
                             int n, int m)
{
 
    // Calculating extra element
    int extra_element = sum(arrA, n) -
                        sum(arrB, m);
     
    // returns index of extra element
    return indexOf(arrA, extra_element, n);
}
 
// Driver Code
int main()
{
    int arrA[] = {2, 4, 6, 8, 10, 12, 13};
    int arrB[] = {2, 4, 6, 8, 10, 12};
    int n = sizeof(arrA) / sizeof(arrA[0]);
    int m = sizeof(arrB) / sizeof(arrB[0]);
    cout << find_extra_element_index(arrA, arrB, n, m);
}
 
// This code is contributed by mohit kumar


Java
// Java code for above approach
class GFG
{
 
    // Function to find Index
    static int find_extra_element_index(int[] arrA,
                                        int[] arrB)
    {
 
        // Calculating extra element
        int extra_element = sum(arrA) - sum(arrB);
         
        // returns index of extra element
        return indexOf(arrA, extra_element);
    }
     
    // function return sum of array elements
    static int sum(int[] arr)
    {
        int sum = 0;
        for (int i = 0; i < arr.length; i++)
        {
            sum += arr[i];
        }
        return sum;
    }
     
    // function return index of given element
    static int indexOf(int[] arr, int element)
    {
        for (int i = 0; i < arr.length; i++)
        {
            if (arr[i] == element)
            {
                return i;
            }
        }
        return -1;
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int[] arrA = {2, 4, 6, 8, 10, 12, 13};
        int[] arrB = {2, 4, 6, 8, 10, 12};
        System.out.println(find_extra_element_index(arrA, arrB));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 code for above approach
 
# Function to find Index
def find_extra_element_index(arrA, arrB):
     
    # Calculating extra element
    extra_element = sum(arrA) - sum(arrB)
     
    # returns index of extra element
    return arrA.index(extra_element)
 
# Driver Code
arrA = [2, 4, 6, 8, 10, 12, 13]
arrB = [2, 4, 6, 8, 10, 12]
print(find_extra_element_index(arrA,arrB))
 
# This code is contributed by Dravid


C#
// C# code for above approach
using System;
 
class GFG
{
 
    // Function to find Index
    static int find_extra_element_index(int[] arrA,
                                        int[] arrB)
    {
 
        // Calculating extra element
        int extra_element = sum(arrA) - sum(arrB);
         
        // returns index of extra element
        return indexOf(arrA, extra_element);
    }
     
    // function return sum of array elements
    static int sum(int[] arr)
    {
        int sum = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            sum += arr[i];
        }
        return sum;
    }
     
    // function return index of given element
    static int indexOf(int[] arr, int element)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] == element)
            {
                return i;
            }
        }
        return -1;
    }
     
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arrA = {2, 4, 6, 8, 10, 12, 13};
        int[] arrB = {2, 4, 6, 8, 10, 12};
        Console.WriteLine(find_extra_element_index(arrA, arrB));
    }
}
 
// This code has been contributed by 29AjayKumar


输出 :

6

复杂度分析:

  • 时间复杂度: O(n)。
    由于需要遍历数组,因此时间复杂度是线性的。
  • 空间复杂度: O(1)。
    由于不需要额外的空间,因此时间复杂度是恒定的。

方法2:此方法是解决上述问题的一种更好的方法,它使用了二进制搜索的概念。

方法:要在少于线性时间内找到丢失元素的索引,可以使用二进制搜索,其思想是所有大于或等于丢失元素索引的索引在数组和所有元素中都有不同的元素小于该索引的索引将在两个数组中具有相似的元素。

算法:

  1. 创建三个变量, low = 0high = n-1midans = n
  2. 循环运行,直到low小于或等于high,即直到我们的搜索范围小于零。
  3. 如果两个数组的中间元素(即(低+高)/ 2)相似,则将搜索更新到搜索范围的后一半,即低=中间+ 1
  4. 否则,将搜索更新到搜索范围的前一半,即high = mid – 1 ,并更新当前索引的答案ans = mid
  5. 打印索引。

执行:

C++

// C++ program to find an extra
// element present in arr1[]
#include 
using namespace std;
 
// Returns index of extra element
// in arr1[]. n is size of arr2[].
// Size of arr1[] is n-1.
int findExtra(int arr1[],
              int arr2[], int n)
{
    // Initialize result
    int index = n;
 
    // left and right are end
    // points denoting the current range.
    int left = 0, right = n - 1;
    while (left <= right)
    {
        int mid = (left + right) / 2;
 
        // If middle element is same
        // of both arrays, it means
        // that extra element is after
        // mid so we update left to mid+1
        if (arr2[mid] == arr1[mid])
            left = mid + 1;
 
        // If middle element is different
        // of the arrays, it means that
        // the index we are searching for
        // is either mid, or before mid.
        // Hence we update right to mid-1.
        else
        {
            index = mid;
            right = mid - 1;
        }
    }
 
    // when right is greater than
    // left our search is complete.
    return index;
}
 
// Driver code
int main()
{
    int arr1[] = {2, 4, 6, 8, 10, 12, 13};
    int arr2[] = {2, 4, 6, 8, 10, 12};
    int n = sizeof(arr2) / sizeof(arr2[0]);
 
    // Solve is passed both arrays
    cout << findExtra(arr1, arr2, n);
    return 0;
}

Java

// Java program to find an extra
// element present in arr1[]
class GFG
{
    // Returns index of extra element
    // in arr1[]. n is size of arr2[].
    // Size of arr1[] is n-1.
    static int findExtra(int arr1[],
                         int arr2[], int n)
    {
        // Initialize result
        int index = n;
     
        // left and right are end
        // points denoting the current range.
        int left = 0, right = n - 1;
        while (left <= right)
        {
            int mid = (left+right) / 2;
     
            // If middle element is same
            // of both arrays, it means
            // that extra element is after
            // mid so we update left to mid+1
            if (arr2[mid] == arr1[mid])
                left = mid + 1;
     
            // If middle element is different
            // of the arrays, it means that
            // the index we are searching for
            // is either mid, or before mid.
            // Hence we update right to mid-1.
            else
            {
                index = mid;
                right = mid - 1;
            }
        }
     
        // when right is greater than
        // left, our search is complete.
        return index;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr1[] = {2, 4, 6, 8, 10, 12,13};
        int arr2[] = {2, 4, 6, 8, 10, 12};
        int n = arr2.length;
     
        // Solve is passed both arrays
        System.out.println(findExtra(arr1, arr2, n));
    }
}
 
// This code is contributed by Harsh Agarwal

Python3

# Python3 program to find an extra
# element present in arr1[]
 
# Returns index of extra element
# in arr1[]. n is size of arr2[].
# Size of arr1[] is n-1.
def findExtra(arr1, arr2, n) :
 
    index = n # Initialize result
 
    # left and right are end points
    # denoting the current range.
    left = 0
    right = n - 1
    while (left <= right) :
        mid = (int)((left + right) / 2)
 
        # If middle element is same
        # of both arrays, it means
        # that extra element is after
        # mid so we update left to
        # mid + 1
        if (arr2[mid] == arr1[mid]) :
            left = mid + 1
 
        # If middle element is different
        # of the arrays, it means that
        # the index we are searching for
        # is either mid, or before mid.
        # Hence we update right to mid-1.
        else :
            index = mid
            right = mid - 1
         
    # when right is greater than left our
    # search is complete.
    return index
 
# Driver code
arr1 = [2, 4, 6, 8, 10, 12, 13]
arr2 = [2, 4, 6, 8, 10, 12]
n = len(arr2)
 
# Solve is passed both arrays
print(findExtra(arr1, arr2, n))
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to find an extra
// element present in arr1[]
using System;
 
class GFG {
     
    // Returns index of extra
    // element in arr1[]. n is
    // size of arr2[].
    // Size of arr1[] is
    // n - 1.
    static int findExtra(int []arr1,
                         int []arr2,
                         int n)
    {
         
        // Initialize result
        int index = n;
     
        // left and right are
        // end points denoting
        // the current range.
        int left = 0, right = n - 1;
        while (left <= right)
        {
            int mid = (left+right) / 2;
     
            // If middle element is
            // same of both arrays,
            // it means that extra
            // element is after mid
            // so we update left
            // to mid + 1
            if (arr2[mid] == arr1[mid])
                left = mid + 1;
     
            // If middle element is
            // different of the arrays,
            // it means that the index
            // we are searching for is
            // either mid, or before mid.
            // Hence we update right to mid-1.
            else
            {
                index = mid;
                right = mid - 1;
            }
        }
     
        // when right is greater
        // than left our
        // search is complete.
        return index;
    }
     
    // Driver Code
    public static void Main ()
    {
        int []arr1 = {2, 4, 6, 8, 10, 12,13};
        int []arr2 = {2, 4, 6, 8, 10, 12};
        int n = arr2.Length;
     
        // Solve is passed
        // both arrays
        Console.Write(findExtra(arr1, arr2, n));
    }
}
 
// This code is contributed by nitin mittal.

的PHP


Java脚本


输出 :

6

复杂度分析:

  • 时间复杂度: O(log n)。
    二进制搜索的时间复杂度为O(log n)
  • 空间复杂度: O(1)。
    由于不需要额外的空间,因此时间复杂度是恒定的。

方法3:此方法使用预定义函数解决了给定的问题。

方法:要找到不同的元素,请找到每个数组的总和,然后减去总和得出绝对值。搜索更大的数组,并检查绝对值是否等于索引,然后返回该索引。如果缺少一个元素,而所有其他元素都相同,则总和之差将等于缺少的元素。

算法:

  1. 创建一个函数来计算两个Arary的和。
  2. 找出两个数组之和( value )之间的绝对差。
  3. 从头到尾遍历较大的数组
  4. 如果任何索引处的元素等于value,则打印索引并中断循环。

执行:

C++

// C++ code for above approach
#include
using namespace std;
 
// function return sum of array elements
int sum(int arr[], int n)
{
    int summ = 0;
    for (int i = 0; i < n; i++)
    {
        summ += arr[i];
    }
    return summ;
}
 
// function return index of given element
int indexOf(int arr[], int element, int n)
{
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == element)
        {
            return i;
        }
    }
    return -1;
}
 
// Function to find Index
int find_extra_element_index(int arrA[],
                             int arrB[],
                             int n, int m)
{
 
    // Calculating extra element
    int extra_element = sum(arrA, n) -
                        sum(arrB, m);
     
    // returns index of extra element
    return indexOf(arrA, extra_element, n);
}
 
// Driver Code
int main()
{
    int arrA[] = {2, 4, 6, 8, 10, 12, 13};
    int arrB[] = {2, 4, 6, 8, 10, 12};
    int n = sizeof(arrA) / sizeof(arrA[0]);
    int m = sizeof(arrB) / sizeof(arrB[0]);
    cout << find_extra_element_index(arrA, arrB, n, m);
}
 
// This code is contributed by mohit kumar

Java

// Java code for above approach
class GFG
{
 
    // Function to find Index
    static int find_extra_element_index(int[] arrA,
                                        int[] arrB)
    {
 
        // Calculating extra element
        int extra_element = sum(arrA) - sum(arrB);
         
        // returns index of extra element
        return indexOf(arrA, extra_element);
    }
     
    // function return sum of array elements
    static int sum(int[] arr)
    {
        int sum = 0;
        for (int i = 0; i < arr.length; i++)
        {
            sum += arr[i];
        }
        return sum;
    }
     
    // function return index of given element
    static int indexOf(int[] arr, int element)
    {
        for (int i = 0; i < arr.length; i++)
        {
            if (arr[i] == element)
            {
                return i;
            }
        }
        return -1;
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int[] arrA = {2, 4, 6, 8, 10, 12, 13};
        int[] arrB = {2, 4, 6, 8, 10, 12};
        System.out.println(find_extra_element_index(arrA, arrB));
    }
}
 
/* This code contributed by PrinciRaj1992 */

Python3

# Python3 code for above approach
 
# Function to find Index
def find_extra_element_index(arrA, arrB):
     
    # Calculating extra element
    extra_element = sum(arrA) - sum(arrB)
     
    # returns index of extra element
    return arrA.index(extra_element)
 
# Driver Code
arrA = [2, 4, 6, 8, 10, 12, 13]
arrB = [2, 4, 6, 8, 10, 12]
print(find_extra_element_index(arrA,arrB))
 
# This code is contributed by Dravid

C#

// C# code for above approach
using System;
 
class GFG
{
 
    // Function to find Index
    static int find_extra_element_index(int[] arrA,
                                        int[] arrB)
    {
 
        // Calculating extra element
        int extra_element = sum(arrA) - sum(arrB);
         
        // returns index of extra element
        return indexOf(arrA, extra_element);
    }
     
    // function return sum of array elements
    static int sum(int[] arr)
    {
        int sum = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            sum += arr[i];
        }
        return sum;
    }
     
    // function return index of given element
    static int indexOf(int[] arr, int element)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] == element)
            {
                return i;
            }
        }
        return -1;
    }
     
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arrA = {2, 4, 6, 8, 10, 12, 13};
        int[] arrB = {2, 4, 6, 8, 10, 12};
        Console.WriteLine(find_extra_element_index(arrA, arrB));
    }
}
 
// This code has been contributed by 29AjayKumar

输出 :

6

复杂度分析:

  • 时间复杂度: O(n)。
    由于只需要遍历数组三个遍历,因此时间复杂度是线性的。
  • 空间复杂度: O(1)。
    由于不需要额外的空间,因此时间复杂度是恒定的。