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 = 3Output: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 = SInput:N = 5, S =3Output: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

**Time complexity:**O(n)**Space complexity:**O(n)**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++

`// 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;`

`}`

*chevron_right**filter_none*## 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`

*chevron_right**filter_none*## 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`

*chevron_right**filter_none*## 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`

*chevron_right**filter_none***Output:**Yes: 3, 4 No

**Time complexity:**O(1)

**Space complexity:**O(1)- We know that, sum of N natural numbers can be calculated using formula

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if N can be represented as sum of squares of two consecutive integers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Check if the sum of distinct digits of two integers are equal
- Check if a number can be written as sum of three consecutive integers
- Number expressed as sum of five consecutive integers
- Find K consecutive integers such that their sum is N
- Add two integers of different base and represent sum in smaller base of the two
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Split N into two integers whose addition to A and B makes them equal
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Find the first N integers such that the sum of their digits is equal to 10
- Find any K distinct odd integers such that their sum is equal to N
- Check if N can be divided into K consecutive elements with a sum equal to N
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Count of N digit Numbers whose sum of every K consecutive digits is equal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.