Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

what were the causes of swadeshi movement?

4616


the question regarding ROI calculation is not answered properly based on Indian standards .. need a clear cut explanation with a proper example ?

1982


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

2240


Hi friends, did any one has recieved joining letter from karnataka bank for the position of clerk. contact me @ cvimach@yahoo.com, Regards_ soumyakanta

2725


what is the difference between file base architecture and web based architecture in datastage?

1941


what are the leadership qualities?

2238


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

1935


Pls send me the sbi bank recruitment question papers from last 5 years

2080


Why do you want to be a Cabin Crew?

1724


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?

1949


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.

1678


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.

1737


agar pyar ki nishani tajmahal h todosti ki nishani kya hogi

3144


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

1933


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

1840