📜  将给定矩阵的每一行中的对角元素替换为该行的第 K 个最小元素

📅  最后修改于: 2022-05-13 01:57:21.548000             🧑  作者: Mango

将给定矩阵的每一行中的对角元素替换为该行的第 K 个最小元素

给定一个大小为N*N的矩阵mat[ ][ ]和一个包含整数值的整数K ,任务是用行的第 K 个最小元素替换对角线元素。

例子:

方法:解决方案是基于排序的概念。请按照以下步骤操作:

  • 逐行遍历矩阵。
  • 将此行复制到另一个列表中。
  • 对列表进行排序并获得第 K 个最小的元素并用它替换对角线元素。

下面是上述方法的实现。

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
const int N = 4;
 
// Function to print Matrix
void showMatrix(int mat[][N])
{
    int i, j;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Function to return k'th smallest element
// in a given array
int kthSmallest(int arr[], int n, int K)
{
    // Sort the given array
    sort(arr, arr + n);
 
    // Return k'th element
    // in the sorted array
    return arr[K - 1];
}
 
// Function to replace diagonal elements
// with Kth min element of row.
void ReplaceDiagonal(int mat[][N], int K)
{
    int i, j;
    int arr[N];
 
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++)
            arr[j] = mat[i][j];
        mat[i][i] = kthSmallest(arr, N, K);
    }
    showMatrix(mat);
}
 
// Utility Main function.
int main()
{
    int mat[][N] = { { 1, 2, 3, 4 },
                     { 4, 2, 7, 6 },
                     { 3, 5, 1, 9 },
                     { 2, 4, 6, 8 } };
 
    int K = 3;
    ReplaceDiagonal(mat, K);
    return 0;
}


Java
// Java code to find the maximum median
// of a sub array having length at least K.
import java.util.*;
public class GFG
{
 
  static int N = 4;
 
  // Function to print Matrix
  static void showMatrix(int mat[][])
  {
    int i, j;
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        System.out.print(mat[i][j] + " ");
      }
      System.out.println();
    }
  }
 
  // Function to return k'th smallest element
  // in a given array
  static int kthSmallest(int arr[], int n, int K)
  {
    // Sort the given array
    Arrays.sort(arr);
 
    // Return k'th element
    // in the sorted array
    return arr[K - 1];
  }
 
  // Function to replace diagonal elements
  // with Kth min element of row.
  static void ReplaceDiagonal(int mat[][], int K)
  {
    int i, j;
    int arr[] = new int[N];
 
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++)
        arr[j] = mat[i][j];
      mat[i][i] = kthSmallest(arr, N, K);
    }
    showMatrix(mat);
  }
 
  // Driver code
  public static void main(String args[])
  {
    int mat[][] = { { 1, 2, 3, 4 },
                   { 4, 2, 7, 6 },
                   { 3, 5, 1, 9 },
                   { 2, 4, 6, 8 } };
 
    int K = 3;
    ReplaceDiagonal(mat, K);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code for the above approach
N = 4
 
# Function to print Matrix
def showMatrix(mat):
    i = None
    j = None
    for i in range(N):
        for j in range(N):
            print(mat[i][j], end= " ")
        print('')
     
# Function to return k'th smallest element
# in a given array
def kthSmallest(arr, n, K):
 
    # Sort the given array
    arr.sort()
 
    # Return k'th element
    # in the sorted array
    return arr[K - 1]
 
# Function to replace diagonal elements
# with Kth min element of row.
def ReplaceDiagonal(mat, K):
    i = None
    j = None
    arr = [0] * N
 
    for i in range(N):
        for j in range(N):
            arr[j] = mat[i][j]
        mat[i][i] = kthSmallest(arr, N, K)
    showMatrix(mat)
 
# Utility Main function.
mat = [[1, 2, 3, 4], [4, 2, 7, 6], [3, 5, 1, 9], [2, 4, 6, 8]]
 
K = 3
ReplaceDiagonal(mat, K)
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code to find the maximum median
// of a sub array having length at least K.
using System;
 
public class GFG {
 
  static int N = 4;
 
  // Function to print Matrix
  static void showMatrix(int[, ] mat)
  {
    int i, j;
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        Console.Write(mat[i, j] + " ");
      }
      Console.WriteLine();
    }
  }
 
  // Function to return k'th smallest element
  // in a given array
  static int kthSmallest(int[] arr, int n, int K)
  {
    // Sort the given array
    Array.Sort(arr);
 
    // Return k'th element
    // in the sorted array
    return arr[K - 1];
  }
 
  // Function to replace diagonal elements
  // with Kth min element of row.
  static void ReplaceDiagonal(int[, ] mat, int K)
  {
    int i, j;
    int[] arr = new int[N];
 
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++)
        arr[j] = mat[i, j];
      mat[i, i] = kthSmallest(arr, N, K);
    }
    showMatrix(mat);
  }
 
  // Driver code
  public static void Main()
  {
    int[, ] mat = { { 1, 2, 3, 4 },
                   { 4, 2, 7, 6 },
                   { 3, 5, 1, 9 },
                   { 2, 4, 6, 8 } };
 
    int K = 3;
    ReplaceDiagonal(mat, K);
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
3 2 3 4 
4 6 7 6 
3 5 5 9 
2 4 6 6 

时间复杂度: O(N 2 * logN)
辅助空间: O(N)