Total count

PROBLEM :

Given an array and a threshold value k where k is used to divide each element of the array. Find the total number of divisions we get after dividing each element of the array by k.
for example:
A[ ] = 5 8 10 13 6 2 and k is 3
Output will be 17

Explanation:
Number    Parts         counts
5               {3,2}              2
8              {3,3,2}            3
10           {3,3,3,1}          4
13          {3,3,3,3,1}        5
6                {3,3}             2
2                  {2}              1

The result thus will be 2+3+4+5+2+1 = 17

Input:

The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N and threshold value k, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements.

Output:

Corresponding to each test case, in a new line, print the total count.

Constraints:

1 = T = 100
1 = N = 500
1 = A[i] = 1000
1 = Threshold value(k) = 20

Example:

Input
1
6 3
5 8 10 13 6 2

Output
17

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int Total_count(int [],int ,int ) ;
int main()
 {
int t,no,arr[500],k,i ;
cin>>t ;
while(t--)
{
   cin>>no ;
   cin>>k ;
 
   for(i=0;i<no;i++)
       cin>>arr[i] ;
     
   k=Total_count(arr,no,k) ;
   cout<<k<<endl ;
}
return 0;
}

int Total_count(int arr[],int no,int k)
{
    int count,i ;
    count=0 ;
   
    for(i=0;i<no;i++)
    {
        while(arr[i]>=k)
        {
            arr[i]=arr[i]-k ;
            count++ ;
        }
        if(arr[i]!=0)
            count++ ;
    }
    return count ;
}

--------------------------------------------------------------------------------
BETTER c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int main()
 {
int t,no,a[500],i,k,count,q,r;
cin>>t ;
while(t--)
{
   cin>>no>>k ;
   for(i=0;i<no;i++)
   cin>>a[i] ;
   count=0 ;
 
   for(i=0;i<no;i++)
   {
       q=a[i]/k ;
       r=a[i]%k ;
     
       count=count+q ;
       if(r!=0)
       count++ ;
   }
   cout<<count<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )