Dado un entero n , la tarea es encontrar si n se puede escribir como la suma de tres enteros consecutivos. En caso afirmativo, encuentre los tres enteros consecutivos, de lo contrario imprima «-1».
Ejemplos:
Input : n = 6 Output : 1 2 3 6 = 1 + 2 + 3. Input : n = 7 Output : -1
Método 1: (Fuerza bruta):
La idea es ejecutar un bucle desde i = 0 hasta n – 2, comprobar si (i + i+1 + i+2) es igual a n. Además, verifique si n es positivo o negativo y, en consecuencia, incremente o disminuya i en 1.
A continuación se muestra la implementación de este enfoque:
C++
// CPP Program to check if a number can // be written as sum of three consecutive // integers. #include <bits/stdc++.h> using namespace std; // function to check if a number can be written as sum of // three consecutive integer. void checksum(int n) { // if n is 0 if (n == 0) { cout << "-1 0 1" << endl; return; } int inc; // if n is positive, increment loop by 1. if (n > 0) inc = 1; // if n is negative, decrement loop by 1. else inc = -1; // Running loop from 0 to n - 2 for (int i = 0; i <= n - 2; i += inc) { // check if sum of three consecutive // integer is equal to n. if (i + i + 1 + i + 2 == n) { cout << i << " " << i + 1 << " " << i + 2; return; } } cout << "-1"; } // Driver Program int main() { int n = 6; checksum(n); return 0; }
Java
// JAVA Code to check if a number // can be written as sum of // three consecutive integers. import java.util.*; class GFG { // function to check if a number // can be written as sum of // three consecutive integer. static void checksum(int n) { // if n is 0 if (n == 0) { System.out.println("-1 0 1"); return; } int inc; // if n is positive, // increment loop by 1. if (n > 0) inc = 1; // if n is negative, // decrement loop by 1. else inc = -1; // Running loop from 0 to n - 2 for (int i = 0; i <= n - 2; i += inc) { // check if sum of three consecutive // integer is equal to n. if (i + i + 1 + i + 2 == n) { System.out.println(i + " " + (i + 1) + " " + (i + 2)); return; } } System.out.println("-1"); } /* Driver program to test above function */ public static void main(String[] args) { int n = 6; checksum(n); } } // This code is contributed by Arnav Kr. Mandal.
Python3
# Python3 code to check if a number # can be written as sum of three # consecutive integers. # function to check if a number # can be written as sum of three # consecutive integer. def checksum(n): # if n is 0 if n == 0: print("-1 0 1") return 0 inc = 0 # if n is positive, # increment loop by 1. if n > 0: inc = 1 # if n is negative, # decrement loop by 1. else: inc = -1 # Running loop from 0 to n - 2 for i in range(0, n-1, inc): # check if sum of three consecutive # integer is equal to n. if i + i + 1 + i + 2 == n: print(i ," ",i + 1, " ", i + 2) return 0 print("-1") # Driver Code n = 6 checksum(n) # This code is contributed by "Sharad_Bhardwaj".
C#
// C# Code to check if a number // can be written as sum of // three consecutive integers. using System; class GFG { // function to check if a number // can be written as sum of // three consecutive integer. static void checksum(int n) { // if n is 0 if (n == 0) { Console.WriteLine("-1 0 1"); return; } int inc; // if n is positive, // increment loop by 1. if (n > 0) inc = 1; // if n is negative, // decrement loop by 1. else inc = -1; // Running loop from 0 to n - 2 for (int i = 0; i <= n - 2; i += inc) { // check if sum of three consecutive // integer is equal to n. if (i + i + 1 + i + 2 == n) { Console.WriteLine(i + " " + (i + 1) +" " + (i + 2)); return; } } Console.WriteLine("-1"); } /* Driver program to test above function */ public static void Main() { int n = 6; checksum(n); } } // This code is contributed by vt_m.
PHP
<?php // PHP Program to check if a // number can be written // as sum of three consecutive // integers. // function to check if a number // can be written as sum of // three consecutive integer. function checksum($n) { // if n is 0 if ($n == 0) { echo "-1 0 1" ; return; } $inc; // if n is positive, // increment loop by 1. if ($n > 0) $inc = 1; // if n is negative, // decrement loop by 1. else $inc = -1; // Running loop from // 0 to n - 2 for ($i = 0; $i <= $n - 2; $i += $inc) { // check if sum of three consecutive // integer is equal to n. if ($i + $i + 1 + $i + 2 == $n) { echo $i , " " , $i + 1 , " " , $i + 2; return; } } echo "-1"; } // Driver Code $n = 6; checksum($n); // This code is contributed by anuj_67. ?>
Javascript
<script> // Javascript Code to check if a number // can be written as sum of // three consecutive integers. // function to check if a number // can be written as sum of // three consecutive integer. function checksum(n) { // if n is 0 if (n == 0) { document.write("-1 0 1"); return; } var inc; // if n is positive, // increment loop by 1. if (n > 0) inc = 1; // if n is negative, // decrement loop by 1. else inc = -1; // Running loop from 0 to n - 2 for (i = 0; i <= n - 2; i += inc) { // check if sum of three consecutive // integer is equal to n. if (i + i + 1 + i + 2 == n) { document.write(i + " " + (i + 1) + " " + (i + 2)); return; } } document.write("-1"); } /* Driver program to test above function */ var n = 6; checksum(n); // This code is contributed by gauravrajput1 </script>
Producción:
1 2 3
Método 2: (Enfoque eficiente)
La idea es verificar si n es múltiplo de 3 o no.
Sea n la suma de tres enteros consecutivos de k – 1, k, k + 1. Por lo tanto,
k – 1 + k + k + 1 = n
3*k = n
Los tres números serán n/3 – 1, n/ 3, n/3 + 1.
C++
// CPP Program to check if a number can be // written as sum of three consecutive integer. #include <bits/stdc++.h> using namespace std; // function to check if a number can be // written as sum of three consecutive // integers. void checksum(int n) { // if n is multiple of 3 if (n % 3 == 0) cout << n / 3 - 1 << " " << n / 3 << " " << n / 3 + 1; // else print "-1". else cout << "-1"; } // Driver Program int main() { int n = 6; checksum(n); return 0; }
Java
// JAVA Code to check if a number // can be written as sum of three // consecutive integers. import java.util.*; class GFG { // function to check if a number // can be written as sum of three // consecutive integers. static void checksum(int n) { // if n is multiple of 3 if (n % 3 == 0) System.out.println( n / 3 - 1 + " " + n / 3 + " " + (n / 3 + 1)); // else print "-1". else System.out.println("-1"); } /* Driver program to test above function */ public static void main(String[] args) { int n = 6; checksum(n); } } // This code is contributed by Arnav Kr. Mandal.
Python3
# Python3 code to check if a number # can be written as sum of three # consecutive integer. # function to check if a number # can be written as sum of three # consecutive integers. def checksum(n): n = int(n) # if n is multiple of 3 if n % 3 == 0: print(int(n / 3 - 1) ," ", int(n / 3)," ",int(n / 3 + 1)) # else print "-1". else: print("-1") # Driver Code n = 6 checksum(n) # This code is contributed by "Sharad_Bhardwaj".
C#
// C# Code to check if a number // can be written as sum of three // consecutive integers. using System; class GFG { // function to check if a number // can be written as sum of three // consecutive integers. static void checksum(int n) { // if n is multiple of 3 if (n % 3 == 0) Console.WriteLine( n / 3 - 1 + " " + n / 3 + " " + (n / 3 + 1)); // else print "-1". else Console.WriteLine("-1"); } /* Driver program to test above function */ public static void Main() { int n = 6; checksum(n); } } // This code is contributed by vt_m.
PHP
<?php // PHP Code to check if a number // can be written as sum of three // consecutive integers. // function to check if // a number can be written // as sum of three consecutive // integers. function checksum($n) { // if n is multiple of 3 if ($n % 3 == 0) echo $n / 3 - 1, " ", $n / 3, " ", $n / 3 + 1; // else print "-1". else echo "-1"; } // Driver Program $n = 6; checksum($n); // This code is contributed by aj_36 ?>
Javascript
<script> // javascript Code to check if a number // can be written as sum of three // consecutive integers. // function to check if a number // can be written as sum of three // consecutive integers. function checksum(n) { // if n is multiple of 3 if (n % 3 == 0) document.write(n / 3 - 1 + " " + n / 3 + " " + (n / 3 + 1)); // else print "-1". else document.write("-1"); } /* Driver program to test above function */ var n = 6; checksum(n); // This code is contributed by todaysgaurav </script>
Producción:
1 2 3
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)