FastPrepNearest Composite Number
Problems/Nearest Composite Number

Nearest Composite Number

Accenture logoAccenture● MediumNEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reported task identifies this algorithmic exercise. The executable interface, bounds, deterministic tie rules, and examples are the documented practice contract.

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

  • Alternating Index Square SumONSITE INTERVIEW · Seen Sep 2026
  • Minimum Password IncrementsOA · Seen Sep 2026
  • Reduce a Number to One DigitOA · Seen Sep 2026
All Accenture interview questionsSee Accenture hiring insights
Saved
public int nearestComposite(int n) {
    // Write your code here.
}
Checking account…