Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:16:06+00:00 2026-06-02T20:16:06+00:00

I am trying to solve the following codechef problem using scala. The statement of

  • 0

I am trying to solve the following codechef problem using scala. The statement of the problem is as follows:

Harry Potter has n mixtures in front of him, arranged in a row. Each
mixture has one of 100 different colors (colors have numbers from 0 to
99).

He wants to mix all these mixtures together. At each step, he is going
to take two mixtures that stand next to each other and mix them
together, and put the resulting mixture in their place.

When mixing two mixtures of colors a and b, the resulting mixture will
have the color (a+b) mod 100.

Also, there will be some smoke in the process. The amount of smoke
generated when mixing two mixtures of colors a and b is a*b.

Find out what is the minimum amount of smoke that Harry can get when
mixing all the mixtures together.

The sample answer provided is as follows:

Input:

2 
18 
19 
3
40
60
20

Output:

342
2400

In the second test case, there are two possibilities:

first mix 40 and 60 (smoke: 2400), getting 0, then mix 0 and 20 (smoke: 0); total amount of smoke is 2400
first mix 60 and 20 (smoke: 1200), getting 80, then mix 40 and 80 (smoke: 3200); total amount of smoke is 4400

The first scenario is the correct approach since it minimizes the
amount of smoke produced.

I understand that this can be solved using dynamic programming but I am having trouble approaching this problem and expressing the algorithm in scala.

Here’s how I am thinking about this problem, in some kind of a lookup structure(Array,Map with Tuple(Int,Int) as key) store all the calculated values for mixing two colors

This can be accomplished by the following pseudo-code:

for(colors1<-1 through n)
   for(colors2<-k through n)
      if(color1 != color2)
          //Check if color1,color2 combination exists as (color1,color2) or (color2,color1)    
          Map(color1,color2) = (color1+color2)%100

Once all the initial colors have been calculated, now we need to consider the order in which we mix up the colors taking into account the smoke produced. This is where I am having issues. I am hitting a wall trying to formulate the sub-structure that will provide the minimum smoke produced.

It will be great to get some guidance here.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:16:07+00:00Added an answer on June 2, 2026 at 8:16 pm

    I wrote the following dynamic programming solution. It’s also available as a gist.

    /** Find the minimum amount of smoke (second) and resulting color (first) 
        by splitting the sequence at every possible position,
        given `lookup` contains the best mix for subsequence. */
    def minSmokeMixtureSingle(s: Seq[Int], lookup: Map[Seq[Int],(Int,Int)])
      : (Int,Int) = 
        if(s.size == 1)
            (s(0),0)
        else if(s.size == 2)
            mix(s(0),s(1))
        else {
            val splits = (1 to (s.size - 1)).map(s.splitAt)
            val mixes = splits.map{ case (s1,s2) => 
                val (c1,sm1) = lookup(s1)
                val (c2,sm2) = lookup(s2)
                val (newColor,newSmoke) = mix(c1,c2)
                (newColor, newSmoke + sm1 + sm2)
            }
            mixes.minBy(_._2)
        }
    
    def mix(m1: Int, m2: Int): (Int,Int) = ((m1+m2) % 100, m1*m2)
    
    def minSmokeMixture(s: Seq[Int]) = {
        //create the mixture sequences with increasing length
        val windows = for(windowSize <- 1 to s.size;
            window <- s.sliding(windowSize) ) yield window
        //go over the sequences and compute the lookup-table
        val lookup = windows.foldLeft(Map.empty[Seq[Int],(Int,Int)]){
            case (lu,seq) => lu + (seq -> minSmokeMixtureSingle(seq,lu))
        }
        //simply lookup the result
        lookup(s)
    }
    
    println(minSmokeMixture(Seq(18, 19)))
    println(minSmokeMixture(Seq(40, 60, 20)))
    

    This can certainly be improved in terms of style.

    It produces the correct output for the given examples (second number is smoke, first is final color):

    (37,342)
    (20,2400)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve the following problem from Scala for the impatient .
While learning Prolog, I'm trying to solve the following problem, using accumulators: Write a
I am trying to solve the following problem. Lets say you have the the
I am trying to solve the following problem: Find the largest palindrome made from
I'm trying to solve the following problem: Say I have a Python script (let's
I'm trying to solve the following problem: Given an input of, say, 0000000000000000 0011111111110000
I have the following cyclic dependency problem I am trying to solve: typedef std::map<int,
I am trying to solve the following problem but cannot find an elegant solution.
I'm I'm trying to solve the following problem with linq (already solved it with
Using six-bit one's and two's complement representation I am trying to solve the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.