U have an array of +ve integers arranged in the descending
order . Write a functionb thast take an integer & an integer
array as an integer and returns the position of the given
integer in the array , if the integer is not present in the
array it should return -1 . the serching technique that u
use should be very efficient both in terms of time & memory.
Answer Posted / siddarth pillai
public class Test
{
public static void main(String args[])
{
int[] arr = {99,56,31,22,17,11,9,2};
int no=11;
int position = checkPos(arr,no);
if(position == -1)
System.out.println("No such number exists in the array.");
else
System.out.println("Number "+no+" is at position "+position);
}
public static int checkPos(int[] arr,int no)
{
int first = 0;
int last = arr.length;
int mid = (first+last)/2;
int LoopCount = 0;
int MaxLoopCount = arr.length/2;
System.out.println("first : "+first+" last : "+last+" mid : "+mid);
while((arr[mid]!=no) && (LoopCount<MaxLoopCount))
{
if(no>arr[mid])
{
first = 0;
last = mid;
mid =(first+last)/2;
System.out.println("first : "+first+" last : "+last+" mid : "+mid);
}
else if(no<arr[mid])
{
last=arr.length;
first = mid;
mid =(first+last)/2;
System.out.println("first : "+first+" last : "+last+" mid : "+mid);
}
LoopCount++;
}
if(no == arr[mid])
return mid;
else
return -1;
}
}
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
what were the causes of swadeshi movement?
the question regarding ROI calculation is not answered properly based on Indian standards .. need a clear cut explanation with a proper example ?
Hey I like to play online games .I damn crazy about it.My favorite one is sudoku and I have good steps to solve sudoku which I got from http://tictacdo.com/ttd/Solve-a-Sudoku Tell me some good online games list and some steps to solve that..
Hi friends, did any one has recieved joining letter from karnataka bank for the position of clerk. contact me @ cvimach@yahoo.com, Regards_ soumyakanta
what is the difference between file base architecture and web based architecture in datastage?
what are the leadership qualities?
I'M PURSUING MY SECOND YEAR B.TECH .I HAVE GOT SELECTED FOR THE SBI CLERICAL POST AND I'M GOING TO ATTEND THE PERSONNEL INTERVIEW.DO YOU THINK IT IS ADVISABLE TO TAKE UP THE JOB OR CONTINUE MY STUDIES OR THERE IS ANY OTHER ALTERNATIVES.HELP ME OUT PLEASEEEEEEEEEEEEEEEEEE
Pls send me the sbi bank recruitment question papers from last 5 years
Why do you want to be a Cabin Crew?
i am getting different answers as far as the height of the " sri gomateshwara " statue , at shravanabelagola. i had read long back during my school days , as 57ft. but i am seeing now as 55', 3 mts etc. which is correct. pl guide me?
I received a phone call about my verizon yahoo account. The message that was left I did not understand. Could you please tell me what is going to change with my yahoo account. It was something about switching to webmail. It said to go to verizon.com/test. That did not help me. Please responed because it said they are swithing on the 27th of April.
Can you explain a difficult situation in your job and how have to solved it . ( for a risk reporting analyst position) Hopefully answer should be about data.
agar pyar ki nishani tajmahal h todosti ki nishani kya hogi
as i have two rejections on my passport is this affect my approval for visa and whether they will ask me any questions related to past interview done plz inform me soon as my interview is on 20 july
hello, how it is possible as you replied my id i was selected the indian bank clerical exam!!!!!!!!! hmmm. ok if i selected means, send me the result link website.. i will search and tell to thanks for you.. ok