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

Can someone please help me out with interview Questions for SBI management executive exam? Is it expected to be same as SBI PO Interview, or it is expected to be a higher level interview? Please guide. Thanks in advance.

1986


hi, i have work experience in Call centers, where can i get a job if i come out of call center.

1879


My D.O.B is 13/06/1990. How to write date of birth in christian era?

5029


I have a Nokia handset - yet when I try to delete the contacts on the handset it asks me for a security code - How can I rectify this problem.

1979


Sir..which one is better of the two? Auditor or Bank P.O.. Please favour me here..

5180


who is the first blind man riding to the mount everest.

1908


When will the result of Bank of baroda clerical 2010 Intervu be declared?

2005


Hi I am looking for Hadoop Cloudera certification dumps. Please contact me if you some one have any idea about it. nzkhum786@gmail.com

1819


Do IT ians welcome ECE students to software field?

2188


Hi friends I am sasi,Got selected for Syndicate Bank PO.I dont have any idea about Banking sector.I dont know how to prepare for the interview.Can anyone help me plz

2104


why you have chosen to serve with this company as Deputy General Manager?

1837


hai dis is lavanya... i want to know how to prepare for the toughest aptitude tests.. i know tat i have to read rs agarwal... but i find difficulty in facing the aptitude tests even after preparing from tat... i somehow want to get through the aps... PLS SUGGEST ME SOME METHODS AND THE MOST IMPORTANT TOPICS TO BE COVERED... i am ok with technical questions but my biggest hinderance is aps... pls help me frnds... HAPY FRNDSHP DAY.....

2219


I am BSc. Maths. I gave written test for SBI clerical postand I am selected for interview. I am about to join Times group as an executive with a salary of 18000. Some people are telling me to consider SBI job as it is secured. What should I do? Please help me.

1974


I m employee can i describe my daily routine? I can but my daily routine is same all the day its so boring. Can anyone help me? Is there any topic to describe my routine. Thank You

1430


what is the pharmacopoeial apllication of column, paper , thin layer chomatography?

2376