Check if a number N can be represented as a sum of multiples of 3, 5, and 7
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++
#include <iostream>
using namespace std;
void check357( int N)
{
int flag = 0;
for ( int i = 0; i <= N / 3; i++) {
if (flag == 1)
break ;
for ( int j = 0; j <= N / 5; j++) {
if (flag == 1)
break ;
for ( int k = 0; k <= N / 7; k++) {
if (3 * i + 5 * j + 7 * k == N) {
flag = 1;
cout << i << " "
<< j << " " << k;
break ;
}
}
}
}
if (flag == 0)
cout << -1;
}
int main()
{
int N = 10;
check357(N);
}
|
Java
import java.io.*;
class GFG
{
static void check357( int N)
{
int flag = 0 ;
for ( int i = 0 ; i <= N / 3 ; i++) {
if (flag == 1 )
break ;
for ( int j = 0 ; j <= N / 5 ; j++) {
if (flag == 1 )
break ;
for ( int k = 0 ; k <= N / 7 ; k++) {
if ( 3 * i + 5 * j + 7 * k == N) {
flag = 1 ;
System.out.print(i + " " + j + " "
+ k);
break ;
}
}
}
}
if (flag == 0 )
System.out.println(- 1 );
}
public static void main(String[] args)
{
int N = 10 ;
check357(N);
}
}
|
Python3
def check357(N):
flag = 0 ;
for i in range ((N / / 3 ) + 1 ):
if (flag = = 1 ):
break ;
for j in range ((N / / 5 ) + 1 ):
if (flag = = 1 ):
break ;
for k in range ((N / / 7 ) + 1 ):
if ( 3 * i + 5 * j + 7 * k = = N):
flag = 1 ;
print (f "{i} {j} {k}" );
break ;
if (flag = = 0 ):
print ( - 1 );
N = 10 ;
check357(N);
|
C#
using System;
public class GFG
{
static void check357( int N)
{
int flag = 0;
for ( int i = 0; i <= N / 3; i++) {
if (flag == 1)
break ;
for ( int j = 0; j <= N / 5; j++) {
if (flag == 1)
break ;
for ( int k = 0; k <= N / 7; k++) {
if (3 * i + 5 * j + 7 * k == N) {
flag = 1;
Console.Write(i + " " + j + " " + k);
break ;
}
}
}
}
if (flag == 0)
Console.WriteLine(-1);
}
public static void Main( string [] args)
{
int N = 10;
check357(N);
}
}
|
Javascript
<script>
function check357(N)
{
let flag = 0;
for (let i = 0; i <= Math.floor(N / 3); i++) {
if (flag == 1)
break ;
for (let j = 0; j <= Math.floor(N / 5); j++) {
if (flag == 1)
break ;
for (let k = 0; k <= Math.floor(N / 7); k++) {
if (3 * i + 5 * j + 7 * k == N) {
flag = 1;
document.write(i + " " + j + " " + k);
break ;
}
}
}
}
if (flag == 0)
document.write(-1);
}
let N = 10;
check357(N);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int check357( int N)
{
int f = 0;
if (N % 3 == 0)
return (N / 3) * 100 + 0 * 10 + 0;
else if (N % 3 == 1) {
if (N - 7 >= 0)
return ((N - 7) / 3) * 100 + 0 * 10 + 1;
}
else
if (N - 5 >= 0)
return ((N - 5) / 3) * 100 + 1 * 10 + 0;
return -1;
}
int main()
{
int N = 10;
cout << check357(N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int check357( int N)
{
int f = 0 ;
if (N % 3 == 0 )
return (N / 3 ) * 100 + 0 * 10 + 0 ;
else if (N % 3 == 1 ) {
if (N - 7 >= 0 )
return ((N - 7 ) / 3 ) * 100 + 0 * 10 + 1 ;
}
else
if (N - 5 >= 0 )
return ((N - 5 ) / 3 ) * 100 + 1 * 10 + 0 ;
return - 1 ;
}
public static void main(String args[])
{
int N = 10 ;
System.out.print(check357(N));
}
}
|
Python3
def check357(N):
f = 0 ;
if (N % 3 = = 0 ):
return (N / / 3 ) * 100 + 0 * 10 + 0 ;
elif (N % 3 = = 1 ):
if (N - 7 > = 0 ):
return ((N - 7 ) / / 3 ) * 100 + 0 * 10 + 1 ;
else :
if (N - 5 > = 0 ):
return ((N - 5 ) / / 3 ) * 100 + 1 * 10 + 0 ;
return - 1 ;
if __name__ = = '__main__' :
N = 10 ;
print (check357(N));
|
C#
using System;
class GFG
{
static int check357( int N)
{
int f = 0;
if (N % 3 == 0)
return (N / 3) * 100 + 0 * 10 + 0;
else if (N % 3 == 1) {
if (N - 7 >= 0)
return ((N - 7) / 3) * 100 + 0 * 10 + 1;
}
else
if (N - 5 >= 0)
return ((N - 5) / 3) * 100 + 1 * 10 + 0;
return -1;
}
public static void Main()
{
int N = 10;
Console.Write(check357(N));
}
}
|
Javascript
<script>
function check357(N)
{
let f = 0;
if (N % 3 == 0)
return (N / 3) * 100 + 0 * 10 + 0;
else if (N % 3 == 1) {
if (N - 7 >= 0)
return ((N - 7) / 3) * 100 + 0 * 10 + 1;
}
else
if (N - 5 >= 0)
return ((N - 5) / 3) * 100 + 1 * 10 + 0;
return -1;
}
let N = 10;
document.write(check357(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
23 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...