**Example**:

Int [] a = { 6, 8, 1, 9, 2, 10}; Output: Third smallest element is: 6 Int [] a = { 6, 8, 1, 9, 2, 1, 10, 10}; Output: Third smallest element is: 2 Int [] a = {6}; Output: Invalid Input, array size is less than 3

**Approach**:

- Take three variables; let’s call them
*first*,*second*and*third*and mark them as +∞. - Iterate through the array and for each element (let’s call it
*current*),- Check if
*first>current*, assign the*first*value to the*second*and*second*value to*third*and assign*current*to*first*. - If the above step is not true then the
*current*element might be a candidate of the second smallest element, so check if*current<second*, if yes then assign*second*value to*third*and assign*current*to*second*. - If the above step is not true then the
*current*element might be a candidate of the*third-smallest*element, so check if*current<third*, if yes then assign*current*to*third*.

- Check if
- At the end print the third, it will third-smallest element in the array

See the code below for more understanding.

**Code**:

**Output**:

Third smallest element is: 6]]>

Example:

Test Cases: A B C D E F G E depends on B, D, G D depends on B, C C depends on A B depends on A F no dependency G depends on A Output: Test Case sequence: F A B C D E

Approach: Topological Sorting

- This problem is the classic example of “topological sorting“.
- Let’s consider each test case as Vertex and dependency between two tests as Edge between two vertices. So, for example, B depends on A can be seen as A->B, A has a directed edge towards B. OR it can be read as test B can be executed only when test A is executed.
- Now we can draw a graph and do the topological sort which we have discussed here. So the graph for the example above is

See the code below for more understanding.

**Time Complexity:** O(N), N – Number of test cases.

**Code:**

**Output:**

Test Case Sequence: F A B C D G E]]>

**Use Hash Maps:**

- We will use two maps.
- The first map for storing the vertices and edges.
- Second map for storing the index for each vertex. the second map will be used in traversing the graph to avoid going in loops. (used by the visited array in case of DFS or BFS ).
- The first map will have vertex as key and linked list (connected vertices will be stored in the list) as value.
- See the code below for more understanding.

**Complete Code:**

**Output:**

Vertex A is connected to: G C B Vertex B is connected to: E D Vertex C is connected to: D Vertex D is connected to: E Vertex E is connected to: Vertex F is connected to: Vertex G is connected to: E Depth First Traversal A G E C D B F]]>

**Example:**

Input: "abcd 45, 54 and 50 are the numbers" Output: Maximum Number- 54 Input: aabb123ccdefgh54319hij Output: 54319 Input: 213123123and544455523412423423424234234234 Output: 2147483647 (Max value of Integer) Input: abcd Maximum Number- 0

**Approach:**

**Recommended Article- **String to Integer (AtoI – ASCII to Integer)

- Initialize max_so_far=0, current_no=0.
- Iterate the given string from left to right.
- If the character is number then start constructing current_no with all the contiguous characters with are number using ASCII to integer conversion. Once get the character which is not a number then update the max_so_far with current_no if max_so_far<current_no and make current_no =0.
- Keep track of current_no, once it reaches to Max_Value of Integer, return Max_Value.

- return max_so_far.
- See and run the code below for more understanding.

**Complete Code:**

**Output:**

Input: abcd 45, 54 and 50 are the numbers Maximum Number- 54 Input: aabb123ccdefgh54319hij Maximum Number- 54319 Input: 213123123and544455523412423423424234234234 Maximum Number- 2147483647 Input: abcd Maximum Number- 0]]>

**Example:**

**Approach: **Use Depth-First Search

Start the DFS from any random vertex. Once DFS is completed check if all the vertices are visited. If the graph is completed connected then after first DFS all the vertices will be marked as visited. If the graph is disconnected then start another DFS from any vertex which is still not visited after the first round of DFS and check again if all the vertices are visited. Repeat the above process until all the vertices are visited. Keep counting the no of DFS calls. This will be our answer to the number of subgraphs.

**Complete Code:**

**Output:**

Number of Subgraphs: 4 Number of Subgraphs: 3 Number of Subgraphs: 2 Number of Subgraphs: 1]]>

**Example:**

Original Stack: [14, 9, 67, 91, 101, 25] Reversed Stack: [25, 101, 91, 67, 9, 14]

**Approach:** **Use Recursion**

In this solution, we need two recursive functions. reverse() and insert_at_bottom().

**reverse() –** this function will be called by the driver. In this function, Pop the element from the stack make a recursive call to reverse() till the stack is not empty. This will put all the popped elements in the function stack, and our stack will be empty, in tail recursion insert all these popped elements at the bottom of the stack, one after another using insert_at_bottom().

**insert_at_bottom() –** This function is called with element passed as a parameter and objective of this function to insert the element at the bottom of the stack. If stack is empty, just push the element and if stack is not empty then pop the top element and make a recursive call to insert_at_bottom(), this will pop out all the elements to the function stack and our stack will be empty so that element (passed as parameter) will be pushed to the empty stack and after that all the earlier popped element will be pushed back on top of that.

**Walk Through:**

**Given Stack:** [1, 2, 3, 4]

Let’s first see what happens inside the reverse() function

As you can see the insert_at_bottom() is called 4 times, once for each element. insert_at_bottom() itself is a recursive function. Let’s see what is happening inside the function for one instance. insert_at_bottom(4) is the last time this function was called and produced our final result as well. let’s take a look inside.

**Code:**

**Output:**

Original Stack: [14, 9, 67, 91, 101, 25] Reversed Stack: [25, 101, 91, 67, 9, 14]]]>

**Bridge in Graph:** An edge is called a bridge if connects two subgraphs and removing the edge will disconnect the graph.

In this article, we will take the Graph represented by Adjacency List.

**Example:**

**Approach: Depth-First Search(DFS)**

- Do the DFS to count the number of connected components (If the graph is fully connected then count would be 1).
- Remove the given edge.
- Do the DFS again and count the number of connected components, if the count is the same then given edge is not a bridge. If count is increased by 1 then given edge is a bridge.

**Complete Code:**

**Output:**

Given Edge (0-1) is not a BRIDGE ----------------------------------------- Given Edge (3-4) is not a BRIDGE ----------------------------------------- Given Edge (2-3) is a BRIDGE -----------------------------------------]]>

**Example**:

Input: Prefix expression:+ A BOutput: Postfix expression:A B +Input: Prefix expression:*-A/BC-/AKLOutput: Postfix expression:ABC/-AK/L-*

**Approach**: Use Stacks

**Algorithm:**

Iterate the given expression from left to right, one character at a time

- If the character is operand, push it to the stack.
- If the character is operator,
- Pop an operand from the stack, say it’s s1.
- Pop an operand from the stack, say it’s s2.
- perform
**(s1 s2 operator)**and push it to stack.

- Once the expression iteration is completed, initialize the result string and pop out from the stack and add it to the result.
- Return the result.

Please walk through the example below for more understanding.

**Java Code:**

**Output:**

Prefix Expression: *-A/BC-/AKL Postfix Expression: ABC/-AK/L-*]]>

**Roman Number** – Letters used in Roman numerals and the corresponding numerical values are given in the table below.

**Rules: **

Roman numerals are usually written in highest to lowest from left to right except for some special cases where left character is less than the right character. for example ‘IV’ is equivalent to 4 not ‘IIII’. In such cases, subtract the left character value from the right character value. ‘IV’ will be 5-1 = 4, same for ‘IX’ = 10-1 = 9. Below are the cases –

**I**can be placed before**V**or**X,**represents**subtract****one**, so**IV**(5-1) = 4 and 9 is**IX**(10-1)=9.**X**can be placed before**L**or**C**represents**subtract ten**, so**XL**(50-10) = 40 and**XC**(100-10)=90.**C**placed before**D**or**M**represents**subtract hundred**, so**CD (500-100)=400**and**CM**(1000-100)=900.

**Example:**

Roman: XXV Integer: 25 --------------------------------------------------- Roman: XXXVI Integer: 36 --------------------------------------------------- Roman: MXXIII Integer: 1023 --------------------------------------------------- Roman: DXLII Integer: 542

**Approach:**

- Iterate through given roman number from right to left (reverse).
- Initialize result = 0.
- Take one character at a time and check its corresponding numeral from the table and add it to the result.
- The only special case that needs to be considered is when the character at left has smaller value than the character at right. for example ‘IX’ for 9 or ‘IV’ for 4, in such cases subtract left character value from the result. Except for this special case, In all other cases, all the pairs have left character value >= right character value.

**Walk Through:**

values = [1000,900,500,400,100,90,50,40,10,9,5,4,1] String[] romanLiterals = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]; Input: XCIX result =0 Iterate through right to left. Processing character: X result = 0 + 10 = 10 Processing character: I Compare it with the character at right - X I less than X, reduce the value of I from the result result = 10 - 1 = 9 Processing character: C Compare it with the character at right - I C greater than I, add the value of C to the result result = 100 + 9 = 109 Processing character: X Compare it with the character at right - C X less than C, reduce the value of X from the result result = 109 - 10 = 99

**Code:**

**Output:**

Roman Number: MIXX Integer: 1019 ------------------------------------ Roman Number: IXX Integer: 19 ------------------------------------ Roman Number: DXLII Integer: 542 ------------------------------------ Roman Number: MXXIII Integer: 1023 ------------------------------------]]>

**Example:**

Given Stack: [14, 9, 67, 91, 101, 25]

Sorted Stack: [9, 14, 25, 67, 91, 101]

**Approach:**

- Use another stack, let’s call it a temporary stack.
- While given original is not empty
- Pop the element from the original stack, let’s call it
.*x* - while the temporary stack is not empty and top of the temporary stack is greater than the popped element
– pop the element from the temporary stack and push it back to the original stack.*x* - At this point either temporary stack is empty or top of the temporary stack is <=
so push*x*in the temporary stack.*x*

- Pop the element from the original stack, let’s call it
- Return the temporary stack, it is sorted.

**Walk Through:**

Original Stack: [67, 91, 101, 25] ------------------------------------------------ Popped Element from the original stack: 25 Push 25 in the temporary stack Original Stack: [67, 91, 101] Temporary Stack: [25] ------------------------------------------------ Popped Element from the original stack: 101 Push 101 in the temporary stack Original Stack: [67, 91] Temporary Stack: [25, 101] ------------------------------------------------ Popped Element from the original stack: 91 top of temporary stack 101 is greater than popped element 91 101 pop from the temporary stack and push it to the original stack. Original Stack: [67, 101] Temporary Stack: [25] Push 91 in the temporary stack Original Stack: [67, 101] Temporary Stack: [25, 91] ------------------------------------------------ Popped Element from the original stack: 101 Push 101 in the temporary stack Original Stack: [67] Temporary Stack: [25, 91, 101] ------------------------------------------------ Popped Element from the original stack: 67 top of temporary stack 101 is greater than popped element 67 101 pop from the temporary stack and push it to the original stack. Original Stack: [101] Temporary Stack: [25, 91] top of temporary stack 91 is greater than popped element 67 91 pop from the temporary stack and push it to the original stack. Original Stack: [101, 91] Temporary Stack: [25] Push 67 in the temporary stack Original Stack: [101, 91] Temporary Stack: [25, 67] ------------------------------------------------ Popped Element from the original stack: 91 Push 91 in the temporary stack Original Stack: [101] Temporary Stack: [25, 67, 91] ------------------------------------------------ Popped Element from the original stack: 101 Push 101 in the temporary stack Original Stack: [] Temporary Stack: [25, 67, 91, 101] Sorted Stack is:[25, 67, 91, 101]

**Code:**

**Output:**

Original Stack: [14, 9, 67, 91, 101, 25] Sorted Stack is:[9, 14, 25, 67, 91, 101]]]>