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.
Answers were Sorted based on User's Feedback
Answer / argho
function desc
begin
Ineger i;
Array a[n];
x=1;
y=n;
1. for k=1 , loop from x to y while array[n] / 2;
temp[a]= a[n]/2
if temp[a] < i
call step 1 with argument x=1 y=n/2
else if temp[a] > i
call step 1 with argument x=(n/2)+1 y=n
else
return position
end
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / 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 |
Answer / sonu
int array[]={13,232,234,12,34,56,73,21,230,240};
int max ;
max = 230;
for (int i=1;i<array.length;i++){
if (array[i]==max ){
System.out.println
("Position of no"+max +"in array is "+i);
}else {
System.out.println("-1");
}
}
Is This Answer Correct ? | 2 Yes | 12 No |
Does anyone know some strategies for the free android game Grand Battle? Need you help!! I really love the game Grand Battle, and I fell excited when I defeat the enemies and protect my base as a commander. Now I think I need some strategies to improve my performance and to lead a strong army, could anyone help me? Thank you so much!
what guarantee can you give that you won`t leave our organization in 1 year?
as a secretary, how do asset as a good secretary?
i want technical interview question and answer for hpux 11i system administration
(8) What have you learned from your mistakes
hi friends, i completed MCA and searching for software jobs. but i cleared SBI clerical written and called for interview. at this time what is better? joining as clerk in SBI or to wait for some time and getting a job as a s/w engg ? please suggest me. will there be career growth in SBI?
Your passion for helping people has really paid off though your nonprofit organization. What fuels that passion to help?
hello freinds, i have done M.C.A this year. can anybody tells me what is the diffrence between on role and off role jobs in a telecome company & what is the salary diff. of these roles. are software companies also hire employees on onrole & off role ???????????/
why do you the aviation?
hi frds i have done btech i have 70% in degree 10th and 12th but iam 2013 passout but still no job i have done testing course but iam not getting interview calls wat i can do now to get job in IT?
Hiii can anybody tell me which books are best preferable for the preparation of Group1 and Group2 prelims. Also the price of the books. Please post ur ansrs 2 this question
Its So Simple Punchline of Which Co.