The amplitude of a non-empty zero-indexed array A consisting of N numbers is defined as
amplitude(A) = max { A[P] - A[Q] : 0 <= P, Q < N }
Write a function
int amplitude(int[] A);
that given a non-empty zero-indexed array A consisting of N non-negative integers returns its amplitude. Assume that the length of the array does not exceed 1,000,000. Assume that each element of the array is a non-negative integer not exceeding 5,000,000.
For example, given array A such that
A[0]=10, A[1]=2, A[2]=44, A[3]=15, A[4]=39, A[5]=20
the function should return 42.
*/
Java Code
class array
{
public static void main(String a[])
{
System.out.println(amplitude(new int[]{10, 2, 44, 15, 39, 20}));
}
static int amplitude ( int[] A )
{
// write your code here
int min=A[0];
int max=A[0];
int diff=0;
if(A==null)
return 0;
if(A.length==0)
return 0;
if(A.length>1000000)
{
return 0;
}
else
{
for(int i=0;i
if(A[i]
if(A[i]>max)
max=A[i];
diff=max-min;
}
}
return diff;
}
}
1 comment :
CAN YOU DO IT IN #? OR c+++???
Post a Comment