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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print a, b and c
// such that a+b+c=N
#include <bits/stdc++.h>
using namespace std;
  
// Function to print a, b and c
void printCombination(int n)
{
  
    // first loop
    for (int i = 1; i < n; i++) {
  
        // check for 1st number
        if (i % 3 != 0) {
  
            // second loop
            for (int j = 1; j < n; j++) {
  
                // check for 2nd number
                if (j % 3 != 0) {
  
                    // third loop
                    for (int k = 1; k < n; k++) {
  
                        // Check for 3rd number
                        if (k % 3 != 0 && (i + j + k) == n) {
                            cout << i << " " << j << " " << k;
                            return;
                        }
                    }
                }
            }
        }
    }
}
  
// Driver Code
int main()
{
    int n = 233;
  
    printCombination(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print a, 
// b and c such that a+b+c=N
import java.io.*;
  
class GFG
{
  
// Function to print a, b and c
static void printCombination(int n)
{
    // first loop
    for (int i = 1; i < n; i++) 
    {
  
        // check for 1st number
        if (i % 3 != 0
        {
  
            // second loop
            for (int j = 1; j < n; j++)
            {
  
                // check for 2nd number
                if (j % 3 != 0
                {
  
                    // third loop
                    for (int k = 1; k < n; k++) 
                    {
  
                        // Check for 3rd number
                        if (k % 3 != 0 && (i + j + k) == n) 
                        {
                            System.out.println( i + " "
                                                j + " " + k);
                            return;
                        }
                    }
                }
            }
        }
    }
}
  
// Driver Code
public static void main (String[] args) 
{
    int n = 233;
      
    printCombination(n);
}
}
  
// This code is contributed 
// by anuj_67.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print a, b 
# and c such that a+b+c=N
  
# Function to print a, b and c
def printCombination(n):
  
    # first loop
    for i in range(1, n):
      
        # check for 1st number
        if (i % 3 != 0): 
              
            # second loop
            for j in range(1, n):
  
                # check for 2nd number
                if (j % 3 != 0): 
  
                    # third loop
                    for k in range(1, n):
  
                        # Check for 3rd number
                        if (k % 3 != 0 and 
                           (i + j + k) == n): 
                            print(i, j, k);
                            return;
  
# Driver Code
n = 233;
  
printCombination(n);
  
# This code is contributed 
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print a, 
// b and c such that a+b+c=N
class GFG
{
  
// Function to print a, b and c
static void printCombination(int n)
{
    // first loop
    for (int i = 1; i < n; i++) 
    {
  
        // check for 1st number
        if (i % 3 != 0) 
        {
  
            // second loop
            for (int j = 1; j < n; j++)
            {
  
                // check for 2nd number
                if (j % 3 != 0) 
                {
  
                    // third loop
                    for (int k = 1; k < n; k++) 
                    {
  
                        // Check for 3rd number
                        if (k % 3 != 0 && (i + j + k) == n) 
                        {
                            System.Console.WriteLine(i + " "
                                                     j + " " + k);
                            return;
                        }
                    }
                }
            }
        }
    }
}
  
// Driver Code
static void Main () 
{
    int n = 233;
      
    printCombination(n);
}
}
  
// This code is contributed 
// by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print a, b and c
// such that a+b+c=N
  
// Function to print a, b and c
function printCombination($n)
{
  
    // first loop
    for ($i = 1; $i < $n; $i++)
    {
  
        // check for 1st number
        if ($i % 3 != 0) 
        {
  
            // second loop
            for ($j = 1; $j < $n; $j++)
            {
  
                // check for 2nd number
                if ($j % 3 != 0) 
                {
  
                    // third loop
                    for ($k = 1; $k < $n; $k++)
                    {
  
                        // Check for 3rd number
                        if ($k % 3 != 0 && 
                           ($i + $j + $k) == $n
                        {
                            echo $i , " " , $j , " " , $k;
                            return;
                        }
                    }
                }
            }
        }
    }
}
  
// Driver Code
$n = 233;
  
printCombination($n);
  
// This code is contributed 
// inder_verma
?>

chevron_right


Output:

1 2 230

Time Complexity: O(n3)

Efficient approach:

  1. Consider 1 as one of the three numbers.
  2. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print a, b and c
// such that a+b+c=N
#include <bits/stdc++.h>
using namespace std;
  
// Function to print a, b and c
void printCombination(int n)
{
    cout << 1 << " ";
  
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        cout << 2 << " " << n - 3;
    else
        cout << 1 << " " << n - 2;
}
  
// Driver Code
int main()
{
    int n = 233;
  
    printCombination(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print a, b 
// and c such that a+b+c=N
class GFG
{
// Function to print a, b and c
static void printCombination(int n)
{
    System.out.print(1 + " ");
  
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        System.out.print(2 + " " + (n - 3));
    else
        System.out.print(1 + " " + (n - 2));
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 233;
  
    printCombination(n);
}
}
  
// This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print a, b and c
# such that a+b+c=N
  
# Function to print a, b and c
def printCombination(n):
    print("1 ",end="");
  
    # check if n-2 is divisible
    # by 3 or not
    if ((n - 2) % 3 == 0):
        print("2",n - 3,end="");
    else:
        print("1",(n - 2),end="");
  
# Drive code
if __name__=='__main__':
    n = 233;
  
    printCombination(n);
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print a, b 
// and c such that a+b+c=N
  
class GFG
{
// Function to print a, b and c
static void printCombination(int n)
{
    System.Console.Write(1 + " ");
  
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        System.Console.Write(2 + " " + (n - 3));
    else
        System.Console.Write(1 + " " + (n - 2));
}
  
// Driver Code
static void Main()
{
    int n = 233;
  
    printCombination(n);
}
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print a, b and c
// such that a+b+c=N
  
// Function to print a, b and c
function printCombination($n)
{
    echo "1 ";
  
    // check if n-2 is divisible
    // by 3 or not
    if (($n - 2) % 3 == 0)
        echo "2 " . ($n - 3);
    else
        echo "1 " . ($n - 2);
}
  
// Drive code
$n = 233;
  
printCombination($n);
  
// This code is contributed by mits
?>

chevron_right


Output:

1 2 230

Time Complexity: O(1)



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : inderDuMCA, Mithun Kumar