Dada una suma S y un entero N, la tarea es eliminar dos enteros consecutivos de 1 a N para que la suma sea igual a S.
Ejemplos:
Input: N = 4, S = 3 Output: Yes sum(1, 2, 3, 4) = 10, remove 3 and 4 from 1 to N now sum(1, 2) = 3 = S Hence by removing 3 and 4 we got sum = S Input: N = 5, S =3 Output: No Its not possible to remove two elements from 1 to N such that new sum is 3
- Método 1:
- Cree una array con elementos del 1 al N.
- Cada vez que elimine dos elementos consecutivos y compruebe que la diferencia entre la suma de N números naturales y la suma de dos elementos eliminados es S.
- la suma de N números naturales se puede calcular usando la fórmula
sum = (n)(n + 1) / 2
Time complexity: O(n) Space complexity: O(n)
- Método 2:
- Sabemos que la suma de N números naturales se puede calcular usando la fórmula
sum = (n)(n + 1) / 2
- De acuerdo con el enunciado del problema, necesitamos eliminar dos números enteros de 1 a N de modo que la suma de los números enteros restantes sea S.
- Sean i e i + 1 los dos enteros consecutivos a eliminar .
- Por lo tanto,
required sum = S = (n)(n + 1) / 2 - (i) - (i + 1) S = (n)(n + 1) / 2 - (2i - 1) Therefore, i = ((n)(n + 1) / 4) - ((S + 1) / 2)
- Por lo tanto, encuentre i usando la fórmula anterior
- si i es un número entero, entonces la respuesta es Sí, de lo contrario, No
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program remove two consecutive integers
// from 1 to N to make sum equal to S
#include <bits/stdc++.h>
using
namespace
std;
// Function to find the numbers
// to be removed
float
findNumber(
int
N,
int
S)
{
// typecast appropriately
// so that answer is float
float
i = (((
float
)(N) * (
float
)(N + 1)) / 4)
- ((
float
)(S + 1) / 2);
// return the obtained result
return
i;
}
void
check(
int
N,
int
S)
{
float
i = findNumber(N, S);
// Convert i to integer
int
integerI = (
int
)i;
// If i is an integer is 0
// then answer is Yes
if
(i - integerI == 0)
cout <<
"Yes: "
<< integerI <<
", "
<< integerI + 1
<< endl;
else
cout <<
"No"
<< endl;
}
// Driver code
int
main()
{
int
N = 4, S = 3;
check(N, S);
N = 5, S = 3;
check(N, S);
return
0;
}
Java
// Java program remove two consecutive integers
// from 1 to N to make sum equal to S
class
GFG
{
// Function to find the numbers
// to be removed
static
float
findNumber(
int
N,
int
S)
{
// typecast appropriately
// so that answer is float
float
i = (((
float
)(N) * (
float
)(N +
1
)) /
4
)
- ((
float
)(S +
1
) /
2
);
// return the obtained result
return
i;
}
static
void
check(
int
N,
int
S)
{
float
i = findNumber(N, S);
// Convert i to integer
int
integerI = (
int
)i;
// If i is an integer is 0
// then answer is Yes
if
(i - integerI ==
0
)
System.out.println(
"Yes: "
+ integerI +
", "
+ (integerI +
1
)) ;
else
System.out.println(
"No"
);
}
// Driver code
public
static
void
main (String[] args)
{
int
N =
4
, S =
3
;
check(N, S);
N =
5
; S =
3
;
check(N, S);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program remove two consecutive integers
# from 1 to N to make sum equal to S
# Function to find the numbers
# to be removed
def
findNumber(N, S) :
# typecast appropriately
# so that answer is float
i
=
(((N)
*
(N
+
1
))
/
4
)
-
((S
+
1
)
/
2
);
# return the obtained result
return
i;
def
check(N, S) :
i
=
findNumber(N, S);
# Convert i to integer
integerI
=
int
(i);
# If i is an integer is 0
# then answer is Yes
if
(i
-
integerI
=
=
0
) :
print
(
"Yes:"
, integerI,
","
, integerI
+
1
);
else
:
print
(
"No"
);
# Driver code
if
__name__
=
=
"__main__"
:
N
=
4
;
S
=
3
;
check(N, S);
N
=
5
;
S
=
3
;
check(N, S);
# This code is contributed by AnkitRai01
C#
// C# program remove two consecutive integers
// from 1 to N to make sum equal to S
using
System;
class
GFG
{
// Function to find the numbers
// to be removed
static
float
findNumber(
int
N,
int
S)
{
// typecast appropriately
// so that answer is float
float
i = (((
float
)(N) * (
float
)(N + 1)) / 4)
- ((
float
)(S + 1) / 2);
// return the obtained result
return
i;
}
static
void
check(
int
N,
int
S)
{
float
i = findNumber(N, S);
// Convert i to integer
int
integerI = (
int
)i;
// If i is an integer is 0
// then answer is Yes
if
(i - integerI == 0)
Console.WriteLine(
"Yes: "
+ integerI +
", "
+ (integerI + 1)) ;
else
Console.WriteLine(
"No"
);
}
// Driver code
public
static
void
Main()
{
int
N = 4, S = 3;
check(N, S);
N = 5; S = 3;
check(N, S);
}
}
// This code is contributed by AnkitRai01
Producción:Yes: 3, 4 No
Complejidad temporal: O(1)
Complejidad espacial: O(1) - Sabemos que la suma de N números naturales se puede calcular usando la fórmula