All combinations of two numbers [closed] - java

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.

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.

How to get highest possible number between 2 numbers under one condition - Java [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 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

Coding java program to calculate check digit numbers [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
I have this java project(using eclipse) that calculates check digit numbers. I'm stuck on how to code this one.
The check has a check digit so that it makes the sum of the sights including the check digit divisible by 7. Assume there are always 4 digits plus the check digit. The sample is 3875 with the number being 5 Second trial problem is 5862 and check number needs to be found. How do I go about doing this? I got to entering each digit and adding them but how can i do the rest?
This is for an into to computer science class so please no super complex stuff as if we didn't learn it I cant use it.
My teacher sucks by the way we learn none of this. I already did part a I need part b. Thanks. Here's an image to hell clarify.
First of all, you need to develop some "programmer logic", these problems help to develop it.
Airline tickets divide the actual identification number by 7 and assign the remainder to the check digit. Number can be of any length
Example:
12358 #3
Let's break this example:
12358 / 7 = 1765
and the reminder is 3
Let's do the same with the 2nd number on the example:
45349 / 7 = 45346
and the reminder is 3
So, your logic is correct.
An American Express traveler's check has a digit so that it maskes the sum of the digits, including the check digit, evenly divisible by 7.
Example:
3875 #5
In this problem the thing is a little different, you need to sum the digits:
3875 -> 3 + 8 + 7 + 5 = 23
Now you need to get the reminder of 23 / 7
23 / 7 = 3
And a reminder of 2
7 - 2 = 5
That's your checkDigit
5862
5862 -> 5 + 8 + 6 + 2 = 21
21 / 7 = 3
Reminder = 0
checkDigit = 7 - 0 = 7
So the formula is:
Split the number into digits
Sum the digits
Get the mod 7 of the sum
Make a rest of 7 - reminder

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!).

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.

Categories