Check if a number can be written as sum of three consecutive integers

Given an integer n, the task is to find whether n can be written as sum of three consecutive integer. If yes, find the three consecutive integer, else print “-1”.

Examples:

Input : n = 6
Output : 1 2 3
6 = 1 + 2 + 3.

Input : n = 7
Output : -1



Method 1: (Brute Force):
The idea is to run a loop from i = 0 to n – 2, check if (i + i+1 + i+2) is equal to n. Also, check if n is positive or negative and accordingly increment or decrement i by 1.

Below is the implementation of this approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to check if a number can
// be written as sum of three consecutive
// integers.
#include <bits/stdc++.h>
using namespace std;
  
// function to check if a number can be written as sum of
// three consecutive integer.
void checksum(int n)
{
    // if n is 0
    if (n == 0) {
        cout << "-1 0 1" << endl;
        return;
    }
  
    int inc;
  
    // if n is positive, increment loop by 1.
    if (n > 0)
        inc = 1;
  
    // if n is negative, decrement loop by 1.
    else
        inc = -1;
  
    // Running loop from 0 to n - 2
    for (int i = 0; i <= n - 2; i += inc) {
  
        // check if sum of three consecutive
        // integer is equal to n.
        if (i + i + 1 + i + 2 == n) {
            cout << i << " " << i + 1
                 << " " << i + 2;
            return;
        }
    }
  
    cout << "-1";
}
  
// Driver Program
int main()
{
    int n = 6;
    checksum(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA Code to check if a number
// can be written as sum of 
// three consecutive integers.
import java.util.*;
  
class GFG 
{
    // function to check if a number
    // can be written as sum of
    // three consecutive integer.
    static void checksum(int n)
    {
        // if n is 0
        if (n == 0) {
            System.out.println("-1 0 1");
            return;
        }
       
        int inc;
       
        // if n is positive, 
        // increment loop by 1.
        if (n > 0)
            inc = 1;
       
        // if n is negative, 
        // decrement loop by 1.
        else
            inc = -1;
       
        // Running loop from 0 to n - 2
        for (int i = 0; i <= n - 2; i += inc) {
       
            // check if sum of three consecutive
            // integer is equal to n.
            if (i + i + 1 + i + 2 == n) {
                System.out.println(i + " "
                                  (i + 1) +
                                " " + (i + 2));
                return;
            }
        }
       
        System.out.println("-1");
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        int n = 6;
        checksum(n);
    }
}
  
// This code is contributed by Arnav Kr. Mandal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to check if a number
# can be written as sum of three 
# consecutive integers.
  
# function to check if a number 
# can be written as sum of three
# consecutive integer.
def checksum(n):
  
    # if n is 0
    if n == 0:
        print("-1 0 1")
        return 0
          
    inc = 0 
  
    # if n is positive,
    # increment loop by 1.
    if n > 0:
        inc = 1
      
    # if n is negative,
    # decrement loop by 1.
    else:
        inc = -1
      
    # Running loop from 0 to n - 2
    for i in range(0, n-1, inc):
      
        # check if sum of three consecutive
        # integer is equal to n.
        if i + i + 1 + i + 2 == n:
            print(i ," ",i + 1, " ", i + 2)
            return 0
              
    print("-1")
      
# Driver Code 
n = 6
checksum(n)
  
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Code to check if a number
// can be written as sum of 
// three consecutive integers.
using System;
  
class GFG 
{
    // function to check if a number
    // can be written as sum of
    // three consecutive integer.
    static void checksum(int n)
    {
        // if n is 0
        if (n == 0) {
            Console.WriteLine("-1 0 1");
            return;
        }
      
        int inc;
      
        // if n is positive, 
        // increment loop by 1.
        if (n > 0)
            inc = 1;
      
        // if n is negative, 
        // decrement loop by 1.
        else
            inc = -1;
      
        // Running loop from 0 to n - 2
        for (int i = 0; i <= n - 2; i += inc) {
      
            // check if sum of three consecutive
            // integer is equal to n.
            if (i + i + 1 + i + 2 == n) {
                Console.WriteLine(i + " "
                     + (i + 1) +" " + (i + 2));
                return;
            }
        }
      
        Console.WriteLine("-1");
    }
      
    /* Driver program to test above function */
    public static void Main() 
    {
        int n = 6;
        checksum(n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to check if a
// number can be written
// as sum of three consecutive
// integers.
  
// function to check if a number
// can be written as sum of
// three consecutive integer.
function checksum($n)
{
      
    // if n is 0
    if ($n == 0) 
    {
        echo "-1 0 1" ;
        return;
    }
  
    $inc;
  
    // if n is positive,
    // increment loop by 1.
    if ($n > 0)
        $inc = 1;
  
    // if n is negative, 
    // decrement loop by 1.
    else
        $inc = -1;
  
    // Running loop from
    // 0 to n - 2
    for ($i = 0; $i <= $n - 2; $i += $inc
    {
  
        // check if sum of three consecutive
        // integer is equal to n.
        if ($i + $i + 1 + $i + 2 == $n
        {
            echo $i , " " , $i + 1
                , " " , $i + 2;
            return;
        }
    }
  
    echo "-1";
}
  
    // Driver Code
    $n = 6;
    checksum($n);
      
// This code is contributed by anuj_67.
?>

chevron_right


Output:

1 2 3

Method 2: (Efficient Approach)
The idea is to check if n is multiple of 3 or not.
Let n is sum of three consecutive integer of k – 1, k, k + 1. Therefore,
k – 1 + k + k + 1 = n
3*k = n
The three number will be n/3 – 1, n/3, n/3 + 1.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to check if a number can be 
// written as sum of three consecutive integer.
#include <bits/stdc++.h>
using namespace std;
  
// function to check if a number can be
// written as sum of three consecutive 
// integers.
void checksum(int n)
{
    // if n is multiple of 3
    if (n % 3 == 0)
        cout << n / 3 - 1 << " " 
             << n / 3 << " " << n / 3 + 1;
  
    // else print "-1".
    else
        cout << "-1";
}
  
// Driver Program
int main()
{
    int n = 6;
    checksum(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA Code to check if a number
// can be written as sum of three
// consecutive integers.
import java.util.*;
  
class GFG 
{
    // function to check if a number 
    // can be written as sum of three
    // consecutive integers.
    static void checksum(int n)
    {
        // if n is multiple of 3
        if (n % 3 == 0)
            System.out.println( n / 3 - 1 + " "
                 + n / 3 + " " + (n / 3 + 1));
       
        // else print "-1".
        else
            System.out.println("-1");
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        int n = 6;
        checksum(n);
    }
}
  
// This code is contributed by Arnav Kr. Mandal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to check if a number 
# can be written as sum of three
# consecutive integer.
  
# function to check if a number 
# can be written as sum of three
# consecutive integers.
def checksum(n):
    n = int(n)
      
    # if n is multiple of 3
    if n % 3 == 0:
        print(int(n / 3 - 1) ," ",
         int(n / 3)," ",int(n / 3 + 1))
      
    # else print "-1".
    else:
        print("-1")
          
# Driver Code
n = 6
checksum(n)
  
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Code to check if a number
// can be written as sum of three
// consecutive integers.
using System;
  
class GFG 
{
    // function to check if a number 
    // can be written as sum of three
    // consecutive integers.
    static void checksum(int n)
    {
        // if n is multiple of 3
        if (n % 3 == 0)
            Console.WriteLine( n / 3 - 1 + " "
                    + n / 3 + " " + (n / 3 + 1));
      
        // else print "-1".
        else
            Console.WriteLine("-1");
    }
      
    /* Driver program to
     test above function */
    public static void Main() 
    {
        int n = 6;
      
        checksum(n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Code to check if a number
// can be written as sum of three
// consecutive integers.
  
// function to check if
// a number can be written
// as sum of three consecutive 
// integers.
function checksum($n)
{
      
    // if n is multiple of 3
    if ($n % 3 == 0)
        echo $n / 3 - 1, " ",
             $n / 3, " "
             $n / 3 + 1;
  
    // else print "-1".
    else
        echo "-1";
}
  
    // Driver Program
    $n = 6;
    checksum($n);
  
// This code is contributed by aj_36
?>

chevron_right


Output:

1 2 3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : vt_m, jit_t