C++ Program for Collecting Candies (TCS Codevita) | PrepInsta

C++ Program for Collecting Candies

Collecting Candies Problem

In order to filter out the best coding talent from all over the globe TCS every year arranges a Coding Competition promoting Programming-As-A-Sport idea called TCS CodeVita. Collecting Candies is one of the sample problem of TCS CodeVita competition. It is solved by using a sorting algorithm with some changes here and there, here we have provided a C++ solution for this problem.

Problem Description

Question:- Krishna loves candies a lot, so whenever he gets them, he stores them so that he can eat them later whenever he wants to.

He has recently received N boxes of candies each containing Ci candies where Ci represents the total number of candies in the ith box. Krishna wants to store them in a single box. The only constraint is that he can choose any two boxes and store their joint contents in an empty box only. Assume that there are an infinite number of empty boxes available.

At a time he can pick up any two boxes for transferring and if both the boxes contain X and Y number of candies respectively, then it takes him exactly X+Y seconds of time. As he is too eager to collect all of them he has approached you to tell him the minimum time in which all the candies can be collected.

Input Format:

  • The first line of input is the number of test case T
  • Each test case is comprised of two inputs
  • The first input of a test case is the number of boxes N
  • The second input is N integers delimited by whitespace denoting the number of candies in each box

Output Format: Print minimum time required, in seconds, for each of the test cases. Print each output on a new line.

Constraints:

  • 1 < T < 10
  • 1 < N< 10000
  • 1 < [Candies in each box] < 100009
S. No.InputOutput
11
4
1 2 3 4
19
21
5
1 2 3 4
34

Explanation for sample input-output 1:

4 boxes, each containing 1, 2, 3 and 4 candies respectively.Adding 1 + 2 in a new box takes 3 seconds.Adding 3 + 3 in a new box takes 6 seconds.Adding 4 + 6 in a new box takes 10 seconds.Hence total time taken is 19 seconds. There could be other combinations also, but overall time does not go below 19 seconds.

Explanation for sample input-output 2:

5 boxes, each containing 1, 2, 3, 4 and 5 candies respectively.Adding 1 + 2 in a new box takes 3 seconds.Adding 3 + 3 in a new box takes 6 seconds.Adding 4 + 6 in a new box takes 10 seconds.Adding 5 + 10 in a new box takes 15 seconds.Hence total time taken is 34 seconds. There could be other combinations also, but overall time does not go below 33 seconds.

C++ Code

#include <iostream>
using namespace std;

int main() 
{
    int i,j;
    int num_box=0,k=0,sum=0,times=0,tst_case,temp=0;
    long c[10000],s[10000];
    cout<<("Enter the number of test case:");
    cin>>tst_case;
    cout<<("Enter the number of boxes:");
    for(int l=0;l<tst_case;l++) { cin>>num_box;
    }
    cout<<("Enter the number of candies in each box:");
    for(i=0;i<num_box;i++) { cin>>c[i];
    }
    for(i=0;i<num_box;i++)
    {
        for(j=i+1;j<num_box;j++) { if(c[i]>c[j])
            {
                temp=c[i];
                c[i]=c[j];
                c[j]=temp;
            }
        }
    }
    sum=0;
    k=0;
    for(i=0;i<num_box;i++)
    {
        sum=sum+c[i];
        s[k]=sum;
        k++;
    }
    times=0;
    cout<<("Minimum time requried:");
    for(i=1;i<k;i++)
    times=times+s[i];
    
    cout<<times;
    
    return 0;
}
Output
1
4
1 2 3 4
19

Collecting Candies Problem in other Languages

C

To find the solution of Collecting Candies problem in C Programming language click on the button below:

C

Python

To find the solution of Collecting Candies problem in Python Programming language click on the button below:

 Python

Java

To find the solution of Collecting Candies problem in Java Programming language click on the button below:

 Java

2 comments on “C++ Program for Collecting Candies (TCS Codevita) | PrepInsta”


  • DEEPANWITA

    #include
    #include
    using namespace std;
    int main()
    { long n,temp,sum,t;
    temp=0;
    sum=0;
    cin>>t;
    for(int a=0;a>n;
    long *arr=new long[n];
    for(long i=0;i<n;i++){
    arr[i]=i+1;
    //cout<<arr[i];
    }
    for(long j=0;j<n-1;j++){
    sum=arr[j]+arr[j+1];
    arr[j+1]=sum;
    temp+=sum;
    sort(arr+j+1,arr+n);
    }
    cout<<temp<<endl;
    }
    return 0;

    }


  • Kalon

    #include

    using namespace std;

    int main()
    {
    int arr[]={1,2,3,4,5};
    int n = 5;
    int sum=0;
    int k=1;
    while(k<n)
    {
    for(int i=k;i<k+1;i++)
    {
    arr[i]=arr[i]+arr[i-1];
    break;
    }
    k=k+1;
    }
    for(int i=1;i<n;i++)
    {
    sum+=arr[i];
    //cout<<arr[i]<<" ";
    }
    //
    cout<<sum<<endl;

    return 0;
    }