Given an array where each element denotes the number of chocolates corresponding to each station and to move from station i to station i+1, we get A[i] – A[i+1] chocolates for free, if this number is negative, we lose that many chocolates also.
You can only move from station i to station i+1, if we have non-negative number of chocolates.
Given the cost of one chocolate p, the task to find the minimum cost incurred in reaching last station from the first station (station 1).
Note: Initially we have 0 chocolates.
Examples:
Input : arr[] = {1, 2, 3} p = 10
Output : 30
Initial choc_buy = 1 (arr[0])
To reach index 0 to 1, arr[0] - arr[1] = -1,
so choc_buy +=1.
Similarly To reach index 2 to 1,
arr[1] - arr[2] = -1, so choc_buy +=1.
Total choc_buy = 3.
Total cost = choc_by * p = 3*10 = 30.
Input : arr[] = {10, 6, 11, 4, 7, 1} p = 5
Output : 55
Initial choc_buy = 10 (arr[0])
To reach index 0 to 1, arr[0] - arr[1] = 4,
so curr_choc =4.
Similarly To reach index 2 to 1,
arr[1] - arr[2] = -5, so curr_choc=4+-5 = -1.
choc_buy += abs(-1).
So, similarly till last station, Total choc_buy = 11.
Total cost = choc_by * p = 11*5 = 55.
Approach :
1. Initialize choc_buy with the first element of array and curr_choc =0.
2. Add arr[i]-arr[i+1] to curr_choc for every i.
a) Check if curr_choc becomes negative.
choc_buy += abs(arr[i]- arr[i+1])
curr_choc = 0
3. Return choc_buy * p.
C++
#include <bits/stdc++.h>
using namespace std;
int findMinCost( int arr[], int n, int choc_cost) {
int choc_buy = arr[0];
int curr_choc = 0;
for ( int i = 0; i < n - 1; i++) {
int choc = arr[i] - arr[i + 1];
curr_choc += choc;
if (curr_choc < 0) {
choc_buy += abs (curr_choc);
curr_choc = 0;
}
}
return choc_buy * choc_cost;
}
int main() {
int arr[] = {10, 6, 11, 4, 7, 1};
int n = sizeof (arr) / sizeof (arr[0]);
int p = 5;
cout << findMinCost(arr, n, p);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findMinCost( int arr[], int n, int choc_cost)
{
int choc_buy = arr[ 0 ];
int curr_choc = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
int choc = arr[i] - arr[i + 1 ];
curr_choc += choc;
if (curr_choc < 0 )
{
choc_buy += Math.abs(curr_choc);
curr_choc = 0 ;
}
}
return choc_buy * choc_cost;
}
public static void main (String[] args)
{
int arr[] = { 10 , 6 , 11 , 4 , 7 , 1 };
int n = arr.length;
int p = 5 ;
System.out.println ( findMinCost(arr, n, p));
}
}
|
Python3
def findMinCost(arr, n, choc_cost):
choc_buy = arr[ 0 ]
curr_choc = 0
for i in range ( 0 ,n - 1 ):
choc = arr[i] - arr[i + 1 ]
curr_choc + = choc
if (curr_choc < 0 ):
choc_buy + = abs (curr_choc)
curr_choc = 0
return choc_buy * choc_cost
arr = [ 10 , 6 , 11 , 4 , 7 , 1 ]
n = len (arr)
p = 5
print (findMinCost(arr, n, p))
|
C#
using System;
class GFG
{
static int findMinCost( int []arr,
int n, int choc_cost)
{
int choc_buy = arr[0];
int curr_choc = 0;
for ( int i = 0; i < n - 1; i++)
{
int choc = arr[i] - arr[i + 1];
curr_choc += choc;
if (curr_choc < 0)
{
choc_buy += Math.Abs(curr_choc);
curr_choc = 0;
}
}
return choc_buy * choc_cost;
}
public static void Main ()
{
int []arr = {10, 6, 11, 4, 7, 1};
int n = arr.Length;
int p = 5;
Console.WriteLine( findMinCost(arr, n, p));
}
}
|
PHP
<?php
function findMinCost( $arr , $n , $choc_cost )
{
$choc_buy = $arr [0];
$curr_choc = 0;
for ( $i = 0; $i < $n - 1; $i ++) {
$choc = $arr [ $i ] - $arr [ $i + 1];
$curr_choc += $choc ;
if ( $curr_choc < 0) {
$choc_buy += abs ( $curr_choc );
$curr_choc = 0;
}
}
return $choc_buy * $choc_cost ;
}
$arr = array (10, 6, 11, 4, 7, 1);
$n = count ( $arr );
$p = 5;
echo findMinCost( $arr , $n , $p );
?>
|
Javascript
<script>
function findMinCost(arr, n, choc_cost) {
var choc_buy = arr[0];
var curr_choc = 0;
for ( var i = 0; i < n - 1; i++) {
var choc = arr[i] - arr[i + 1];
curr_choc += choc;
if (curr_choc < 0) {
choc_buy += Math.abs(curr_choc);
curr_choc = 0;
}
}
return (choc_buy * choc_cost);
}
var arr = [10, 6, 11, 4, 7, 1];
var n = 6;
var p = 5;
document.write(findMinCost(arr, n, p));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)