TheAlgorithms/Java · error · IllegalArgumentException
k must be between 1 and the size of the array
Error message
k must be between 1 and the size of the array
What it means
Thrown by FindKthNumber.findKthMax(int[] array, int k) when k is less than 1 or greater than the array length. The method finds the k-th largest element using the QuickSelect algorithm, so k must map to a valid 1-based ranking position. The check runs before any partitioning begins.
Source
Thrown at src/main/java/com/thealgorithms/maths/FindKthNumber.java:18
package com.thealgorithms.maths;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Random;
/**
* Use a quicksort-based approach to identify the k-th largest or k-th max element within the provided array.
*/
public final class FindKthNumber {
private FindKthNumber() {
}
private static final Random RANDOM = new Random();
public static int findKthMax(int[] array, int k) {
if (k <= 0 || k > array.length) {
throw new IllegalArgumentException("k must be between 1 and the size of the array");
}
// Convert k-th largest to index for QuickSelect
return quickSelect(array, 0, array.length - 1, array.length - k);
}
private static int quickSelect(int[] array, int left, int right, int kSmallest) {
if (left == right) {
return array[left];
}
// Randomly select a pivot index
int pivotIndex = left + RANDOM.nextInt(right - left + 1);
pivotIndex = partition(array, left, right, pivotIndex);
if (kSmallest == pivotIndex) {
return array[kSmallest];
} else if (kSmallest < pivotIndex) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate k against the array bounds before calling: ensure 1 <= k <= array.length.
- If k comes from user input or external data, clamp or reject it upstream before reaching this method.
- Guard against empty arrays before invoking: check array.length > 0 first.
Example fix
// before
int result = FindKthNumber.findKthMax(arr, userK);
// after
if (arr.length == 0 || userK < 1 || userK > arr.length) {
throw new IllegalArgumentException("Invalid k=" + userK + " for array of length " + arr.length);
}
int result = FindKthNumber.findKthMax(arr, userK); Defensive patterns
Strategy: validation
Validate before calling
if (array == null || array.length == 0 || k < 1 || k > array.length) {
throw new IllegalArgumentException("k must be between 1 and " + (array == null ? 0 : array.length));
}
int result = FindKthNumber.findKthMax(array, k); Type guard
static boolean isValidK(int[] array, int k) {
return array != null && array.length > 0 && k >= 1 && k <= array.length;
} Try / catch
try {
int result = FindKthNumber.findKthMax(array, k);
} catch (IllegalArgumentException e) {
// handle invalid k or empty array
} Prevention
- Always check 1 <= k <= array.length before calling.
- Handle empty arrays explicitly before any k-based selection.
- Clamp k from user input to valid bounds.
When it happens
Trigger: Calling findKthMax with k <= 0 (e.g., k=0 or k=-1), or calling with k > array.length (e.g., findKthMax(new int[]{3}, 2)). Passing k on an empty array also triggers it because array.length is 0, so any k >= 1 fails the k > array.length test.
Common situations: Using a user-supplied or computed k value without clamping it to the array bounds. Off-by-one errors where 0-based indices are passed to a 1-based k parameter. Passing an empty array from upstream filtering that removed all elements.
Related errors
- Array must be non-empty.
- Array must be non-empty.
- Array must be non-empty.
- array must be non-empty.
- Input array must have length of at least two
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/b4ffe8865f82096f.
Report an issue: GitHub.