Number expressed as sum of five consecutive integers

Given an integer n, the task is to find whether n can be expressed as sum of five consecutive integer. If yes, find the five consecutive integers, else print “-1”.
Examples: 
 

Input : n = 15
Output : 1 2 3 4 5
15 = 1 + 2 + 3 + 4 + 5

Input : n = 18
Output : -1

 

Method 1: (Brute Force) 
The idea is to run a loop from i = 0 to n – 4, check if (i + i+1 + i+2 + i+3 + i+4) 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++




// CPP Program to check if a number can
// be expressed as sum of five consecutive
// integers.
#include <bits/stdc++.h>
using namespace std;
 
// function to check if a number can be expressed as
// sum of five consecutive integers.
void checksum(int n)
{
    // if n is 0
    if (n == 0) {
        cout << "-2 -1 0 1 2" << 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 - 4
    for (int i = 0; i <= n - 4; i += inc) {
 
        // check if sum of five consecutive
        // integer is equal to n.
        if (i + i + 1 + i + 2 + i + 3 + i + 4 == n) {
            cout << i << " " << i + 1
                 << " " << i + 2
                 << " " << i + 3
                 << " " << i + 4;
            return;
        }
    }
 
    cout << "-1";
}
 
// Driver Program
int main()
{
    int n = 15;
    checksum(n);
    return 0;
}


Java




// Java Program to check if a number can
// be expressed as sum of five consecutive
// integers.
import java.io.*;
 
class GFG {
 
    // function to check if a number can be
    // expressed as sum of five consecutive
    // integers.
    static void checksum(int n)
    {
        // if n is 0
        if (n == 0) {
            System.out.println("-2 -1 0 1 2");
            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 - 4
        for (int i = 0; i <= n - 4; i += inc) {
     
            // check if sum of five consecutive
            // integer is equal to n.
            if (i + i + 1 + i + 2 + i + 3 + i
                                       + 4 == n)
            {
                System.out.print( (i )
                        + " " + (i + 1)
                        + " " + (i + 2)
                        + " " + (i + 3)
                        + " " + (i + 4));
                return;
            }
        }
     
        System.out.println( "-1");
    }
     
    // Driver Program
    public static void main (String[] args)
    {
        int n = 15;
        checksum(n);
    }
}
 
// This code is contributed by anuj_67


Python3




# Python3 code to check if a number
# can be expressed as sum of five
# consecutive integers.
  
# function to check if a number
# can be expressed as sum of five
# consecutive integer.
def checksum(n):
  
    # if n is 0
    if n == 0:
        print("-2 -1 0 1 2")
        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 - 4
    for i in range(0, n-3, inc):
      
        # check if sum of five consecutive
        # integer is equal to n.
        if i + i + 1 + i + 2 + i + 3 + i + 4 == n:
            print(i, " ", i + 1, " ", i + 2, " ", i + 3, " ", i + 4)
            return 0
              
    print("-1")
      
# Driver Code
n = 15
checksum(n)


C#




// C# Program to check if a number can
// be expressed as sum of five consecutive
// integers.
using System;
 
class GFG {
 
    // function to check if a number can be
    // expressed as sum of five consecutive
    // integers.
    static void checksum(int n)
    {
        // if n is 0
        if (n == 0) {
            Console.Write("-2 -1 0 1 2");
            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 - 4
        for (int i = 0; i <= n - 4; i += inc) {
     
            // check if sum of five consecutive
            // integer is equal to n.
            if (i + i + 1 + i + 2 + i + 3 + i
                                    + 4 == n)
            {
                Console.Write( (i )
                        + " " + (i + 1)
                        + " " + (i + 2)
                        + " " + (i + 3)
                        + " " + (i + 4));
                return;
            }
        }
     
        Console.WriteLine( "-1");
    }
     
    // Driver Program
    public static void Main ()
    {
        int n = 15;
        checksum(n);
    }
}
 
// This code is contributed by anuj_67


PHP




<?php
// PHP Program to check
// if a number can be
// expressed as sum of
// five consecutive integers.
 
// function to check if a
// number can be expressed
// as sum of five consecutive
// integers.
function checksum($n)
{
    // if n is 0
    if ($n == 0)
    {
        echo "-2 -1 0 1 2" , "\n";
            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 - 4
    for ($i = 0;
         $i <= $n - 4; $i += $inc)
    {
 
        // check if sum of five
        // consecutive integer
        // is equal to n.
        if ($i + $i + 1 + $i + 2 +
            $i + 3 + $i + 4 == $n)
        {
            echo $i , " " , $i + 1,
                      " " , $i + 2,
                      " " , $i + 3,
                      " " , $i + 4;
            return;
        }
    }
 
    echo "-1";
}
 
// Driver Code
$n = 15;
checksum($n);
 
// This code is contributed
// by ajit
?>


Javascript




<script>
 
// JavaScript program to check if a number can
// be expressed as sum of five consecutive
// integers.
 
    // function to check if a number can be
    // expressed as sum of five consecutive
    // integers.
    function checksum(n) {
        // if n is 0
        if (n == 0) {
            document.write("-2 -1 0 1 2");
            return;
        }
 
        var 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 - 4
        for (i = 0; i <= n - 4; i += inc) {
 
            // check if sum of five consecutive
            // integer is equal to n.
            if (i + i + 1 + i + 2 + i + 3 + i + 4 == n) {
                document.write((i) + " "
                + (i + 1) + " "
                + (i + 2) + " "
                + (i + 3) + " "
                + (i + 4));
                return;
            }
        }
 
        document.write("-1");
    }
 
    // Driver Program
     
        var n = 15;
        checksum(n);
 
// This code contributed by gauravrajput1
 
</script>


Output : 

1 2 3 4 5

Method 2: (Efficient Approach) 
The idea is to check if n is multiple of 5 or not. 
Let n is sum of five consecutive integer of k – 2, k-1, k, k + 1, k+2. Therefore, 
k-2 + k-1 + k + k+1 + k+2= n 
5*k = n 
The five numbers will be n/5 – 2, n/5 – 1, n/5, n/5 + 1, n/5 + 2. 
 
Below is the implementation of this approach: 
 

C++




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


Java




// Java Program to check if a number can
// be expressed as sum of five consecutive
// integer.
import java.io.*;
 
class GFG {
 
    // function to check if a number can
    // be expressed as sum of five
    // consecutive integers.
    static void checksum(int n)
    {
        // if n is multiple of 5
        if (n % 5 == 0)
            System.out.println( (n / 5 - 2)
                  + " " + (n / 5 - 1) + " "
                  + (n / 5) + " " + (n / 5
                + 1 ) + " " + (n / 5 + 2));
     
        // else print "-1".
        else
            System.out.println( "-1");
    }
     
    // Driver Program
    public static void main (String[] args)
    {
        int n = 15;
        checksum(n);
    }
}
 
// This code is contributed by vt_m.


Python3




# Python3 code to check if a number
# can be expressed as sum of five
# consecutive integer.
  
# function to check if a number
# can be expressed as sum of five
# consecutive integers.
def checksum(n):
    n = int(n)
      
    # if n is multiple of 5
    if n % 5 == 0:
        print(int(n / 5 - 2), " ",
         int(n / 5 - 1), " ", int(n / 5), " ", int(n / 5 + 1), " ", int(n / 5 + 2))
      
    # else print "-1".
    else:
        print("-1")
          
# Driver Code
n = 15
checksum(n)


C#




// C# Program to check if a number can
// be expressed as sum of five consecutive
// integer.
using System;
 
class GFG {
 
    // function to check if a number can
    // be expressed as sum of five
    // consecutive integers.
    static void checksum(int n)
    {
        // if n is multiple of 5
        if (n % 5 == 0)
            Console.WriteLine( (n / 5 - 2)
                + " " + (n / 5 - 1) + " "
                + (n / 5) + " " + (n / 5
                + 1 ) + " " + (n / 5 + 2));
     
        // else print "-1".
        else
            Console.WriteLine( "-1");
    }
     
    // Driver Program
    public static void Main ()
    {
        int n = 15;
        checksum(n);
    }
}
 
// This code is contributed by anuj_67.


PHP




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


Javascript




<script>
// javascript Program to check if a number can
// be expressed as sum of five consecutive
// integer.
 
    // function to check if a number can
    // be expressed as sum of five
    // consecutive integers.
    function checksum(n) {
        // if n is multiple of 5
        if (n % 5 == 0)
            document.write((n / 5 - 2) + " "
            + (n / 5 - 1) + " " + (n / 5)
            + " " + (n / 5 + 1) + " " + (n / 5 + 2));
 
        // else print "-1".
        else
            document.write("-1");
    }
 
    // Driver Program
     
        var n = 15;
        checksum(n);
 
// This code is contributed by todaysgaurav
</script>


Output: 
 

1 2 3 4 5

 Time Complexity : O(1)

Auxilitary Space Complexity : O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button