Tuesday, February 21, 2023
HomeSoftware DevelopmentType the Matrix primarily based on the given column quantity

Type the Matrix primarily based on the given column quantity


Given a Matrix of measurement M * N (0-indexed) and an integer Okay, the matrix accommodates distinct integers solely, the duty is to kind the Matrix (i.e., the rows of the matrix) by their Values within the Okayth column, from Most to Minimal. Return the matrix after sorting it.

Examples:

Enter: Matrix = [[10, 6, 9, 1], [7, 5, 11, 2], [4, 8, 3, 15]], Okay = 2
Output: [[7, 5, 11, 2], [10, 6, 9, 1], [4, 8, 3, 15]]
Rationalization: Within the above Instance:

  • The index Row 1 has 11 in Column 2, which is the very best, so put it first place.
  • The index Row 0 has 9 in Column 2, which is the second highest, so put it in second place.
  • The index Row 2 has 3 in Column 2, which is the bottom, so put it in third place. 

Enter: Matrix = [[3, 4], [5, 6]], Okay = 0
Output: [[5, 6], [3, 4]]
Rationalization: Within the above instance:

  • The index Row 1 has 5 in Column 0, which is the very best, so put it first place.
  • The index Row 0 has 3 in Column 0, which is the bottom, so put it in final place.

Strategy: To resolve the issue comply with the beneath steps:

  • Create a vector that consists of a pair of components vector<pair<int, int> v. 
  •  Push the Okayth Column Components In that Vector Together with every row.
  • Type that Column through the use of the type operate.
  • Reverse the vector.
  • Now create a 2D vector and push every aspect from the vector to the corresponding row together with the identical column variety of the vector resultant.
  • Return new 2D vector.

Beneath is the implementation of the above method: 

C++

// C++ Program to kind the matrix primarily based
// on the column values.
#embody <bits/stdc++.h>
utilizing namespace std;
vector<vector<int> >
sortTheMatrix(vector<vector<int> >& matrix, int ok)
{

    // Rows
    int n = matrix.measurement();

    vector<pair<int, int> > v;
    for (int i = 0; i < n; i++) {

        // Kth col in ith row and
        // their ith row worth
        v.push_back({ matrix[i][k], i });
    }

    // Based mostly on growing method
    // of kth col
    kind(v.start(), v.finish());

    // Based mostly on lowering method
    // of kth col
    reverse(v.start(), v.finish());

    vector<vector<int> > a;
    for (auto& it : v) {
        a.push_back(

            // Now put every row one by
            // one into our ans in
            // decresing method(primarily based
            // on Kth col)
            matrix[it.second]);
    }
    return a;
}

// Print operate
void print(vector<vector<int> > matrix, int ok)
{
    vector<vector<int> > ans = sortTheMatrix(matrix, ok);
    cout << "Sorted Matrix Based mostly on column 2 Values :"
         << endl;
    int N = ans.measurement();
    int M = ans[0].measurement();
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {

            cout << ans[i][j] << " ";
        }
        cout << 'n';
    }
}

// Drivers code
int principal()
{
    vector<vector<int> > matrix = { { 10, 6, 9, 1 },
                                    { 7, 5, 11, 2 },
                                    { 4, 8, 3, 15 } };
    int Okay = 2;

    // Operate Name
    print(matrix, Okay);
    return 0;
}
Output

Sorted Matrix Based mostly on column 2 Values :
7 5 11 2 
10 6 9 1 
4 8 3 15 

Time Complexity: O(N*logN) i., e O(N) operating for loop and LogN for STL. 
Auxiliary Area: O(N)



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments