-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTotal count
73 lines (51 loc) · 1.58 KB
/
Total count
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
PROBLEM:-
Given an array Arr of N positive integers and a number K where K is used as a threshold value to divide each element of the array into sum of different numbers. Find the sum of count of the numbers in which array elements are divided.
Example 1:
Input:
N = 4, K = 3
Arr[] = {5, 8, 10, 13}
Output: 14
Explanation: Each number can be expressed as sum
of different numbers less than or equal to K as
5 (3 + 2), 8 (3 + 3 + 2), 10 (3 + 3 + 3 + 1),
13 (3 + 3 + 3 + 3 + 1). So, the sum of count
of each element is (2+3+4+5)=14.
Example 2:
Input:
N = 5, K = 4
Arr[] = {10, 2, 3, 4, 7}
Output: 8
Explanation: Each number can be expressed as sum of
different numbers less than or equal to K as
10 (4 + 4 + 2), 2 (2), 3 (3), 4 (4) and 7 (4 + 3).
So, the sum of count of each element is
(3 + 1 + 1 + 1 + 2) = 8.
Your Task:
You don't need to read input or print anything. Your task is to complete the function totalCount() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 107
0 ≤ Arri ≤ 107
1 ≤ K ≤ 107
SOLUTION:-
JAVA CODE:
//User function Template for Java
class Solution {
int totalCount(int[] arr, int n, int k) {
// code here
int sum = 0;
for(int i = 0; i < n; i++)
{
if(arr[i]%k == 0)
{
sum += arr[i]/k;
}
else
{
sum += (arr[i]/k) + 1;
}
}
return sum;
}
}