Dados dos números L y R, la tarea es contar el número de números impares en el rango L a R.
Ejemplos:
Entrada: l = 3, r = 7
Salida: 3 2 La
cuenta de números impares es 3, es decir, 3, 5, 7 La
cuenta de números pares es 2, es decir, 4, 6
Entrada: l = 4, r = 8
Salida: 2
Enfoque: los números totales en el rango serán (R – L + 1), es decir, N.
- Si N es par, la cuenta de los números pares e impares será N/2 .
- Si N es impar,
- Si L o R son impares, entonces el conteo de los números impares será N/2 + 1, y los números pares = N – conteo de impares .
- De lo contrario, el recuento de números impares será N/2 y los números pares = N – countofOdd .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Return the number of odd numbers // in the range [L, R] int countOdd(int L, int R){ int N = (R - L) / 2; // if either R or L is odd if (R % 2 != 0 || L % 2 != 0) N += 1; return N; } // Driver code int main() { int L = 3, R = 7; int odds = countOdd(L, R); int evens = (R - L + 1) - odds; cout << "Count of odd numbers is " << odds << endl; cout << "Count of even numbers is " << evens << endl; return 0; } // This code is contributed by Rituraj Jain
Java
// Java implementation of the above approach class GFG { // Return the number of odd numbers // in the range [L, R] static int countOdd(int L, int R) { int N = (R - L) / 2; // if either R or L is odd if (R % 2 != 0 || L % 2 != 0) N++; return N; } // Driver code public static void main(String[] args) { int L = 3, R = 7; int odds = countOdd(L, R); int evens = (R - L + 1) - odds; System.out.println("Count of odd numbers is " + odds); System.out.println("Count of even numbers is " + evens); } }
Python 3
# Python 3 implementation of the # above approach # Return the number of odd numbers # in the range [L, R] def countOdd(L, R): N = (R - L) // 2 # if either R or L is odd if (R % 2 != 0 or L % 2 != 0): N += 1 return N # Driver code if __name__ == "__main__": L = 3 R = 7 odds = countOdd(L, R) evens = (R - L + 1) - odds print("Count of odd numbers is", odds) print("Count of even numbers is", evens) # This code is contributed by ita_c
C#
// C# implementation of the above approach using System; class GFG { // Return the number of odd numbers // in the range [L, R] static int countOdd(int L, int R) { int N = (R - L) / 2; // if either R or L is odd if (R % 2 != 0 || L % 2 != 0) N++; return N; } // Driver code public static void Main() { int L = 3, R = 7; int odds = countOdd(L, R); int evens = (R - L + 1) - odds; Console.WriteLine("Count of odd numbers is " + odds); Console.WriteLine("Count of even numbers is " + evens); } } // This code is contributed by Ryuga
PHP
<?php // PHP implementation of the above approach // Return the number of odd numbers // in the range [L, R] function countOdd($L, $R) { $N = ($R - $L) / 2; // if either R or L is odd if ($R % 2 != 0 || $L % 2 != 0) $N++; return intval($N); } // Driver code $L = 3; $R = 7; $odds = countOdd($L, $R); $evens = ($R - $L + 1) - $odds; echo "Count of odd numbers is " . $odds . "\n"; echo "Count of even numbers is " . $evens; // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // Javascript implementation // of the above approach // Return the number of odd numbers // in the range [L, R] function countOdd( L, R){ let N = Math.floor((R - L) / 2); // if either R or L is odd if (R % 2 != 0 || L % 2 != 0) N += 1; return N; } // Driver Code let L = 3, R = 7; let odds = countOdd(L, R); let evens = (R - L + 1) - odds; document.write( "Count of odd numbers is " + odds + "</br>" ); document.write( "Count of even numbers is " + evens + "</br>" ); </script>
Producción
Count of odd numbers is 3 Count of even numbers is 2
Complejidad de Tiempo: O(1), ya que solo hay una operación aritmética básica que toma tiempo constante.
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.
Publicación traducida automáticamente
Artículo escrito por AmanKumarSingh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA