The below method return 5 if you give n = 20.
My question is how is 1 incremented on each iteration?
mystery(10) + 1
= mystery(5) + 2
= mystery(2) + 3
= mystery(1) + 4
= mystery(0) + 5 = 5.
I am having some hard time with recursion.
public static int mystery(int n){
if(n <= 0){
return 0;
}else{
return mystery(n / 2 ) + 1;
}
}
mystery(20) = mystery(10) + 1
mystery(20) = (mystery(5) + 1) + 1
mystery(20) = ((mystery(2) + 1) + 1) + 1
mystery(20) = (((mystery(1) + 1) + 1) + 1) + 1
mystery(20) = ((((mystery(0) + 1) + 1) + 1) + 1) + 1
and we know that mystery(0) = 0.
mystery(20) = ((((0 + 1) + 1) + 1) + 1) + 1
mystery(20) = (((1 + 1) + 1) + 1) + 1
mystery(20) = ((2 + 1) + 1) + 1
mystery(20) = (3 + 1) + 1
mystery(20) = 4 + 1
mystery(20) = 5
Or, simply put, we get 1+1+1+1+1=5
Pretty good video on recursion here: https://www.youtube.com/watch?v=Mv9NEXX1VHc
Looking at the code should make it obvious that:
mystery(20) = mystery(10) + 1
mystery(10) = mystery(5) + 1
mystery(5) = mystery(2) + 1
mystery(2) = mystery(1) + 1
mystery(1) = mystery(0) + 1
mystery(0) = 0
Now go back and plug in all the values, e.g.
mystery(1) = mystery(0) + 1 = 0 + 1 = 1
mystery(2) = mystery(1) + 1 = 1 + 1 = 2
mystery(5) = mystery(2) + 1 = 2 + 1 = 3, etc.
Every time mystery() is called, it returns the value returned by calling itself, plus 1. So, for every call, the returned number gets incremented by 1.
Related
Here I got a function like this and I want to find the recursive relation of that and after that calculate time complexity of that recursive relation.
public static void f(int n) {
if (n == 1) {
//" Do sth "
} else {
for (int i = 1; i < n; i++) {
f(i - 1);
//" Do sth "
}
}
}
actually I tried a lot for that and I got T(n) = n * f( n-1) for this function as the relation but I am not sure about that . could you help me find the correct relation and solve it ?
Assuming T(1) = "Do sth" is constant work i.e. it doesn't depend on the input size n, you could write the recursive time function as :
T(n) = T(1) + T(2) + ... + T(n-1)
= { T(1) } + { T(1) } + { T(1) + T(2) } + { T(1) + T(2) + T(3) } + { T(1) + T(2) + T(3) + T(4) } +....
[let T(1) = x]
= x + x + {x + x} + {x + x + (x + x)} + {x + x + (x + x) + x + x + (x + x)} +....
= x + x + 2x + 4x + 8x + ...
~ x.2^(n-2)
~ O(2^n)
Here is a python program to demonstrate the sequence of coefficients for the summation:
t = [0 for i in range(10)]
for i in range(1,10):
if i == 1:
t[i] = 1
else:
val = 0
for j in range(1,i):
val += t[j]
t[i] = val
print(t[1:])
prints : [1, 1, 2, 4, 8, 16, 32, 64, 128]
You can see that 2(n-2) for n >= 2 holds good at each 'n' and complexity is O(2n)
Given a number between 1 and 30, find all three-number combinations that sum up to this number and return the amount of combinations without using loops.
for example, given 5, print
1 + 1 + 3
1 + 2 + 2
1 + 3 + 1
2 + 1 + 2
2 + 2 + 1
3 + 1 + 1
This is what I have right now, using Java.
private static int numbers(int num, int num1, int num2, int num3){
boolean valid_solution = num1+num2+num3 == num;
int counter = 0;
if (valid_solution){
System.out.println(num1+" + "+num2+" + "+num3);
counter++;
}
if (num1>10 || num2>10 || num3>10 || num1+num2+num3>num){
return counter;
}
counter += numbers(num, num1 + 1, num2, num3)+numbers(num, num1, num2 + 1, num3)+numbers(num, num1, num2, num3 + 1);
return counter;
}
public static int solutions(int num){
if (num < 0 || num > 30) return 0;
return numbers(num, 1, 1, 1);
}
I seem to get duplicates, for example for 5-
3 + 1 + 1
2 + 2 + 1
2 + 1 + 2
2 + 2 + 1
1 + 3 + 1
1 + 2 + 2
2 + 1 + 2
1 + 2 + 2
1 + 1 + 3
edit - I'm also not allowed to use global variables.
You can see why the duplicates are happening if you add a little logging
1:1:1
2:1:1
3:1:1
3 + 1 + 1
4:1:1
3:2:1
3:1:2
2:2:1
2 + 2 + 1
3:2:1
2:3:1
2:2:2
2:1:2
2 + 1 + 2
3:1:2
2:2:2
2:1:3
1:2:1
2:2:1
2 + 2 + 1
3:2:1
2:3:1
2:2:2
1:3:1
1 + 3 + 1
2:3:1
1:4:1
1:3:2
1:2:2
1 + 2 + 2
2:2:2
1:3:2
1:2:3
1:1:2
2:1:2
2 + 1 + 2
3:1:2
2:2:2
2:1:3
1:2:2
1 + 2 + 2
2:2:2
1:3:2
1:2:3
1:1:3
1 + 1 + 3
2:1:3
1:2:3
1:1:4
counter:9
so since you are making recursive calls on incrementing numbers, when you're recursively calling num2+1, you want to make sure that num1 is less than or equal to num2 in order to avoid duplicates
I have a suspicions that this code of mine is far from being a good solution, but who knows, may be it will be helpful for you in some way.
public class FindSumCombinations {
static int start = 5;
static int comb1 = 0;
static int comb2 = 0;
public static void main(String[] args) {
comb1(start);
}
public static int comb1(int x){
if(x == 0) return 0;
comb1 = x;
comb2(x);
return comb1(x-1);
}
public static int comb2(int x){
if(x == 0) return 0;
comb2 = x;
comb3(x);
return comb2(x-1);
}
public static int comb3(int x){
if(x == 0) return 0;
if(x + comb2 + comb1 == start){
System.out.println(comb1 + "+" + comb2 + "+" + x);
System.out.println(x + "+" + comb1 + "+" + comb2);
System.out.println(comb2 + "+" + x + "+" + comb1);
}
return comb3(x-1);
}
}
I am totally lost why I get these results:
int i = 1;
int[] a = new int[6];
a[0] = 0;
a[1] = 1;
a[2] = 2;
a[3] = 3;
a[4] = 4;
a[5] = 5;
i += i + a[i++] + a[i++];
//i is 5
i = 1;
i += i + a[i++] + a[i++] + a[i++];
// i is 8
I (wrongly) thought that there are these options:
i = i(=1) + a[i++] + etc - meaning that i = 1 is cached and not
changed then. Expression evaluation order is exactly from left to
right, I guess (?!).
i is increased, which results (for first example) in i = i(=3) + a[1] + a[2] now i = 3 and written to leftmost i
value from rightmost a[THIS_VALUE] is substituted into leftmost i, and last increment is never made.
But actual results leave me with no clue...
i += i + a[i++] + a[i++];
adds the original value of i to the value of the expression i + a[i++] + a[i++] and assigns the result to i.
It's equivalent to
i = i + i + a[i++] + a[i++];
1 + 1 + a[1] + a[2] = 1 + 1 + 1 + 2 = 5
Then you assign 1 to i and calculate:
i += i + a[i++] + a[i++] + a[i++];
which is equivalent to
i = i + i + a[i++] + a[i++] + a[i++];
1 + 1 + a[1] + a[2] + a[3] = 1 + 1 + 1 + 2 + 3 = 8
The important thing to note is that each a[i++] increments i, but accesses the element of a at the index of the previous value of i (i.e. the value prior to the increment).
Therefore the first a[i++] returns a[1] (even though i is incremented to 2), the second a[i++] returns a[2] (even though i is incremented to 3) and the third a[i++] returns a[3] (even though i is incremented to 4).
This important to notice differences between i++ and ++i, when you use i++ it use i value to calculate the operation and then increment i, but when you are using ++i it increment i and the calculate the operation.
it also worth mentioning that i+=X is equal to i = i + X.
So
i += i + a[i++] + a[i++];
is equal to
i = i + i + a[i++] + a[i++]
for i = 1 it is
i = 1 + 1 + a[1] + a[2]
which with your initialisation it will be :
i = 1 + 1 + 1 + 2
and thats how you get i = 5. and same for next line.
Its quite easy acutally, your using post increment. Post increment increments the value after the expression was executed, versus pre which increments before the expression is executed. So i += i + a[i++] + a[i++] + a[i++];, is 1 += 1 + 1 + 2 + 3, the first access, uses 1, then increment, next access uses two then increment then last access uses three.
int count = 0, result = 0;
while(count <= 10)
{
if(count % 2 ==0)
{
result = result + count;
}
else
{
result = 0;
}
count = count + 2;
}
System.out.println("Result: " + result);
Can someone explain why the answer is 30?
count starts at 0 and number 2 is added to it at each iteration. The loop stops when count is equal or higher than 10. Therefore the values of count will be :
0, 2, 4, 6, 8, 10
These are all even numbers so count % 2 == 0 will be true.
result starts at 0 and at each iteration count is added to it. Therefore the final result will be the sum of all the above numbers. And
0 + 2 + 4 + 6 + 8 + 10 = 30
To start with, count is 0. 0 % 2 == 0, so the if block is executed (and not the else block). result + count evaluates to 0, so result is set to 0. Repeat with 2 (count + 2) and this is added to result, as 2 % 2 == 0.
For every value of count, count % 2 == 0 evaluates to true. This is because you are incrementing it by 2 each time, so it will always remain even. Because of this, the else block is completely redundant and should be removed. Anyhow, what you are doing is summing all even numbers up to 10 (inclusive, as you use <=). This is 2 + 4 + 6 + 8 + 10 = 30, which is printed.
Side note: assignments which look like this (where v is a variable , o is an operator and e is an expression) may be simplified:
v = v o e;
is essentially (can anyone link me that question explaining the difference involving casting?) equivalent to
v o= e;
for arithmetic operators.
I just added two lines of code into yours and the code is now self-explaining.
int count = 0, result = 0;
while(count <= 10)
{
if(count % 2 ==0)
{
result = result + count;
System.out.println("count%2 " + count%2 + " and Count is " + count + " and Result is " + result);
}
else
{
result = 0;
System.out.println("count%2 " + count%2 + " Count is " + count + " and Result is " + result);
}
count = count + 2;
}
System.out.println("Result: " + result);
and it prints to the console following output
count%2 0 and Count is 0 and Result is 0
count%2 0 and Count is 2 and Result is 2
count%2 0 and Count is 4 and Result is 6
count%2 0 and Count is 6 and Result is 12
count%2 0 and Count is 8 and Result is 20
count%2 0 and Count is 10 and Result is 30
Result: 30
I hope this helps.
It simply increments result by every even number from zero to 10. And by adding those numbers together, you get, well, 30.
Even numbers between 0 and 10 include:
0, 2, 4, 6, 8, 10
by adding them all up, you get:
0 + 2 + 4 + 8 + 10 = 30
You can even prove this by keeping track of result as you go along. See below.
int count = 0, result = 0;
do {
if (count % 2 == 0) {
System.out.println("Result went from " + result + " to " + (result += count));
continue;
}
result = 0;
System.out.println("Result is now " + 0);
} while ((count += 2) <= 10);
System.out.println("Result: " + result);
Mathematical Expression
1 + + 1 results 2
1 + - 1 returns 0
1 + - + 1 returns 0
can anybody know the reason of this. because I only know ++ -- operation but in this case the operator is '+ +' and still not giving an error.
Its because + is also a unary operator which means positive, just like - means negative.
1 + + 1 = 1 + (+1) = 1 + 1 = 2
1 + - 1 = 1 + (-1) = 1 - 1 = 0
1 + - + 1 = 1 + -(+1) = 1 + -1 = 1 - 1 = 0;
Unary + and - operators at work here.
1 + (+1) = 2
1 + (-1) = 0
1 + (-(+1)) = 0
JLS ยง15.15.3