Answer to Question #223739 in Java | JSP | JSF for Khushbu

Question #223739
Addition number

Victor has an array of size n. He loves to play with these n numbers. Each time he plays
with them, he picks up any two consecutive numbers and adds them. On adding both the
numbers, it costs him k*(sum of both numbers).
Find the minimum cost of adding all the numbers in the array.
Input Specification:
input1: Size of array.
input2: Elements of the array.
input3: Value of k.
Output Specification:
Return the minimum cost of adding all the numbers of the array.
1
Expert's answer
2021-08-06T07:51:17-0400


package sum;
import java.io.*;
 
class SUM
{
    
    static int mini(int x, int y, int z)
    {
        return Math.min(Math.min(x, y), z);
    }
     
   
    static int MinSum(int arr1[], int x)
    {
        
        int summation1[] =new int[x];
     
       
        summation1[0] = arr1[0];
        summation1[1] = arr1[1];
        summation1[2] = arr1[2];
     
        
        for (int i = 3; i < x; i++)
        summation1[i] = arr1[i] + mini(summation1[i - 3],
                         summation1[i - 2], summation1[i - 1]);
     
        return mini(summation1[x - 1], summation1[x - 2],summation1[x - 3]);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arra[] = {1, 2, 3, 20, 2, 10, 1};
        int n = arra.length;
        System.out.println("Minimum Sum is " + MinSum(arra, n));
             
    }
}
 

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