Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi I am having problems trying to develope code that will work for this puzzle solver
I have 2 integer arrays of 5 elements each
a[] = {1,1,0,2,2};
b[] = {2,2,0,1,1};
my program is suppose to step by step re arrange array a into array b and compare if they match.
the rules of solving are: integers 1 can only move right, integers 2 can only move left, 0 acts as a holding spot for jumping 1 over 2 vice-versa;
basically the way to solve this puzzle on paper is:
a b c d e
1 1 0 2 2 - >starting positiong
1 0 1 2 2 - >b moves to c therfore b is now vacated 0
1 2 1 0 2 - >d can jump over c because they are opposites so b = d
1 2 1 2 0 - > e moves over to the vacated d
1 2 0 2 1 - > c can jump over d to e because e is vacated
0 2 1 2 1
2 0 1 2 1
2 2 1 0 1
2 2 0 1 1 = b[]
I have written code just to declare the arrays but i have no idea how to solve any help is appreciated.
I see two possibilities here. If the puzzles are as simple as the example, you can just recursively solve it depth first. For every status, you evaluate which rules you could use to make a step. You try them all out, calling your solve function recursively to try the next level. To evaluate you should memorize the steps taken to find the shortest route.
If things get more complex, you could look at the Minimax algorithm. If memory serves me, you need some way to evaluate the different states, for instance by the number of items that are in the right spot. You can also design heuristics to trunc possibilities that are doomed to fail.
good luck!
Related
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 2 years ago.
Improve this question
I would appreciate any help on this problem that I have:
User enters a numeric value, like 6.
Now program has to fill out an array of 20 elements:
for (i=0; i <= 20; i++)
But I need to fill an array till certain number that the user has entered, for example if User entered 6
then the output must be:
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2
Or if user entered 2, the output must be:
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
Arrays element count needs to be 20, not more, not less.
I get stuck in the place where I need to define that the arrays maximum valued element must be the one the User has written.
Will appreciate any help, thanks!
Have you looked into the Modulo operator? In Java, the modulo operator allows someone to get the remainder of a division. For example: 4 % 3 = 1, and 3 % 3 = 0.
https://en.wikipedia.org/wiki/Modulo_operation
Because this is homework, I’ll give you direction rather than code.
The modulus operator % returns the remainder after division.
Consider how the result of the following operation relates to the value of the desired element at index i:
i % userInput
Modulo operator is your friend here.
Just iterate ( for loop from 0 to 19 ) over array you have to fill and put index % userValue + 1 into it
I tried the Modulo operator and it worked like a charm to solve this problem. And Bohemian, you are right, it is for a homework. Thank you, my question is answered.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In some programming contest, I saw this question:
A person wants to travel some cities and come back to the city where
he/she started. Total 5 cities are present and each city is connected
to its adjacent city i.e.: c1 - c2, c2 - c3, c3 - c4, c4 - c5, c5 -
c1. The person will start the journey from city 1. Also, the person
has some limited number of trips to use for traveling. Traveling from one city to another costs him/her 1 trip. We need to find the
total number of ways a person can travel the given cities and come
back to city 1 using the allocated number of trips.
Example:
Input: 4
(means 4 trips are allowed)
Output: 6
(As the output can be large, so it should be modulo 10^9+7)
the possible ways are:
1-2-1-2-1
1-2-1-5-1
1-5-1-2-1
1-5-1-5-1
1-2-3-2-1
1-5-4-5-1
If the question has limits on the number of trips to be less than 10^9 then its easy to solve as the problem has optimal substructure and overlapping subproblems. But the constraint given was 1<=n<=10^18. I couldn't initialize an array with a dimension that long, so couldn't apply dp. Please help me on how to solve this using dp. Or is this question just a permutation or combinations type of question?
Thanks.
I would suggest:
Store the number of ways to reach each city in a vector
Work out a matrix that allows you to update this vector by 1 step
Raise the matrix to the power n using binary exponentiation
Multiple this power matrix by the starting vector (1,0,0,0,0)
Read off in entry 1 the number of ways to reach city 1 after n steps
(edit)
Sorry read your question a bit too fast.
If all cities are connected then this is a simple permutation problem.
If not you can see it as a graph problem and solve it with an extended breadth first search that caps off as soon as you have reached the limit of possible trips.
This problem is purely mathematical; the answer is a mathematical formula which, once computed on paper, you just plot in your program.
You can reach the initial point either by going back and forth (requiring a multiple of 2 steps), or by doing complete circles(requiring a multiple of 5 steps). Thus you need to check the divisibility of n with 2 and 5, or, to put it in a mathematical form, check if n=2*k or n=5*q. We will compute the number of paths independently for the 2 cases, but if n is divisible with both 2 and 5, we will add the two results (since n steps can be done in both ways, either as a multiple of 2 or multiple of 5).
If n=5*q, we will do our n steps as q cycles of 5 steps. Each cycle can be in either direction, clockwise or counter clockwise. For the first cycle, there are 2 possibilities (the 2 directions), for the second cycle, another 2 possibilities, independent of the previous cycle, and so on. Thus, the number of paths is 2*2*2... = 2q .
If n=2*k, we will do our n steps as k back-and-forth paths (one step forward, one step backward). For each of these k paths, there are two directions to go, thus, for k closed-paths, there are 2k paths.
Note: I have given the name back-and-forth path, or closed path, for paths such as 1-2-1. Actually, from the graph theory perspective, they are cycles too, but i did not name them cycles as not to confound them with the 1-2-3-4-5-1 cycles.
The above computation, however, does not take into account the paths such as 1-2-3-2-1, but only 1-2-1-2-1, or 1-2-1-5-1. I am still working on that> I will post this answer the way it is right now as to give inspiration on how the computation might look like.
Another aspect to take into account is the number of ways we could write n as 2*a + 5*b, giving 2a * 5b paths. I think n=2*a+5*b is a dyophantic equation.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I was recently asked this question in an interview . Can anyone help me with a code to solve this problem?
We have a sequence of non-zero digits 123456789. The problem is to place plus or minus signs between them so that the result of thus described arithmetic operation will be 100. We can use a number only once . However we can use the operators(+,-,*,/) any number of times
Edit : I was asked to write a Java code for this question . So i believe its relevant
The followup question was to get all possible combinations
Example
Here is an example.
Split the numbers as follows
1 with multiply
4,7,89 with sum
3,6 with sum
4,5 with subtract
3+6 - 4 - 5 = 0
4 + 7 + 89 = 100
1 * 100 = 100
Automatic way of finding all possible combinations.
You essentially have 1 set:
the set {1..9} merged with the set {-,+,/,*,nothing} (nothing being the absence of a symbol)
You need to iterate over all the order possibilities. That will take for a long time. Exclude cases where there are 2 symbols side by side e.g. -/.
I believe this will lead to k-combinations.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a set of 1s and 0s. How do I count the maximum number of consecutive 1s?
(For example, x = [ 1 1 0 0 1 1 0 0 0 1 0 0 1 1 1 ]). Here the answer is 3 because the maximum number of times 1 occurs consecutively is 3.
Thank you so much.
Iterate over the array, keeping the count of how many consecutive 1s you've seen. Whenever you see a 1, increment the count. Whenever you see a 0, reset the count to zero. The answer to your problem is the largest counter value seen during the iteration.
int cnt = 0,max=0;
for(int i=0;i<x.length;i++){
cnt=0;
while(i<x.length&&x[i]==1){
cnt++;
i++;
}
if(cnt>max) max=cnt;
}
this should work
Since this smells like homework, all you get is an algorithm!
Initialize counter, max
For all the elements in the Array.
If element is '1'
increment the counter
Else
max=GetMaxOf(max, counter)
reset counter
End If
End For
I want to write a simple recursive function for a game plan in java.
I have 2^k teams and want an output like this (e.g. 4 teams):
(team)(day1)(day2)(day3)
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
My idea was to call the function recursive with half of its original size, but I can't figure out how to code it properly. If called with n/2, the output has to go into the upper left corner of the plan, the output PLUS n/2 has to go to the lower left corner and the rest is symmetric to the center.
Thanks
My code so far
public void plan(int size) {
if(size==2){}
else{}
}
make a Set for each day (unique)
then use n(max number)
itterate a loop n times and each itteration
newRandomNumber % n (fetch a random number a limit it to 0 to (n-1)
now add the (generatedValue+1) to the set
if it already exists (do check ) then increment the value till its new value then add to set
note: i dont get ur symetric requirement