Java program for Sorting Boxes problem
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 Java 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 program for Sorting Boxes problem
import java.util.Arrays; import java.util.Scanner; class Main { static int i,j; static int[] array; static int[] array1; static int[] array2; static int sum=0; static int max=0; static int min=0; static int min1=0; static int max1=0; static int swap1=0,swap2=0; static int swapping(int c ,int a , int b) { int temp; if(c==1) { temp=array2[a]; array2[a]=array2[b]; array2[b]=temp; return array[2]*array2[b]; } else { temp=array[a]; array[a]=array[b]; array[b]=temp; return array[a]*array[b]; } } static int prep() { for (int k = 0; k< array.length; k++) { for (int k2 = 0; k2 < array1.length; k2++) { if(array[k2]==array1[k]) { if(k2==k) { break; } else { if(array[k2]>array[k]) { min1=k; max1=k2; } else { min1=k2; max1=k; } swap1=swapping(1,k2,k); swap2=swapping(2,0,min1)+swapping(2,min1,max1)+swapping(2,0,max1); if(swap1<swap1) { sum=sum+swap1; for (int i = 0; i < array.length; i++) { array[i]=array2[i]; } } else { for (int i = 0; i < array.length; i++) { array2[i]=array1[i]; } sum=sum+swap2; } } } } } return sum; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); i=sc.nextInt(); j=sc.nextInt()-1; array=new int[i]; array1=new int[i]; array2=new int[i]; for (int k = 0; k < i; k++) { array[k]=sc.nextInt(); } for (int k = 0; k < i; k++) { array2[k]=array[k]; } for (int k = 0; k < i; k++) { array1[k]=array[k]; } Arrays.sort(array1); int max=array1[array1.length-1]; for (int i = array1.length-1; i >j; i--) { array1[i]=array1[i-1]; } array1[j]=max1; System.out.println(prep()); System.out.println(); } }
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
The code is giving wrong output on testcase second
Kindly join our Discord channel for all the technical and course related queries.