Find Maximum value
PROBLEM :
Given an array A[ ] your task is to complete the function max_val which finds the maximum value of abs(i – j) * min(arr[i], arr[j]) where i and j vary from 0 to n-1.
Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow. Each test case contains 2 lines. The First line of each test case contains an integer N denoting the size of the array. In the next line are N space separated values of the array A[ ] .
Output:
For each test case in a new line output will be the max values obtained.
Constraints:
1<=T<=100
1<=N<=100
-100<=A[]<=1000
Example(To be used only for expected output):
Input
2
4
3 2 1 4
4
8 1 9 4
Output
9
16
Explanation:
(i) For the first test case a[0] = 3 and a[3] = 4 and thus result is abs(0-3)*min(3,4) = 9.
(ii) For the second test ase a[0]=8 and a[2]=9 thus result is abs(0-2)*min(8,9) = 16.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*The function returns an integer
which denotes the max value
of abs(i – j) * min(arr[i], arr[j])
in the array.
*/
/*You are required to complete this method*/
int min_of(int ,int ) ;
int max_val(int arr[], int n)
{
int i,j ;
int curr ,final=INT_MIN ;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
curr=(abs(i-j))*min_of(arr[i],arr[j]) ;
if(curr>final)
final=curr ;
}
}
return final ;
}
int min_of(int a,int b)
{
return a<b?a:b ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Better solution O(n))
--------------------------------------------------------------------------------
/*The function returns an integer
which denotes the max value
of abs(i – j) * min(arr[i], arr[j])
in the array.
*/
/*You are required to complete this method*/
int max_of(int ,int ) ;
int max_val(int arr[], int n)
{
int i,j,curr,final ;
i=0 ;
j=n-1 ;
final=INT_MIN ;
while(i<j)
{
if(arr[i]<arr[j])
{
curr=abs(i-j)*arr[i] ;
i++ ;
}
else
{
curr=abs(i-j)*arr[j] ;
j-- ;
}
final=max_of(final,curr) ;
}
return final ;
}
int max_of(int a,int b)
{
return a>b?a:b ;
}
---------------------------------------------------------------------------------
Given an array A[ ] your task is to complete the function max_val which finds the maximum value of abs(i – j) * min(arr[i], arr[j]) where i and j vary from 0 to n-1.
Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow. Each test case contains 2 lines. The First line of each test case contains an integer N denoting the size of the array. In the next line are N space separated values of the array A[ ] .
Output:
For each test case in a new line output will be the max values obtained.
Constraints:
1<=T<=100
1<=N<=100
-100<=A[]<=1000
Example(To be used only for expected output):
Input
2
4
3 2 1 4
4
8 1 9 4
Output
9
16
Explanation:
(i) For the first test case a[0] = 3 and a[3] = 4 and thus result is abs(0-3)*min(3,4) = 9.
(ii) For the second test ase a[0]=8 and a[2]=9 thus result is abs(0-2)*min(8,9) = 16.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*The function returns an integer
which denotes the max value
of abs(i – j) * min(arr[i], arr[j])
in the array.
*/
/*You are required to complete this method*/
int min_of(int ,int ) ;
int max_val(int arr[], int n)
{
int i,j ;
int curr ,final=INT_MIN ;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
curr=(abs(i-j))*min_of(arr[i],arr[j]) ;
if(curr>final)
final=curr ;
}
}
return final ;
}
int min_of(int a,int b)
{
return a<b?a:b ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Better solution O(n))
--------------------------------------------------------------------------------
/*The function returns an integer
which denotes the max value
of abs(i – j) * min(arr[i], arr[j])
in the array.
*/
/*You are required to complete this method*/
int max_of(int ,int ) ;
int max_val(int arr[], int n)
{
int i,j,curr,final ;
i=0 ;
j=n-1 ;
final=INT_MIN ;
while(i<j)
{
if(arr[i]<arr[j])
{
curr=abs(i-j)*arr[i] ;
i++ ;
}
else
{
curr=abs(i-j)*arr[j] ;
j-- ;
}
final=max_of(final,curr) ;
}
return final ;
}
int max_of(int a,int b)
{
return a>b?a:b ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment