public static int mystery(int[] elements, int target)
{
for (int j = 0; j < elements.length; j++)
{
if (elements[j] == target)
{
return j;
}
}
return -1;
}
What would the following code return from mystery ([90, -30, 50], -30)?
input code:
output:
code:
public class Main
{
public static int mystery(int[] elements, int target)
{
/*take a loop*/
for (int j = 0; j < elements.length; j++)
{
/*check elements is there or not*/
if (elements[j] == target)
{
/*there than return index*/
return j;
}
}
/*else return -1*/
return -1;
}
public static void main(String[] args)
{
/*initialize array*/
int a[]={90, -30, 50};
/*call functiona*/
System.out.println(mystery (a,
-30));
}
}
Get Answers For Free
Most questions answered within 1 hours.