C# program to find K'th smallest element in a 2D array

Finding the K'th smallest element in a 2D array requires flattening the array and sorting it to locate the element at the K'th position. This is a common programming problem that demonstrates array manipulation and sorting techniques.

Approach

The approach involves three main steps −

  • Flatten the 2D array into a 1D array

  • Sort the flattened array in ascending order

  • Access the element at index k-1 (since arrays are zero-indexed)

Finding K'th Smallest Element 2D Array 65 45 32 97 23 75 Flattened 65 45 32 97 23 75 Sorted 23 32 45 59 65 75 5th smallest array[4] = 65

Using Array Flattening and Sorting

Example

using System;
using System.Linq;

class Program {
    static void Main(string[] args) {
        int[,] array2D = new int[,] {
            {65, 45, 32},
            {97, 23, 75}
        };
        
        int k = 5;
        
        // Flatten the 2D array
        int[] flattened = new int[array2D.GetLength(0) * array2D.GetLength(1)];
        int index = 0;
        
        for (int i = 0; i < array2D.GetLength(0); i++) {
            for (int j = 0; j < array2D.GetLength(1); j++) {
                flattened[index++] = array2D[i, j];
            }
        }
        
        // Sort the flattened array
        Array.Sort(flattened);
        
        Console.WriteLine("2D Array:");
        for (int i = 0; i < array2D.GetLength(0); i++) {
            for (int j = 0; j < array2D.GetLength(1); j++) {
                Console.Write(array2D[i, j] + " ");
            }
            Console.WriteLine();
        }
        
        Console.WriteLine("\nSorted flattened array:");
        for (int i = 0; i < flattened.Length; i++) {
            Console.Write(flattened[i] + " ");
        }
        
        Console.WriteLine($"\nThe {k}th smallest element = {flattened[k - 1]}");
    }
}

The output of the above code is −

2D Array:
65 45 32 
97 23 75 

Sorted flattened array:
23 32 45 65 75 97 

The 5th smallest element = 75

Using LINQ for Simplified Approach

Example

using System;
using System.Linq;

class Program {
    static void Main(string[] args) {
        int[,] array2D = new int[,] {
            {15, 8, 92},
            {34, 7, 61},
            {28, 43, 5}
        };
        
        int k = 3;
        
        // Using LINQ to flatten and sort
        var flattened = array2D.Cast<int>().OrderBy(x => x).ToArray();
        
        Console.WriteLine("Original 2D Array:");
        for (int i = 0; i < array2D.GetLength(0); i++) {
            for (int j = 0; j < array2D.GetLength(1); j++) {
                Console.Write($"{array2D[i, j],3} ");
            }
            Console.WriteLine();
        }
        
        Console.WriteLine($"\nSorted elements: {string.Join(", ", flattened)}");
        Console.WriteLine($"The {k}th smallest element = {flattened[k - 1]}");
    }
}

The output of the above code is −

Original 2D Array:
 15   8  92 
 34   7  61 
 28  43   5 

Sorted elements: 5, 7, 8, 15, 28, 34, 43, 61, 92
The 3rd smallest element = 8

Using Custom Method

Example

using System;

class Program {
    static int FindKthSmallest(int[,] array, int k) {
        int rows = array.GetLength(0);
        int cols = array.GetLength(1);
        int[] temp = new int[rows * cols];
        
        // Flatten the 2D array
        int index = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                temp[index++] = array[i, j];
            }
        }
        
        // Sort and return kth element
        Array.Sort(temp);
        return temp[k - 1];
    }
    
    static void Main(string[] args) {
        int[,] matrix = new int[,] {
            {50, 20, 80},
            {30, 10, 70}
        };
        
        Console.WriteLine("Matrix:");
        for (int i = 0; i < matrix.GetLength(0); i++) {
            for (int j = 0; j < matrix.GetLength(1); j++) {
                Console.Write($"{matrix[i, j],3} ");
            }
            Console.WriteLine();
        }
        
        for (int k = 1; k <= 6; k++) {
            int result = FindKthSmallest(matrix, k);
            Console.WriteLine($"{k}th smallest element: {result}");
        }
    }
}

The output of the above code is −

Matrix:
 50  20  80 
 30  10  70 

1th smallest element: 10
2th smallest element: 20
3th smallest element: 30
4th smallest element: 50
5th smallest element: 70
6th smallest element: 80

Conclusion

Finding the K'th smallest element in a 2D array involves flattening the array into a 1D structure and sorting it. The element at index k-1 gives the desired result. LINQ provides a concise alternative using Cast<int>().OrderBy() for simpler implementations.

Updated on: 2026-03-17T07:04:35+05:30

647 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements