Skip to content

Commit aad77be

Browse files
authored
Merge pull request #21 from vim12345/main
Add Some New Problem
2 parents e5ab01c + 7f1328a commit aad77be

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Question - Replace diagonal elements in each row of given Matrix by Kth smallest element of that row
2+
3+
---------------------------------------------------------------------------------------------------------------
4+
5+
Approach: The solution is based on the concept of sorting. Follow the steps mentioned below:
6+
7+
Traverse the matrix row-wise.
8+
Copy this row in another list.
9+
Sort the list and get the Kth smallest element and replace the diagonal element with that.
10+
11+
Below is the implementation of the above approach.
12+
13+
----------------------------------------------------------------------------------------------------------------
14+
Given a matrix mat[ ][ ] of size N*N and an integer K, containing integer values, the task is to replace diagonal elements by the Kth smallest element of row.
15+
16+
Examples:
17+
18+
Input: mat[][]= {{1, 2, 3, 4}
19+
{4, 2, 7, 6}
20+
{3, 5, 1, 9}
21+
{2, 4, 6, 8}}
22+
K = 2
23+
Output: 2, 2, 3, 4
24+
4, 4, 7, 6
25+
3, 5, 3, 8
26+
2, 4, 6, 4
27+
Explanation: 2nd smallest element of 1st row = 2
28+
2nd smallest element of 2nd row is 4
29+
2nd smallest element of 3rd row is 3
30+
2nd smallest element of 4th row is 4
31+
32+
Input: mat[][] = {{1, 2, 3}
33+
{7, 9, 8}
34+
{2, 3, 6}}
35+
K = 2
36+
Output: 2, 2, 3
37+
7, 8, 8
38+
2, 3, 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// JavaScript code for the above approach
2+
let N = 4;
3+
4+
// Function to print Matrix
5+
function showMatrix(mat) {
6+
let i, j;
7+
for (i = 0; i < N; i++) {
8+
for (j = 0; j < N; j++) {
9+
document.write(mat[i][j] + " ");
10+
}
11+
document.write('<br>')
12+
}
13+
}
14+
15+
// Function to return k'th smallest element
16+
// in a given array
17+
function kthSmallest(arr, n, K)
18+
{
19+
20+
// Sort the given array
21+
arr.sort(function (a, b) { return a - b })
22+
23+
// Return k'th element
24+
// in the sorted array
25+
return arr[K - 1];
26+
}
27+
28+
// Function to replace diagonal elements
29+
// with Kth min element of row.
30+
function ReplaceDiagonal(mat, K)
31+
{
32+
let i, j;
33+
let arr = new Array(N);
34+
35+
for (i = 0; i < N; i++) {
36+
for (j = 0; j < N; j++)
37+
arr[j] = mat[i][j];
38+
mat[i][i] = kthSmallest(arr, N, K);
39+
}
40+
showMatrix(mat);
41+
}
42+
43+
// Utility Main function.
44+
let mat = [[1, 2, 3, 4],
45+
[4, 2, 7, 6],
46+
[3, 5, 1, 9],
47+
[2, 4, 6, 8]];
48+
49+
let K = 3;
50+
ReplaceDiagonal(mat, K);
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Given a matrix, the task is to find the maximum element of each row.
2+
Examples:
3+
4+
Input : [1, 2, 3]
5+
[1, 4, 9]
6+
[76, 34, 21]
7+
8+
Output :
9+
3
10+
9
11+
76
12+
13+
Input : [1, 2, 3, 21]
14+
[12, 1, 65, 9]
15+
[1, 56, 34, 2]
16+
Output :
17+
21
18+
65
19+
56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
function maxelement(no_of_rows, arr)
3+
{
4+
var i = 0;
5+
6+
// Initialize max to 0 at beginning
7+
// of finding max element of each row
8+
var max = 0;
9+
var result = Array.from({length: no_of_rows}, (_, i) => 0);
10+
while (i < no_of_rows)
11+
{
12+
for (var j = 0; j < arr[i].length; j++)
13+
{
14+
if (arr[i][j] > max)
15+
{
16+
max = arr[i][j];
17+
}
18+
}
19+
result[i] = max;
20+
max = 0;
21+
i++;
22+
23+
}
24+
printArray(result);
25+
26+
}
27+
28+
// Print array element
29+
function printArray(result)
30+
{
31+
for (var i = 0; i < result.length; i++)
32+
{
33+
document.write(result[i]+"<br>");
34+
}
35+
36+
}
37+
38+
// Driver code
39+
var arr = [[3, 4, 1, 8],
40+
[ 1, 4, 9, 11],
41+
[ 76, 34, 21, 1],
42+
[ 2, 1, 4, 5] ];
43+
44+
// Calling the function
45+
maxelement(4, arr);
46+

0 commit comments

Comments
 (0)