Given a sum S and an integer N, The task is to remove two consecutive integers from 1 to N to make sum equal to S. Examples:
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
- Method 1:
- Create an array with elements from 1 to N.
- Each time remove two consecutive elements and check the difference between sum of N natural numbers and sum of two removed elements is S.
- sum of N natural numbers can be calculated using formula
sum = (n)(n + 1) / 2
- Method 2:
- We know that, sum of N natural numbers can be calculated using formula
sum = (n)(n + 1) / 2
- According to the problem statement, we need to remove two integers from 1 to N such that the sum of remaining integers is S.
- Let the two consecutive integers to be removed be i and i + 1.
- Therefore,
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)
- Hence find i using above formula
- if i is an integer, then answer is Yes else No
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float findNumber( int N, int S)
{
float i = ((( float )(N) * ( float )(N + 1)) / 4)
- (( float )(S + 1) / 2);
return i;
}
void check( int N, int S)
{
float i = findNumber(N, S);
int integerI = ( int )i;
if (i - integerI == 0)
cout << "Yes: "
<< integerI << ", "
<< integerI + 1
<< endl;
else
cout << "No"
<< endl;
}
int main()
{
int N = 4, S = 3;
check(N, S);
N = 5, S = 3;
check(N, S);
return 0;
}
|
Java
class GFG
{
static float findNumber( int N, int S)
{
float i = ((( float )(N) * ( float )(N + 1 )) / 4 )
- (( float )(S + 1 ) / 2 );
return i;
}
static void check( int N, int S)
{
float i = findNumber(N, S);
int integerI = ( int )i;
if (i - integerI == 0 )
System.out.println( "Yes: " + integerI +
", " + (integerI + 1 )) ;
else
System.out.println( "No" );
}
public static void main (String[] args)
{
int N = 4 , S = 3 ;
check(N, S);
N = 5 ; S = 3 ;
check(N, S);
}
}
|
Python3
def findNumber(N, S) :
i = (((N) * (N + 1 )) / 4 ) - ((S + 1 ) / 2 );
return i;
def check(N, S) :
i = findNumber(N, S);
integerI = int (i);
if (i - integerI = = 0 ) :
print ( "Yes:" , integerI,
"," , integerI + 1 );
else :
print ( "No" );
if __name__ = = "__main__" :
N = 4 ;
S = 3 ;
check(N, S);
N = 5 ;
S = 3 ;
check(N, S);
|
C#
using System;
class GFG
{
static float findNumber( int N, int S)
{
float i = ((( float )(N) * ( float )(N + 1)) / 4)
- (( float )(S + 1) / 2);
return i;
}
static void check( int N, int S)
{
float i = findNumber(N, S);
int integerI = ( int )i;
if (i - integerI == 0)
Console.WriteLine( "Yes: " + integerI +
", " + (integerI + 1)) ;
else
Console.WriteLine( "No" );
}
public static void Main()
{
int N = 4, S = 3;
check(N, S);
N = 5; S = 3;
check(N, S);
}
}
|
Javascript
function findNumber(N, S)
{
let i = (((N) * (N + 1)) / 4) - ((S + 1) / 2);
return i;
}
function check(N, S)
{
let i = findNumber(N, S);
let integerI = parseInt(i);
if (i - integerI == 0){
let value = integerI + 1;
console.log( "Yes: " + integerI + "," + value);
}
else
console.log( "No" )
}
let N = 4, S = 3;
check(N, S);
N = 5, S = 3;
check(N, S);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!