
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Lucky Numbers in a Matrix in JavaScript
Given a m * n matrix of distinct numbers, we have to return all lucky numbers in the 2-D array (matrix) in any order.
A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.
For example − If the input array is −
const arr = [ [3,7,8], [9,11,13], [15,16,17] ];
Then the output should be −
const output = [15];
because 15 is the only lucky number since it is the minimum in its row and the maximum in its column.
Example
The code for this will be −
const arr = [ [3,7,8], [9,11,13], [15,16,17] ]; const luckyNumbers = (arr, res = []) => { let M = arr.length, N = arr[0].length; let min = Array(M).fill( Infinity); let max = Array(N).fill(-Infinity); for (let i = 0; i < M; ++i) for (let j = 0; j < N; ++j) min[i] = Math.min(min[i], arr[i][j]), max[j] = Math.max(max[j], arr[i][j]); for (let i = 0; i < M; ++i) for (let j = 0; j < N; ++j) if (min[i] == max[j]) res.push(arr[i][j]); return res; }; console.log(luckyNumbers(arr));
Output
And the output in the console will be −
[15]
- Related Articles
- Finding Lucky Numbers in a Matrix in JavaScript
- Lucky Numbers
- Can we distinguish numbers as lucky and unlucky numbers?
- JavaScript Program for Frequencies of even and odd numbers in a matrix
- C++ code to count number of lucky numbers with k digits
- Finding words in a matrix in JavaScript
- Traversing Diagonally in a matrix in JavaScript
- JavaScript Update specific index in a boolean matrix?
- JavaScript Program to Form coils in a matrix
- Square matrix rotation in JavaScript
- Rotating a 2-D (transposing a matrix) in JavaScript
- Take in two 2-D arrays of numbers and returns their matrix multiplication result- JavaScript
- Why are the numbers 9, 99, 999, 9999 considered 'Lucky Numbers' by the Chinese and many others?
- Prime numbers in a range - JavaScript
- Print numbers in matrix diagonal pattern in C Program.

Advertisements