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
how can i improve my personal development..what should i do daily...and also individual skills..?
valuation of share is it base of the company reputation
The ration of stone,sand and cement in a concrete mixture is 6;3;1.If you have 20m^3 of concrete,what is the volume of;sand,stone and cement?
why you want to join LIC as DSE?
Can you please tell me the requirements to start a bank
hi, im working as a domestic helper in malaysia and i want to ask what are the documents needed if i want to apply for a visit visa to australia alone without my employer.is there a big chance for me to get a visa. hope you can help me.
bubble sort source code in turbo c++
generally asked question in bpo backoffice
What about our position do you find the most and least attractive?
hi friends hey i need a help. i m doin my 6thsem(B.E) i need to do a project on opengl(graphics). so can u please suggest me some good topics for my project.M NOT GETTIN ANY IDEA OF WAT TO DO. PLEASE INFORM ME as soon as possible. it shud be simple and innovative.
How can i talk by using Pc/Laptop to our frnds? please tell the steps in details.
HI EVERY1, PLZ HELP.......HAS THE INTERVIEW DATE CHANGED FROM 17 SEPTEMBER TO 22 SEPTEMBER FOR PO INTERVIEWS OF SYNDICATE BANK........I GOT THE PHONECALL REGARDING THIS FROM BANK...PLZ INFORM........
iam getting offers for back door process .form the comapanies medhasolutions,lanticsolutions,precessglobal.inc(banglore) cybate commitment 1 lakh salary 1.44per annum mind tech soft commitment 90000rs salary 1.44per annum profilent commitment 90000rs salary 1.80per annum config commitment 70000rs salary 1.20per annum v one infotech commitment 1 lakh salary 1.44per annum finch commitment 90000rs salary 1.44per annum bin soft tech commitment 1.20 lakh salary 2.40per annum impact commitment 1 lakh salary 2.44per annum jkl consol commitment 1 lakh salary 1.8 - 2.4per annum oligoi solutions commitment 1lakh 2.64 per annum plz tell me which one to beleve if any one know details plz send me msg @9885090595
Why do you want to join Genpact?
how to get c & f agengy licence or ragistration ??