-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay04.java
More file actions
35 lines (32 loc) · 1.04 KB
/
Copy pathDay04.java
File metadata and controls
35 lines (32 loc) · 1.04 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
//Problem:Count distinct numbers on board(leetcode)
//https://leetcode.com/problems/count-distinct-numbers-on-board/description/
//Approach:Every number when divided by its previous number will give remainder 1.
class Solution {
public int distinctIntegers(int n) {
if(n==1){
return 1;
}
return n-1;
}
}
//TC=O(1)
//SC=O(1)
//Problem:chocolate distribution problem(gfg)
//https://www.geeksforgeeks.org/problems/chocolate-distribution-problem3825/1
//Approach:First,sorted the array and took a window of size m,starting from 0th index and then kept shifting the window by 1 unit to find the minimum difference.
class Solution {
public int findMinDiff(ArrayList<Integer> arr, int m) {
// your code here
Collections.sort(arr);
int i=0,j=m-1,mindiff=Integer.MAX_VALUE;
while(j<arr.size()){
if(arr.get(j)-arr.get(i)<mindiff){
mindiff=arr.get(j)-arr.get(i);
}
i++;j++;
}
return mindiff;
}
}
//TC:O(nlogn)
//SC:O(1)