Given a non-negative integer N, the task is to check if that integer can be represented as a summation of multiples of 3, 5, and 7, and print their respective values. If the task is not possible then print -1.
Examples:
Input: 10
Output: 1 0 1
Explanation: 10 can be represented as: (3 * 1) + (5 * 0) + (7 * 1) = 10. Other valid representation is (3 * 0) + (5 * 2) + (7 * 0)Input: 4
Output: -1
Explanation: 4 cannot be represented as a sum of multiples of 3, 5, and 7.
Naive Approach: The given problem can be solved by using three nested for loops, to iterate over multiples of 3, 5, and 7, and keeping track of whether there exists a combination with sum as N.
Below is the implementation of the above approach:
// C++ implementation of the above approach
#include <iostream>
using namespace std;
// Function to check if a number can
// be represented as summation of the
// multiples of 3, 5 and 7
void check357(int N)
{
// flag indicates if combination
// is possible or not
int flag = 0;
// Loop for multiples of 3
for (int i = 0; i <= N / 3; i++) {
if (flag == 1)
break;
// Loop for multiples of 5
for (int j = 0; j <= N / 5; j++) {
if (flag == 1)
break;
// Loop for multiples of 7
for (int k = 0; k <= N / 7; k++) {
// If sum is N
if (3 * i + 5 * j + 7 * k == N) {
// Combination found
flag = 1;
// Print Answer
cout << i << " "
<< j << " " << k;
break;
}
}
}
}
// No valid combination found
if (flag == 0)
cout << -1;
}
// Driver code
int main()
{
int N = 10;
check357(N);
}
// Java code for the above approach
import java.io.*;
class GFG
{
// Function to check if a number can
// be represented as summation of the
// multiples of 3, 5 and 7
static void check357(int N)
{
// flag indicates if combination
// is possible or not
int flag = 0;
// Loop for multiples of 3
for (int i = 0; i <= N / 3; i++) {
if (flag == 1)
break;
// Loop for multiples of 5
for (int j = 0; j <= N / 5; j++) {
if (flag == 1)
break;
// Loop for multiples of 7
for (int k = 0; k <= N / 7; k++) {
// If sum is N
if (3 * i + 5 * j + 7 * k == N) {
// Combination found
flag = 1;
// Print Answer
System.out.print(i + " " + j + " "
+ k);
break;
}
}
}
}
// No valid combination found
if (flag == 0)
System.out.println(-1);
}
// Driver code
public static void main(String[] args)
{
int N = 10;
check357(N);
}
}
// This code is contributed by Potta Lokesh
# Python implementation of the above approach
# Function to check if a number can
# be represented as summation of the
# multiples of 3, 5 and 7
def check357(N):
# flag indicates if combination
# is possible or not
flag = 0;
# Loop for multiples of 3
for i in range((N // 3) + 1):
if (flag == 1):
break;
# Loop for multiples of 5
for j in range((N // 5) + 1):
if (flag == 1):
break;
# Loop for multiples of 7
for k in range((N // 7) + 1):
# If sum is N
if (3 * i + 5 * j + 7 * k == N):
# Combination found
flag = 1;
# Print Answer
print(f"{i} {j} {k}");
break;
# No valid combination found
if (flag == 0):
print(-1);
# Driver code
N = 10;
check357(N);
# This code is contributed by saurabh_jaiswal.
// C# code for the above approach
using System;
public class GFG
{
// Function to check if a number can
// be represented as summation of the
// multiples of 3, 5 and 7
static void check357(int N)
{
// flag indicates if combination
// is possible or not
int flag = 0;
// Loop for multiples of 3
for (int i = 0; i <= N / 3; i++) {
if (flag == 1)
break;
// Loop for multiples of 5
for (int j = 0; j <= N / 5; j++) {
if (flag == 1)
break;
// Loop for multiples of 7
for (int k = 0; k <= N / 7; k++) {
// If sum is N
if (3 * i + 5 * j + 7 * k == N) {
// Combination found
flag = 1;
// Print Answer
Console.Write(i + " " + j + " " + k);
break;
}
}
}
}
// No valid combination found
if (flag == 0)
Console.WriteLine(-1);
}
// Driver code
public static void Main(string[] args)
{
int N = 10;
check357(N);
}
}
// This code is contributed by AnkThon
<script>
// Javascript implementation of the above approach
// Function to check if a number can
// be represented as summation of the
// multiples of 3, 5 and 7
function check357(N)
{
// flag indicates if combination
// is possible or not
let flag = 0;
// Loop for multiples of 3
for (let i = 0; i <= Math.floor(N / 3); i++) {
if (flag == 1)
break;
// Loop for multiples of 5
for (let j = 0; j <= Math.floor(N / 5); j++) {
if (flag == 1)
break;
// Loop for multiples of 7
for (let k = 0; k <= Math.floor(N / 7); k++) {
// If sum is N
if (3 * i + 5 * j + 7 * k == N) {
// Combination found
flag = 1;
// Print Answer
document.write(i + " " + j + " " + k);
break;
}
}
}
}
// No valid combination found
if (flag == 0)
document.write(-1);
}
// Driver code
let N = 10;
check357(N);
// This code is contributed by saurabh_jaiswal.
</script>
Output
0 2 0
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The given problem can be solved by using maths.
Since every number can be represented in terms of multiple of 3, as 3x, 3x+1 or 3x+2. Using this fact, we can say that 5 can be represented in form 3x+2 and 7 can be represented in form 3x+1.
With the help of this observation, N can be represented in the 3 following ways:
- If N is of the form 3x, it can be represented as 3x.
- If N is of the form 3x + 1,
- If N > 7, then N can be represented as 3*(x - 2) + 7 as 7 is similar to 3*2 + 1.
- Else if N <= 7, then it cannot be represented in the given form.
- If N is of the form 3n + 2,
- If N > 5, then N can be represented as 3x + 5 as 5 is similar to 3*1 + 2.
- Else if N <= 5, then it cannot be represented in the given form.
Below is the implementation of the above approach:
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
int check357(int N)
{
// Stores if a valid
// combination exists
int f = 0;
// if N is of the form 3n
if (N % 3 == 0)
return (N / 3) * 100 + 0 * 10 + 0;
else if (N % 3 == 1) {
// if N is of the form 3n + 1
if (N - 7 >= 0)
return ((N - 7) / 3) * 100 + 0 * 10 + 1;
}
else
// if N is of the form 3n + 2
if (N - 5 >= 0)
return ((N - 5) / 3) * 100 + 1 * 10 + 0;
// If no valid combinations exists
return -1;
}
// Driver code
int main()
{
int N = 10;
cout << check357(N);
return 0;
}
// Java implementation of the above approach
import java.util.*;
public class GFG
{
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
static int check357(int N)
{
// Stores if a valid
// combination exists
int f = 0;
// if N is of the form 3n
if (N % 3 == 0)
return (N / 3) * 100 + 0 * 10 + 0;
else if (N % 3 == 1) {
// if N is of the form 3n + 1
if (N - 7 >= 0)
return ((N - 7) / 3) * 100 + 0 * 10 + 1;
}
else
// if N is of the form 3n + 2
if (N - 5 >= 0)
return ((N - 5) / 3) * 100 + 1 * 10 + 0;
// If no valid combinations exists
return -1;
}
// Driver code
public static void main(String args[])
{
int N = 10;
System.out.print(check357(N));
}
}
// This code is contributed by Samim Hossain Mondal.
# Python implementation of the above approach
# Function to check if a number can be
# represented as summation of multiples
# of 3, 5 and 7
def check357(N):
# Stores if a valid
# combination exists
f = 0;
# if N is of the form 3n
if (N % 3 == 0):
return (N // 3) * 100 + 0 * 10 + 0;
elif (N % 3 == 1):
# if N is of the form 3n + 1
if (N - 7 >= 0):
return ((N - 7) // 3) * 100 + 0 * 10 + 1;
else:
if(N - 5 >= 0):
# if N is of the form 3n + 2
return ((N - 5) // 3) * 100 + 1 * 10 + 0;
# If no valid combinations exists
return -1;
# Driver code
if __name__ == '__main__':
N = 10;
print(check357(N));
# This code is contributed by shikhasingrajput
// C# implementation of the above approach
using System;
class GFG
{
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
static int check357(int N)
{
// Stores if a valid
// combination exists
int f = 0;
// if N is of the form 3n
if (N % 3 == 0)
return (N / 3) * 100 + 0 * 10 + 0;
else if (N % 3 == 1) {
// if N is of the form 3n + 1
if (N - 7 >= 0)
return ((N - 7) / 3) * 100 + 0 * 10 + 1;
}
else
// if N is of the form 3n + 2
if (N - 5 >= 0)
return ((N - 5) / 3) * 100 + 1 * 10 + 0;
// If no valid combinations exists
return -1;
}
// Driver code
public static void Main()
{
int N = 10;
Console.Write(check357(N));
}
}
// This code is contributed by Samim Hossain Mondal.
<script>
// Javascript implementation of the above approach
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
function check357(N)
{
// Stores if a valid
// combination exists
let f = 0;
// if N is of the form 3n
if (N % 3 == 0)
return (N / 3) * 100 + 0 * 10 + 0;
else if (N % 3 == 1) {
// if N is of the form 3n + 1
if (N - 7 >= 0)
return ((N - 7) / 3) * 100 + 0 * 10 + 1;
}
else
// if N is of the form 3n + 2
if (N - 5 >= 0)
return ((N - 5) / 3) * 100 + 1 * 10 + 0;
// If no valid combinations exists
return -1;
}
// Driver code
let N = 10;
document.write(check357(N));
// This code is contributed by gfgking.
</script>
Output:
101
Time Complexity: O(1)
Auxiliary Space: O(1)