Dado un número N. Encuentra el número de formas de dividir un número en cuatro partes (a, b, c, d) tales que a = c y b = d y a no es igual a b.
Ejemplos:
Entrada: N = 6
Salida: 1
Explicación: las cuatro partes son {1, 2, 1, 2}
Entrada: N = 20
Salida: 4
Explicación: las formas posibles son {1, 1, 9, 9}, {2, 2, 8, 8}, {3, 3, 7, 7} y {4, 4, 6, 6}.
Enfoque:
si el N dado es impar, la respuesta es 0, porque la suma de cuatro partes no será un número par.
Si n es divisible por 4, la respuesta será n/4 -1 (aquí es posible que sea igual a b, así que reste esa posibilidad), de lo contrario, n/4.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation for above approach #include <bits/stdc++.h> using namespace std; // Function to find the number of ways to divide // N into four parts such that a = c and b = d int possibleways(int n) { if (n % 2 == 1) return 0; else if (n % 4 == 0) return n / 4 - 1; else return n / 4; } // Driver code int main() { int n = 20; cout << possibleways(n); return 0; }
Java
// Java implementation for above approach class GFG { // Function to find the number of ways to divide // N into four parts such that a = c and b = d static int possibleways(int n) { if (n % 2 == 1) return 0; else if (n % 4 == 0) return n / 4 - 1; else return n / 4; } // Driver code public static void main(String[] args) { int n = 20; System.out.println(possibleways(n)); } } // This code contributed by Rajput-Ji
Python3
# Python3 implementation for above approach # Function to find the number of ways # to divide N into four parts # such that a = c and b = d def possibleways(n): if (n % 2 == 1): return 0; elif (n % 4 == 0): return n // 4 - 1; else: return n // 4; # Driver code n = 20; print(possibleways(n)); # This code is contributed by mits
C#
// C# implementation for above approach class GFG { // Function to find the number of ways to divide // N into four parts such that a = c and b = d static int possibleways(int n) { if (n % 2 == 1) return 0; else if (n % 4 == 0) return n / 4 - 1; else return n / 4; } // Driver code static void Main() { int n = 20; System.Console.WriteLine(possibleways(n)); } } // This code is contributed by mits
PHP
<?php // PHP implementation for above approach // Function to find the number of ways to divide // N into four parts such that a = c and b = d function possibleways($n) { if ($n % 2 == 1) return 0; else if ($n % 4 == 0) return $n / 4 - 1; else return $n / 4; } // Driver code $n = 20; echo possibleways($n); // This code is contributed by ajit ?>
Javascript
<script> // java script implementation for above approach // Function to find the number of ways to divide // N into four parts such that a = c and b = d function possibleways(n) { if (n % 2 == 1) return 0; else if (n % 4 == 0) return n / 4 - 1; else return n / 4; } // Driver code let n = 20; document.write( possibleways(n)); // This code is contributed by Gottumukkala Bobby </script>
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA