Adding numbers victor ha an array of size n. He loves to play with these n numbers each numbers. Plays with them he pics up any two consecutive numbers and adds them on adding both numbers it costs him k*(sum of both numbers) find all minimum cost of adding all the numbers in the array. Input1:size of array,
Input2:element of the array
Input3:value of K
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;//size of the array
cin>>n;
int arr[n]; // array of size n
for (int i = 0; i < n; i++){
cin>>arr[i];
}
int miniCost=INT_MAX; // define minimum cost is INT_MAX
int k;
cin>>k;
// if only one number in array then the minimum cost is a[0]
if (n == 1){
miniCost = arr[0];
} else{
for (int i = 0; i < n - 1; i++)
{
if (k * (arr[i] + arr[i + 1]) < miniCost)
miniCost = k * (arr[i] + arr[i + 1]);
}
}
cout<<miniCost; // printing of minimum cost
return 0;
}
Comments
Leave a comment