Hone logo
Hone
Problems

Apartment Numbering Scheme

Imagine you're developing a system for a new apartment complex. The building has a unique numbering scheme where apartments are grouped by floor. Each floor houses a certain number of apartments. Given an apartment number, you need to determine which floor it belongs to. This is a common problem in data management and user interface design where you might need to display or organize information based on hierarchical groupings.

Problem Description

Your task is to write a Javascript function that takes two arguments: apartmentNumber (an integer representing the apartment's unique number) and apartmentsPerFloor (an integer representing how many apartments are on each floor). The function should return the floor number (an integer) where the given apartment is located.

The building's floor numbering starts from 1. The first apartmentsPerFloor apartments are on floor 1, the next apartmentsPerFloor are on floor 2, and so on.

Key Requirements:

  • The function must accept two positive integer arguments: apartmentNumber and apartmentsPerFloor.
  • The function should return a positive integer representing the floor number.
  • Assume that apartmentNumber will always be a positive integer.
  • Assume that apartmentsPerFloor will always be a positive integer and greater than or equal to 1.

Expected Behavior:

  • For any valid apartmentNumber and apartmentsPerFloor, the function should correctly identify and return the floor.

Edge Cases to Consider:

  • The first apartment on a floor (e.g., apartment 1, apartment apartmentsPerFloor + 1).
  • The last apartment on a floor (e.g., apartment apartmentsPerFloor, apartment 2 * apartmentsPerFloor).

Examples

Example 1:

Input: apartmentNumber = 7, apartmentsPerFloor = 3
Output: 3
Explanation:
Apartment 1, 2, 3 are on floor 1.
Apartment 4, 5, 6 are on floor 2.
Apartment 7, 8, 9 are on floor 3.
Since apartment 7 falls within the range of apartments for floor 3, the output is 3.

Example 2:

Input: apartmentNumber = 10, apartmentsPerFloor = 10
Output: 1
Explanation:
Apartments 1 through 10 are on floor 1.
Since apartment 10 is the last apartment on floor 1, the output is 1.

Example 3:

Input: apartmentNumber = 21, apartmentsPerFloor = 5
Output: 5
Explanation:
Floor 1: 1-5
Floor 2: 6-10
Floor 3: 11-15
Floor 4: 16-20
Floor 5: 21-25
Apartment 21 falls into the range for floor 5.

Constraints

  • 1 <= apartmentNumber <= 1000000
  • 1 <= apartmentsPerFloor <= 1000
  • The function should complete its execution efficiently, handling large inputs within reasonable time limits.

Notes

Consider how you can use mathematical operations to directly calculate the floor number without needing to iterate. Think about division and remainders. Remember that floor numbering starts from 1.

Loading editor...
javascript