

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find range whose sum is same as n
Suppose we have a number n. We need to find two integers l and r, such that l < r and l + (l + 1) + ... + (r - 1) + r = n.
So, if the input is like n = 25, then the output will be l = -2 and r = 7, because (−2) + (−1) + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 = 25. Other answers are also possible.
Steps
To solve this, we will follow these steps −
return -(n-1) and n
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; void solve(int n){ cout << -(n-1) << ", " << n; } int main(){ int n = 25; solve(n); }
Input
25
Output
-24, 25
- Related Questions & Answers
- Program to check we can find four elements whose sum is same as k or not in Python
- C++ program to find two numbers with sum and product both same as N
- C++ code to find three numbers whose sum is n
- Program to find minimum number of subsequence whose concatenation is same as target in python
- Find two numbers with sum and product both same as N in C++ Program
- Program to find an element in list whose value is same as its frequency in Python
- Find two numbers with sum and product both same as N in C++
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Program to count number of consecutive lists whose sum is n in C++
- Program to find higher number with same number of set bits as n in Python?\n
- Python program to find list of triplets for which i+j+k is not same as n
- C++ program to find permutation for which sum of adjacent elements sort is same as given array
- Program to remove all nodes of a linked list whose value is same as in Python
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Maximum Primes whose sum is equal to given N in C++
Advertisements