Dado un número entero N , nuestra tarea es imprimir N números distintos de modo que su suma sea 0.
Ejemplos:
Entrada: N = 3
Salida: 1, -1, 0
Explicación:
Al sumar los números que es 1 + (-1) + 0 la suma es 0.
Entrada: N = 4
Salida: 1, -1, 2, -2
Explicación:
Al sumar los números que es 1 + (-1) + 2 + (-2) la suma es 0.
Enfoque: para resolver el problema mencionado anteriormente, la idea principal es imprimir pares simétricos como (+x, -x) para que la suma siempre sea 0. El caso extremo del problema es observar que si el número entero N es impar , entonces imprima un 0 junto con los números para que la suma no se vea afectada.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to Print N distinct // numbers such that their sum is 0 #include <bits/stdc++.h> using namespace std; // Function to print distinct n // numbers such that their sum is 0 void findNumbers(int N) { for (int i = 1; i <= N / 2; i++) { // Print 2 symmetric numbers cout << i << ", " << -i << ", "; } // print a extra 0 if N is odd if (N % 2 == 1) cout << 0; } // Driver code int main() { int N = 10; findNumbers(N); }
Java
// Java implementation to Print N distinct // numbers such that their sum is 0 class GFG{ // Function to print distinct n // numbers such that their sum is 0 static void findNumbers(int N) { for (int i = 1; i <= N / 2; i++) { // Print 2 symmetric numbers System.out.print(i + ", " + -i + ", "); } // Print a extra 0 if N is odd if (N % 2 == 1) System.out.print(0); } // Driver code public static void main(String[] args) { int N = 10; findNumbers(N); } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation to print N distinct # numbers such that their sum is 0 # Function to print distinct n # numbers such that their sum is 0 def findNumbers(N): for i in range(1, N // 2 + 1): # Print 2 symmetric numbers print(i, end = ', ') print(-i, end = ', ') # Print a extra 0 if N is odd if N % 2 == 1: print(0, end = '') # Driver code if __name__=='__main__': N = 10 findNumbers(N) # This code is contributed by rutvik_56
C#
// C# implementation to print N distinct // numbers such that their sum is 0 using System; class GFG { // Function to print distinct n // numbers such that their sum is 0 static void findNumbers(int N) { for(int i = 1; i <= (N / 2); i++) { // Print 2 symmetric numbers Console.Write(i + ", " + -i + ", "); } // Print a extra 0 if N is odd if (N % 2 == 1) Console.Write(0); } // Driver code static void Main() { int N = 10; findNumbers(N); } } // This code is contributed by divyeshrabadiya07
Javascript
<script> // Javascript implementation to Print N distinct // numbers such that their sum is 0 // Function to print distinct n // numbers such that their sum is 0 function findNumbers(N) { for (var i = 1; i <= N / 2; i++) { // Print 2 symmetric numbers document.write( i + ", " + -i + ", "); } // print a extra 0 if N is odd if (N % 2 == 1) document.write( 0); } // Driver code var N = 10; findNumbers(N); </script>
1, -1, 2, -2, 3, -3, 4, -4, 5, -5,
Complejidad de tiempo: O(N)
Espacio Auxiliar : O(1)