I have implemented a Heap-Based Priority Queue in Java but my challenge now is implementing an efficient Iterator that has a definite (increasing or decreasing) order of traversal. Is there any algorithm that does this at constant auxiliary space and time complexity of O(n)? The O(nlogn) is quite trivial. Also, there is absolutely no subtlety in O(n) space algorithm. Please
Tag: algorithm
How can I get all subarrays of a given array faster than O(N^2)?
I’m trying to print all subarrays of given array like below. I can print it with nested loops. But I want to know how to implement it with O(N) or O(2^N). Answer There are O(n^2) subarrays of an array in the first place. Printing them — no matter how you generate them — will always take at least O(n^2); you’re
How to output direction of shortest path?
This is a simple maze solver program. this is the simple maze i’m working on. I implemented a solution to output cordinates of the path as follow.(cordinates aquired from a BFS algorithm) but I want to output like below(omit same direction and only output direction and last coordinate to same direction), this all coordinates are allocated to a stack.below is
Code that takes a string and recognizes the number of consecutive letters
To do my job, I need a code that takes a word from the user, then recognizes the number of consecutive letters and outputs it in such a way that it prints the letter and the number of times it is repeated. Example 1 input: Example 1 output: Example 2 input: Example 2 output: Answer Here is one way. You
Get time period ( hour / half-hour / quarter hour ) of day
Is there any way to download every hour and another period of the day? I mean exactly the result in the form of a list. and and 14.03 means March 14 for the sake of the example. Of course it is possible to add this manually, but it would not be a particularly elegant solution. Is it possible to do
Checking a tree to be a BST
Here is my attempt to check whether a tree is a BST or not: Code works fine as tested with multiple test cases. But I am not sure if this is a good, clean approach. Recursive method is big it seems. I am dealing with scenarios like null left node, null right node, node itself null, both child nodes null
method to find the the distance of two characters of a string apart and entering them into an array
trying to solve this question for school “Given a string s and a character c, return a new list of integers of the same length as s where for each index i its value is set the closest distance of s[i] to c. You can assume c exists in s.” for example Input s = “aabaab” c = “b” Output
Index 16 out of bounds for length 16
I am trying to sort an array of x elements using quickSort but I am getting an error “Index 16 out of bounds for length 16” please help. Answer For an array of 16 elements the indexes are 0 to 15. Thus index 16 is not part of the array and you get the OutOfBoundsException.
Kth smallest number algorithm doing extra work?
So I’m preparing for a technical interview, and one of my practice questions is the Kth smallest number. I know that I can do a sort for O(n * log(n)) time and use a heap for O(n * log(k)). However I also know I can partition it (similar to quicksort) for an average case of O(n). The actual calculated average
How to solve this java problem efficiently [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 3 months ago. Improve this question I am trying to solve this problem and I worked out a solution but its too slow, this is my approach: loop to add all