I came across an algorithm to tell if a given number is perfect square or not in O(logN) time. Here is the implementation(JAVA) of the idea. This works fine for numbers like 256, 808201 , etc But fails for numbers like 999966000289. I cannot figure out why? Answer As mentioned in the comments, the problem is that the intermediate mid*mid
Tag: algorithm
Using Knapsack approach without value?
Regarding to question, I am wondering that according to the following approach: Assume that there is no value and I am wondering that which approach or algorithm should be used for a Knapsack problem when the values are equal and there is a parameter (called n) that describes times for carrying items in a time? For example, the bag has
Java program failing on specific test case [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 last year. Improve this question I wrote a java program that does basically what wordle does. Given 2 3×3 rows, for example If the guess matches the answer (in this case
Prefix To Postfix Java
I need some help with my prefix to postfix code. When ever I generate the action through GUI from the expression “* 2 + 2 – + 12 9 2” it returns 2*. It should be returning “2 2 12 9 + 2 -+*”. I keep changing the code around and I keep ending up with the same result. Please
Decompressing/inflating zlib-compressed data without adler32 checksum
Update 2 (newest) Here’s the situation: A foreign application is storing zlib deflated (compressed) data in this format: 78 9C BC (…data…) 00 00 FF FF – let’s call it DATA1 If I take original XML file and deflate it in Java or Tcl, I get: 78 9C BD (…data…) D8 9F 29 BB – let’s call it DATA2 Definitely
Determine minimum number of moves to reach top left corner of 2-D array
I’m working through some leetcode problems and came across this one: I’m given a 2-D array, for example: Each cell in the array represents the number of moves I must make from that cell. I can move only up or only left each time I stop on a cell. I want to find the minimum number of moves necessary to
Longest common prefix in binary representation
We are given a undirected tree with N (1 to N) nodes rooted at node 1. Every node has a value assigned with it, represented by array – A[i] where i:[1:N]. We need to answer Q queries of type : -> V X : longest length of the common prefix between value V and any ancestor of node X including
How to get better time complexity of N queens validator
How can we lower the time complexity of this solution of NxN queens matrix validator? I have this solution while I check every row and every column and every diagonal of the matrix. If every row and column has exactly 1 queen, and the matrix has no more than 1 queen the output is true. This solution works but I
Image compression and decompression Java
I want to create a program which compresses and decompresses images , there are many algorithms to do that , but I was asked to use the LZSS algorithm to compress and decompress the images , my question is that isn’t the LZSS a dictionary type data compression method ? used only for text files ? or am I wrong
Java Quick Sort Performance
I was doing sorting array problems and found one of the quick sorting solution extremely fast, and the only difference is the two lines of code in function 1Partition. Wondering why the following two lines of code in 1Partition can greatly improve the performance: Here’s the full source code: } Answer I guess you are doing the question on a