mail2chandreshv
BAN USERThis is my simple dp solution. O(n^2) and O(1). The idea is to keep two pointers,
sumSoFar = max count you have encountered so far that meets the condition and by including current element.
result = final result updated after adding sumSoFar after iteration of every element.
public static int count(int[] inp,int N){
if(inp==null || inp.length==0)
return -1;
Arrays.sort(inp);
int sumSoFar = 0;
int result = 0;
for(int i=0;i<inp.length;i++){
for(int j=i;j<inp.length;j++){
if(i==j)
sumSoFar=inp[i]+inp[j]<= N?1:0;
else{
int temp = (inp[i]+inp[j]<= N)?sumSoFar*2:sumSoFar;
sumSoFar = temp;
}
}
result+=sumSoFar;
}
return result;
}
public int rangeCount(T lo, T hi){
if(lo==null || hi==null || this.root==null){
return 0;
}
return rangeCount(this.root, lo, hi);
}
private int rangeCount(Node n, T lo, T hi){
if(n==null)
return 0;
int temp=0,leftCount=0,rightCount=0;
if(min(n).data.compareTo(lo)>=0 && max(n).data.compareTo(hi)<=0){
return n.count;
}
if(n.data.compareTo(lo)>=0 && n.data.compareTo(hi)<=0)
temp++;
if(n.data.compareTo(lo)>=0)
leftCount = rangeCount(n.left, lo, hi);
if(n.data.compareTo(hi)<=0)
rightCount=rangeCount(n.right, lo, hi);
return temp+leftCount+rightCount;
}
I believe, we can implement observer pattern between all application servers serving as event creator and a listener working as a observer which is subscribed to this app servers. The moment desired condition is encountered in any app server, it calls its update function to pass on this information to the listener (could be a messaging queue), and then listener pass on the this message to a parser module which will parse the message and accordingly will send the message to the concrete implementation of logging interface (Error, Warning, DiskFull... etc)
This way the system remains open to be scaled in future.
1. New app servers can be added in future. All it has to do is to implement the subject Interface and implement the functionality to register the observer.
2. Observer (listener will have to subcribe to the new subject)
3. In case new pattern comes (Out of Memory, Application Error...) in future, they will have their own concrete implementation of logging interface.
In total allowing all major actors to be independent to each other.
This is my simple dp solution. O(n^2) and O(1). The idea is to keep two pointers,
sumSoFar = max count you have encountered so far that meets the condition and by including current element.
result = final result updated after adding sumSoFar after iteration of every element.
- mail2chandreshv May 05, 2019