Dada una array de N enteros. La tarea es encontrar la diferencia máxima entre el índice de dos números diferentes cualesquiera. Tenga en cuenta que hay un mínimo de dos números diferentes.
Ejemplos:
Entrada: a[] = {1, 2, 3, 2, 3}
Salida: 4
La diferencia entre 1 y los últimos 3.
Entrada: a[] = {1, 1, 3, 1, 1, 1}
Salida: 3
La diferencia entre el índice de 3 y el último 1.
Enfoque: Inicialmente, verifique el primer número que es diferente de a[0] comenzando desde el final, almacene la diferencia de su índice como ind1 . Además, verifique el primer número que es diferente de a[n – 1] desde el principio, almacene la diferencia de su índice como ind2 . La respuesta será max(ind1, ind2) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum difference int findMaximumDiff(int a[], int n) { int ind1 = 0; // Iteratively check from back for (int i = n - 1; i > 0; i--) { // Different numbers if (a[0] != a[i]) { ind1 = i; break; } } int ind2 = 0; // Iteratively check from // the beginning for (int i = 0; i < n - 1; i++) { // Different numbers if (a[n - 1] != a[i]) { ind2 = (n - 1 - i); break; } } return max(ind1, ind2); } // Driver code int main() { int a[] = { 1, 2, 3, 2, 3 }; int n = sizeof(a) / sizeof(a[0]); cout << findMaximumDiff(a, n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the maximum difference static int findMaximumDiff(int []a, int n) { int ind1 = 0; // Iteratively check from back for (int i = n - 1; i > 0; i--) { // Different numbers if (a[0] != a[i]) { ind1 = i; break; } } int ind2 = 0; // Iteratively check from // the beginning for (int i = 0; i < n - 1; i++) { // Different numbers if (a[n - 1] != a[i]) { ind2 = (n - 1 - i); break; } } return Math.max(ind1, ind2); } // Driver code public static void main(String args[]) { int []a = { 1, 2, 3, 2, 3 }; int n = a.length; System.out.println(findMaximumDiff(a, n)); } } // This code is contributed by Akanksha_Rai
Python3
# Python3 implementation of the approach # Function to return the maximum difference def findMaximumDiff(a, n): ind1 = 0 # Iteratively check from back for i in range(n - 1, -1, -1): # Different numbers if (a[0] != a[i]): ind1 = i break ind2 = 0 # Iteratively check from # the beginning for i in range(n - 1): # Different numbers if (a[n - 1] != a[i]): ind2 = (n - 1 - i) break return max(ind1, ind2) # Driver code a = [1, 2, 3, 2, 3] n = len(a) print(findMaximumDiff(a, n)) # This code is contributed by mohit kumar
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum difference static int findMaximumDiff(int []a, int n) { int ind1 = 0; // Iteratively check from back for (int i = n - 1; i > 0; i--) { // Different numbers if (a[0] != a[i]) { ind1 = i; break; } } int ind2 = 0; // Iteratively check from // the beginning for (int i = 0; i < n - 1; i++) { // Different numbers if (a[n - 1] != a[i]) { ind2 = (n - 1 - i); break; } } return Math.Max(ind1, ind2); } // Driver code static void Main() { int []a = { 1, 2, 3, 2, 3 }; int n = a.Length; Console.WriteLine(findMaximumDiff(a, n)); } } // This code is contributed by mits
PHP
<?php // PHP implementation of the approach // Function to return the maximum difference function findMaximumDiff($a, $n) { $ind1 = 0; // Iteratively check from back for ($i = $n - 1; $i > 0; $i--) { // Different numbers if ($a[0] != $a[$i]) { $ind1 = $i; break; } } $ind2 = 0; // Iteratively check from // the beginning for ($i = 0; $i < $n - 1; $i++) { // Different numbers if ($a[$n - 1] != $a[$i]) { $ind2 = ($n - 1 - $i); break; } } return max($ind1, $ind2); } // Driver code $a = array( 1, 2, 3, 2, 3 ); $n = count($a); echo findMaximumDiff($a, $n); // This code is contributed by Ryuga ?>
Javascript
<script> // javascript implementation of the approach // Function to return the maximum difference function findMaximumDiff(a , n) { var ind1 = 0; // Iteratively check from back for (i = n - 1; i > 0; i--) { // Different numbers if (a[0] != a[i]) { ind1 = i; break; } } var ind2 = 0; // Iteratively check from // the beginning for (i = 0; i < n - 1; i++) { // Different numbers if (a[n - 1] != a[i]) { ind2 = (n - 1 - i); break; } } return Math.max(ind1, ind2); } // Driver code var a = [ 1, 2, 3, 2, 3 ]; var n = a.length; document.write(findMaximumDiff(a, n)); // This code is contributed by todaysgaurav </script>
4
Complejidad temporal: O(n)
Espacio auxiliar: O(1)