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
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.
hi, i have work experience in Call centers, where can i get a job if i come out of call center.
My D.O.B is 13/06/1990. How to write date of birth in christian era?
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.
Sir..which one is better of the two? Auditor or Bank P.O.. Please favour me here..
who is the first blind man riding to the mount everest.
When will the result of Bank of baroda clerical 2010 Intervu be declared?
Hi I am looking for Hadoop Cloudera certification dumps. Please contact me if you some one have any idea about it. nzkhum786@gmail.com
Do IT ians welcome ECE students to software field?
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
why you have chosen to serve with this company as Deputy General Manager?
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.....
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.
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
what is the pharmacopoeial apllication of column, paper , thin layer chomatography?