Find Minimum Operations Required To Convert String1 To String2

Write a program to find out the minimum number of operations required to convert source string to target string.
To convert source string to target string, only insert and delete character operation are allowed.
For eg:

  • Source = “ABCD”, Target = “ACDB” – Operations = 2 (Delete B and insert B at end)
  • Source = “ABCDEF”, Taget = “ACDEF” – Operations = 1 (Delete B)
(more…)
Find Minimum Operations Required To Convert String1 To String2 Read More

Find 3 Peak And Valley In An Array Separated By N Indices

Given an array of size n, we have to find 3 peak values and 3 trough values in the array such that each peak values are separated by another peak value by n entries.
Similarly, each trough value must be n days separated from the other trough values. For eg:

int arr[] = {1, 5, 6, 4, 3, 3, 10, 2, 8, 6, 11, 4, 10};
int distance = 2;

In above example, peak values are – 6, 10 and 11 (which are 2 indices apart).

valley values are – 1, 3 and 2 (which are 2 indices apart).

(more…)
Find 3 Peak And Valley In An Array Separated By N Indices Read More

Connected Graph Property Explained With Simple Example

Graph is a data structure which consists of a set of vertices which is called as Node, together with a set of collection of pair of vertices which is called as an Edge.
A graph data structure can be represented as a pair (V, E) where V is a set of nodes called vertices and E is a collection of pairs of vertices called edges. Graphs are used to solve many real-life problems such as fastest ways to go from A to B etc.

A Graph is called connected graph if each of the vertices of the graph is connected from each of the other vertices which means there is a path available from any vertex to any other vertex in the Graph.

Before going ahead have a look into Graph Basics.

(more…)
Connected Graph Property Explained With Simple Example Read More

Topological Sort Explained With Simple Example

Topological sort is a method to sort the vertices in directed acyclic graph in which each node comes before all the nodes to which it has edges going to. Topological sort is mainly used in cases where a certain node can be visited if and only if certain nodes has been visited before.

A Directed Acyclic graph or DAG is a graph which doesn’t have any cycle.

All pairs of consecutive vertices in topological sorted order are connected by edges which forms a directed Hamiltonian Path.

(more…)
Topological Sort Explained With Simple Example Read More

Replace Character of String With Another Character N Place Down the Alphabet

Given a string ‘s’ and a number ‘n’, write a function that returns a string with each character in s replaced with another character that is ‘n’ positions down the alphabet.

If n is 1, A is replaced with B, B is replaced with C, Z is replaced with A and so on.

If n is 2, A is replaced with C, B is replaced with D, Z is replaced with B and so on.

(more…)
Replace Character of String With Another Character N Place Down the Alphabet Read More

Friend Function Explained With Simple Example

A Friend function is a function defined outside the class, but it has access to all private and protected members of the class. To declare a friend function, it’s prototype must be declared inside the class, preceding it with keyword “friend”. For eg:

class Demo
{
    private:
        int m;
        int y;

    public:
        friend int sum (Demo d);
        void print_val ();
        Demo (int m, int y);
};
(more…)
Friend Function Explained With Simple Example Read More

Replace every character by character that is N positions down the alphabet of the String

Given a string ‘s’ and a number ‘n’, write a function that returns a string with each character in s replaced with another character that is ‘n’ positions down the alphabet.
For example:

  • If n is 1, A is replaced with B, B is replaced with C and so on.
  • If n is 2, A is replaced with C, B is replaced with D and so on.
  • If n is 3, X is replaced with A, Y is replaced with B and so on.
(more…)
Replace every character by character that is N positions down the alphabet of the String Read More