Se le da una array ordenada de N enteros de 1 a N con un número que falta encontrar el número que falta Complejidad de tiempo esperada O (logn)
Ejemplos:
Input :ar[] = {1, 3, 4, 5} Output : 2 Input : ar[] = {1, 2, 3, 4, 5, 7, 8} Output : 6
Una solución simple es atravesar linealmente la array dada. Encuentre el punto donde el elemento actual no es uno más que el anterior.
Una solución eficiente es utilizar la búsqueda binaria . Usamos el índice para buscar el elemento que falta y la búsqueda binaria modificada. Si el elemento en mid != index+1 y este es el primer elemento faltante, mid + 1 es el elemento faltante. De lo contrario, si este no es el primer elemento faltante sino ar[mid] != mid+1 busca en la mitad izquierda. De lo contrario, busque en la mitad derecha y, si izquierda>derecha, no falta ningún elemento.
C++
// CPP program to find the only missing element. #include <iostream> using namespace std; int findmissing(int ar[], int N) { int l = 0, r = N - 1; while (l <= r) { int mid = (l + r) / 2; // If this is the first element // which is not index + 1, then // missing element is mid+1 if (ar[mid] != mid + 1 && ar[mid - 1] == mid) return mid + 1; // if this is not the first missing // element search in left side if (ar[mid] != mid + 1) r = mid - 1; // if it follows index+1 property then // search in right side else l = mid + 1; } // if no element is missing return -1; } // Driver code int main() { int arr[] = {1, 2, 3, 4, 5, 7, 8}; int N = sizeof(arr)/sizeof(arr[0]); cout << findmissing(arr, N); return 0; }
Java
// Java program to find // the only missing element. class GFG { static int findmissing(int [] ar, int N) { int l = 0, r = N - 1; while (l <= r) { int mid = (l + r) / 2; // If this is the first element // which is not index + 1, then // missing element is mid+1 if (ar[mid] != mid + 1 && ar[mid - 1] == mid) return (mid + 1); // if this is not the first // missing element search // in left side if (ar[mid] != mid + 1) r = mid - 1; // if it follows index+1 // property then search // in right side else l = mid + 1; } // if no element is missing return -1; } // Driver code public static void main(String [] args) { int arr[] = {1, 2, 3, 4, 5, 7, 8}; int N = arr.length; System.out.println(findmissing(arr, N)); } } // This code is contributed // by Shivi_Aggarwal
Python3
# PYTHON 3 program to find # the only missing element. def findmissing(ar, N): l = 0 r = N - 1 while (l <= r): mid = (l + r) / 2 mid= int (mid) # If this is the first element # which is not index + 1, then # missing element is mid+1 if(ar[mid] != mid + 1 and ar[mid - 1] == mid): return (mid + 1) # if this is not the first # missing element search # in left side elif(ar[mid] != mid + 1): r = mid - 1 # if it follows index+1 # property then search # in right side else: l = mid + 1 # if no element is missing return (-1) def main(): ar= [1, 2, 3, 4, 5, 7, 8] N = len(ar) res= findmissing(ar, N) print (res) if __name__ == "__main__": main() # This code is contributed # by Shivi_Aggarwal
C#
// C# program to find // the only missing element. using System; class GFG { static int findmissing(int []ar, int N) { int l = 0, r = N - 1; while (l <= r) { int mid = (l + r) / 2; // If this is the first element // which is not index + 1, then // missing element is mid+1 if (ar[mid] != mid + 1 && ar[mid - 1] == mid) return (mid + 1); // if this is not the first // missing element search // in left side if (ar[mid] != mid + 1) r = mid - 1; // if it follows index+1 // property then search // in right side else l = mid + 1; } // if no element is missing return -1; } // Driver code public static void Main() { int []arr = {1, 2, 3, 4, 5, 7, 8}; int N = arr.Length; Console.WriteLine(findmissing(arr, N)); } } // This code is contributed // by Akanksha Rai(Abby_akku)
PHP
<?php // PHP program to find // the only missing element. function findmissing(&$ar, $N) { $r = $N - 1; $l = 0; while ($l <= $r) { $mid = ($l + $r) / 2; // If this is the first element // which is not index + 1, then // missing element is mid+1 if ($ar[$mid] != $mid + 1 && $ar[$mid - 1] == $mid) return ($mid + 1); // if this is not the first // missing element search // in left side if ($ar[$mid] != $mid + 1) $r = $mid - 1; // if it follows index+1 // property then search // in right side else $l = $mid + 1; } // if no element is missing return (-1); } // Driver Code $ar = array(1, 2, 3, 4, 5, 7, 8); $N = sizeof($ar); echo(findmissing($ar, $N)); // This code is contributed // by Shivi_Aggarwal ?>
Javascript
<script> // JavaScript program to find the only missing element. function findmissing(ar, N) { var l = 0, r = N - 1; while (l <= r) { var mid = parseInt((l + r) / 2); // If this is the first element // which is not index + 1, then // missing element is mid+1 if (ar[mid] != mid + 1 && ar[mid - 1] == mid) return mid + 1; // if this is not the first missing // element search in left side if (ar[mid] != mid + 1) r = mid - 1; // if it follows index+1 property then // search in right side else l = mid + 1; } // if no element is missing return -1; } // Driver code var arr = [1, 2, 3, 4, 5, 7, 8]; var N = arr.length; document.write(findmissing(arr, N)); </script>
6
Complejidad de Tiempo: O(Log n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA