I am new learner for Java 8 lambda .
I found a statement in a book (Java 8 for Really Impatient ), saying ,
"It is illegal for a lambda expression to return a value in some branches but not in others. For example, (int x) -> { if (x >= 0) return 1; } is invalid."
Can anyone explain this ? Please provide some examples.
Thanks in advance.
I don't get it where you don't get it. Will this compile for example?
static int test(int x) {
if(x >= 0) {
return 1;
}
}
Same goes for the lambda expression.
you have to ensure no matter "if" statement is true or false, there is always a return value or there is always not.
In your case:
illegal: (int x) -> { if (x >= 0) return 1; }
legal: (int x) -> { if (x >= 0) return 1; else return 0;}
This is because this method below is illegal
int lambda (int x){
if (x >= 0)
return 1;
}
Related
here x can be negative
i am not able to understand that why we have written d+x in if(x<0) condition and why we have taken modulo ans%d at last since we have already taken modulo with d while finding ans inside if-else condition
public class Solution {
public int pow(int x, int n, int d) {
long ans;
if(x==0) return 0;
if(n==0) return 1;
if(x<0) return pow(d+x,n,d);
long temp = pow(x,n/2,d);
if(n%2==0)
ans = ((temp%d)*(temp%d))%d;
else
ans = ((((x%d)*(temp%d))%d)*(temp%d))%d;
return (int)ans%d;
}
}
From definition of modular exponentiation,
c = xn % d where 0 <= c < d
When x < 0, the answer returned can be negative. So by changing x to x+d,
if(x<0) return pow(d+x,n,d);
we are trying to avoid negative answer as solution.
At the last you don't need to perform modulo again,
(int)ans;
However, you can altogether ignore the x < 0 case by changing last line to ,
return (int)(ans+d)%d
Code,
public class Solution {
public int pow(int x, int n, int d) {
long ans;
if(x==0) return 0;
if(n==0) return 1;
long temp = pow(x,n/2,d);
if(n%2==0)
ans = ((temp%d)*(temp%d))%d;
else
ans = ((((x%d)*(temp%d))%d)*(temp%d))%d;
return (int)(ans+d)%d;
}
}
bool binsearch(int x) {
int i = 0, j = N;
while(i < j) {
int m = (i+j)/2;
if(arr[m] <= x) {
if(arr[m] == x)
return true;
i = m+1;
}
else {
j = m;
}
}
return false;
}
This is my implementation of binary search which returns true if x is in arr[0:N-1] or
returns false if x is not in arr[0:N-1].
And I'm wondering how can I figure out right loop invariant to prove this implementation is correct.
How can I solve this problem?
Thanks a lot :D
Think about the variables holding state within your loop. In your case, they are variables i and j. You start with the fact that all elements < i and less than the value you are searching for (x) and all elements > j and greater than the x. This is the invariant you are trying to maintain.
I've come up with a working code for the above problem, for both positive and negative values of x. And the answers for the majority of situations is correct, however the code fails at a corner case and I can't seem to find what the issue is! What condition am I missing:`
int pow(int x, int n, int d) {
int i=0,rem=1;
long long l;
if(x==0)
{
return 0;
}
if(x==1)
{
return 1;
}
if(n==0)
{
return 1;
}
x=x%d;
if(n%2==0||n==0)
{
l = ((pow(x,n/2,d))*(pow((x),n/2,d)))%d;
}
else
{
l = ((x)*(pow(x,(n-1)/2,d))*(pow((x),(n-1)/2,d)))%d;
}
if(x<0 && n%2!=0)
{
return d+l;
}
else
{
return l;
}
}
`
The case at which the code gives wrong ans:
A: 71045970
B : 41535484
C : 64735492
My Output: 12942068
Actual Output:20805472
Here is the simplified version of yours.
int pow(int x, int n, int d) {
long long l;
if(n==0)
{
return 1;
}
if(n%2==0)
{
l = (long long)pow(x,n/2,d);
return (int)((l * l) % (long long)d);
}
else
{
//check negative value of x, and make it positive, so that negative values never come in result
if (x < 0) {
x += d;
}
return (int)(((long long)x * (long long)pow(x,n-1,d)) % (long long)d);
}
}
I think your code is logically correct but there exists a data overflow problem on int data type if the modulo d value is large enough.
eg. (pow(x,n/2,d))*(pow((x),n/2,d)) here two big int value multiplication can result in datatype overflow.
I am implementing binary search algorithm , but i am facing return statement issue.Here is my method binarySearch() implementation
public static int binarySearch(int[] a, int n, int x) {
int start = 0;
int end = n - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (a[mid] == x) {
return mid;
} else if (a[mid] < x) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}
Any help would be appreciated.
Thanks
It is possible that this method is called on an array that does not have the value.
You can
return 0;
at the end of the method call. Or...
throw new RuntimeException("Value not found in array");
Whichever is right for this code.
Your function does not return all the paths.
public static int binarySearch(int[] a, int n, int x) {
int start = 0;
int end = n - 1;
int result = 0; // Or something you define yourself for not found case
while (start <= end) {
int mid = (start + end) / 2;
if (a[mid] == x) {
result = mid;
} else if (a[mid] < x) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return result;
}
This is what I have got but it doesn't work. When I try to compile I get this error message:
int result = 0;
^^^^^^^^^^^^^^^
Unreachable code
My code:
public int sumOfOddIntegers (int n) {
if(n < 1);
return 0;
int result = 0;
for(int i = n - 1; i > 0; i--)
{
if(i % 2 != 0) {
result = result + i;
}
}
return result;
}
if(n < 1);
return 0;
is equivalent to :
if(n < 1) {
}
return 0;
It shoud be replaced by :
if(n < 1)
return 0;
or (the right way)
if(n < 1) {
return 0;
}
The statement:
if(n < 1);
Is a no op because of the semi-colon. The comparison is evaluated, and nothing is done, whatever the result of the comparison is.
Then, the next line is executed, which returns 0.
if(n < 1); is your problem. The rest of the code is unreachable beacuse the following return' is always exectued.
Remove the ; after if(n < 1).
As others have said, the semi colon on your if statement is the problem. Personally however I would just do it like this:
public int sumOfOddIntegers (int n)
{
int result = 0;
if(n < 1)
return result;
for(int i = 1; i <= n; i += 2)
{
result += i;
}
return result;
}
This way you can halve the number of iterations. We know every other number is odd, so why even bother iterating the even ones and checking if they're odd when we know they're not?
the sequence is a arithmetic progression with common difference of 2.
so its sum would be given by formula :
sum = n/2(2a+(n-1)d
where n = Math.ceil(k); where k is the given number.
and d = 2, a=1
public int sumOfOddIntegers (int n) {
if(n < 1);
return 0;
int totalNumber = Math.ceil(n/2);
return (totalNumber/2)*(2 + (totalNumber-1)*2);
`