Matrix Index Validation and Update
Last Updated :
04 Dec, 2023
Given an integer two-dimensional array (matrix), and a query for an index in the matrix with a value, the task is to check whether the index in the query is valid or out of bound. If the index is valid, update the value at that index with the value provided in the query.
Examples:
Input: matrix[3][4] = {{1, 0, 0, 0}, {0, 0, 5, 0}, {0 ,0 ,0 ,0}}, matrix[4][2] = 42
Output: Error: Row index out of range
Explanation: we are accessing the index in the matrix which is out of range.
Input: matrix[3][4] = {{1, 0, 0, 0}, {0, 0, 5, 0}, {0, 0, 0, 0}} matrix[1][2]=100
Output: {{1, 0, 0, 0}, {0, 0, 100,0}, {0, 0, 0, 0}}
Explanation: we are accessing the index in the matrix in the range and updating, so we are printing the matrix
Approach: To solve the problem follow the below idea:
- Cell (r,c) is in the matrix if the number of rows is greater than r and the number of columns is greater than c.
- If the cell is out of range of the matrix, it is impossible to update the cell in the matrix.
- If the cell is present in the matrix then update the cell and print the the matrix.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void update(vector<vector< int >> &matrix, int r, int c, int value)
{
if (matrix.size() <= r || matrix[0].size() <= c) {
cout << "Trying to update cell which is "
"out of range in the matrix\n" ;
return ;
}
else {
matrix[r] = value;
for ( int i = 0; i < matrix.size(); i++) {
for ( int j = 0; j < matrix[0].size(); j++) {
cout << matrix[i][j] << " " ;
}
cout << "\n" ;
}
}
}
int main()
{
vector<vector< int >> matrix = { { 1, 0, 0, 0 },
{ 0, 0, 5, 0 },
{ 0, 0, 0, 0 } };
int i = 2;
int j = 1;
int val = 100;
update(matrix, i, j, val);
return 0;
}
|
Java
import java.util.ArrayList;
public class Main {
public static void
update(ArrayList<ArrayList<Integer> > matrix, int r,
int c, int value)
{
if (matrix.size() <= r
|| matrix.get( 0 ).size() <= c) {
System.out.println(
"Trying to update cell which is out of range in the matrix" );
return ;
}
else {
matrix.get(r).set(c, value);
for ( int i = 0 ; i < matrix.size(); i++) {
for ( int j = 0 ; j < matrix.get( 0 ).size();
j++) {
System.out.print(matrix.get(i).get(j)
+ " " );
}
System.out.println();
}
}
}
public static void main(String[] args)
{
ArrayList<ArrayList<Integer> > matrix
= new ArrayList<>();
matrix.add( new ArrayList<Integer>() {
{
add( 1 );
add( 0 );
add( 0 );
add( 0 );
}
});
matrix.add( new ArrayList<Integer>() {
{
add( 0 );
add( 0 );
add( 5 );
add( 0 );
}
});
matrix.add( new ArrayList<Integer>() {
{
add( 0 );
add( 0 );
add( 0 );
add( 0 );
}
});
int i = 3 ;
int j = 2 ;
int val = 100 ;
update(matrix, i, j, val);
}
}
|
Python3
def update(matrix, r, c, value):
if len (matrix) < = r or len (matrix[ 0 ]) < = c:
print ( "Trying to update cell which is out of range in the matrix" )
return
else :
matrix[r] = value
for i in range ( len (matrix)):
for j in range ( len (matrix[ 0 ])):
print (matrix[i][j], end = " " )
print ()
if __name__ = = "__main__" :
matrix = [[ 1 , 0 , 0 , 0 ],
[ 0 , 0 , 5 , 0 ],
[ 0 , 0 , 0 , 0 ]]
i = 3
j = 2
val = 100
update(matrix, i, j, val)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void Update(List<List< int >> matrix, int r, int c, int value)
{
if (matrix.Count <= r || matrix[0].Count <= c)
{
Console.WriteLine( "Trying to update cell which is out of range in the matrix" );
return ;
}
else
{
matrix[r] = value;
for ( int i = 0; i < matrix.Count; i++)
{
for ( int j = 0; j < matrix[0].Count; j++)
{
Console.Write(matrix[i][j] + " " );
}
Console.WriteLine();
}
}
}
static void Main()
{
List<List< int >> matrix = new List<List< int >>
{
new List< int > { 1, 0, 0, 0 },
new List< int > { 0, 0, 5, 0 },
new List< int > { 0, 0, 0, 0 }
};
int i = 3;
int j = 2;
int val = 100;
Update(matrix, i, j, val);
}
}
|
Javascript
function update(matrix, r, c, value) {
if (matrix.length <= r || matrix[0].length <= c) {
console.log( "Trying to update cell which is out of range in the matrix" );
return ;
} else {
matrix[r] = value;
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
console.log(matrix[i][j] + " " );
}
console.log( "\n" );
}
}
}
function main() {
let matrix = [
[1, 0, 0, 0],
[0, 0, 5, 0],
[0, 0, 0, 0]
];
let i = 2;
let j = 1;
let val = 100;
update(matrix, i, j, val);
}
main();
|
Output
Trying to update cell which is out of range in the matrix
Time Complexity: O(n*m), where n and m are rows and columns.
Auxillary space: O(1)
Share your thoughts in the comments
Please Login to comment...