
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find Maximum value of abs(i β j) * min(arr[i], arr[j]) in an array arr[] in C++
In this problem, we are given an array arr[] insisting of N integer values.Our task is to Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[].
Problem description − we need to find the maximum product value of the minimum value of two elements and the absolute differences between their indexes. i.e. for two values i and j, we need to maximise, abs(i - j) * min(arr[i] , arr[j]).
Input
arr[] = {5, 7, 3, 6, 4}
Output
16
Explanation
The maximum value is 16, between index 0 and 4 => abs(0 - 4)*min(arr[0], arr[4]) => 4*min(5, 4) => 4*4 = 16
Solution approach
A simple solution to the problem is using nested loops. we will take two loops and compute the value for each pair of i and j. And then return the maximum of all values found.
This approach is good but has a time complex of the order O(n2).
An effective solution to the problem is using two iterator values, from the start of the other from the end of the array. For each value of the iterators we will find the required value and compare them and store the maximum in maxVal variable. We will do this till both the iterators cross each other and at the end return the maxVal.
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; int calcMaxProdValue(int arr[], int n) { int maxVal = -100; int currentVal; int start = 0, end = n-1; while (start < end) { if (arr[start] < arr[end]) { currentVal = arr[start]*(end-start); start++; } else { currentVal = arr[end]*(end-start); end--; } maxVal = max(maxVal, currentVal); } return maxVal; } int main(){ int arr[] = {5, 7, 3, 6, 4}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"The maximum value of abs(i – j) * min(arr[i], arr[j]) in an array is "<<calcMaxProdValue(arr,n); return 0; }
Output
The maximum value of abs(i – j) * min(arr[i], arr[j]) in an array is 16
- Related Articles
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Maximum value of |arr[i] β arr[j] - + |i β j| in C++
- Maximize value of (arr[i] β i) β (arr[j] β j) in an array in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Maximize arr[j] β arr[i] + arr[l] β arr[k], such that i < j < k < l in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
- Rearrange an array such that βarr[j]β becomes βiβ if βarr[i]β is βjβ in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Maximum value of |arr[0] β arr[1] - + |arr[1] β arr[2] - + β¦ +|arr[n β 2] β arr[n β 1] - when elements are from 1 to n in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed in C++
