TCS-NQT-C program-Collecting candies

Collecting candies

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.

C-program:

#include<stdio.h>
int main()
{
  int i,test,box,j,sum=0,total=0,ch;
  scanf("%d",&test);
  for(i=0;i<test;i++)
  {
    scanf("%d",&box);
    for(j=0;j<box;j++)
    {
      scanf("%d",&ch);
      sum = sum + ch;
      if(j!=0)
      {
        total += sum;
      }
    }
    printf("%d",total);
  }
  return 0;
}

INPUT : test case : 1
              box : 4
              1 2 3 4
OUTPUT : 19

Comments

Popular Posts