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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:23:07+00:00 2026-05-31T20:23:07+00:00

I came across a post in SO where the algorithm is implemented in python

  • 0

I came across a post in SO where the algorithm is implemented in python code .This is a direct implementation of the pseudocode in this article .

However,in the pseudocode ,there is a line where count is incremented by the number of remaining elements in array ‘a’.In the above python code,it is given as

count += len(a) - i

can’t we do the same by

count += len(a[i:])

Also,instead of

c += a[i:] + b[j:]

I wrote,

c.append(a[i:])
c.append(b[j:])

In total my version looks like this

def merge(left,right):
    i=0
    j=0
    count=0
    c=[]
    while i<len(left) and j<len(right):
        c.append(min(left[i],right[j]))
        if right[j]< left[i]:
            count+=len(left[i:])
            j+=1
        else:
            i+=1
    c.append(left[i:])
    c.append(right[j:])
    return count,c


def dnc(input):
    if len(input)==1:
        return 0,input
    mid=len(input)/2
    left=input[:mid]
    right=input[mid:]
    l,left=dnc(left)
    r,right=dnc(right)
    m,result=merge(left,right)
    count=l+r+m
    return count,result

Alas!,when I compute this on a sorted array,I get 3 instead of 0

if __name__=='__main__':
    input =[1,2,3,4,5]
    ct,res=dnc(input)
    print ct

What have I done wrong? can someone help me find out?

  • 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-05-31T20:23:08+00:00Added an answer on May 31, 2026 at 8:23 pm

    Here’s what I can suggest:

    • Don’t do count += len(a[i:]). It’s slower than your original and is unnecessarily complicating the logic. I would instead cache len(a) as len_a and calculate it once outside of your loop, as a doesn’t seem to be modified.

    • c += a[i:] + b[j:] is the same as c.extend(a[i:]) and c.extend(b[j:]). extend appends the contents of the list to c while append appends the list itself, which might be causing your problem.

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

Sidebar

Related Questions

Well I was reading this post and then I came across a code which
While stumbling through the chromium code documentation, I came across this post: http://code.google.com/p/chromium/wiki/UsingGit#Windows If
I'm investigating different optimization techniques, and I came across this post Analyzing Code for
I came across this plain js ajax upload code (it seems that jquery $.post
While browsing I came across this blog post about using the Wikipedia API from
Kicking around some small structures while answering this post , I came across the
Came across something like this today, and was wondering if there was an equivalent
I came across this article written by Andrei Alexandrescu and Petru Marginean many years
I came across this post pass array to method Java on how to pass
i came across this post where Reader Comment #16 mentions: The ringtone syncing functionality

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.