Skip to content
Advertisement

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

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

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

Advertisement