Minimum cost of choosing 3 increasing elements in an array of size N
Given two arrays arr[] and cost[] where cost[i] is the cost associated with arr[i], the task is to find the minimum cost of choosing three elements from the array such that arr[i] < arr[j] < arr[k].
Examples:
Input: arr[] = {2, 4, 5, 4, 10}, cost[] = {40, 30, 20, 10, 40}
Output: 90
(2, 4, 5), (2, 4, 10) and (4, 5, 10) are
the only valid triplets with cost 90.
Input: arr[] = {1, 2, 3, 4, 5, 6}, cost[] = {10, 13, 11, 14, 15, 12}
Output: 33
Naive approach: A basic approach is two-run three nested loops and to check every possible triplet. The time complexity of this approach will be O(n3).
Approach:
- We can use a brute-force approach to iterate over all possible triplets of elements in the array and check if they form an increasing sequence.
- If a triplet forms an increasing sequence, we calculate its cost by adding the costs of its elements.
- We keep track of the minimum cost seen so far and return it as a result.
The array arr, the related costs cost, and the length of the arrays n are passed to the min_cost_triplet method. It iterates through all potential triplets of elements, tests to see if they form an increasing sequence then computes the triplet’s cost. It maintains track of the lowest cost encountered thus far and returns it as the result.
We define the input arrays, determine their length, call the min_cost_triplet function, then print the result in the main function.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int min_cost_triplet( int arr[], int cost[], int n) {
int min_cost = INT_MAX;
for ( int i = 0; i < n - 2; i++) {
for ( int j = i + 1; j < n - 1; j++) {
for ( int k = j + 1; k < n; k++) {
if (arr[i] < arr[j] && arr[j] < arr[k]) {
int curr_cost = cost[i] + cost[j] + cost[k];
if (curr_cost < min_cost) {
min_cost = curr_cost;
}
}
}
}
}
return min_cost;
}
int main() {
int arr[] = {2, 4, 5, 4, 10};
int cost[] = {40, 30, 20, 10, 40};
int n = sizeof (arr) / sizeof (arr[0]);
cout << min_cost_triplet(arr, cost, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
public class GFG {
public static int minCostTriplet( int [] arr, int [] cost, int n) {
int minCost = Integer.MAX_VALUE;
for ( int i = 0 ; i < n - 2 ; i++) {
for ( int j = i + 1 ; j < n - 1 ; j++) {
for ( int k = j + 1 ; k < n; k++) {
if (arr[i] < arr[j] && arr[j] < arr[k]) {
int currCost = cost[i] + cost[j] + cost[k];
if (currCost < minCost) {
minCost = currCost;
}
}
}
}
}
return minCost;
}
public static void main(String[] args) {
int [] arr = { 2 , 4 , 5 , 4 , 10 };
int [] cost = { 40 , 30 , 20 , 10 , 40 };
int n = arr.length;
System.out.println(minCostTriplet(arr, cost, n));
}
}
|
Python
def min_cost_triplet(arr, cost, n):
min_cost = float ( 'inf' )
for i in range (n - 2 ):
for j in range (i + 1 , n - 1 ):
for k in range (j + 1 , n):
if arr[i] < arr[j] < arr[k]:
curr_cost = cost[i] + cost[j] + cost[k]
if curr_cost < min_cost:
min_cost = curr_cost
return min_cost
arr = [ 2 , 4 , 5 , 4 , 10 ]
cost = [ 40 , 30 , 20 , 10 , 40 ]
n = len (arr)
print (min_cost_triplet(arr, cost, n))
|
C#
using System;
public class GFG
{
public static int MinCostTriplet( int [] arr, int [] cost, int n)
{
int minCost = int .MaxValue;
for ( int i = 0; i < n - 2; i++)
{
for ( int j = i + 1; j < n - 1; j++)
{
for ( int k = j + 1; k < n; k++)
{
if (arr[i] < arr[j] && arr[j] < arr[k])
{
int currCost = cost[i] + cost[j] + cost[k];
if (currCost < minCost)
{
minCost = currCost;
}
}
}
}
}
return minCost;
}
public static void Main( string [] args)
{
int [] arr = { 2, 4, 5, 4, 10 };
int [] cost = { 40, 30, 20, 10, 40 };
int n = arr.Length;
Console.WriteLine(MinCostTriplet(arr, cost, n));
}
}
|
Javascript
<script>
function minCostTriplet(arr, cost, n) {
let minCost = Number.MAX_VALUE;
for (let i = 0; i < n - 2; i++) {
for (let j = i + 1; j < n - 1; j++) {
for (let k = j + 1; k < n; k++) {
if (arr[i] < arr[j] && arr[j] < arr[k]) {
const currCost = cost[i] + cost[j] + cost[k];
if (currCost < minCost) {
minCost = currCost;
}
}
}
}
}
return minCost;
}
function main() {
const arr = [2, 4, 5, 4, 10];
const cost = [40, 30, 20, 10, 40];
const n = arr.length;
console.log(minCostTriplet(arr, cost, n));
}
main();
</script>
|
Time Complexity: O(n^3), where n is the size of the given arrays.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Efficient approach: An efficient approach is to fix the middle element and search for the smaller element with minimum cost on its left and the larger element with minimum cost on its right in the given array. If a valid triplet is found then update the minimum cost far. The time complexity of this approach will be O(n2).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minCost( int arr[], int cost[], int n)
{
int costThree = INT_MAX;
for ( int j = 0; j < n; j++) {
int costI = INT_MAX, costK = INT_MAX;
for ( int i = 0; i < j; i++) {
if (arr[i] < arr[j])
costI = min(costI, cost[i]);
}
for ( int k = j + 1; k < n; k++) {
if (arr[k] > arr[j])
costK = min(costK, cost[k]);
}
if (costI != INT_MAX && costK != INT_MAX) {
costThree = min(costThree, cost[j]
+ costI
+ costK);
}
}
if (costThree == INT_MAX)
return -1;
return costThree;
}
int main()
{
int arr[] = { 2, 4, 5, 4, 10 };
int cost[] = { 40, 30, 20, 10, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minCost(arr, cost, n);
return 0;
}
|
Java
class GFG
{
static int minCost( int arr[], int cost[], int n)
{
int costThree = Integer.MAX_VALUE;
for ( int j = 0 ; j < n; j++)
{
int costI = Integer.MAX_VALUE;
int costK = Integer.MAX_VALUE;
for ( int i = 0 ; i < j; i++)
{
if (arr[i] < arr[j])
costI = Math.min(costI, cost[i]);
}
for ( int k = j + 1 ; k < n; k++)
{
if (arr[k] > arr[j])
costK = Math.min(costK, cost[k]);
}
if (costI != Integer.MAX_VALUE &&
costK != Integer.MAX_VALUE)
{
costThree = Math.min(costThree, cost[j] +
costI + costK);
}
}
if (costThree == Integer.MAX_VALUE)
return - 1 ;
return costThree;
}
public static void main (String[] args)
{
int arr[] = { 2 , 4 , 5 , 4 , 10 };
int cost[] = { 40 , 30 , 20 , 10 , 40 };
int n = arr.length;
System.out.println(minCost(arr, cost, n));
}
}
|
Python3
def minCost(arr, cost, n):
costThree = 10 * * 9
for j in range (n):
costI = 10 * * 9
costK = 10 * * 9
for i in range (j):
if (arr[i] < arr[j]):
costI = min (costI, cost[i])
for k in range (j + 1 , n):
if (arr[k] > arr[j]):
costK = min (costK, cost[k])
if (costI ! = 10 * * 9 and costK ! = 10 * * 9 ):
costThree = min (costThree, cost[j] +
costI + costK)
if (costThree = = 10 * * 9 ):
return - 1
return costThree
arr = [ 2 , 4 , 5 , 4 , 10 ]
cost = [ 40 , 30 , 20 , 10 , 40 ]
n = len (arr)
print (minCost(arr, cost, n))
|
C#
using System;
class GFG
{
static int minCost( int []arr,
int []cost, int n)
{
int costThree = int .MaxValue;
for ( int j = 0; j < n; j++)
{
int costI = int .MaxValue;
int costK = int .MaxValue;
for ( int i = 0; i < j; i++)
{
if (arr[i] < arr[j])
costI = Math.Min(costI, cost[i]);
}
for ( int k = j + 1; k < n; k++)
{
if (arr[k] > arr[j])
costK = Math.Min(costK, cost[k]);
}
if (costI != int .MaxValue &&
costK != int .MaxValue)
{
costThree = Math.Min(costThree, cost[j] +
costI + costK);
}
}
if (costThree == int .MaxValue)
return -1;
return costThree;
}
static public void Main ()
{
int []arr = { 2, 4, 5, 4, 10 };
int []cost = { 40, 30, 20, 10, 40 };
int n = arr.Length;
Console.Write(minCost(arr, cost, n));
}
}
|
Javascript
<script>
function minCost(arr,cost,n)
{
let costThree = Number.MAX_VALUE;
for (let j = 0; j < n; j++)
{
let costI = Number.MAX_VALUE;
let costK = Number.MAX_VALUE;
for (let i = 0; i < j; i++)
{
if (arr[i] < arr[j])
costI = Math.min(costI, cost[i]);
}
for (let k = j + 1; k < n; k++)
{
if (arr[k] > arr[j])
costK = Math.min(costK, cost[k]);
}
if (costI != Number.MAX_VALUE &&
costK != Number.MAX_VALUE)
{
costThree = Math.min(costThree, cost[j] +
costI + costK);
}
}
if (costThree == Number.MAX_VALUE)
return -1;
return costThree;
}
let arr=[2, 4, 5, 4, 10];
let cost=[40, 30, 20, 10, 40 ];
let n = arr.length;
document.write(minCost(arr, cost, n));
</script>
|
Time Complexity: O(n2), where n is the size of the given arrays.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
10 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...