Dado un rango [L, R], la tarea es encontrar todos los pares coprimos posibles del rango de modo que un elemento no aparezca en más de un solo par.
Ejemplos:
Input : L=1 ; R=6 Output : 3 The answer is 3 [(1, 2) (3, 4) (5, 6)], all these pairs have GCD 1. Input : L=2 ; R=4 Output : 1 The answer is 1 [(2, 3) or (3, 4)] as '3' can only be chosen for a single pair
Enfoque: La observación clave del problema es que los números con la diferencia de ‘1’ siempre son primos relativos entre sí, es decir, coprimos.
El MCD de este par siempre es ‘1’. Entonces, la respuesta será (R-L+1)/2 [ (recuento total de números en el rango) / 2]
- Si R-L+1 es impar, quedará un elemento que no puede formar un par.
- Si R-L+1 es par, todos los elementos pueden formar pares.
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 count possible pairs void CountPair(int L, int R) { // total count of numbers in range int x = (R - L + 1); // Note that if 'x' is odd then // there will be '1' element left // which can't form a pair // printing count of pairs cout << x / 2 << "\n"; } // Driver code int main() { int L, R; L = 1, R = 8; CountPair(L, R); return 0; }
Java
// Java implementation of the approach import java.util.*; class solution { // Function to count possible pairs static void CountPair(int L, int R) { // total count of numbers in range int x = (R - L + 1); // Note that if 'x' is odd then // there will be '1' element left // which can't form a pair // printing count of pairs System.out.println(x / 2 + "\n"); } // Driver code public static void main(String args[]) { int L, R; L = 1; R = 8; CountPair(L, R); } } //contributed by Arnab Kundu
Python3
# Python3 implementation of # the approach # Function to count possible # pairs def CountPair(L,R): # total count of numbers # in range x=(R-L+1) # Note that if 'x' is odd then # there will be '1' element left # which can't form a pair # printing count of pairs print(x//2) # Driver code if __name__=='__main__': L,R=1,8 CountPair(L,R) # This code is contributed by # Indrajit Sinha.
C#
// C# implementation of the approach using System; class GFG { // Function to count possible pairs static void CountPair(int L, int R) { // total count of numbers in range int x = (R - L + 1); // Note that if 'x' is odd then // there will be '1' element left // which can't form a pair // printing count of pairs Console.WriteLine(x / 2 + "\n"); } // Driver code public static void Main() { int L, R; L = 1; R = 8; CountPair(L, R); } } // This code is contributed // by inder_verma..
PHP
<?php // PHP implementation of the above approach // Function to count possible pairs function CountPair($L, $R) { // total count of numbers in range $x = ($R - $L + 1); // Note that if 'x' is odd then // there will be '1' element left // which can't form a pair // printing count of pairs echo $x / 2, "\n"; } // Driver code $L = 1; $R = 8; CountPair($L, $R); // This code is contributed by ANKITRAI1 ?>
Javascript
<script> // Javascript implementation of the approach // Function to count possible pairs function CountPair(L, R) { // total count of numbers in range let x = (R - L + 1); // Note that if 'x' is odd then // there will be '1' element left // which can't form a pair // printing count of pairs document.write(x / 2 + "<br/>"); } // driver code let L, R; L = 1; R = 8; CountPair(L, R); </script>
Producción
4
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)