Find Three Consecutive Integers That Sum to a Given Number

Medium
854.

Find Three Consecutive Integers That Sum to a Given Number

Find Three Consecutive Integers That Sum to a Given Number is a medium coding problem on math and simulation, asked in Amazon, Adobe and Infosys interviews.

Return three consecutive integers that sum to num, in increasing order. If no such triple exists, return an empty array.

Example 1

Input
num = 33
Output
[10,11,12]
Explanation
10 + 11 + 12 = 33.

Example 2

Input
num = 4
Output
[]
Explanation
4 is not divisible by 3.

Example 3

Input
num = 0
Output
[-1,0,1]

Constraints

  • 0 <= num <= 1000000000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} num
 * @return {number[]}
 */
var sumOfThree = function(num) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Find Three Consecutive Integers That Sum to a Given Number Solution

Find Three Consecutive Integers That Sum to a Given Number solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Find Three Consecutive Integers That Sum to a Given Number is a medium coding problem on math and simulation, asked in Amazon, Adobe and Infosys interviews.

  • Difficulty: Medium
  • Topics: Math, Simulation
  • Asked at: Amazon, Adobe, Infosys
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Return three consecutive integers that sum to num, in increasing order. If no such triple exists, return an empty array.

Example 1

Input: num = 33
Output: [10,11,12]
Explanation: 10 + 11 + 12 = 33.

Example 2

Input: num = 4
Output: []
Explanation: 4 is not divisible by 3.

Example 3

Input: num = 0
Output: [-1,0,1]

Constraints

  • 0 <= num <= 1000000000

Hints for Find Three Consecutive Integers That Sum to a Given Number

  1. Hint 1

    Write the three numbers as m - 1, m and m + 1.

  2. Hint 2

    Their sum is exactly 3m, so num must be divisible by 3.

  3. Hint 3

    When it is, m = num / 3 and the triple follows.

How to solve Find Three Consecutive Integers That Sum to a Given Number

Centre the triple on its middle value. Three consecutive integers around m sum to 3m, which makes divisibility by 3 both necessary and sufficient.

Approach

  1. If num % 3 != 0, return an empty array.
  2. Otherwise set m = num / 3 and return [m - 1, m, m + 1].

Why it works

(m-1) + m + (m+1) = 3m collapses the whole search to one division. Since m is determined uniquely, the triple is unique too — there is no choice to make and nothing to search.

Complexity

  • Time — O(1)
  • Space — O(1)

Pitfalls

  • Searching for the triple by iteration is unnecessary and slow at num = 10^9.
  • The middle value is num / 3, not num / 3 - 1 or the first element.
  • num = 0 is legal and yields [-1,0,1].

Reference solution

Python

from typing import List

def sumOfThree(num: int) -> List[int]:
    if num % 3 != 0:
        return []
    m = num // 3
    return [m - 1, m, m + 1]

Java

public static int[] sumOfThree(int num) {
    if (num % 3 != 0) return new int[0];
    int m = num / 3;
    return new int[] { m - 1, m, m + 1 };
}

C++

vector<int> sumOfThree(int num) {
    if (num % 3 != 0) return vector<int>();
    int m = num / 3;
    return vector<int>{ m - 1, m, m + 1 };
}

JavaScript

var sumOfThree = function(num) {
    if (num % 3 !== 0) return [];
    var m = num / 3;
    return [m - 1, m, m + 1];
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue