Skip to content
Advertisement

Tag: algorithm

Find closest factor to a number, of a number

I am trying to automate the finding of the closest factor of a number to another number; Example: Closest factor of 700 to 30 is 28 (30 does not go into 700, but 28 does). An obvious solution is just to get all the factors of 700 and do a simple distance calculation to find the nearest factor to 30,

Finding Minimum Distance Between Words in An Array

Example: WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList(“the”, “quick”, “brown”, “fox”, “quick”)); assert(finder.distance(“fox”,”the”) == 3); assert(finder.distance(“quick”, “fox”) == 1); I have the following solution, which appears to be O(n), but I’m not sure if there is a better solution out there. Does anyone have any idea? Answer You solution is O(N^2) because you traverse the whole list when finding each word. First

Knights tour – results in an infinite loop and i can’t figure out why

I’m trying to solve the knight’s tour problem using backtracking. I think the algorithm I have should work. I’ve tried but I can’t figure out why it isn’t working. It results in an infinite loop. However if I comment out the line that back track solutionBoard[dst.x][dst.y]=-1; it works! I just don’t understand why! Any help would be appreciated. Answer Your

Why is this called backtracking?

I have read in Wikipedia and have also Googled it, but I cannot figure out what “Backtracking Algorithm” means. I saw this solution from “Cracking the Code Interviews” and wonder why is this a backtracking algorithm? Answer “Backtracking” is a term that occurs in enumerating algorithms. You built a “solution” (that is a structure where every variable is assigned a

Efficient solution to codingBat riddle starOut in Java

The problem I am talking about is this Problem statment: Return a version of the given string, where for every star () in the string the star and the chars immediately to its left and right are gone. So “abcd” yields “ad” and “ab**cd” also yields “ad”. starOut(“ab*cd”) → “ad” starOut(“ab**cd”) → “ad” starOut(“sm*eilly”) → “silly” The solution I got

How to generate a CUSIP check digit

CUSIPs are a 9-digit alphanumeric code for uniquely identifying a financial security. https://en.wikipedia.org/wiki/CUSIP They were invented in the 1964, and given the reliability of data transmission in the 60’s, the 9th digit is actually a check digit used to confirm the validity of the first 8 characters. Sometimes, even today, you might find reason to want to validate a CUSIP,

java codility Frog-River-One

I have been trying to solve a Java exercise on a Codility web page. Below is the link to the mentioned exercise and my solution. https://codility.com/demo/results/demoH5GMV3-PV8 Can anyone tell what can I correct in my code in order to improve the score? Just in case here is the task description: A small frog wants to get to the other side

Big O – O(log(n)) code example

Like the Big O notation “O(1)” can describe following code: What code can O(log(n)) describe? Another question: What solutions are there for “Big O problems” (what to do, when getting a lot of data as an input)? Answer Classic example: This will be: 2k = x → Applying log to both sides → k = log(x)

Josephus sequence

Description: There are people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as

Advertisement