Convert given upper triangular Matrix to 1D Array
Given an upper triangular matrix M[][] of dimensions N * N, the task is to convert it into an one-dimensional array storing only non-zero elements from the matrix.
Examples:
Input: M[][] = {{1, 2, 3, 4}, {0, 5, 6, 7}, {0, 0, 8, 9}, {0, 0, 0, 10}}
Output: Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Column-wise: {1, 2, 5, 3, 6, 8, 4, 7, 9, 10}
Explanation: All the non-zero elements of the matrix are {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Input: M[][] = {{1, 2, 3, }, {0, 4, 5}, {0, 0, 6}}
Output: Row-wise: {1, 2, 3, 4, 5, 6}
Column-wise: {1, 2, 4, 3, 5, 6}
Explanation: All the non-zero elements of the matrix are {1, 2, 3, 4, 5, 6}
Approach: To convert given 2-dimensional matrix to a 1-dimensional array, following two methods are used:
Row – Major Order:
- In this method, elements are stored such that consecutive elements of a row are placed consecutively in the array.
ROW-MAJOR ORDER
- The following formula is used to find the correct position of non-zero matrix elements in the array:
Element present at index (i, j) in the matrix is placed at [N * (i – 1) – (i – 2) * (i -1) /2] + (j – i)
where 1 ? i, j ? N and i ? j
Column-Major Order:
- In this method, elements are stored such that consecutive elements of a column are placed consecutively in the array.
COLUMN-MAJOR ORDER
- The following formula is used to find out the correct position of non-zero matrix elements:
Element present at index (i, j) in the matrix is placed at [j * (j – 1) / 2] + i – 1
where 1 ? i, j ? N and i ? j.
Follow the steps below to solve the problem:
- Initialize an array A[] to store non-zero matrix elements.
- Traverse the matrix M[][].
- Find the correct indices of non-zero matrix elements in the array A[] using the above formulas.
- Place the non-zero elements at the correct indices of A[] accordingly.
- Finally, print the array A[] obtained.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class UTMatrix {
private :
int n;
int * A;
int tot;
public :
UTMatrix( int N)
{
this ->n = N;
tot = N * (N + 1) / 2;
A = new int [N * (N + 1) / 2];
}
~UTMatrix() { delete [] A; }
void Display( bool row = true );
void setRowMajor( int i, int j, int x);
void setColMajor( int i, int j, int x);
int getN() { return n; }
};
void UTMatrix::setColMajor( int i, int j, int x)
{
if (i <= j) {
int index = ((j * (j - 1)) / 2) + i - 1;
A[index] = x;
}
}
void UTMatrix::setRowMajor( int i, int j, int x)
{
if (i <= j) {
int index
= (n * (i - 1) - (((i - 2) * (i - 1)) / 2))
+ (j - i);
A[index] = x;
}
}
void UTMatrix::Display( bool row)
{
for ( int i = 0; i < tot; i++) {
cout << A[i] << " " ;
}
cout << endl;
}
void displayRowMajor( int N)
{
UTMatrix rm(N);
rm.setRowMajor(1, 1, 1);
rm.setRowMajor(1, 2, 2);
rm.setRowMajor(1, 3, 3);
rm.setRowMajor(1, 4, 4);
rm.setRowMajor(2, 2, 5);
rm.setRowMajor(2, 3, 6);
rm.setRowMajor(2, 4, 7);
rm.setRowMajor(3, 3, 8);
rm.setRowMajor(3, 4, 9);
rm.setRowMajor(4, 4, 10);
cout << "Row-Wise: " ;
rm.Display();
}
void displayColMajor( int N)
{
UTMatrix cm(N);
cm.setColMajor(1, 1, 1);
cm.setColMajor(1, 2, 2);
cm.setColMajor(1, 3, 3);
cm.setColMajor(1, 4, 4);
cm.setColMajor(2, 2, 5);
cm.setColMajor(2, 3, 6);
cm.setColMajor(2, 4, 7);
cm.setColMajor(3, 3, 8);
cm.setColMajor(3, 4, 9);
cm.setColMajor(4, 4, 10);
cout << "Column-wise: " ;
cm.Display( false );
}
int main()
{
int N = 4;
displayRowMajor(N);
displayColMajor(N);
return 0;
}
|
Java
class UTMatrix{
private int n;
private int [] A = new int [n];
private int tot;
public UTMatrix( int N)
{
this .n = N;
tot = N * (N + 1 ) / 2 ;
A = new int [N * (N + 1 ) / 2 ];
}
void Display( boolean row)
{
for ( int i = 0 ; i < tot; i++)
{
System.out.print(A[i] + " " );
}
System.out.println();
}
void setRowMajor( int i, int j, int x)
{
if (i <= j)
{
int index = (n * (i - 1 ) - (((i - 2 ) *
(i - 1 )) / 2 )) + (j - i);
A[index] = x;
}
}
void setColMajor( int i, int j, int x)
{
if (i <= j) {
int index = ((j * (j - 1 )) / 2 ) + i - 1 ;
A[index] = x;
}
}
int getN()
{
return n;
}
}
class GFG{
static void displayRowMajor( int N)
{
UTMatrix rm = new UTMatrix(N);
rm.setRowMajor( 1 , 1 , 1 );
rm.setRowMajor( 1 , 2 , 2 );
rm.setRowMajor( 1 , 3 , 3 );
rm.setRowMajor( 1 , 4 , 4 );
rm.setRowMajor( 2 , 2 , 5 );
rm.setRowMajor( 2 , 3 , 6 );
rm.setRowMajor( 2 , 4 , 7 );
rm.setRowMajor( 3 , 3 , 8 );
rm.setRowMajor( 3 , 4 , 9 );
rm.setRowMajor( 4 , 4 , 10 );
System.out.print( "Row-Wise: " );
rm.Display( false );
}
static void displayColMajor( int N)
{
UTMatrix cm = new UTMatrix(N);
cm.setColMajor( 1 , 1 , 1 );
cm.setColMajor( 1 , 2 , 2 );
cm.setColMajor( 1 , 3 , 3 );
cm.setColMajor( 1 , 4 , 4 );
cm.setColMajor( 2 , 2 , 5 );
cm.setColMajor( 2 , 3 , 6 );
cm.setColMajor( 2 , 4 , 7 );
cm.setColMajor( 3 , 3 , 8 );
cm.setColMajor( 3 , 4 , 9 );
cm.setColMajor( 4 , 4 , 10 );
System.out.print( "Column-wise: " );
cm.Display( false );
}
public static void main(String[] args)
{
int N = 4 ;
displayRowMajor(N);
displayColMajor(N);
}
}
|
Python3
class UTMatrix :
def __init__( self , N):
self .n = N;
self .tot = int (N * (N + 1 ) / 2 );
self .A = [ 0 ] * ( int (N * (N + 1 ) / 2 ));
def Display( self , row) :
print ( * self .A[: int ( self .tot)])
def setRowMajor( self , i, j, x):
if (i < = j) :
index = ( self .n * (i - 1 ) - (((i - 2 ) * (i - 1 )) / 2 )) + (j - i);
self .A[ int (index)] = x;
def setColMajor( self , i, j, x) :
if (i < = j) :
index = int ((j * (j - 1 )) / 2 ) + i - 1 ;
self .A[index] = x;
def getN( self ):
return n;
def displayRowMajor(N) :
rm = UTMatrix(N);
rm.setRowMajor( 1 , 1 , 1 );
rm.setRowMajor( 1 , 2 , 2 );
rm.setRowMajor( 1 , 3 , 3 );
rm.setRowMajor( 1 , 4 , 4 );
rm.setRowMajor( 2 , 2 , 5 );
rm.setRowMajor( 2 , 3 , 6 );
rm.setRowMajor( 2 , 4 , 7 );
rm.setRowMajor( 3 , 3 , 8 );
rm.setRowMajor( 3 , 4 , 9 );
rm.setRowMajor( 4 , 4 , 10 );
print ( "Row-Wise: " );
rm.Display( False );
def displayColMajor(N) :
cm = UTMatrix(N);
cm.setColMajor( 1 , 1 , 1 );
cm.setColMajor( 1 , 2 , 2 );
cm.setColMajor( 1 , 3 , 3 );
cm.setColMajor( 1 , 4 , 4 );
cm.setColMajor( 2 , 2 , 5 );
cm.setColMajor( 2 , 3 , 6 );
cm.setColMajor( 2 , 4 , 7 );
cm.setColMajor( 3 , 3 , 8 );
cm.setColMajor( 3 , 4 , 9 );
cm.setColMajor( 4 , 4 , 10 );
print ( "Column-wise: " );
cm.Display( False );
N = 4 ;
displayRowMajor(N);
displayColMajor(N);
|
C#
using System;
public class UTMatrix{
public int n;
public int [] A;
public int tot;
public UTMatrix( int N)
{
this .n = N;
tot = N * (N + 1) / 2;
A = new int [N * (N + 1) / 2];
}
public void Display( bool row)
{
for ( int i = 0; i < tot; i++)
{
Console.Write(A[i] + " " );
}
Console.WriteLine();
}
public void setRowMajor( int i, int j, int x)
{
if (i <= j)
{
int index = (n * (i - 1) - (((i - 2) *
(i - 1)) / 2)) + (j - i);
A[index] = x;
}
}
public void setColMajor( int i, int j, int x)
{
if (i <= j) {
int index = ((j * (j - 1)) / 2) + i - 1;
A[index] = x;
}
}
public int getN()
{
return n;
}
}
class GFG{
static void displayRowMajor( int N)
{
UTMatrix rm = new UTMatrix(N);
rm.setRowMajor(1, 1, 1);
rm.setRowMajor(1, 2, 2);
rm.setRowMajor(1, 3, 3);
rm.setRowMajor(1, 4, 4);
rm.setRowMajor(2, 2, 5);
rm.setRowMajor(2, 3, 6);
rm.setRowMajor(2, 4, 7);
rm.setRowMajor(3, 3, 8);
rm.setRowMajor(3, 4, 9);
rm.setRowMajor(4, 4, 10);
Console.Write( "Row-Wise: " );
rm.Display( false );
}
static void displayColMajor( int N)
{
UTMatrix cm = new UTMatrix(N);
cm.setColMajor(1, 1, 1);
cm.setColMajor(1, 2, 2);
cm.setColMajor(1, 3, 3);
cm.setColMajor(1, 4, 4);
cm.setColMajor(2, 2, 5);
cm.setColMajor(2, 3, 6);
cm.setColMajor(2, 4, 7);
cm.setColMajor(3, 3, 8);
cm.setColMajor(3, 4, 9);
cm.setColMajor(4, 4, 10);
Console.Write( "Column-wise: " );
cm.Display( false );
}
public static void Main( string [] args)
{
int N = 4;
displayRowMajor(N);
displayColMajor(N);
}
}
|
Javascript
<script>
class UTMatrix {
constructor(N) {
this .n = N;
this .tot = Math.floor(N * (N + 1) / 2);
this .A = new Array(Math.floor(N * (N + 1) / 2));
}
Display(row) {
for (let i = 0; i < this .tot; i++) {
document.write( this .A[i] + " " );
}
document.write( "<br>" );
}
setRowMajor(i, j, x) {
if (i <= j) {
let index = ( this .n * (i - 1) - (((i - 2) *
(i - 1)) / 2)) + (j - i);
this .A[index] = x;
}
}
setColMajor(i, j, x) {
if (i <= j) {
let index = Math.floor((j * (j - 1)) / 2) + i - 1;
this .A[index] = x;
}
}
getN() {
return n;
}
}
function displayRowMajor(N) {
let rm = new UTMatrix(N);
rm.setRowMajor(1, 1, 1);
rm.setRowMajor(1, 2, 2);
rm.setRowMajor(1, 3, 3);
rm.setRowMajor(1, 4, 4);
rm.setRowMajor(2, 2, 5);
rm.setRowMajor(2, 3, 6);
rm.setRowMajor(2, 4, 7);
rm.setRowMajor(3, 3, 8);
rm.setRowMajor(3, 4, 9);
rm.setRowMajor(4, 4, 10);
document.write( "Row-Wise: " );
rm.Display( false );
}
function displayColMajor(N) {
let cm = new UTMatrix(N);
cm.setColMajor(1, 1, 1);
cm.setColMajor(1, 2, 2);
cm.setColMajor(1, 3, 3);
cm.setColMajor(1, 4, 4);
cm.setColMajor(2, 2, 5);
cm.setColMajor(2, 3, 6);
cm.setColMajor(2, 4, 7);
cm.setColMajor(3, 3, 8);
cm.setColMajor(3, 4, 9);
cm.setColMajor(4, 4, 10);
document.write( "Column-wise: " );
cm.Display( false );
}
let N = 4;
displayRowMajor(N);
displayColMajor(N);
</script>
|
Output:
Row-Wise: 1 2 3 4 5 6 7 8 9 10
Column-wise: 1 2 5 3 6 8 4 7 9 10
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Last Updated :
21 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...