# Find if given number is sum of first n natural numbers

Given a number s (1 <= s <= 1000000000). If this number is the sum of first n natural number then print, otherwise print -1.

Examples :

```Input : s = 10
Output : n = 4
1 + 2 + 3 + 4 = 10

Input : s = 17
Output : n -1
17 can't be expressed as a
sum of consecutive from 1.
```

# Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Method 1 (Simple):

Start adding numbers from i = 1 to n.
a) Check if sum is equal to n, return i.
b) Else if sum > n, return -1.

## C++

 `// C++ program for above implementation ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find no. of elements ` `// to be added from 1 to get n ` `int` `findS(``int` `s) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// Start adding numbers from 1 ` `    ``for` `(``int` `n = 1; sum < s; n++) { ` `        ``sum += n; ` ` `  `        ``// If sum becomes equal to s ` `        ``// return n ` `        ``if` `(sum == s) ` `            ``return` `n; ` `    ``} ` ` `  `    ``return` `-1; ` `} ` ` `  `// Drivers code ` `int` `main() ` `{ ` `    ``int` `s = 15; ` `    ``int` `n = findS(s); ` `    ``n == -1 ? cout << ``"-1"` `            ``: cout << n; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program for above implementation ` `class` `GFG { ` `     `  `    ``// Function to find no. of elements ` `    ``// to be added from 1 to get n ` `    ``static` `int` `findS(``int` `s) ` `    ``{ ` `        ``int` `sum = ``0``; ` ` `  `        ``// Start adding numbers from 1 ` `        ``for` `(``int` `n = ``1``; sum < s; n++)  ` `        ``{ ` `            ``sum += n; ` `     `  `            ``// If sum becomes equal to s ` `            ``// return n ` `            ``if` `(sum == s) ` `                ``return` `n; ` `        ``} ` ` `  `        ``return` `-``1``; ` `    ``} ` ` `  `    ``// Drivers code ` `    ``public` `static` `void` `main(String[]args) ` `    ``{ ` `         `  `        ``int` `s = ``15``; ` `        ``int` `n = findS(s); ` `        ``if``(n == -``1``)  ` `            ``System.out.println(``"-1"``); ` `        ``else` `            ``System.out.println(n); ` `    ``} ` `} ` ` `  `//This code is contributed by Azkia Anam. `

## Python3

 `# Python3 program to check if  ` `# given number is sum of first n  ` `# natural numbers ` ` `  `# Function to find no. of elements ` `# to be added from 1 to get n ` `def` `findS (s): ` `    ``_sum ``=` `0` `    ``n ``=` `1` `     `  `    ``# Start adding numbers from 1 ` `    ``while``(_sum < s): ` `        ``_sum ``+``=` `n ` `        ``n``+``=``1` `    ``n``-``=``1` `     `  `    ``# If sum becomes equal to s ` `    ``# return n ` `    ``if` `_sum ``=``=` `s: ` `        ``return` `n ` `    ``return` `-``1` ` `  `# Driver code ` `s ``=` `15` `n ``=` `findS (s) ` `if` `n ``=``=` `-``1``: ` `    ``print``(``"-1"``) ` `else``: ` `    ``print``(n) ` ` `  `# This code is contributed by "Abhishek Sharma 44". `

## C#

 `// C# program for above implementation ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to find no. of elements ` `    ``// to be added from 1 to get n ` `    ``static` `int` `findS(``int` `s) ` `    ``{ ` `        ``int` `sum = 0; ` ` `  `        ``// Start adding numbers from 1 ` `        ``for` `(``int` `n = 1; sum < s; n++)  ` `        ``{ ` `            ``sum += n; ` `     `  `            ``// If sum becomes equal to s ` `            ``// return n ` `            ``if` `(sum == s) ` `                ``return` `n; ` `        ``} ` ` `  `        ``return` `-1; ` `    ``} ` ` `  `    ``// Drivers code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `         `  `        ``int` `s = 15; ` `        ``int` `n = findS(s); ` `         `  `        ``if``(n == -1)  ` `            ``Console.WriteLine(``"-1"``); ` `        ``else` `            ``Console.WriteLine(n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output :

```5
```

Time Complexity : O(s)
where s are the no. of consecutive numbers from 1 to s

Method 2 (Binary Search):

```1- Initialize l = 1 and r = n / 2.
2- Apply binary search from l to r.
a) Find mid = (l+r) / 2
b) Find sum from 1 to mid using formula
mid*(mid+1)/2
c) If sum of mid natural numbers is equal
to n, return mid.
d) Else if sum > n, r = mid - 1.
e) Else sum < n, l = mid + 1.
3- Return -1, if not possible.```

## C++

 `// C++ program for finding s such ` `// that sum from 1 to s equals to n ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find no. of elements ` `// to be added to get s ` `int` `findS(``int` `s) ` `{ ` `    ``int` `l = 1, r = (s / 2) + 1; ` ` `  `    ``// Apply Binary search ` `    ``while` `(l <= r) { ` ` `  `        ``// Find mid ` `        ``int` `mid = (l + r) / 2; ` ` `  `        ``// find sum of 1 to mid natural numbers ` `        ``// using formula ` `        ``int` `sum = mid * (mid + 1) / 2; ` ` `  `        ``// If sum is equal to n ` `        ``// return mid ` `        ``if` `(sum == s) ` `            ``return` `mid; ` ` `  `        ``// If greater than n ` `        ``// do r = mid-1 ` `        ``else` `if` `(sum > s) ` `            ``r = mid - 1; ` ` `  `        ``// else do l = mid + 1 ` `        ``else` `            ``l = mid + 1; ` `    ``} ` ` `  `    ``// If not possible, return -1 ` `    ``return` `-1; ` `} ` ` `  `// Drivers code ` `int` `main() ` `{ ` `    ``int` `s = 15; ` `    ``int` `n = findS(s); ` `    ``n == -1 ? cout << ``"-1"` `            ``: cout << n; ` ` `  `    ``return` `0; ` `} `

## Java

 `// java program for finding s such ` `// that sum from 1 to s equals to n ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` `     `  `    ``// Function to find no. of elements ` `    ``// to be added to get s ` `    ``static` `int` `findS(``int` `s) ` `    ``{ ` `        ``int` `l = ``1``, r = (s / ``2``) + ``1``; ` `     `  `        ``// Apply Binary search ` `        ``while` `(l <= r) { ` `     `  `            ``// Find mid ` `            ``int` `mid = (l + r) / ``2``; ` `     `  `            ``// find sum of 1 to mid natural ` `            ``// numbers using formula ` `            ``int` `sum = mid * (mid + ``1``) / ``2``; ` `     `  `            ``// If sum is equal to n ` `            ``// return mid ` `            ``if` `(sum == s) ` `                ``return` `mid; ` `     `  `            ``// If greater than n ` `            ``// do r = mid-1 ` `            ``else` `if` `(sum > s) ` `                ``r = mid - ``1``; ` `     `  `            ``// else do l = mid + 1 ` `            ``else` `                ``l = mid + ``1``; ` `        ``} ` `     `  `        ``// If not possible, return -1 ` `        ``return` `-``1``; ` `    ``} ` `     `  `    ``// Drivers code ` `    ``static` `public` `void` `main (String[] args) ` `    ``{ ` `        ``int` `s = ``15``; ` `        ``int` `n = findS(s); ` `         `  `        ``if``(n==-``1``) ` `            ``System.out.println(``"-1"``); ` `        ``else` `            ``System.out.println(n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python3

 `# python program for finding s such ` `# that sum from 1 to s equals to n ` ` `  `# Function to find no. of elements ` `# to be added to get s ` `def` `findS(s): ` `     `  `    ``l ``=` `1` `    ``r ``=` `int``(s ``/` `2``) ``+` `1` `  `  `    ``# Apply Binary search ` `    ``while` `(l <``=` `r) : ` `        ``# Find mid ` `        ``mid ``=` `int``((l ``+` `r) ``/` `2``) ` `  `  `        ``# find sum of 1 to mid natural numbers ` `        ``# using formula ` `        ``sum` `=` `int``(mid ``*` `(mid ``+` `1``) ``/` `2``) ` `  `  `        ``# If sum is equal to n ` `        ``# return mid ` `        ``if` `(``sum` `=``=` `s): ` `            ``return` `mid ` `  `  `        ``# If greater than n ` `        ``# do r = mid-1 ` `        ``elif` `(``sum` `> s): ` `            ``r ``=` `mid ``-` `1` `  `  `        ``# else do l = mid + 1 ` `        ``else``: ` `            ``l ``=` `mid ``+` `1` `     `  `  `  `    ``# If not possible, return -1 ` `    ``return` `-``1` ` `  ` `  `s ``=` `15` `n ``=` `findS(s) ` `if``(n ``=``=` `-``1``): ` `    ``print``( ``"-1"``) ` `else``: ` `    ``print``( n ) ` ` `  `# This code is contributed by Sam007 `

## C#

 `// C# program for finding s such ` `// that sum from 1 to s equals to n ` `using` `System; ` ` `  `public` `class` `GFG { ` `     `  `    ``// Function to find no. of elements ` `    ``// to be added to get s ` `    ``static` `int` `findS(``int` `s) ` `    ``{ ` `        ``int` `l = 1, r = (s / 2) + 1; ` `     `  `        ``// Apply Binary search ` `        ``while` `(l <= r) { ` `     `  `            ``// Find mid ` `            ``int` `mid = (l + r) / 2; ` `     `  `            ``// find sum of 1 to mid natural ` `            ``// numbers using formula ` `            ``int` `sum = mid * (mid + 1) / 2; ` `     `  `            ``// If sum is equal to n ` `            ``// return mid ` `            ``if` `(sum == s) ` `                ``return` `mid; ` `     `  `            ``// If greater than n ` `            ``// do r = mid-1 ` `            ``else` `if` `(sum > s) ` `                ``r = mid - 1; ` `     `  `            ``// else do l = mid + 1 ` `            ``else` `                ``l = mid + 1; ` `        ``} ` `     `  `        ``// If not possible, return -1 ` `        ``return` `-1; ` `    ``} ` `     `  `    ``// Drivers code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `s = 15; ` `        ``int` `n = findS(s); ` `         `  `        ``if``(n==-1) ` `            ``Console.WriteLine(``"-1"``); ` `        ``else` `            ``Console.WriteLine(n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` ``\$s``) ` `            ``\$r` `= ``\$mid` `- 1; ` ` `  `        ``// else do l = mid + 1 ` `        ``else` `            ``\$l` `= ``\$mid` `+ 1; ` `    ``} ` ` `  `    ``// If not possible, ` `    ``// return -1 ` `    ``return` `-1; ` `} ` ` `  `// Drivers code ` `\$s` `= 15; ` `\$n` `= findS(``\$s``); ` `if``(``\$n` `== -1 ) ` `echo` `"-1"``; ` `else` `echo` `\$n``; ` ` `  `// This code is contributed by Sam007 ` `?> `

Output :

```5
```

Time Complexity : O(log n)

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.