Dado un número entero N , la tarea es contar los pares decrecientes de los números 1 a N.
Se dice que un par (x, y) es decreciente si x > y
Ejemplos:
Entrada: N = 8
Salida: 3
Explicación:
Los pares decrecientes son: (7, 1), (6, 2), (5, 3).
Entrada: N = 9
Salida: 4
Explicación:
Los pares decrecientes son: (8, 1), (7, 2), (6, 3), (5, 4).
Enfoque: Considere los siguientes casos:
Si N = 1 => Cuenta = 0
Si N = 2 => Cuenta = 1 {(2, 1)}
Si N = 3 => Cuenta = 1 {(3, 1) o (3, 2)}
Si N = 4 => Cuenta = 2 {(4, 3), (2, 1)} Si
N = 5 => Cuenta = 2 {(5, 4), (3, 2)}
Si N = 6 => Cuenta = 3 {(6, 5), (4, 3), (2, 1)}
.
.
y así
Se puede observar claramente que
a continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to count decreasing // pairs formed from numbers 1 to N #include <bits/stdc++.h> using namespace std; // Function to count the // possible number of pairs int divParts(int N) { if (N % 2 == 0) // if the number is even // then the answer in (N/2)-1 cout << (N / 2) - 1 << endl; else // if the number is odd // then the answer in N/2 cout << N / 2 << endl; } // Driver code int main() { int N = 8; divParts(N); return 0; }
Java
// Java program to count decreasing // pairs formed from numbers 1 to N import java.util.*; class GFG{ // Function to count the // possible number of pairs static void divParts(int N) { if (N % 2 == 0) // if the number is even // then the answer in (N/2)-1 System.out.println((N / 2) - 1); else // if the number is odd // then the answer in N/2 System.out.println((N / 2)); } // Driver code public static void main(String[] args) { int N = 8; divParts(N); } } // This code is contributed by offbeat
Python3
# Python3 program to count decreasing # pairs formed from numbers 1 to N # Function to count the # possible number of pairs def divParts(N): if (N % 2 == 0): # if the number is even # then the answer in (N/2)-1 print((N / 2) - 1); else: # if the number is odd # then the answer in N/2 print(N / 2); # Driver code N = 8; divParts(N); # This code is contributed by Code_Mech
C#
// C# program to count decreasing // pairs formed from numbers 1 to N using System; class GFG{ // Function to count the // possible number of pairs static void divParts(int N) { if (N % 2 == 0) // if the number is even // then the answer in (N/2)-1 Console.WriteLine((N / 2) - 1); else // if the number is odd // then the answer in N/2 Console.WriteLine((N / 2)); } // Driver code public static void Main() { int N = 8; divParts(N); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript program to count decreasing // pairs formed from numbers 1 to N // Function to count the // possible number of pairs function divParts(N) { if (N % 2 == 0) // if the number is even // then the answer in (N/2)-1 document.write((N / 2) - 1); else // if the number is odd // then the answer in N/2 document.write((N / 2)); } // Driver Code let N = 8; divParts(N); </script>
Producción:
3