Answer to Question #239160 in Electrical Engineering for Mallikarjun

Question #239160

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


1
Expert's answer
2021-09-20T02:35:30-0400
#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;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS