Start learning free

LESSON 118 OF 291 · ARRAYS - CHALLENGES

Middle Element in Scala: Integer Division and Indexing

Practice Read the full explanation

Given an ArrayBuffer of integers nums with an odd number of items, return the item in the middle.

The middle index is the length of nums divided by 2, rounded down. For example, 5 items have the indexes 0 to 4, and 5 divided by 2, rounded down, is 2: two items come before index 2 and two come after it.

 

Example 1:

Input: nums = [1, 2, 3]
Output: 2
Explanation: The length is 3, and 3 divided by 2, rounded down, is 1. The item at index 1 is 2.

Example 2:

Input: nums = [5, 9, 1, 8, 4]
Output: 1
Explanation: The middle index is 2, and the item there is 1. The middle is about the position, not the size of the number.

Example 3:

Input: nums = [7]
Output: 7
Explanation: 1 divided by 2, rounded down, is 0: the only item is the middle one.

 

Constraints:

  • nums has an odd number of items: 1, 3, 5 and so on.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 2
    Ctrl + Enter to check

    SCALA · ARRAYS - CHALLENGES

    Middle Element in Scala, explained

    Problem overview

    The method middleElement receives an ArrayBuffer[Int] named nums with an odd number of items and returns the item in the middle. For [1, 2, 3] the answer is 2. For [5, 9, 1, 8, 4] it is 1: the middle is about the position, not the size of the number. For [7] it is 7.

    Approach

    The middle index is the number of items divided by 2, rounded down. With 5 items the indexes run from 0 to 4, and 5 divided by 2, rounded down, is 2: two items come before index 2 and two come after it. With 1 item the middle index is 0, and the only item is the middle one.

    Scala makes the rounding easy. Dividing one Int by another with / keeps only the whole part, so 5 / 2 is 2, not 2.5. The size of an ArrayBuffer is an Int too, so nums.size / 2 is already the middle index.

    Step by step

    1. Find the middle index. Store nums.size / 2 in a val named middle.
    2. Return the item at that index with nums(middle).

    The solution in Scala

    class Solution {
        def middleElement(nums: ArrayBuffer[Int]): Int = {
            val middle = nums.size / 2
            return nums(middle)
        }
    }
    

    A few Scala details:

    • Integer division truncates. For the non-negative sizes used here, that is the same as rounding down.
    • An ArrayBuffer reads an item with parentheses, nums(middle), like an Array does. Its length is nums.size, and nums.length gives the same number.
    • Splitting the work into two lines, with the index in a named val, makes the code easy to read and to check while you debug. nums(nums.size / 2) in one line is equivalent.
    • The type ArrayBuffer lives in scala.collection.mutable. In a file of your own, add import scala.collection.mutable.ArrayBuffer to use it.

    Tracing an example

    For [5, 9, 1, 8, 4], nums.size is 5 and middle is 5 / 2 = 2. The item at index 2 is 1, which is the result. For [1, 2, 3], middle is 1 and the item at index 1 is 2. For [7], middle is 0, and nums(0) is 7.

    The same formula picks the position for every odd size: 7 items give index 3, and 9 items give index 4. In each case there are as many items before the middle as after it.

    Edge cases and common mistakes

    • Returning the middle index. The method returns the item at that index, not the index itself. In [5, 9, 1, 8, 4] the index is 2 and the answer is 1.
    • Using nums.size / 2 - 1. That is one place too far left. With 3 items it gives index 0, which is the first item rather than the middle one.
    • Rounding up. Division of Int values rounds down. The lesson's rule is the same.
    • Even sizes. The lesson only gives odd sizes. For an even size such as 4 items, nums.size / 2 is 2, the upper of the two middle items, in a buffer with no single middle.
    • An empty buffer. Index 0 doesn't exist, so nums(0) would throw an exception. The lesson never passes one.

    Testing

    • [1, 2, 3] gives 2.
    • [5, 9, 1, 8, 4] gives 1.
    • [7] gives 7.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free