FastPrepNearest Composite Number

Nearest Composite Number

Accenture logoAccenture● MediumNEW GRADOA
Learn

Problem statement

Return the composite integer with the smallest absolute difference from n. A composite integer is greater than 1 and has a divisor other than 1 and itself.

If two composite numbers are equally near, return the smaller one.

Function

nearestComposite(n: int) → int

Examples

Example 1

n = 4return = 4

Case 1 exercises the documented deterministic contract.

Example 2

n = 5return = 4

Case 2 exercises the documented deterministic contract.

Example 3

n = 7return = 6

Case 3 exercises the documented deterministic contract.

Constraints

  • 0 <= n <= 10^9.

More Accenture problems

See Accenture hiring insights
public int nearestComposite(int n) {
    // Write your code here.
}
n4
expected4
Checking account…