Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
C++ code to find three numbers whose sum is n
Suppose we have a number n. We are going to find three numbers a, b and c, such that a + b + c = n and none of these three numbers are multiple of 3.
So, if the input is like n = 233, then the output will be [77, 77, 79]
Steps
To solve this, we will follow these steps −
if (n - 2) mod 3 is same as 0, then: return 1, 2, and n - 3 Otherwise return 1, 1, and n - 2
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h>
using namespace std;
void solve(int n){
if ((n - 2) % 3 == 0)
cout << 1 << ", " << 2 << ", " << n - 3;
else
cout << 1 << ", " << 1 << ", " << n - 2;
}
int main(){
int n = 233;
solve(n);
}
Input
233
Output
1, 2, 230
Advertisements
