Count the occurrence of consecutive 1s in 0-1 array [closed] - java

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

Related

How to fill an array with numbers till certrain number in Java that user has entered? [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 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.

What is the time complexity of the code (generating permutations) [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 3 months ago.
Improve this question
Please see the code at the link below:
https://stackoverflow.com/a/20614037/1676147
My thoughts:
Time complexity here would depend on two factors:
1) Number of recursive calls: O(n) where n is the number of characters in the original input string
2) The work involved in the two for loops: O(n!)?
Basically, the first for loop is iterating each string in the set 'permSet'
The number of permutations for an n-character string can be n!.
Thus, this set would contain n! strings. Correct?
The second for loop places one character (variable 'a' in the code) at each
of the potential positions in each of these strings.
I am confused at this step.
Your analysis is correct. For a string of length N, you have N-1 recursions, with string length of M, one each for lengths 1 through N-1. Each recursion deals with a list of size (M-1)!, and inserts the next character at each of M positions (0 through M-1).
Each call has time M * (M-1)!, or M! Sum these over M = 1 to N.
1! + 2! + 3! + ... + N!
This sum is O(n!).

Calculating How Many Balls in Bins Over Several Values Using Dynamic Programming [closed]

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 6 years ago.
Improve this question
Regarding the classic problem of putting N identical balls into M distinct bins and printing all the combinations: What if you would want to extend the problem by printing all cases 0< M, N
The brute force method could be done something like this:
for (int i =0; i<M; i++)
{
for (int j =0; j <N; j++)
{
PrintAllCombinations(j,i)
}
}
Now if we study the output of the first couple m and n, we see that the output of each previous iteration is a subset of the next. It seems to me that we can apply a dynamic algorithm to exploit this phenomenon. However, because we still need to partition every n, for example n=3 = 3 +0, 2+1, 1+2. we still need to do alot of redundant combination calculations. Any ideas fir improvments?
Let S[i][j] be the number of combinations for i balls in j bins.
S[0][j] = 1 for all j since the only combination is to have all bins empty.
S[i][1] = 1 for all i since the only combination is to put all the balls in the one bin.
For every other i, j S[i][j] = sum(x = 0 -> i, S[i-x][j-1]). That is for every other position you can compute the number of combinations by assigning every possible number of balls to the last bin and sum the number of combinations you get.
If you want to print out the combinations you can replace the count with the actual combinations and append the value x when you take the internal combinations in the sum. That will take a lot of memory without a lot of gain in speed. Just do the recursion and repeat the computation since you're bound by the number of solutions anyway.

Logic for Nine to one equals 100 [closed]

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.

Array jumping back and forth algorithm puzzle solver [closed]

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!

Categories