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 7846023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:26:28+00:00 2026-06-02T17:26:28+00:00

I came across another codechef problem which I am attempting to solve in Scala.

  • 0

I came across another codechef problem which I am attempting to solve in Scala. The problem statement is as follows:

Stepford Street was a dead end street. The houses on Stepford Street
were bought by wealthy millionaires. They had them extensively altered
so that as one progressed along the street, the height of the
buildings increased rapidly. However, not all millionaires were
created equal. Some refused to follow this trend and kept their houses
at their original heights. The resulting progression of heights was
thus disturbed. A contest to locate the most ordered street was
announced by the Beverly Hills Municipal Corporation. The criteria for
the most ordered street was set as follows: If there exists a house
with a lower height later in the street than the house under
consideration, then the pair (current house, later house) counts as 1
point towards the disorderliness index of the street. It is not
necessary that the later house be adjacent to the current house. Note:
No two houses on a street will be of the same height For example, for
the input: 1 2 4 5 3 6 The pairs (4,3), (5,3) form disordered pairs.
Thus the disorderliness index of this array is 2. As the criteria for
determining the disorderliness is complex, the BHMC has requested your
help to automate the process. You need to write an efficient program
that calculates the disorderliness index of a street.

A sample input output provided is as follows:

Input: 1 2 4 5 3 6

Output: 2

The output is 2 because of two pairs (4,3) and (5,3)

To solve this problem I thought I should use a variant of MergeSort,incrementing by 1 when the left element is greater than the right element.

My scala code is as follows:

    def dysfunctionCalc(input:List[Int]):Int = {
        val leftHalf = input.size/2
        println("HalfSize:"+leftHalf)

        val isOdd = input.size%2
        println("Is odd:"+isOdd)

        val leftList = input.take(leftHalf+isOdd)
        println("LeftList:"+leftList)

        val rightList = input.drop(leftHalf+isOdd)
        println("RightList:"+rightList)

        if ((leftList.size <= 1) && (rightList.size <= 1)){
                println("Entering input where both lists are <= 1")
                             if(leftList.size == 0 || rightList.size == 0){
                                      println("One of the lists is less than 0")
                                        0
                                }
                             else if(leftList.head > rightList.head)1 else 0
     }       
     else{
             println("Both lists are greater than 1")
             dysfunctionCalc(leftList) + dysfunctionCalc(rightList)
     }
   }

First off, my logic is wrong,it doesn’t have a merge stage and I am not sure what would be the best way to percolate the result of the base-case up the stack and compare it with the other values. Also, using recursion to solve this problem may not be the most optimal way to go since for large lists, I maybe blowing up the stack. Also, there might be stylistic issues with my code as well.

I would be great if somebody could point out other flaws and the right way to solve this problem.

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-02T17:26:29+00:00Added an answer on June 2, 2026 at 5:26 pm

    Suppose you split your list into three pieces: the item you are considering, those on the left, and those on the right. Suppose further that those on the left are in a sorted set. Now you just need to walk through the list, moving items from “right” to “considered” and from “considered” to “left”; at each point, you look at the size of the subset of the sorted set that is greater than your item. In general, the size lookup can be done in O(log(N)) as can the add-element (with a Red-Black or AVL tree, for instance). So you have O(N log N) performance.

    Now the question is how to implement this in Scala efficiently. It turns out that Scala has a Red-Black tree used for its TreeSet sorted set, and the implementation is actually quite simple (here in tail-recursive form):

    import collection.immutable.TreeSet
    final def calcDisorder(xs: List[Int], left: TreeSet[Int] = TreeSet.empty, n: Int = 0): Int = xs match {
      case Nil => n
      case x :: rest => calcDisorder(rest, left + x, n + left.from(x).size)
    }   
    

    Unfortunately, left.from(x).size takes O(N) time (I believe), which yields a quadratic execution time. That’s no good–what you need is an IndexedTreeSet which can do indexOf(x) in O(log(n)) (and then iterate with n + left.size - left.indexOf(x) - 1). You can build your own implementation or find one on the web. For instance, I found one here (API here) for Java that does exactly the right thing.

    Incidentally, the problem with doing a mergesort is that you cannot easily work cumulatively. With merging a pair, you can keep track of how out-of-order it is. But when you merge in a third list, you must see how out of order it is with respect to both other lists, which spoils your divide-and-conquer strategy. (I am not sure whether there is some invariant one could find that would allow you to calculate directly if you kept track of it.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across this blog post Yet another post about gamma correction which
After getting fine answer to my previous question , I came across another problem.
I came across the following article which got me interested in this particular problem.
In another question I came across two different ways of doing a string replacement.
Reading on another forum I've came across the world of CSS Frameworks. The one
I came across the task to find all occurences of a substring in another
Came across a problem whereby I wanted the last time data was imported to
As part of answering another question, I came across a piece of code like
I came across a problem while trying to manipulate the FlowDocumentScrollViewer control in WPF.
I came across the following issue: I have a main simple page on which

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.