-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2779.java
More file actions
63 lines (49 loc) · 1.43 KB
/
LC2779.java
File metadata and controls
63 lines (49 loc) · 1.43 KB
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
/*
* LC2779
*/
import java.util.*;
public class LC2779 {
public static int maximumBeauty(int[] nums, int k) {
// Base Case
if (nums.length == 1) {
return 1;
}
// Create count array
int max = 0;
for (int num : nums) {
max = Math.max(max, num);
}
int[] count = new int[max + 1];
// Insert range in count array
for (int num : nums) {
count[Math.max(num - k, 0)]++;
count[Math.min(num + k + 1, max)]--;
}
// Find max of prefix sum
int curSum = 0, maxSum = 0;
for (int c : count) {
curSum += c;
maxSum = Math.max(maxSum, curSum);
}
return maxSum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Nums Array Length : ");
int n = sc.nextInt();
System.out.println();
int[] nums = new int[n];
System.out.println("Enter The Number Array Elements : ");
for (int i = 0; i < nums.length; i++) {
System.out.printf("[%d] : ", i);
nums[i] = sc.nextInt();
}
System.out.println();
System.out.print("Enter The K Elements : ");
int k = sc.nextInt();
System.out.println();
int ans = maximumBeauty(nums, k);
System.out.println(ans);
sc.close();
}
}