Max value
PROBLEM :
In a given array A find the maximum value of |Ai – i| – |Aj – j| where i is not equal to j.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.The second line of each test case contains N space separated integers denoting array elements.
Output:
Corresponding to each test case, in a new line, print the maximum value.
Constraints:
1 = T = 100
1 = N = 200
1 = A[i] = 1000
Example:
Input
1
5
9 15 4 12 13
Output
12
Explanation
a[1]-1 - a[2]-2 = (15-1)-(4-2) = 12
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<limits.h>
int Max_value(int [],int ) ;
int main()
{
int t,no,arr[200],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
no=Max_value(arr,no) ;
cout<<no<<endl ;
}
return 0;
}
int Max_value(int arr[],int no)
{
int i,min,max ;
for(i=0;i<no;i++)
arr[i]=arr[i]-i ;
max=INT_MIN ;
min=INT_MAX ;
for(i=0;i<no;i++)
{
if(arr[i]<min)
min=arr[i] ;
if(arr[i]>max)
max=arr[i] ;
}
return max-min ;
}
---------------------------------------------------------------------------------
In a given array A find the maximum value of |Ai – i| – |Aj – j| where i is not equal to j.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.The second line of each test case contains N space separated integers denoting array elements.
Output:
Corresponding to each test case, in a new line, print the maximum value.
Constraints:
1 = T = 100
1 = N = 200
1 = A[i] = 1000
Example:
Input
1
5
9 15 4 12 13
Output
12
Explanation
a[1]-1 - a[2]-2 = (15-1)-(4-2) = 12
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<limits.h>
int Max_value(int [],int ) ;
int main()
{
int t,no,arr[200],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
no=Max_value(arr,no) ;
cout<<no<<endl ;
}
return 0;
}
int Max_value(int arr[],int no)
{
int i,min,max ;
for(i=0;i<no;i++)
arr[i]=arr[i]-i ;
max=INT_MIN ;
min=INT_MAX ;
for(i=0;i<no;i++)
{
if(arr[i]<min)
min=arr[i] ;
if(arr[i]>max)
max=arr[i] ;
}
return max-min ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment