Find max element in each column in a 2-D array [closed] - c++

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 5 years ago.
Improve this question
For finding max element in i th row in a 2-D matrix[n][m], this is working
int t= *max_element(matrix[i],matrix[i]+m);
but I am not getting how to find max element in i th column in the same way.

The reason why std::max_element works for rows is quite simple: the algorithm accepts forward iterators to specify a range, so one might pass pointers as well: a pointer points to the first element in the row and a pointer points behind the last one. The elements in the row form a continuous block in memory, so the approach with pointers works well.
On the other hand, there is no way to use std::max_element for column elements in the same way as column elements do not form a continuous block in memory.
The most natural way will be just to write a simple cycle and do not use STL algorithms.

Related

Vector Dummy Declaration [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 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
A Vectors in C++ are sequence containers representing arrays that can change in size. They use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays.
Can anyone explain to me, in easy layman language, what the meaning of vector<int> dummy1(rows,-1); is in C++? Specifically, what does dummy1(rows,-1) mean?
The code is declaring and constructing an object named dummy1 whose type is vector<int>, using constructor #3 on this reference page to initialize the object with rows number of elements that are all set to the value -1.
The first argument is the number of elements, the second argument is the value used to fill all of those elements. So it creates a vector with rows number of elements, all with the value -1

Minimal swaps for insertion sort [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 2 years ago.
Improve this question
I was solving a question in codeforces,which was about insertion sort,
the link:the problem
I am unable to solve the question the editorial says
old - 2 * (di, ai + dj, aj - di, aj - dj, ai) - 1
I am not understanding the solution kindly help!
here is the editorial link:
https://codeforces.com/blog/entry/9584
For an insertion sort into a linked list, the number of "swaps" is maximum of 2, if you consider inserting a new item as "swapping pointers". The minimal number would be zero, if the key already exists; otherwise you'll swap one pointer when inserting before the head or after the tail.
The concept of an insertion sort is to keep the container sorted. Most of the time is spent searching for the proper place in the container to insert the new datum.
For an array container, you would need to move elements in order to make room for the new data; which doesn't involve swapping.

What are some pros and cons for making a matrix with arrays vs vectors? [closed]

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 3 years ago.
Improve this question
I have been doing some research and cannot figure out which method/approach would be better and for what reasons. I want to create a simple empty matrix with a given length.
int n = 5;
int m[1...n][1...n];
int s[1...n-1][2...n];
I am not sure if it would be best to do this with vectors or arrays. Can the answers also include a code snippet?
NOTE: The above code snippet works and compiles in gcc -std=c++11, but with actual values in replace of the 1...n stuff
Arrays, like vectors, can store arbitrary objects except references(there are no type of reference in arrays and vectors)
Compared with vector, arrays has the following shortcomings:
The dimension of an array must be a constant expression, that is, it must be given at initialization. And will be not change in the whole process of running the program.
Arrays are not allowed to copy and assign, that is, the contents of arrays cannot be copied to other arrays as their initial values, but vectors can.
The process of using arrays is easy to go into array crossing, but vectors can use more mechanisms to control, such as iterators.

Best possible way to search for a given value among N unsorted numbers [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 6 years ago.
Improve this question
One of my friend has been asked with a question in an interview:
The best possible way to search for a given value among N unsorted numbers in a array.
If the array is unsorted, you need to perform a linear scan of the list. This examines (worst case) every element in the array. Such a search is O(n).
Sorting won't help here, since the best sorts run in O(n log n).
If it was sorted I'd say std::binary_search, but for unsorted, just go with std::find (unless the container you use has a member find; if it does, then use that as it is probably faster).
It depends:
-if you just want to search a single value, the answer given by bush is enough.
-if you know what you want to perform repeated "query", it could be better to perform before some kind of preprocessing,in order to find fastly these value.If you want to know only if a value is contained in the array, you can use structures like hashset,bloom filter,etc..
In other cases,you would like to know also position of items inside the array. In this scenario,you can consider to use an hashmap

Identification possibility of sorting an array by deleting not more than one element [closed]

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
Program gets a number of int arrays with size [2; 2000]. The question is: can that arrays get sorted after deleting not more than one element.
Examples:
2 16 3 3 - after deleting '16' it would be non-increasing array.
4 16 3 15 - there is no way to make it sorted.
Simple way: deliting first incorrect element and checking the fact of sorting. It takes too much time if there is a great number of arrays or that arrays are big-sizes.
Except cases, there incorrect element is first or last, and there array size is less than 4, in what way cheking that possibility can be accelerated
simply iterate through the array, and if you notice N_x > N_x+1 remove N_x, and mark that you removed 1 number...
in case you found another N_y > N_y+1 where y!=x, then it can't be sorted.
I'll leave the implementation for you, as it is your homework after-all.