Finding Lucky Numbers in a Matrix in JavaScript

In the given problem statement we have to write a function to get the lucky numbers in a matrix with the help of Javascript. A lucky number is defined as an element that is the minimum value in its row and the maximum value in its column.

Understanding the Problem Statement

A lucky number in a matrix must satisfy two conditions:

  • It is the minimum element in its row
  • It is the maximum element in its column

For example, in the matrix [[3, 7], [9, 11]], the number 7 is the maximum in its column (7 > 9) but not the minimum in its row (3 Lucky Number Concept 3 7 9 11 Row min: 3 Row min: 9 Col max: 9 Col max: 11 No element satisfies both conditions

Algorithm

Step 1 ? Create a function named luckyNumbers that takes a matrix as parameter.

Step 2 ? For each row, find the minimum element and its column index.

Step 3 ? For that column index, check if the minimum element is also the maximum in its column.

Step 4 ? If both conditions are met, add it to the result array.

Step 5 ? Return the array of lucky numbers.

Implementation

function luckyNumbers(matrix) {
   const m = matrix.length;
   const n = matrix[0].length;
   const luckyNums = [];
    
   for (let i = 0; i < m; i++) {
      let minIndex = 0;
      
      // Find minimum element index in current row
      for (let j = 1; j < n; j++) {
         if (matrix[i][j] < matrix[i][minIndex]) {
            minIndex = j;
         }
      }
      
      // Check if this minimum is also maximum in its column
      let maxIndex = 0;
      for (let k = 1; k < m; k++) {
         if (matrix[k][minIndex] > matrix[maxIndex][minIndex]) {
            maxIndex = k;
         }
      }
      
      // If minimum in row is also maximum in column
      if (maxIndex === i) {
         luckyNums.push(matrix[i][minIndex]);
      }
   }
    
   return luckyNums;
}

// Example 1: Matrix with a lucky number
const matrix1 = [
   [3, 7, 8],
   [9, 11, 13],
   [15, 16, 17]
];
console.log("Matrix 1:", luckyNumbers(matrix1));

// Example 2: Matrix with no lucky numbers
const matrix2 = [
   [1, 10, 4, 2],
   [9, 3, 8, 7],
   [15, 16, 17, 12]
];
console.log("Matrix 2:", luckyNumbers(matrix2));

// Example 3: Matrix with lucky number
const matrix3 = [
   [7, 8],
   [1, 2]
];
console.log("Matrix 3:", luckyNumbers(matrix3));
Matrix 1: [15]
Matrix 2: []
Matrix 3: [7]

How It Works

Let's trace through matrix1 [[3,7,8], [9,11,13], [15,16,17]]:

  • Row 0: Min is 3 at index 0. Column 0 max is 15 (at row 2). 3 ? max, so not lucky.
  • Row 1: Min is 9 at index 0. Column 0 max is 15 (at row 2). 9 ? max, so not lucky.
  • Row 2: Min is 15 at index 0. Column 0 max is 15 (at row 2). 15 = max, so it's lucky!

Optimized Approach

We can optimize by pre-calculating row minimums and column maximums:

function luckyNumbersOptimized(matrix) {
   const m = matrix.length;
   const n = matrix[0].length;
   
   // Find minimum in each row
   const rowMins = matrix.map(row => Math.min(...row));
   
   // Find maximum in each column
   const colMaxs = [];
   for (let j = 0; j < n; j++) {
      let maxVal = matrix[0][j];
      for (let i = 1; i < m; i++) {
         maxVal = Math.max(maxVal, matrix[i][j]);
      }
      colMaxs[j] = maxVal;
   }
   
   // Find lucky numbers
   const luckyNums = [];
   for (let i = 0; i < m; i++) {
      for (let j = 0; j < n; j++) {
         if (matrix[i][j] === rowMins[i] && matrix[i][j] === colMaxs[j]) {
            luckyNums.push(matrix[i][j]);
         }
      }
   }
   
   return luckyNums;
}

const testMatrix = [
   [3, 7, 8],
   [9, 11, 13],
   [15, 16, 17]
];

console.log("Optimized result:", luckyNumbersOptimized(testMatrix));
Optimized result: [15]

Complexity Analysis

Approach Time Complexity Space Complexity
Basic Solution O(m × n) O(k) where k = lucky numbers
Optimized Solution O(m × n) O(m + n)

Conclusion

Lucky numbers in a matrix are rare elements that are simultaneously row minimums and column maximums. The algorithm efficiently identifies these by checking both conditions for each potential candidate, with O(m × n) time complexity.

Updated on: 2026-03-15T23:19:00+05:30

965 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements