Dada una array arr[] de N elementos. La tarea es contar el número total de índices (i, j) tales que arr[i] != arr[j] e i < j .
Ejemplos:
Entrada: arr[] = {1, 1, 2}
Salida: 2
(1, 2) y (1, 2) son los únicos pares válidos.
Entrada: arr[] = {1, 2, 3}
Salida: 3
Entrada: arr[] = {1, 1, 1}
Salida: 0
Enfoque: Inicialice una variable de conteo cnt = 0 y ejecute dos bucles anidados para verificar cada par posible si el par actual es válido o no. Si es válido, incremente la variable de conteo. Finalmente, imprima el conteo de pares válidos.
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 // count of valid pairs int countPairs(int arr[], int n) { // To store the required count int cnt = 0; // For each index pair (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // If current pair is valid // then increment the count if (arr[i] != arr[j]) cnt++; } } return cnt; } // Driven code int main() { int arr[] = { 1, 1, 2 }; int n = sizeof(arr) / sizeof(int); cout << countPairs(arr, n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the // count of valid pairs static int countPairs(int arr[], int n) { // To store the required count int cnt = 0; // For each index pair (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // If current pair is valid // then increment the count if (arr[i] != arr[j]) cnt++; } } return cnt; } // Driven code public static void main (String[] args) { int arr[] = { 1, 1, 2 }; int n = arr.length; System.out.println(countPairs(arr, n)); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach # Function to return the # count of valid pairs def countPairs(arr, n): # To store the required count cnt = 0; # For each index pair (i, j) for i in range(n): for j in range(i + 1, n): # If current pair is valid # then increment the count if (arr[i] != arr[j]): cnt += 1; return cnt; # Driver code if __name__ == '__main__': arr = [ 1, 1, 2 ]; n = len(arr); print(countPairs(arr, n)); # This code is contributed by 29AjayKumar
C#
// C# implementation of the approach using System; class GFG { // Function to return the // count of valid pairs static int countPairs(int []arr, int n) { // To store the required count int cnt = 0; // For each index pair (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // If current pair is valid // then increment the count if (arr[i] != arr[j]) cnt++; } } return cnt; } // Driven code public static void Main() { int []arr = { 1, 1, 2 }; int n = arr.Length; Console.WriteLine(countPairs(arr, n)); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the approach // Function to return the // count of valid pairs function countPairs(arr, n) { // To store the required count var cnt = 0; // For each index pair (i, j) for (var i = 0; i < n; i++) { for (var j = i + 1; j < n; j++) { // If current pair is valid // then increment the count if (arr[i] != arr[j]) cnt++; } } return cnt; } // Driven code var arr = [ 1, 1, 2 ]; var n = arr.length; document.write(countPairs(arr, n)); </script>
Producción:
2
Complejidad del Tiempo: O(N 2 )