14 June 2018

count sum based on given kth value

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

public class NewClass18 {
   
void sum(int ar[],int k)
    {
        int sum=0;
        for(int i=0;i<ar.length;i++)
        {
            sum+=ar[i]/k;
            if(ar[i]%k!=0)
                sum=sum+1;
        }
        System.out.println(sum);
    }

    public static void main(String args[])
    {
        int ar[]={5,8,10,13,6,2};
        int k=3;
        NewClass18 ob=new NewClass18();
        ob.sum(ar, k);
    }
}
 
                                  

No comments:

Post a Comment