Sum of Minimum Trees from List of Leaves in Python

Arnab Chakraborty
Updated on 25-Nov-2020 12:40:48

221 Views

Suppose we have a list of numbers called nums. This list is representing the leaf nodes in inorder traversal of a tree. Here the internal nodes have have 2 children and their value is same as the product of the largest leaf value of its left subtree and the largest leaf value of its right subtree. We have to find the sum of the tree with the minimum sum of its valuesSo, if the input is like nums = [3, 5, 10], then the output will be 83.To solve this, we will follow these steps:res := sum of all elements ... Read More

Minimum Sum Subsequence from Consecutive 3 Elements in Python

Arnab Chakraborty
Updated on 25-Nov-2020 12:38:27

991 Views

Suppose we have a a list of numbers called nums, we have to find a minimum sum subsequence from the given list such that at least one number for all groups of three consecutive numbers is selected. If the length of given list is less than 3, a number should still be selected.So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 7, as we can select 2 and 5.To solve this, we will follow these steps:n := size of numsif n is same as 0, thenreturn 0if n is same ... Read More

Find Two Pairs of Numbers with Minimal Sum Difference in Python

Arnab Chakraborty
Updated on 25-Nov-2020 12:36:20

256 Views

Suppose we have a list of numbers called nums and we want to select two pairs of numbers from it such that the absolute difference between the sum of these two pairs is minimized.So, if the input is like nums = [3, 4, 5, 10, 7], then the output will be 1, as we can select these pairs (3 + 7) - (4 + 5) = 1.To solve this, we will follow these steps:distances := a new listfor i in range 0 to size of nums - 2, dofor j in range i + 1 to size of nums - ... Read More

Minimum Operations to Make Lists Strictly Increasing in Python

Arnab Chakraborty
Updated on 25-Nov-2020 12:29:52

264 Views

Suppose we have two list of numbers called A and B, and they are of same length. Now consider we can perform an operation where we can swap numbers A[i] and B[i]. We have to find the number of operations required to make both lists strictly increasing.So, if the input is like A = [2, 8, 7, 10] B = [2, 4, 9, 10], then the output will be 1, as we can swap 7 in A and 9 in B. Then the lists will be like A = [2, 8, 9, 10] and B = [2, 4, 7, 10] ... Read More

Proxy Design Pattern and Its Implementation in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:19:51

285 Views

The Proxy pattern provides a surrogate or placeholder object to control access to another, different object.The Proxy object can be used in the same manner as its containing objectThe ParticipantsThe Subject defines a common interface for the RealSubject and the Proxy such that the Proxy can be used anywhere the RealSubject is expected.The RealSubject defines the concrete object which the Proxy represents.The Proxy maintains a reference to the RealSubject and controls access to it. It must implement the same interface as the RealSubject so that the two can be used interchangeablyProbably. If you've ever had a need to change the ... Read More

Interface Segregation Principle in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:16:41

360 Views

Clients should not be forced to depend upon interfaces that they don't use.The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use.Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one submoduleBefore Interface SegregationExamplepublic interface IProduct {    int ID { get; set; }    double Weight { get; set; }    int Stock { get; set; }    int Inseam { get; set; }    int WaistSize { get; set; } } public class Jeans : IProduct {    public int ID { ... Read More

What is Facade and How to Implement in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:14:49

174 Views

The Facade pattern is a simple structure laid over a more complex structure.The ParticipantsThe Subsystems are any classes or objects which implement functionality but can be "wrapped" or "covered" by the Facade to simplify an interface.The Facade is the layer of abstraction above the Subsystems, and knows which Subsystem to delegate appropriate work to.The Facade pattern is so general that it applies to almost every major app especially those where I couldn't refactor or modify pieces of said apps for various reasons.Examplepublic class HomeFacade {    LightsFacade light;    MusicSystemFacade musicSystem;    AcFacade ac;    public HomeFacade() {     ... Read More

Liskov Substitution Principle and Its Implementation in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:12:32

337 Views

Derived types must be completely substitutable for their base types.Definition:We should be able to treat a child class as though it were the parent class. Essentially this means that all derived classes should retain the functionality of their parent class and cannot replace any functionality the parent provides.Before Liskov Substitutionpublic class Ellipse {    public double MajorAxis { get; set; }    public double MinorAxis { get; set; }    public virtual void SetMajorAxis(double majorAxis){       this.MajorAxis = majorAxis;    }    public virtual void SetMinorAxis(double minorAxis){       this.MajorAxis = minorAxis;    }    public ... Read More

Sum of All Multiples in JavaScript

AmitDiwan
Updated on 25-Nov-2020 12:09:24

270 Views

We are required to write a JavaScript function that takes in a number, say n, as the first argument, and then any number of arguments following that.The idea is to sum all numbers upto n which are divided by any of the numbers specified by second argument and after.For example −If the function is called like this −sumMultiples(15, 2, 3);Then the output should be −const output = 83;Because the numbers are −2, 3, 4, 6, 8, 9, 10, 12, 14, 15ExampleThe code for this will be −const num = 15; const sumMultiple = (num, ...arr) => {    const dividesAny ... Read More

Why Singleton Class is Always Sealed in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:08:03

2K+ Views

The sealed keyword means that the class cannot be inherited from. Declaring constructors private means that instances of the class cannot be created.You can have a base class with a private constructor, but still inherit from that base class, define some public constructors, and effectively instantiate that base class.Constructors are not inherited (so the derived class won't have all private constructors just because the base class does), and that derived classes always call the base class constructors first.Marking the class sealed prevents someone from trivially working around your carefully-constructed singleton class because it keeps someone from inheriting from the class.Examplestatic ... Read More

Advertisements