Find Maximum element in given range in 2D array for each query [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Given a 2D array, To find the element maximum for given sub rectangle corresponding to each query.
For example
for array of 3*4
1 2 3 7
13 6 34 7
12 5 7 8
for eg:
MAX in sub array between (0,0) and (1,1) is 13
MAX in sub array between (1,1) and (2,2) is 34

I believe you are looking something like this:
int row = 0, col = 0;
cout << "please enter row and column"<<endl;
cin >> row >> col;
int** p = (int**) new int[row*col];
for (int i = 0; i < row; ++i)
{
p[i] = new int[col];
for (int j = 0; j < col; ++j)
{
cout << "Enter the number at position p[" << i << "][" << j << "]" << endl;
int temp = 0;
cin >> temp;
p[i][j] = temp;
}
}
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
cout << p[i][j] << "\t";
cout << endl;
}
cout << "Now find the max number provided rectangle co-ordinate[a,b] to [x,y]"<<endl;
cout << "please specify the starting and ending co-ordinates"<<endl;
int a = 0, b = 0, c = 0, d = 0;
cin >> a >> b >> c >> d;
int max = p[a][b];
for (int i = a; i <= c; ++i)
{
for (int j = b; j <= d; ++j)
{
if (p[i][j] > max)
{
max = p[i][j];
}
}
}
cout << "max of the rectangle = " << max<<endl;

Related

Cannot find the error in this code that multiplies matricies

#include <iostream>
#include <vector>
using namespace std;
int main() {
int matrix1_rows;
int matrix1_columns;
int matrix2_columns;
vector<vector<int>> matrix1;
vector<vector<int>> matrix2;
vector<vector<int>> output_matrix;
int current_input;
cout << "Please specify the total row of your first matrix:" << endl;
cin >> matrix1_rows;
cout << "Please specify the total column of your first matrix:" << endl;
cin >> matrix1_columns;
for (int i = 0; i < matrix1_rows; i++) {
vector<int> row;
for (int j = 0; j < matrix1_columns; i++) {
cout << "Please specify the element of " << i << " row " << j << " col:" << endl;
cin >> current_input;
row.push_back(current_input);
}
matrix1.push_back(row);
}
cout << "Please specify the total column of your second matrix:" << endl;
cin >> matrix2_columns;
for (int i = 0; i < matrix1_columns; i++) {
vector<int> row;
for (int j = 0; j < matrix2_columns; j++) {
cout << "Please specify the element of " << i << " row " << j << " col:" << endl;
cin >> current_input;
row.push_back(current_input);
}
matrix2.push_back(row);
}
for (int i = 0; i < matrix1.size(); i++) {
vector<int> current_row;
for (int j = 0; j < matrix2[0].size(); j++) {
int current_entry = 0;
for (int k = 0; k < matrix1[0].size(); k++) {
current_entry += matrix1[i][k]*matrix2[k][j];
}
current_row.push_back(current_entry);
}
output_matrix.push_back(current_row);
}
cout << "The input matrix A is:" << endl;
for (int i = 0; i < matrix1.size(); i++) {
for (int j = 0; j < matrix1[i].size(); j++) {
cout << " " << matrix1[i][j];
}
cout << endl;
}
cout << "The input matrix B is:" << endl;
for (int i = 0; i < matrix2.size(); i++) {
for (int j = 0; j < matrix2[i].size(); j++) {
cout << " " << matrix2[i][j];
}
cout << endl;
}
cout << "The final matrix is:" << endl;
for (int i = 0; i < output_matrix.size(); i++) {
for (int j = 0; j < output_matrix.size(); j++) {
cout << " " << output_matrix[i][j];
}
cout << endl;
}
}
The output allows me to put an infinite number of values for a row and won't allow for input into the columns, I have checked many times but I must be missing something stupid.
I think it may be due to an issue with the current_entry or in my for loops a bit later on but I can't be sure. I have tried to adjust both of them but I haven't gotten anything to work
the output should look like this
Please specify the total row of your first matrix:
2
Please specify the total column of your first matrix:
2
Please specify the element of 0 row 0 col:
10
Please specify the element of 0 row 1 col:
-12
Please specify the element of 1 row 0 col:
55
Please specify the element of 1 row 1 col:
74
Please specify the total row of your second matrix:
2
Please specify the total column of your second matrix:
2
Please specify the element of 0 row 0 col:
-1
Please specify the element of 0 row 1 col:
0
Please specify the element of 1 row 0 col:
10
However, it looks like this:
Please specify the total row of your first matrix:
2
Please specify the total column of your first matrix:
2
Please specify the element of 0 row 0 col:
2
Please specify the element of 1 row 0 col:
12
Please specify the element of 2 row 0 col:
23
Please specify the element of 3 row 0 col:
23
Please specify the element of 4 row 0 col:
2
Please specify the element of 5 row 0 col:
24
Please specify the element of 6 row 0 col:
24
Please specify the element of 7 row 0 col:
As your output shows, you are not incrementing the right counter variable.
Instead of
for (int j = 0; j < matrix1_columns; i++) use
for (int j = 0; j < matrix1_columns; j++) in the inner loop.

find all common part between vector

I have n vectors with unique integers. I want to find all the common part between them. Is there any specific algorithm or data struct for this problem?
example:
std::vector<int> a = {0,1,2,3,4,5,6,7,8,9,10,11,12,13};
std::vector<int> b = {1,7,8,9,2,10,11,12};
std::vector<int> c = {4,9,8,7,0,1,2,3};
result:
ignore result with only one interge
7,8,9 between a and b
10,11,12 bewteen a and b
0,1,2,3 between a and c
if you want all common subarrays with a length greater than 1, then for each element from the first array iterate over all elements in the second array if you match two elements then go to the next element in the first and second array, and so on.
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr1[i] == arr2[j]) {
int ii = i, jj = j, cnt = 0;
std::vector<int> res;
res.push_back(arr1[ii]);
while (++ii < n and ++jj < m and arr1[ii] == arr2[jj])res.push_back(arr1[ii]);
if (res.size() > 1) {
for (auto x: res)std::cout << x << " ";
}
}
}
}
time complexity:O(n^3)
and this another way by LCS.
memset(dp, 0, sizeof dp);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = 0;
if (arr1[i] == arr2[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
std::cout << dp[i][j] << " ";
}
std::cout << "\n";
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (dp[i][j] > 1) {
for (int ii = i, jj = j, k = dp[i][j]; k; ii--, jj--, k--) {
std::cout << arr1[ii] << " ";
}
std::cout << "\n";
}
}
}
O(n^3)
It seems to me that you are looking for Longest Common Subsequence
These images are calculated by a diff like program which compares lines (unordered), like shortest edit distance
Blue lines : Deleted lines to come from left to right
Red lines : Changed lines
Green lines: Inserted lines
Lines without color are unchanged = longest common subsequence. Diff result looks pretty much the same as the given results.
Reference:
A Fast Algorithm for computing longest common subsequences
by James W. Hunt and Thomas G. Szymanski
from Communications of the ACM May 1977 Volume 20 no. 5

Queries on permutation 1...N

So we are given a permutation of the numers {1... N}.
We are given an integer k and then k queries of this type:
q(x,y,l,r) - count numbers between position X and Y in the permutation, which are >=l and <=r.
For example:
N - 7: (1 6 3 5 7 4 2)
q(1,4,2,7) -> 3 numbers ( 6, 3 and 5 , since 2<=6<=7 , 2<=3<=7 and 2<=5<=7)
So my attempt was to store the permutation and and position array (too have fast acces to the position of each number)
Then i check which interval is smaller [x,y] or [l,r] and iterate through the smaller.
The answers i get are correct, but i get 0 points, since my solution it's too slow.
Any tips how to make this queries as fast as possible for big N?
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int q;
cin >> q;
int* perm = new int[n+1];
int* pos = new int[n+1];
for (int i = 1; i <= n; i++)
{
int num;
cin >> num;
perm[i] = num;
pos[num] = i;
}
for (int i = 0; i < q; i++)
{
int x, y, l, r;
cin >> x >>y>> l>> r;
int count = 0;
if (y - x < r - l)
{
for (int i = x; i <= y; i++)
{
if (perm[i] >= l && perm[i] <= r)
count++;
}
cout << count << endl;
}
else
{
int count = 0;
for (int i = l; i <= r; i++)
{
if (pos[i] >= x && pos[i] <= y)
count++;
}
cout << count << endl;
}
}
}

Sorting almost ordered sequence [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a sequence, which can be really long.
I am inputing the number of elements and the special number, which will cut my sequence into the parts, for example:
10 2
here I have 10 numbers and 5 blocks (10/2)
I must sort this sequence using merge sort
here is the code:
int number_of_elements, k;
cin >> number_of_elements;
cin >> k;
int* massiv_1 = new int[k];
int* massiv_2 = new int[k];
int* resulted_massiv = new int[number_of_elements];
for(int i = 0; i < number_of_elements; i++) {
resulted_massiv[i] = 0;
}
int i = 0;
while( i < number_of_elements) {
int counter_1 = 0;
int counter_2 = 0;
cin >> massiv_1[counter_1];
counter_1++;
if( i != 0 ) {
quick_Sort( massiv_1, k-1 );
for(; counter_2 < k; counter_2++) {
cin >> massiv_2[counter_2];
counter_2++;
}
quick_Sort( massiv_2, k-1 );
merge(massiv_1, k, massiv_2, k, resulted_massiv, i);
}
counter_1 = 0;
counter_2 = 0;
i = i + k;
}
here is the merge sort
void merge(int *a, int a_len, int *b, int b_len, int *c, int z1) {
int i = 0, j = 0;
for(;i < a_len && j < b_len;) {
if(a[i] < b[j]) {
c[z1] = a[i];
++i;
} else {
c[z1] = b[j];
j++;
}
}
if(i == a_len) {
for(; j < b_len; ++j) {
c[z1] = b[j];
}
} else {
for(; i < a_len; ++i) {
c[z1] = a[i];
}
}
}
In other words here is the algorithm:
First I cut sequnce into k parts
I am going through it, if I see the index = k, I am using quick_sort,
then I go to the nearest block and also use quick_sort,
and then I am using merge sorting puting it into other array
...and this I am doing till the end of the sequence
this algorithm doesn't work, if enter this:
10 4
0 4 3 2 1 8 7 6 5 9
it must sort this sequence, but it shows this:
0 0 0 0 4 0 0 0 8 0
I can't get it
I will really appreciate your help, thank you in advance!!!
You always write in single c[z1] element in merge function, and never increment z1 index.
Index in "c" array should be incremented at every step, so you can use c[z1++] everywhere in the funcion

iterative algorithm for combination generation [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Algorithm to return all combinations of k elements from n
Is there any iterative algorithm to generate combinations of N numbers taking 'r' at a time ?
Yes there is.
Here is code from the wrong answer Library.
void generate_combos(int n, int k) {
int com[100];
for (int i = 0; i < k; i++) com[i] = i;
while (com[k - 1] < n) {
for (int i = 0; i < k; i++)
cout << com[i] << " ";
cout << endl;
int t = k - 1;
while (t != 0 && com[t] == n - k + t) t--;
com[t]++;
for (int i = t + 1; i < k; i++) com[i] = com[i - 1] + 1;
}
}
This generates the combinations in lexicographic order.

Resources