What is the correct approach to solve SPOJ BALNUM? - algorithm

I am trying to solve BALNUM problem on SPOJ. However I am unable to find approach to solve it. I am understanding that this is a dynamic programming problem but I am not able to find the recurrence function since number of occurrence of all digits(0-9) matter here.
Can someone please give me a hint. Whole algorithm or code is not required. Just a hint as how can I find the recurrence function for it?

Can you calculate the ammount of balanced numbers of given length?
Try to think about it that way - knowing how many balanced numbers have length 0, 1, 2, ... n how can you calculate how many balanced numbers have length n+1

Related

Knuth Morris Pratt algorithm comparison

I have been studying for my exams when I came across with this problem, guys help please
For any alignment of pattern P and text T, suppose a mismatch occurs
at P[i+1] and T[k] during the execution of KMP algorithm How many
times does T[k] come into comparison in total during the execution of
KMP algorithm (SPi is non-optimized)
the possible solutions I came across with are
i-SPi
SPi+1
n-i
n-SPi
but all of them fail on some scenarios,
There is no single answer for all cases.
Still you can give an upper bound on the number of comparisons and it is found if all symbols in P are the same. Try and compute that.
If this is not enough try using this property: KMP will compare T[k] against P[SPi] and then against P[SPSPi+1] and so on until one of two options happens:
The given letter matches T[k]
The value of the given SP is 0
The two above may happen in many different ways depending on P and T and so it is impossible to give a closed formula.

Partitioning floating-point array

While writing code, i found the following problem, to state it in a simple way:
Partition an array of floats X in array A and B such that the difference between the sum of the values in A and the sum of values of B is minimized
This was part of an investigation I was doing, but I can't find a way to efficiently perform this operation.
Edit:
To answer to those who believe this is from a math contest like PE, SPOJ or homework, it is not. I just had curiosity about this when i was trying to partition an already factorized number p in the set of factors a and b such that b=a+1. If we take logs from both sides, we can show this problem is equivalent to minimize a diference of sums, but that is where i have got stuck.
Just a first simple idea. Use dynamic programming methods.
I assume that this problem can be transformed to knapsack problem. You need to pick items from X (there'll be array A) to maximize sum but don't exceed (sumX - sumA) value (there'll be sum of items from array B). For algorithm to solve knapsack problem by dynamic programming approach look at wiki e.g.
This solution can be wrong, btw... but even if it'll work I'm more than sure that more efficient, elegant and short solutions exist.

What is a "naive" algorithm, and what is a "closed - form" solution?

I have a few questions regarding the semantics of terminology used when describing algorithms.
Firstly, what is meant by a 'naive' algorithm? How does this differ from other solutions to a given problem? What other forms can solutions take?
Secondly, I have heard much reference to having a 'closed - form' solution. I have no idea what this means either - but often it appears when trying to solve recurrence relations...
Thanks for your time
A Naive algorithm is usually the most obvious solution when one is asked a problem. It may not be a smart algorithm but will probably get the job done (...eventually.)
Eg. Trying to search for an element in a sorted array.
A Naive algorithm would be to use a Linear Search.
A Not-So Naive Solution would be to use the Binary Search.
A better example, would be in case of substring search Naive Algorithm is far less efficient than Boyer–Moore or Knuth–Morris–Pratt Algorithm
A Closed Form Solution is a simple Solution that works instantly without any loops,functions etc..
Eg:
Iterative Algorithm for sum of integer from 1 to n
s= 0
for i in 1 to n
s = s + i
end for
print s
Closed Form (for the same problem)
s = n * (n + 1 ) /2
Naive algorithm is a very simple algorithm, one with very simple rules. Sometimes the first one that comes to mind. It may be stupid and very slow, it may not even solve the problem. It may sometimes be the best possible. Here's an example of a problem and "naive" algorithms:
Problem: You are in a (2-dimensional) maze. Find your way out. (meaning: to a spot with an "EXIT" sign :)
Naive algorithm 1: Start walking and choose the right one in every intersection you meet (until you find "EXIT").
Naive algorithm 2: Start walking and choose a random one in every intersection you meet (until you find "EXIT").
Algorithm 1 will not even get you out of some mazes!
Algorithm 2 will get you out of all mazes (although this is rather hard to prove).
Closed form means you can give the one expression as solution, that does solve it without recurrence/recursive. Here one should remark, that it is not always possible to find such a closed form.
Naive means just that what it says: A first, stupid solution to the problem, that solves it, but maybe not very time-/space efficient. What one really considers 'naive' depends on the speaker, the context, and the weather of the next day. Often it is used to distinguish a very sophisticated solution (that uses some kind of trick) from the obvious implementation.

Permutation Problem

I am struck very badly in a problem. my problem goes this way; I need to find permutations of n objects(there could be repetitions) such a way that every permutation differs from the other by atleast k objects.
For ex: if there are 5 objects a,b,c,d,e and each permutation differs by 2 or more object and if aabcd is a permutation then I cannot have aabdd as a permutation as both differ by just one object.
If anyone can point me out a general formula or procedure to solve this problem, I'd be highly grateful
Thanks for your time and consideration of this request
--Ady
This sounds like it's related to Conway's Lexicode thereom. I heard him give a lecture about it once. It was quite entertaining.
http://www.dpmms.cam.ac.uk/seminars/Kuwait/abstracts/L25.pdf

Create a sum of 1000, 2000, etc. from set of numers

Ok, so here's the problem:
I need to find any number of intem groups from 50-100 item set that add up to 1000, 2000, ..., 10000.
Input: list of integers
Integer can be on one list only.
Any ideas on algorithm?
Googling for "Knapsack problem" should get you quite a few hits (though they're not likely to be very encouraging -- this is quite a well known NP-complete problem).
Edit: if you want to get technical, what you're describing seems to really be the subset sum problem -- which is a special case of the knapsack problem. Of course, that's assuming I'm understanding your description correctly, which I'll admit may be open to some question.
You might find Algorithm 3.94 in The Handbook of Applied Cryptography helpful.
I'm not 100% on what you are asking, but I've used backtracking searches for something like this before. This is a brute force algorithm that is the slowest possible solution, but it will work. The wiki article on Backtracking Search may help you. Basically, you can use a recursive algorithm to examine every possible combination.
This is the knapsack problem. Are there any constraints on the integers you can choose from? Are they divisible? Are they all less than some given value? There may be ways to solve the problem in polynomial time given such constraints - Google will provide you with answers.

Resources