Classify three integers as an arithmetic progression, a geometric progression, both or neither

Quick Overview

Decide whether three integers in a fixed order form an arithmetic progression, a geometric progression, both or neither, and return the matching label. It checks how you handle sequences that contain zeros and sequences where every term is equal.

Classify three integers as an arithmetic progression, a geometric progression, both or neither

Company: Sap

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given three integers `a`, `b` and `c`, taken in that order, decide whether they form an arithmetic progression (AP), a geometric progression (GP), both, or neither. The three numbers form an AP when the difference between consecutive terms is constant, that is `b - a == c - b`. They form a GP when each term is the previous term multiplied by the same ratio: there is a number `r` with `b == a * r` and `c == b * r`. Return: - `"AP"` if they form an AP and not a GP. - `"GP"` if they form a GP and not an AP. - `"AP and GP"` if they form both. - `"Neither"` if they form neither. ### Function Signature ```python def identify_progression(a: int, b: int, c: int) -> str: ``` ### Rules - The ratio `r` can be any real number, including a negative number, a fraction or 0. For example, `4, 2, 1` (ratio 0.5) and `-2, 4, -8` (ratio -2) are GPs, and `5, 0, 0` is a GP with ratio 0. - When `a` is 0, the numbers form a GP only if `b` and `c` are also 0. So `0, 0, 0` is a GP and `0, 0, 5` is not. - The order of the three numbers is fixed. Do not rearrange them. - Return one of the four strings exactly as written above. ### Constraints - `-10^4 <= a, b, c <= 10^4` ### Examples ```text Input: a = 2, b = 4, c = 6 Output: "AP" ``` The differences are 2 and 2, so it is an AP. The ratios are 2 and 1.5, so it is not a GP. ```text Input: a = 2, b = 6, c = 18 Output: "GP" ``` The ratios are 3 and 3, so it is a GP. The differences are 4 and 12, so it is not an AP. ```text Input: a = 0, b = 0, c = 5 Output: "Neither" ``` The differences are 0 and 5, so it is not an AP. No ratio turns 0 into 5, so it is not a GP either, even though `b * b == a * c` holds (both sides are 0).

Overview: Decide whether three integers in a fixed order form an arithmetic progression, a geometric progression, both or neither, and return the matching label. It checks how you handle sequences that contain zeros and sequences where every term is equal.

Sap logo
Sap
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given three integers a, b and c, taken in that order, decide whether they form an arithmetic progression (AP), a geometric progression (GP), both, or neither.

The three numbers form an AP when the difference between consecutive terms is constant, that is b - a == c - b.

They form a GP when each term is the previous term multiplied by the same ratio: there is a number r with b == a * r and c == b * r.

Return:

  • "AP" if they form an AP and not a GP.
  • "GP" if they form a GP and not an AP.
  • "AP and GP" if they form both.
  • "Neither" if they form neither.

Function Signature

def identify_progression(a: int, b: int, c: int) -> str:

Rules

  • The ratio r can be any real number, including a negative number, a fraction or 0. For example, 4, 2, 1 (ratio 0.5) and -2, 4, -8 (ratio -2) are GPs, and 5, 0, 0 is a GP with ratio 0.
  • When a is 0, the numbers form a GP only if b and c are also 0. So 0, 0, 0 is a GP and 0, 0, 5 is not.
  • The order of the three numbers is fixed. Do not rearrange them.
  • Return one of the four strings exactly as written above.

Constraints

  • -10^4 <= a, b, c <= 10^4

Examples

Input: a = 2, b = 4, c = 6
Output: "AP"

The differences are 2 and 2, so it is an AP. The ratios are 2 and 1.5, so it is not a GP.

Input: a = 2, b = 6, c = 18
Output: "GP"

The ratios are 3 and 3, so it is a GP. The differences are 4 and 12, so it is not an AP.

Input: a = 0, b = 0, c = 5
Output: "Neither"

The differences are 0 and 5, so it is not an AP. No ratio turns 0 into 5, so it is not a GP either, even though b * b == a * c holds (both sides are 0).