Find Maximum value | Practice | GeeksforGeeks

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. 
Example 1:
Input:
N = 4
arr[] = 3 2 1 4
Output: 9
Explanation:
Here, a[0] = 3 and a[3] = 4 
and thus result is  
abs(0-3)*min(3,4) = 9.
Example 2:
Input:
N= 4
arr[] = 8 1 9 4
Output: 16
Explanation:
Here, a[0]=8 and a[2]=9 thus 
result is abs(0-2)*min(8,9)=16. 
+1
ayerajkumar2 months ago
class Solution
{
    /*You are required to complete this method*/
    int max_val(int arr[], int n)
    {
	    int max=Integer.MIN_VALUE;
	    int i=0,j=n-1;
	    while(i<j){
	        max=Math.max(max, Math.abs(i-j)*Math.min(arr[i],arr[j]));
            if(arr[i]<arr[j])
                i++;
            else
                j--;
	        }
	    return max;
    }
}
0
guleriatarun672Premium3 months ago
// { Driver Code Starts
what is error?#include <bits/stdc++.h>using namespace std;
// } Driver Code Ends/*The function returns an integerwhich denotes the max value of abs(i â€“ j) * min(arr[i], arr[j]) in the array.*/
/*You are required to complete this method*/class Solution{   public:   int max_val(int arr[], int n)   {       //Your code here       int max = INT_MIN;      long int indexm = 0;       int max1 = INT_MIN;       long int indexm1 = 0;       for(int i = 0; i<n; i++)       {                      if(arr[i]>max){               max = arr[i];               indexm = i;                          }       }       for(int i = 0; i<n; i++)       {           if(arr[i]>max1 && arr[i]<max)           {               max1 = arr[i];               indexm1 = i;           }       }              long long int ans = abs((indexm1-indexm))*(max1);        return ans;                       }};
// { Driver Code Starts.
int main() {// your code goes hereint t;cin>>t;while(t--){ int n; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i];  Solution ob; cout<<ob.max_val(a,n)<<endl; }return 0;}  // } Driver Code Ends
0
shubhanshujhas196 months ago
Solution in Java
class Solution {   int max_val(int arr[], int n) {       int i = 0, j = n - 1, max = Integer.MIN_VALUE;       while(i < j) {           if (max < Math.abs(i - j) * Math.min(arr[i], arr[j]))               max = Math.abs(i - j) * Math.min(arr[i], arr[j]);           if (arr[i] < arr[j])               i++;           else               j--;       }       return max;   }}
0
ankitsinha1112007 months ago
class Solution
{
    public:
    int max_val(int arr[], int n)
    {
        int i=0,j=n-1,max1=INT_MIN;
        while(i<j)
        {
            if(max1<abs(i-j)*min(arr[i],arr[j]))
            max1=abs(i-j)*min(arr[i],arr[j]);
            if(arr[i]<arr[j])
            i++;
            else
            j--;
        }
        return max1;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.