Question

In: Computer Science

C programming problem < purpose: need to find time and space complexities in this problem and...

C programming problem < purpose: need to find time and space complexities in this problem and which function(algorithm) will be used in this problem>

I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm.
There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order.
A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers.

when it is 4 times 4 matrix, and integer M is 19

2 29 37 60
9 11 33 87
19 35 17 5
22 15 91 7

the answer will be (3,1)

I need to find an efficient way to find this position

Solutions

Expert Solution

Time Complexity = O(N)
Space Complexity = O(1)


Algorithm

1. Start from topmost right element
2) Compare the given key with the element in matrix if the value match the key then print and just return
3) If the element in matrix is greater than the key then move left i.e by decrementing index j
4) If the element in matrix is smaller than the key then move down i.e by incrementing index i
5) If we end up reaching a stage where the element in matrix is not found then just print not found

We do not iterate the complete matrix, At max we will be visiting N element based on element is greater than or less than the key


Hence Time Complexity = O(N) and since we dont use any extra space Space Complexity = O(1)

Working Code in C

#include <stdio.h>

#define ROW 4

#define COL 4

void FindInSortedMatrix(int arr[ROW][COL], int key) {

int i = 0, j = ROW - 1;

while (i < ROW && j >= 0) {

if (arr[i][j] == key) {

printf("(%d, %d)\n", i+1, j+1);

return;

}

if (arr[i][j] > key)

j--;

else

i++;

}

printf("Not found\n");

}

int main()

{

int input[ROW][COL] = {

{ 2,29,37,  60 },

{ 9,  11, 33, 87 },

{ 19, 35, 17, 5 },

{ 22, 15, 91, 7 },

};

FindInSortedMatrix(input, 19);

return 0;

}




SEE OUTPUT



Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
NEED IN C++ For the following programming problems, you need to time a section of code...
NEED IN C++ For the following programming problems, you need to time a section of code in C++. For example, the following statements time the execution of the function doSomething: #include clock_t start = clock(); doSomething(); clock_t finish = clock(); double overallTime = static_cast(finish - start)/ CLOCKS_PER_SEC; Consider the following two loops: //Loop A for(i = 1; i <= n; i++)    for(j = 1; j <= 10000; j++)     sum = sum + j; //Loop B for(i = 1;...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity of O(n^2)? Justify.
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
I need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT