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 2 years ago.
Improve this question
An integer is a lucky number if each number in it is 3 or 7. To give an example, the numbers 7, 73, and 33777 are lucky numbers. Your task is to calculate the number of lucky numbers between int(a) and int(b).
How could one achieve this efficiently (in less than 1 sec) without going through all the numbers? I have attempted many solutions in Java however none of them are fast enough.
Since you have to count the numbers and not list the numbers, you can use permutation and combination to find the answer.
Eg let say find between 1 and 999 where you can use 3, 7
Then you have 3 lengths single, double and triple digits with constraints on single and triple digits.
For single since minimum number is 1 and 3, 7 both are greater there 2 numbers.
For double digits you have no constraints hence you have 2 * 2 = 4 combinations
Similarly for 3 digits as max number allowed is 9 in each place and 3,7 are lesser than them there will be 2 * 2 * 2 = 8
So answer is 14 after summing them all... This algorithm will run fast as it depends on the size of the numbers to generate ie o(n) time complexity where n is max length of number.
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 3 years ago.
Improve this question
I am looking for an efficient/simplest way to find highest possible number between 2 numbers under 2 condition. The condition being that the numbers should be added or decremented as specified by a parameter and that every number after the preceding cannot be greater than adjacent numbers by more than one
Example
If given numbers 1 and 5 with a = 7, (1) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 6 -> (5). Every number is not greater by its adjacent number by more than a factor of 1. Given the range 1 and 5 and with count = 7, the highest possible number should be 7. I apologize if the question isn't explained properly.
I am looking for something mathematically or in java
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 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 9 years ago.
Improve this question
A friend of mine writes poems and he has a very special rhythmic schema behind it. All his poems have 4 staves and each stave has 4 lines. So the rhyme schema is now this:
1
2
3
4
2
5
4
6
5
7
6
8
7
1
8
3
He asked me how many permutations there are and if I could calculate them all, but I don't really know where to start except brute force which, i guess, is not the optimal solution.
(Programming language preferably java(script)/pseudo)
Cheers, Daniel
As each rhyme type occurs exactly twice and 2! = 2 you have 2^8 = 256 possibilities assuming the lines are unequal
As there are 8 kinds of line 1 2 3 4 5 6 7 8
number of kinds the 1st stave can be is 8*7*6*5 = 1680
2nd stave can be 1*4*1*3 = 12
3rd stave can be 1*2*1*1 = 2
and 4th save can be 1*1*1*1 = 1
So the total number of possibility is 1680*12*2*1 = 40320
Consider what it takes to create a poem-permutation, paying close attention to the choices we have to make along the way. First, we make 8 categories, with two rhyming lines (or words) in each:
rhymes = {
'A': ['fade', 'made'],
'B': ['cow', 'how'],
'C': ['can', 'fan'],
'D': ['a', 'hey'],
'E': ['answer', 'hampster'],
'F': ['whiz', 'is'],
'G': ['smut', 'what'],
'H': ['key', 'we'],
}
To create a poem, we need to pick an ordering of the categories. Given the rhyming scheme [1,2,3,4,2,5,4,6,5,7,6,8,7,1,8,3], that could be ABCDBEDFEGFHGAHC. But it could equally well be HGFEGDECDBCABHAF. There are many possible orderings for the categories which fit your rhyming scheme. In total there are 8! = 8*7*6*5*4*3*2*1 = 40320 orderings for the categories. In combinatorics this is called the number of permutations of 8 items.
Now once we have an ordering, such as ABCDBEDFEGFHGAHC, we can construct a poem by selecting 1 of the 2 possible items from category A, then 1 of the 2 possible items from category B, and so on.
How many ways are there to do this? Well there are 2^8 = 256 ways to make 8 independent binary choices. Even though there are 16 lines, after you make the first 8 choices, the rest of the "choices" are forced since there is only one choice left for each category.
So in total there are
8! * 2**8 = 40320 * 256 = 10321920
or a little over 10 million poem-permutations possible.
In Python, which is somewhat close to pseudocode, you could enumerate the poems like this:
import itertools as IT
rhymes = [
['fade', 'made'],
['cow', 'how'],
['can', 'fan'],
['a', 'hey'],
['answer', 'hampster'],
['whiz', 'is'],
['smut', 'what'],
['key', 'we'],
]
scheme = [1,2,3,4,2,5,4,6,5,7,6,8,7,1,8,3]
# shift by 1 since Python uses 0-based indexing
scheme = [i-1 for i in scheme]
# 40320 itmes in orderings
orderings = IT.permutations(rhymes)
count = 0
for ordering in orderings:
# 256 ways to select the lines given an ordering
for lines in IT.product(*[IT.permutations(line)
for line in ordering]):
lines = map(iter, lines)
for i in scheme:
print(next(lines[i]))
count += 1
print
print(count)
which yields, for example,
fade
cow
can
a
how
answer
hey
whiz
hampster
smut
is
key
what
made
we
fan