Posts

Showing posts with the label design pattern

Split array into two equal length subsets such that all repetitions of a number lies in a single subset

Given an array arr[] consisting of N integers, the task is to check if it is possible to split the integers into two equal length subsets such that all repetitions of any array element belongs to the same subset. If found to be true, print “Yes”. Otherwise, print “No”. Examples: Input: arr[] = {2, 1, 2, 3} Output: Yes Explanation: One possible way of dividing the array is {1, 3} and {2, 2} Input: arr[] = {1, 1, 1, 1} Output: No Naive Approach: The simplest approach to solve the problem is to try all possible combinations of splitting the array into two equal subsets. For each combination, check whether every repetition belongs to only one of the two sets or not. If found to be true, then print “Yes”. Otherwise, print “No”. Time Complexity: O(2N), where N is the size of the given integer. Auxiliary Space: O(N) Efficient Approach: The above approach can be optimized by storing the frequency of all elements of the given array in an array freq[]. For elements to be divided into two equal s...

Java OOPS - SOLID Design Principles

1) Composition Vs Aggregation When studying UML , one of the most important questions which comes to mid of a programmer is , What is the difference between Aggregation and Composition ? Composition implies that the child objects share a lifespan with the parent. Aggregation doesn't. For example, a chess board is composed of chess squares - the chess squares don't really exist without the board. However, a car is an aggregation of parts - a car exhaust is still a car exhaust if it's not part of a car at the time. Composition and Aggregation are types of associations. They are very closely related and in terms of programming there does not appear much difference.  I will try to explain the difference between these two by java code examples Aggregation: the object exists outside the other, is created outside, so it is passed as an argument (for example) to the construtor. Ex: People – car. The car is create in a different context and than becomes a person property. Compositio...

Why Microservices ?

Problem statement: What is microservices? 1. Microservices is a   method of developing software applications as a  unit of independently deployable ,  small ,  modular services  in which  each service runs a unique process  and  communicates through a well defined,   light weight mechanism  to serve a  business goal. 2. microservice gives developer the freedom to independently develop and deploy services. 3. microservices is a variant/type of Service  Oriented Architecture (SOA)  that structures an application as collection (group of individual service) of loosely coupled services. 4. Easy integration and automatic deployment (using open source continuous integration tools like Jenkins, Hudson) 5. When change is required in a certain part of the application,  only the related service can be modified and redeployed — no need to modify and redeploy the entire application 6. Better fault isolation: ...