Start learning free

LESSON 117 OF 289 · ARRAYS - CHALLENGES

Middle Element in C#: Integer Division and List Index

Practice Read the full explanation

Given a List 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

    C# · ARRAYS - CHALLENGES

    Middle Element in C#, explained

    Problem overview

    The method MiddleElement receives a List<int> named nums with an odd number of items and returns the one in the middle. For [1, 2, 3] the answer is 2. For [5, 9, 1, 8, 4] it is 1: the middle is about position, not about size, so the biggest or smallest number is not involved. For [7] the answer is 7, because the only item is the middle one.

    Approach: compute the middle index

    A list with an odd number of items has one item that has as many items before it as after it. With 5 items, the indexes are 0 to 4, and index 2 has two items on each side. The index is the count divided by 2, rounded down: 5 / 2 is 2. With 3 items it is 1, and with 1 item it is 0.

    In C#, dividing two int values with / already rounds toward zero and drops the fraction, so no extra step is needed:

    Console.WriteLine(5 / 2);
    Console.WriteLine(3 / 2);
    Console.WriteLine(1 / 2);
    Console.WriteLine(5 / 2.0);
    2
    1
    0
    2.5

    The last line shows the trap: if one side of the division is a double, the result keeps the fraction. A double cannot be used as a list index, so the compiler would reject it.

    Step by step

    1. Find the middle index. A list has a Count, the number of items it holds. Store nums.Count / 2 in an int named middle. For [5, 9, 1, 8, 4], Count is 5 and middle is 2.
    2. Return that item. nums[middle] is the item at index 2, which is 1.

    The solution in C#

    class Solution {
        public int MiddleElement(List<int> nums) {
            int middle = nums.Count / 2;
            return nums[middle];
        }
    }

    A List<int> uses Count where an array uses Length, and both are read with the same square-bracket indexing. The method only reads the list; it does not change it.

    Edge cases and common mistakes

    • One item. 1 / 2 is 0, so middle is 0 and the single item is returned.
    • Using nums.Length. It does not compile on a list: use Count.
    • Dividing by a double. nums.Count / 2.0 gives 2.5 for five items, and a double cannot be an index: the compiler reports that it cannot convert it to int.
    • Even-length lists. The lesson only gives odd counts. For an even count there is no single middle item: with [1, 2, 3, 4] the same code returns 3, the second of the two central items.
    • Off-by-one. Subtracting 1 first, as in (nums.Count - 1) / 2, happens to give the same index for odd counts, but Count / 2 is the form the lesson's description uses.

    Why does the index equal half the count? Count the items on each side. With Count items, an item at index i has i items before it and Count - 1 - i items after it. Setting those equal gives i = (Count - 1) / 2, and for an odd count that is the same number as Count / 2 with the fraction dropped. The solution relies on this: no loop is needed, because one division finds the position, and a list gives direct access to any index.

    Testing

    numsMiddle indexResult
    [1, 2, 3]12
    [5, 9, 1, 8, 4]21
    [7]07
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free