I want to split a number into different numbers.(e.g. 123456 -> 123,456 or 12,3456 or 123,45,6...etc). They have to be in the same order as well. So one method I was thinking was putting each digit of the number into an array first. Then I would go through the array and find each combination of them. But I think this method would have a long run-time to find all possible combination especially if the number can be split more than 2 times. Would there be a more efficient method in doing this?
Would there be a more efficient method in doing this?
No.
There will be 2n-1 combinations, e.g. for a 6-digit number, 25 = 32 combinations.
Think of it like this: Each "space" between digits can be either separated or not, true/false, 1/0, i.e. a "bit", so you can determine split combination using a 5-bit number:
1 2 3 4 5 6
^ ^ ^ ^ ^
0 0 0 0 0 -> 123456
0 0 0 0 1 -> 12345,6
0 0 0 1 0 -> 1234,56
. . . .
1 1 1 1 0 -> 1,2,3,4,56
1 1 1 1 1 -> 1,2,3,4,5,6
Related
Given n bulbs, initially, all bulbs are off and m switches, each switch controls a range of light bulbs l to r (both inclusive)
We neeed to determine if it is possible to switch on all the bulbs using any of m switches any no of time.
The answer can be yes if one can or can be no if one cant.
E.g first given n then m and then next m lines give the range which the switch controls. In the following example n = 5 and m = 2.
5 2
1 2
3 5
here answer is yes because 2 switches can control all the bulbs. and can be turned on.
Second example
5 2
1 2
3 4
here the answer is no because one of the bulbs is not controlled by any switch.
What I did is declare an array count with size m and for each switch I counted how many bulbs it control by (r-l +1) if the sum of count is equal to n then yes else no.
But only sample test cases are passed rest all fails.
Note: Ranges might intersect
If ranges intersect for example
1 3
3 5
The answer is no because when one turn the second switch on the 3 rd bulb flip from on to off
So the answer will be no.
We can see this problem as an instance XOR-SAT problem, though it is more general than the problem posed here, that's one way to go.
Just to gain some intuition I provide a very simple example. Suppose you have systems of three switches and three bulbs like this:
S B
1 1, 3 // toggles bulbs 1 and 3
2 1, 2
3 1, 2, 3
It is equivalent to have the following formula, which we want to satisfy:
(x1^x2^x3)&(x1^x2)&(x1^x3).
And now we want to satisfy this formula. We start with writing it as system of boolean equations modulo 2:
|1 1 1| |x_1| |1|
|0 1 1| * |x_2| = |1| mod 2
|1 0 1| |x_3| |1|
Now solve it with Gaussian elimination.
First, add the first and the second rows to the third:
1 1 1 1 1 1 1 1
0 1 1 1 -> 0 1 1 1
1 0 1 1 0 0 1 1 // for RHS, 1+1+1 = 1 mod 2
Second, back-substitute: x1 = 0, x2 = 0, x3 = 1, which is obviously the answer.
So, the main complexity here is to program Gaussian elimination process.
I'm trying to understand question and solving it using java.
But first I'm not able to understand properly.
Here is the question:
You are given an array a of length n and an integer c.
The value of some array b of length k is the sum of its elements except for the smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.
Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.
Input
The first line contains integers n and c (1 ≤ n, c ≤ 100 000).
The second line contains n integers ai (1 ≤ ai ≤ 109) — elements of a.
Output
Output a single integer — the smallest possible sum of values of these subarrays of some partition of a.
Examples
inputCopy
3 5
1 2 3
output
6
inputCopy
12 10
1 1 10 10 10 10 10 10 9 10 10 10
output
92
inputCopy
7 2
2 3 6 4 5 7 1
output
17
inputCopy
8 4
1 3 4 5 5 3 4 1
output
23
In the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively.
My Understanding:
1) Partition is being being done within continuous numbers. Correct ?
2) What is the significance of Integer c in input ?
3) How is being done in third example ? I mean after having subarrays, How 13 came out from second subarray ?
Can anyone help me to understand the question ? I can write code myself.
I want to find efficient algorithm based on which subset it is. New condition is to be executed for each subset.
For eg: I have 4 flags ABCD and each subset will have seperate condition. What is the most efficient algorithm to solve the following condition. It can be made easily but I want to find the most efficient algorithm. Is there already an algorithm which solves this kind of problem?
A B C D
0 0 0 0 Subset 1 Execute Condition 1
0 0 0 1 Subset 2 Execute Condition 2
0 0 1 0 Subset 3 Execute Condition 3
0 0 1 1 Subset 4 Execute Condition 4
0 1 0 0 Subset 5 Execute Condition 5
0 1 0 1 Subset 6 Execute Condition 6
0 1 1 0 Subset 7 Execute Condition 7
0 1 1 1 Subset 8 Execute Condition 8
1 0 0 0 Subset 9 Execute Condition 9
1 0 0 1 Subset 10 Execute Condition 10
1 0 1 0 Subset 11 Execute Condition 11
1 0 1 1 Subset 12 Execute Condition 12
1 1 0 0 Subset 13 Execute Condition 13
1 1 0 1 Subset 14 Execute Condition 14
1 1 1 0 Subset 15 Execute Condition 15
1 1 1 1 Subset 16 Execute Condition 16
Bitmasking can be used to generate all subsets. There are four values. Therefore, you have 2^4 subsets. All you have to do is iterate this mask 2^4 times and mask it with each of the four values. In each iteration, the result of masking is a subset of the given values. Here's an idea:
allSubsets = {}
for mask in range(1<<4):
subsets = []
for i in range(0,3):
val = mask & (1<<i)
if(val)
subsets.append(a[i]) # Individual subset. Here assume array a has 4 values. Can be just 1s and 0s as in your case.
allSubsets[mask] = subset #keep appending each generated subset
return allSubsets # Do your operation by iterating on each of these subsets
hello guys i have 2d char array opt[][] and i have 2 sequence in my arrays like in example
my
`opt[0][0]=A
opt[0][1]=T
opt[0][2]=G
opt[0][3]=A`
and
opt[1][0]=A
opt[2][0]=G
opt[3][0]=C
opt[4][0]=T
i have this output currently
x/y| A T G A -
_______________________
0 A | 0 0 0 0
1 G | 0 0 0 0
2 C | 0 0 0 0
3 T | 0 0 0 0
4 - | 0 0 0 0
my problem is this how can i use dynamic programming
to create this array into this
http://i.stack.imgur.com/ViHc9.png
if its a match 0 penalty
if its a mismatch 1 penalty
if its a gap its 2 penalty
i can compare chars of my array like this
for(int i=0;i<4;i++){
if(opt[0][i]==opt[i+1][0]){
result[0][i] =1;
}
but this is just a simple test i made to see if i can compare and it turned out i can.
how can i go from here to there(to the picture array
I suggest you read these articles.
http://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm
http://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm
The implementation in any language is pretty trivial.
And if you need information about dynamic programming in general,
either Google for it yourself, or check these two links.
http://en.wikipedia.org/wiki/Dynamic_programming
https://www.topcoder.com/tc?d1=tutorials&d2=dynProg&module=Static
I've been looking into algorithms using a class on coursera. In one of the first lectures, Quick Union Weighted is being discussed. I get what it does and I've tested it out using their code and written a small test for it.
Everything is clear but one point: when you create a union of two objects, it will add the object with the smallest tree to the bigger one. At the same time, the size of the larger tree will be incremented with the size of the smaller tree in a separate array which is used to determine what tree is bigger. Since the array is initiated with value 1 for every index (every node on its own basically is a tree of 1 object), why isn't the value of this index set to 0 instead of remaining on 1?
In order to illustrate this:
// Quick Union Weighted
ID: 0 1 2 3 4 5 6 7 8 9
SZ: 1 1 1 1 1 1 1 1 1 1
quw.union(2, 4);
ID: 0 1 2 3 2 5 6 7 8 9
SZ: 1 1 2 1 1 1 1 1 1 1
quw.union(5, 4);
ID: 0 1 2 3 2 2 6 7 8 9
SZ: 1 1 3 1 1 1 1 1 1 1
quw.union(2, 7);
ID: 0 1 2 3 2 2 6 2 8 9
SZ: 1 1 4 1 1 1 1 1 1 1
// Whereas I would've expected to end up with this
// to point out that the index is empty.
SZ: 1 1 4 1 0 0 1 0 1 1
Why are the sizes of merged indices 1 instead of 0?
You can find the code to test it out here. Note that the implementation is the same as the example provided by the lecturers, which is why I'm assuming my code is correct.
I think this is because the node itself is also size 1 and does not have any children. It can however have children. I'm actually not familiar with Quick-Union Weighted but if it's bit like the other union find algoritmes I've seen you can for example do
quw.union(0, 1);
ID: 0 0 2 3 2 2 6 2 8 9
SZ: 1 1 4 1 1 1 1 1 1 1
quw.union(0, 2);
ID: 2 2 2 3 2 2 6 2 8 9
SZ: 2 1 6 1 1 1 1 1 1 1
So now 0 en 1 have merged and the entire tree starting from 0 is merged with 2 again, still making the subtree starting at 0 size 2.
Like I said, I'm not sure it that's possible in Quick-Union Weighted but the reason for the '1' is still because it's also size 1 on its own.