well you are using the in-built function, which may be using the if-else loop...so i guess the purpose is not served completely...but it might be the case that we have no other way :)
We create an array with of the size of first number. In a try/catch block we access the element with the index of the second number. If the second nr. is smaller then no exception is raised; meaning the index was inside of the bounds. On the countrary if an exception is raised then the second nr was greater, thus an exception is raised.
Works only for positive values of m, n:
public static void compare(int m, int n) { int[] A = new int[m];
try { A[n-1] = 0; // n is smaller then m // nothing is returned } catch (ArgumentOutOfBounds) { // exception raised // n is greater then m }
@greatI indeed its good effort but still for checking ArgumentOutOfBounds java must be using comparison operator.And if you will think in terms of space for comparing two numbers you are creating space of the order (either of two numbers)
@all i have come up with a solution suggested by my friend.. int large(int x,int y) { return x-((x-y)&((x-y)>>sizeof(int)-1)); } i.e for any two value x and y if x-y is +ve msb will be 0 and 1 in other case.
Implement a function getbits, that returns the(right adjusted) n bits that begin at position p of an integer. Assume bit position 0 is at the right end and that n and p are sensible positive values.
You are given n real numbers in an array. A number in the array is called a decimal dominant if it occurs more than n/10 times in the array. Give an O(n) time algorithm to determine if the given array has a decimal dominant.
int greater( int a,int b )
ReplyDelete{
int r = ( (b/a)*a+(a/b)*b )/( (b/a)+(a/b) );
r = ( a+b )-r;
return r;
}
there is a limitation with the above code that it works for positive integers only :(
@jainendra your code works fine for positive numbers but it is taking in account only magnitude so it is not working for negative numbers...
ReplyDeleteint greater( int a,int b )
{
int r = ((a+b)+abs(a-b))/2;
return r;
}
well you are using the in-built function, which may be using the if-else loop...so i guess the purpose is not served completely...but it might be the case that we have no other way :)
ReplyDelete@jainendra may be you can try it some other way but you are right abs() may be using some conditional operator internally...
ReplyDeleteWe create an array with of the size of first number. In a try/catch block we access the element with the index of the second number. If the second nr. is smaller then no exception is raised; meaning the index was inside of the bounds. On the countrary if an exception is raised then the second nr was greater, thus an exception is raised.
ReplyDeleteWorks only for positive values of m, n:
public static void compare(int m, int n) {
int[] A = new int[m];
try {
A[n-1] = 0;
// n is smaller then m
// nothing is returned
}
catch (ArgumentOutOfBounds)
{
// exception raised
// n is greater then m
}
}
@greatI indeed its good effort but still for checking ArgumentOutOfBounds java must be using comparison operator.And if you will think in terms of space for comparing two numbers you are creating space of the order (either of two numbers)
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteint min a= (j + ((i-j)&(i<j)))
ReplyDeleteint max a= (j - ((i-j)&(i<j)))
compare operator is serving the different purpose as askd in qs..but i will not giv any point to myself for this :) !
@softy32 but still you are not allowed to use this operator..:)
ReplyDeleteyeah ...agreed :)!
ReplyDelete@all i have come up with a solution suggested by my friend..
ReplyDeleteint large(int x,int y)
{
return x-((x-y)&((x-y)>>sizeof(int)-1));
}
i.e for any two value x and y if x-y is +ve msb will be 0 and 1 in other case.