Python program for Sorting Boxes problem

Sorting-boxes-problem-python

Sorting Boxes Problem

This is a TCS CodeVita problem which is used to find the minimum effort done to sort the boxes placed in the van. This is a real word problem and can be categorized into the good problems asked in CodeVita. Here is the Python program for Sorting Boxes problem, which gives the minimum effort required to sort the boxes in increasing order.

Problem Statement

The parcel section of the Head Post Office is in a mess. The parcels that need to be loaded to the vans have been lined up in a row in an arbitrary order of weights. The Head Post Master wants them to be sorted in the increasing order of the weights of the parcels, with one exception. He wants the heaviest (and presumably the most valuable) parcel kept nearest his office.

You and your friend try to sort these boxes and you decide to sort them by interchanging two boxes at a time. Such an interchange needs effort equal to the product of the weights of the two boxes.

The objective is to reposition the boxes as required with minimum effort.

Input Format:

  • The first line consists of two space-separated positive integers giving the number of boxes (N) and the position of the Head Post Masters office (k) where the heaviest box must be.
  • The second line consists of N space-separated positive integers giving the weights of the boxes. You may assume that no two weights are equal

Output Format:

  • The output is one line giving the total effort taken to get the boxes in sorted order, and the heaviest in position k.

Constraints:

 N<=50 and Weights <= 1000

Sample Input 1:
5 2
20 50 30 80 70
Sample Output 1:
3600

#JAVA SOLUTION FOR THIS IS TOO LENGTHY YOU CAN USE THIS LOGIC SAME IN JAVA AS WELL

size,k = map(int,input().split())
parcel = list(map(int,input().split()))
effort = 2*parcel[k-1]*min(parcel) + max(parcel)*min(parcel)
print(effort)

Sorting Boxes Problem in few other Coding Languages

C

We don’t have the solution for this problem, you can contribute the answer of this code in C programming language, we post that answer on our page

C++

We don’t have the solution for this problem, you can contribute the answer of this code in C++ programming language, we post that answer on our page

Java

For Java solution of the sorting boxes problem please click on the below button-

Java

2 comments on “Python program for Sorting Boxes problem”


  • Rithanya

    It doesnt work for this below input
    Input

    6 3

    30 20 40 80 70 60
    Output
    7600
    (600) 20 30 40 80 70 60

    (3200) 20 30 80 40 70 60

    (1200) 60 30 80 40 70 20

    (1400) 60 30 80 40 20 70

    (1200) 20 30 80 40 60 70

    A total effort of 7600


    • ellapujaiprakash8524

      size,k = map(int,input().split())
      parcel = list(map(int,input().split()))
      effort=min(parcel)*parcel[k-1]+min(parcel)*max(parcel)
      t1=min(parcel)
      t2=max(parcel)
      t3=parcel[k-1]
      l1=parcel.index(t1)
      l2=parcel.index(t2)
      parcel[k-1]=t2
      parcel[l1]=t3
      parcel[l2]=t1
      parcel.remove(max(parcel))
      lst1=sorted(parcel)
      i=0
      while lst1[i]!=max(lst1):
      if parcel[i]==lst1[i]:
      parcel.remove(parcel[i])
      i-=1
      i+=1
      while len(parcel)!=0:
      i=len(parcel)-1
      if max(parcel)==parcel[i]:
      pass
      else:
      if parcel[i]!=min(parcel):
      effort=effort+(min(parcel)*parcel[i]+min(parcel)*max(parcel))
      t1=min(parcel)
      t2=max(parcel)
      t3=parcel[i]
      l1=parcel.index(t1)
      l2=parcel.index(t2)
      parcel[i]=t2
      parcel[l1]=t3
      parcel[l2]=t1
      else:
      effort=effort+min(parcel)*max(parcel)
      parcel.remove(max(parcel))
      print(effort)