Whats the difference between the two approaches? [closed] - c++

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 this question in my assignment, and the solution states that we must have two pointing indices one at the start and one at the last element and we can progress from last element to the middle and respectively the first element to the middle. However, my solution was to have one index at the middle and another at last, and the last index will just move until the middle while the middle index will move until it reaches the start. But i am confused with what's wrong with my solution
Question:
Two stacks of positive integers are needed: one containing elements with values less than or equal to 1000 and the other containing elements with values larger than 1000. The total number of elements in the small-value stack and the large-value stack combined is never more than 500 at any time, but we cannot predict how many will be in each stack. (Initially both will be empty; later on the stacks could be evenly divided, or all the elements could be in the small-value stack, and so on.) For efficiency reasons, we want to implement both stacks using a single array of size 500. Can you think of a way to do this

Take the number 10 (less than 1000) and 400 numbers from 2001 to 2400 (greater than 1000) and try to put them on the two stacks. You will see that the solution with pointers on the two ends will work. But the solution with one pointer in the middle will not work, one of the stacks will be too small or the small value will be overwritten.

Related

Most frequent substring of fixed length - simple solution needed [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 4 years ago.
Improve this question
Please describe (without implementation!) algorithm (possibly fastest)
which receives string of n letters and positive integer k as
arguments, and prints the most frequent substring of length k (if
there are multiple such substrings, algorithm prints any one of them).
String is composed of letters "a" and "b". For example: for string
ababaaaabb and k=3 the answer is "aba", which occurs 2 times (the fact
that they overlap doesn't matter). Describe an algorithm, prove its
correctness and calculate its complexity.
I can use only most basic functions of C++: no vectors, classes, objects etc. I also don't know about strings, only char tables. Can someone please explain to me what the algorithm would be, possibly with implementation in code for easier understanding? That's question from university exam, that's why it's so weird.
A simple solution is by trying all possible substrings from left to right (i.e. starting from indices i=0 to n-k), and comparing each to the next substrings (i.e. starting from indices j=i+1 to n-k).
For every i-substring, you count the number of occurrences, and keep a trace of the most frequentso far.
As a string comparison costs at worst k character comparisons, and you will be performing (n-k-1)(n-k)/2 such comparisons and the total cost is of order O(k(n-k)²). [In fact the cost can be lower because some of the string comparisons may terminate early, but I an not able to perform the evaluation.]
This solution is simple but probably not efficient.
In theory you can reduce the cost using a more efficient string matching algorithm, such as Knuth-Morris-Pratt, resulting in O((n-k)(n+k)) operations.

Find max element in each column in a 2-D array [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 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.

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.

Sorting on pairs in c++ [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 7 years ago.
Improve this question
I am writing a code for activity schedule problem. If i store the starting time and finishing time in an array of pairs like pair<int,int>p[10] and after this i need to sort the activities as per finishing time with increasing finishing time,omitted the part where it asks user to input and then make pair for simplicity,hope it won't be a problem for the guy to answer so i apply sort(p,p+n) where n means no of activities ,i don't get like will it sort all starting activities this way or the finishing activities this way.So this is the code.pair <int,int>p[10]
Sort(p,p+6)
When sorting pair, the first items are compared and only when they are equal, the second ones are compared. So this won't sort it by the finishing time but by the starting time. But sort can have a third argument which is a method used for sorting (it returns whether the first argument is before the the second). Pass there a lambda that will compare second items (finishing time).
It sorts the activities in the increasing order of their starting time i.e the first elements in every pair is compared to the first element in second pair and so on. .Suppose (7,9),(0,10),(4,5),(8,9),(4,10),(5,17) are the starting time and finishing time then after sorting the result would be (0,10)(4,5),(4,10),(5,17),(7,9),(8,9).

Using induction to prove bubble sort is correct [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How can we show, using induction, that bubble sort is correct? How do we choose the invariant to follow throughout the formulation of the proof (this step seems like an arbitrary task to me, so if it can be explained more deeply I would greatly appreciate it)?
I understand that the largest elements will always end up at the end of the list after each iteration, but I don't know how to use this fact to show that the algorithm is correct.
Thanks for the help!
I am not sure if this is what you want, but his is how I see it.
The idea behind bubble sort is that you go though the vector of values (left to right). I am calling this a pass. During the pass pairs of values are checked and swapped to be in correct order (higher right).
During first pass the maximum value will be reached. When reached the max will be higher then value next to it, so they will be swapped. This means that max will become part of next pair in the pass. This repeats until pass is completed and max is left at the right end of the vector.
During second pass the same is true for the second highest value in the vector. Only difference is it will not be swapped with the max at the end. Now two most right values are correctly set.
I every next pass one value will be sorted out to the right.
There are N values and N passes. This means that after N passes all N values will be sorted