Triplet with no element divisible by 3 and sum N
Given an integer N greater than 2, the task is to print any combination of a, b and c such that:
- a + b + c = N
- a, b and c are not divisible by 3.
Examples:
Input: N = 233
Output: 77 77 79
Input: N = 3
Output: 1 1 1
A naive approach is to use three loops and check for the given condition.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printCombination( int n)
{
for ( int i = 1; i < n; i++) {
if (i % 3 != 0) {
for ( int j = 1; j < n; j++) {
if (j % 3 != 0) {
for ( int k = 1; k < n; k++) {
if (k % 3 != 0 && (i + j + k) == n) {
cout << i << " " << j << " " << k;
return ;
}
}
}
}
}
}
}
int main()
{
int n = 233;
printCombination(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void printCombination( int n)
{
for ( int i = 1 ; i < n; i++)
{
if (i % 3 != 0 )
{
for ( int j = 1 ; j < n; j++)
{
if (j % 3 != 0 )
{
for ( int k = 1 ; k < n; k++)
{
if (k % 3 != 0 && (i + j + k) == n)
{
System.out.println( i + " " +
j + " " + k);
return ;
}
}
}
}
}
}
}
public static void main (String[] args)
{
int n = 233 ;
printCombination(n);
}
}
|
Python3
def printCombination(n):
for i in range ( 1 , n):
if (i % 3 ! = 0 ):
for j in range ( 1 , n):
if (j % 3 ! = 0 ):
for k in range ( 1 , n):
if (k % 3 ! = 0 and
(i + j + k) = = n):
print (i, j, k);
return ;
n = 233 ;
printCombination(n);
|
C#
class GFG
{
static void printCombination( int n)
{
for ( int i = 1; i < n; i++)
{
if (i % 3 != 0)
{
for ( int j = 1; j < n; j++)
{
if (j % 3 != 0)
{
for ( int k = 1; k < n; k++)
{
if (k % 3 != 0 && (i + j + k) == n)
{
System.Console.WriteLine(i + " " +
j + " " + k);
return ;
}
}
}
}
}
}
}
static void Main ()
{
int n = 233;
printCombination(n);
}
}
|
PHP
<?php
function printCombination( $n )
{
for ( $i = 1; $i < $n ; $i ++)
{
if ( $i % 3 != 0)
{
for ( $j = 1; $j < $n ; $j ++)
{
if ( $j % 3 != 0)
{
for ( $k = 1; $k < $n ; $k ++)
{
if ( $k % 3 != 0 &&
( $i + $j + $k ) == $n )
{
echo $i , " " , $j , " " , $k ;
return ;
}
}
}
}
}
}
}
$n = 233;
printCombination( $n );
?>
|
Javascript
<script>
function printCombination(n)
{
for (let i = 1; i < n; i++) {
if (i % 3 != 0) {
for (let j = 1; j < n; j++) {
if (j % 3 != 0) {
for (let k = 1; k < n; k++) {
if (k % 3 != 0 && (i + j + k) == n) {
document.write(i + " " + j + " " + k);
return ;
}
}
}
}
}
}
}
let n = 233;
printCombination(n);
</script>
|
Time Complexity: O(N3), as we are using a loop to traverse N3 times.
Auxiliary Space: O(1), as we are not using any extra space.
Efficient approach:
- Consider 1 as one of the three numbers.
- The other two numbers will be:
- (2, n-3) if n-2 is divisible by 3.
- Otherwise (1, n-2) if n-2 is not divisible by 3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printCombination( int n)
{
cout << 1 << " " ;
if ((n - 2) % 3 == 0)
cout << 2 << " " << n - 3;
else
cout << 1 << " " << n - 2;
}
int main()
{
int n = 233;
printCombination(n);
return 0;
}
|
Java
class GFG
{
static void printCombination( int n)
{
System.out.print( 1 + " " );
if ((n - 2 ) % 3 == 0 )
System.out.print( 2 + " " + (n - 3 ));
else
System.out.print( 1 + " " + (n - 2 ));
}
public static void main(String[] args)
{
int n = 233 ;
printCombination(n);
}
}
|
Python3
def printCombination(n):
print ( "1 " ,end = "");
if ((n - 2 ) % 3 = = 0 ):
print ( "2" ,n - 3 ,end = "");
else :
print ( "1" ,(n - 2 ),end = "");
if __name__ = = '__main__' :
n = 233 ;
printCombination(n);
|
C#
class GFG
{
static void printCombination( int n)
{
System.Console.Write(1 + " " );
if ((n - 2) % 3 == 0)
System.Console.Write(2 + " " + (n - 3));
else
System.Console.Write(1 + " " + (n - 2));
}
static void Main()
{
int n = 233;
printCombination(n);
}
}
|
PHP
<?php
function printCombination( $n )
{
echo "1 " ;
if (( $n - 2) % 3 == 0)
echo "2 " . ( $n - 3);
else
echo "1 " . ( $n - 2);
}
$n = 233;
printCombination( $n );
?>
|
Javascript
<script>
function printCombination(n)
{
document.write(1 + " " );
if ((n - 2) % 3 == 0)
document.write(2 + " " + (n - 3));
else
document.write(1 + " " + (n - 2));
}
let n = 233;
printCombination(n);
</script>
|
Time Complexity: O(1), as we are not using any loop or recursion to traverse.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
08 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...