The Wayback Machine - https://web.archive.org/web/20240915183731/https://www.geeksforgeeks.org/middle-of-three-using-minimum-comparisons/
Open In App

Middle of three using minimum comparisons

Last Updated : 07 Jul, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given three distinct numbers a, b and c find the number with a value in middle.

Examples:

Input : a = 20, b = 30, c = 40
Output : 30

Input : a = 12, n = 32, c = 11
Output : 12

Simple Approach:

C++




// CPP program to find  middle of three distinct
// numbers
#include <bits/stdc++.h>
using namespace std;
 
int middleOfThree(int a, int b, int c)
{
    // Checking for b
    if ((a < b && b < c) || (c < b && b < a))
       return b;
 
    // Checking for a
    else if ((b < a && a < c) || (c < a && a < b))
       return a;
 
    else
       return c;
}
 
// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}


Java




// Java program to find middle of
// three distinct numbers
import java.util.*;
 
class Middle
{  
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // Checking for b
        if ((a < b && b < c) || (c < b && b < a))
            return b;
 
        // Checking for a
        else if ((b < a && a < c) || (c < a && a < b))
        return a;
 
        else
        return c;
    }
     
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by rishabh_jain


Python3




# Python3 program to find middle
# of three distinct numbers
 
def middleOfThree(a, b, c):
     
    # Checking for b
    if ((a < b and b < c) or (c < b and b < a)) :
        return b;
 
    # Checking for a
    if ((b < a and a < c) or (c < a and a < b)) :
        return a;
 
    else :
        return c
 
# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))
 
# This code is contributed by rishabh_jain


C#




// C# program to find middle of
// three distinct numbers
using System;
 
class Middle
{
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                        int c)
    {
        // Checking for b
        if ((a < b && b < c) || (c < b && b < a))
            return b;
 
        // Checking for a
        else if ((b < a && a < c) || (c < a && a < b))
        return a;
 
        else
        return c;
    }
     
    // Driver code
    public static void Main()
    {
        int a = 20, b = 30, c = 40;
        Console.WriteLine( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by vt_m


PHP




<?php
// PHP program to find middle
// of three distinct numbers
 
function middleOfThree($a, $b, $c)
{
     
    // Checking for b
    if (($a < $b && $b < $c) or
        ($c < $b && $b < $a))
    return $b;
 
    // Checking for a
    else if (($b < $a and $a < $c) or
             ($c < $a and $a < $b))
    return $a;
 
    else
    return $c;
}
 
    // Driver Code
    $a = 20;
    $b = 30;
    $c = 40;
    echo middleOfThree($a, $b, $c);
     
// This code is contributed by anuj_67.
?>


Javascript




<script>
 
// Javascript program to find  middle of three distinct
// numbers
 
function middleOfThree( a,  b, c)
{
    // Checking for b
    if ((a < b && b < c) || (c < b && b < a))
       return b;
 
    // Checking for a
    else if ((b < a && a < c) || (c < a && a < b))
       return a;
 
    else
       return c;
}
 
    // driver code
 
    let a = 20, b = 30, c = 40;
    document.write(middleOfThree(a, b, c));
     
</script>


Output

30

Time Complexity: O(1)
Auxiliary Space: O(1)

But approach used above is not efficient because of extra comparisons, which can be easily minimized. In case first part is false, it will execute remaining half to check the condition. This problem remains same if we are checking for a also. 

Better Approach (Needs less comparison):

C++




// CPP program to find middle of three distinct
// numbers
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the middle of three numbers
int middleOfThree(int a, int b, int c)
{
    // Compare each three number to find middle
    // number. Enter only if a > b
    if (a > b)
    {
        if (b > c)
            return b;
        else if (a > c)
            return c;
        else
            return a;
    }
    else
    {
        // Decided a is not greater than b.
        if (a > c)
            return a;
        else if (b > c)
            return c;
        else
            return b;
    }
}
 
// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}


Java




// Java program to find middle of
// three distinct numbers
import java.util.*;
 
class Middle
{  
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // Compare each three number to find
        // middle number. Enter only if a > b
        if (a > b)
        {
            if (b > c)
                return b;
            else if (a > c)
                return c;
            else
                return a;
        }
        else
        {
            // Decided a is not greater than b.
            if (a > c)
                return a;
            else if (b > c)
                return c;
            else
                return b;
        }
    }
     
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println(middleOfThree(a, b, c));
    }
}
 
// This code is contributed by rishabh_jain


Python3




# Python3 program to find middle
# of three distinct numbers
 
# Function to find the middle of three numbers
def middleOfThree(a, b, c) :
     
    # Compare each three number to find
    # middle number. Enter only if a > b
    if a > b :
        if (b > c):
            return b
        elif (a > c) :
            return c
        else :
            return a
    else:
        # Decided a is not greater than b.
        if (a > c) :
            return a
        elif (b > c) :
            return c
        else :
            return b
 
# Driver Code
a = 20
b = 30
c = 40
print( middleOfThree(a, b, c) )
 
# This code is contributed by rishabh_jain


C#




// C# program to find middle of
// three distinct numbers
using System;
 
class Middle
{
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                            int c)
    {
        // Compare each three number to find
        // middle number. Enter only if a > b
        if (a > b)
        {
            if (b > c)
                return b;
            else if (a > c)
                return c;
            else
                return a;
        }
        else
        {
            // Decided a is not greater than b.
            if (a > c)
                return a;
            else if (b > c)
                return c;
            else
                return b;
        }
    }
     
    // Driver code
    public static void Main()
    {
        int a = 20, b = 30, c = 40;
        Console.WriteLine(middleOfThree(a, b, c));
    }
}
 
// This code is contributed by vt_m


PHP




<?php
// PHP program to find middle
// of three distinct numbers
 
// Function to find the middle
// of three numbers
function middleOfThree( $a, $b, $c)
{
     
    // Compare each three number
    // to find middle number.
    // Enter only if a > b
    if ($a > $b)
    {
        if ($b > $c)
            return $b;
        else if ($a > $c)
            return $c;
        else
            return $a;
    }
    else
    {
         
        // Decided a is not
        // greater than b.
        if ($a > $c)
            return $a;
        else if ($b > $c)
            return $c;
        else
            return $b;
    }
}
 
    // Driver Code
    $a = 20; $b = 30;
    $c = 40;
    echo middleOfThree($a, $b, $c);
 
// This code is contributed by anuj_67.
?>


Javascript




<script>
    // Javascript program to find middle of three distinct numbers
     
    // Function to find the middle of three number
    function middleOfThree(a, b, c)
    {
        // Compare each three number to find
        // middle number. Enter only if a > b
        if (a > b)
        {
            if (b > c)
                return b;
            else if (a > c)
                return c;
            else
                return a;
        }
        else
        {
            // Decided a is not greater than b.
            if (a > c)
                return a;
            else if (b > c)
                return c;
            else
                return b;
        }
    }
     
    let a = 20, b = 30, c = 40;
    document.write(middleOfThree(a, b, c));
     
    // This code is contributed by divyesh072019.
</script>


Output

30

Time Complexity: O(1)
Auxiliary Space: O(1)
This approach is efficient and having less number of comparisons. Outer IF loop will only be executed if a > b otherwise, outer ELSE will be executed. 

Other Efficient Approach:

This approach is condensed version of the 1st approach.

(a>b and b>c) or (a<b and b<c) can also be decoded as a-b>0, b-c>0 or a-b<0,b-c<0 means the difference of a, b and b, c should be of same sign. So let x = a-b and y = b-c and if x, y have same sign then their result will be always positive. So b is middle element.

Similarly we can check for c and a.

let z = c – a and if  (b < a and a < c) or (c < a and a < b) can be decoded as a-b>0 , c-a>0 or a-b < 0, c-a<0 means x*z > 0. So the middle element will be a.

C++




// CPP program to find middle of three distinct
// numbers
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the middle of three number
int middleOfThree(int a, int b, int c)
{
    // x is positive if a is greater than b.
    // x is negative if b is greater than a.
    int x = a - b;
 
    int y = b - c;  // Similar to x
    int z = a - c;  // Similar to x and y.
 
    // Checking if b is middle (x and y both
    // are positive)
    if (x * y > 0)
        return b;
 
    // Checking if c is middle (x and z both
    // are positive)
    else if (x * z > 0)
        return c;
    else
        return a;
}
 
// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}


Java




//java program to find middle of three distinct
// numbers
import java.util.*;
 
class Middle
{  
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // x is positive if a is greater than b.
        // x is negative if b is greater than a.
        int x = a - b;
 
        int y = b - c; // Similar to x
        int z = a - c; // Similar to x and y.
 
        // Checking if b is middle (x and y
        // both are positive)
        if (x * y > 0)
            return b;
 
        // Checking if c is middle (x and z
        // both are positive)
        else if (x * z > 0)
            return c;
        else
            return a;
    }
     
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by rishabh_jain


Python3




# Python3 program to find middle
# of three distinct numbers
 
# Function to find the middle of three number
def middleOfThree(a, b, c) :
     
    # x is positive if a is greater than b.
    # x is negative if b is greater than a.
    x = a - b
 
    # Similar to x
    y = b - c
     
    # Similar to x and y.
    z = a - c
 
    # Checking if b is middle (x and y
    # both are positive)
    if x * y > 0:
        return b
 
    # Checking if c is middle (x and z
    # both are positive)
    elif (x * z > 0) :
        return c
    else :
        return a
 
# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))
 
# This code is contributed by rishabh_jain


C#




//C# program to find middle of three distinct
// numbers
using System;
 
class Middle
{
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                        int c)
    {
        // x is positive if a is greater than b.
        // x is negative if b is greater than a.
        int x = a - b;
         
        // Similar to x
        int y = b - c;
         
         // Similar to x and y.
        int z = a - c;
 
        // Checking if b is middle (x and y
        // both are positive)
        if (x * y > 0)
            return b;
 
        // Checking if c is middle (x and z
        // both are positive)
        else if (x * z > 0)
            return c;
        else
            return a;
    }
     
    // Driver code
    public static void Main()
    {
        int a = 20, b = 30, c = 40;
        Console.WriteLine( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by vt_m


PHP




<?php
// PHP program to find middle
// of three distinct numbers
 
// Function to find the
// middle of three number
function middleOfThree($a, $b, $c)
{
     
    // x is positive if a
    // is greater than b.
    // x is negative if b
    // is greater than a.
    $x = $a - $b;
 
    // Similar to x
    $y = $b - $c;
     
    // Similar to x and y.
    $z = $a - $c;
 
    // Checking if b is
    // middle (x and y both
    // are positive)
    if ($x * $y > 0)
        return $b;
 
    // Checking if c is
    // middle (x and z both
    // are positive)
    else if ($x * $z > 0)
        return $c;
    else
        return $a;
}
 
    // Driver Code
    $a = 20;
    $b = 30;
    $c = 40;
    echo middleOfThree($a, $b, $c);
 
// This code is contributed by anuj_67.
?>


Javascript




<script>
 
// javascript program to find middle of
//  three distinct numbers
  
    // Function to find the middle of three number
    function middleOfThree(a, b, c)
    {
        // x is positive if a is greater than b.
        // x is negative if b is greater than a.
        let x = a - b;
  
        let y = b - c; // Similar to x
        let z = a - c; // Similar to x and y.
  
        // Checking if b is middle (x and y
        // both are positive)
        if (x * y > 0)
            return b;
  
        // Checking if c is middle (x and z
        // both are positive)
        else if (x * z > 0)
            return c;
        else
            return a;
    }
     
// Driver code
 
        let a = 20, b = 30, c = 40;
        document.write( middleOfThree(a, b, c) );
 
</script>


Output

30

Time Complexity: O(1)
Auxiliary Space: O(1)



Similar Reads

Second minimum element using minimum comparisons
Given an array of integers, find the minimum (or maximum) element and the element just greater (or smaller) than that in less than 2n comparisons. The given array is not necessarily sorted. Extra space is allowed. Examples: Input: {3, 6, 100, 9, 10, 12, 7, -1, 10}Output: Minimum: -1, Second minimum: 3We have already discussed an approach in below p
14 min read
Maximum and minimum of an array using minimum number of comparisons
Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons. Examples: Input: arr[] = {3, 5, 4, 1, 9}Output: Minimum element is: 1 Maximum element is: 9 Input: arr[] = {22, 14, 8, 17, 35, 3}Output: Minimum element is: 3 Maximum element is: 35 Recommended ProblemFind minimum
15+ min read
Search an element in an unsorted array using minimum number of comparisons
Given an array of n distinct integers and an element x. Search the element x in the array using minimum number of comparisons. Any sort of comparison will contribute 1 to the count of comparisons. For example, the condition used to terminate a loop, will also contribute 1 to the count of comparisons each time it gets executed. Expressions like whil
7 min read
Minimum comparisons required to find the only heavier ball from N balls
Given N (N &gt; 1) number of balls and a weight balancing machine. There are N-1 balls of the same weight and one ball heavier than the others. The task is to find out the minimum number of times weighing is required to find the heavier ball where any number of balls can be weighted each time. Note: The weight balancing machine can tell relative we
5 min read
Comparisons involved in Modified Quicksort Using Merge Sort Tree
In QuickSort, ideal situation is when median is always chosen as pivot as this results in minimum time. In this article, Merge Sort Tree is used to find median for different ranges in QuickSort and number of comparisons are analyzed.Examples: Input : arr = {4, 3, 5, 1, 2} Output : 11 Explanation We have to make 11 comparisons when we apply quick so
15+ min read
Number of comparisons in each direction for m queries in linear search
Given an array containing N distinct elements. There are M queries, each containing an integer X and asking for the index of X in the array. For each query, the task is to perform linear search X from left to right and count the number of comparisons it took to find X and do the same thing right to left. In the end, print the total number of compar
7 min read
How to make Mergesort to perform O(n) comparisons in best case?
As we know, Mergesort is a divide and conquer algorithm that splits the array to halves recursively until it reaches an array of the size of 1, and after that it merges sorted subarrays until the original array is fully sorted. Typical implementation of merge sort works in O(n Log n) time in all three cases (best, average and worst). We need to red
3 min read
Multiple comparisons in a C++ priority queue?
What is a Priority Queue? A Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in which they are removed). If in any case, the elements have the same prior
5 min read
Number of quadruples where the first three terms are in AP and last three terms are in GP
Given an array arr[] of N integers. The task is to find the number of index quadruples (i, j, k, l) such that a[i], a[j] and a[k] are in AP and a[j], a[k] and a[l] are in GP. All the quadruples have to be distinct.Examples: Input: arr[] = {2, 6, 4, 9, 2} Output: 2 Indexes of elements in the quadruples are (0, 2, 1, 3) and (4, 2, 1, 3) and correspon
9 min read
Find three element from given three arrays such that their sum is X | Set 2
Given three sorted integer arrays A[], B[] and C[], the task is to find three integers, one from each array such that they sum up to a given target value X. Print Yes or No depending on whether such triplet exists or not.Examples: Input: A[] = {2}, B[] = {1, 6, 7}, C[] = {4, 5}, X = 12 Output: Yes A[0] + B[1] + C[0] = 2 + 6 + 4 = 12Input: A[] = {2}
9 min read
Find three closest elements from given three sorted arrays
Given three sorted arrays A[], B[] and C[], find 3 elements i, j and k from A, B and C respectively such that max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i])) is minimized. Here abs() indicates absolute value. Example : Input : A[] = {1, 4, 10} B[] = {2, 15, 20} C[] = {10, 12} Output: 10 15 10Explanation: 10 from A, 15 from B and 10 from C
15+ min read
Find three element from different three arrays such that a + b + c = sum
Given three integer arrays and a "sum", the task is to check if there are three elements a, b, c such that a + b + c = sum and a, b and c belong to three different arrays. Examples : Input : a1[] = { 1 , 2 , 3 , 4 , 5 }; a2[] = { 2 , 3 , 6 , 1 , 2 }; a3[] = { 3 , 2 , 4 , 5 , 6 }; sum = 9 Output : Yes 1 + 2 + 6 = 9 here 1 from a1[] and 2 from a2[] a
15+ min read
Program to find GCD or HCF of two numbers using Middle School Procedure
Given two positive integers M and N, the task is to find the greatest common divisor (GCD) using Middle School Procedure. Note: GCD of two integers is the largest positive integer that divides both of the integers. Examples: Input: m = 12, n = 14 Output: 2 Prime factor of 12 = 1*2*2*3 Prime factor of 14 = 1*2*7 GCD(12, 14) = 2 Input: m = 5, n = 10
10 min read
Shortest Path using Meet In The Middle
Given a permutation P = p1, p2, ...., pn of first n natural numbers (1 ? n ? 10). One can swap any two consecutive elements pi and pi + 1 (1 ? i &lt; n). The task is to find the minimum number of swaps to change P to another permutation P' = p'1, p'2, ...., p'n. Examples: Input: P = "213", P' = "321"Output: 2213 &lt;-&gt; 231 &lt;-&gt; 321 Input: P
8 min read
Traverse Linked List from middle to left-right order using recursion
Given a Linked List. The task is to traverse the Linked List from middle to left-right order using recursion.For Example: If the given Linked List is: 2 -&gt; 5 -&gt; 8 -&gt; 3 -&gt; 7 -&gt; 9 -&gt; 12 -&gt; NULL The Middle to left-right order is : 3, 8, 7, 5, 9, 2, 12 Explanation: Middle of the given linked list is '3' so, we start traversing from
15+ min read
Minimum Difference between multiples of three integers
Given three integers X, Y, and Z. the task is to find the minimum distance between any two multiples of the given integers. Note: Common multiples are not considered. Examples: Input: X = 5, Y = 2, Z = 3Output: 1Explanation: The multiples if arranged in sorted order are 0, 2, 3, 4, 5, 6, 8. . . Out of which the minimum possible difference is 1 betw
4 min read
Minimum area of a Polygon with three points given
Given three points of a regular polygon(n &gt; 3), find the minimum area of a regular polygon (all sides same) possible with the points given.Examples: Input : 0.00 0.00 1.00 1.00 0.00 1.00 Output : 1.00 By taking point (1.00, 0.00) square is formed of side 1.0 so area = 1.00 . One thing to note in question before we proceed is that the number of s
14 min read
Minimum characters that are to be inserted such that no three consecutive characters are same
Given a string str and the task is to modify the string such that no three consecutive characters are the same. In a single operation, any character can be inserted at any position in the string. Find the minimum number of such operations required. Examples: Input : str = "aabbbcc" Output: 1 Explanation: "aabbdbcc" is the modified string. Input: st
5 min read
Remove minimum characters from string to split it into three substrings under given constraints
Given a string str of lowercase alphabets, the task is to remove minimum characters from the given string so that string can be break into 3 substrings str1, str2, and str3 such that each substring can be empty or can contains only characters 'a', 'b', and 'c' respectively.Example: Input: str = "aaaabaaxccac" Output: 3 Explanation: String after rem
8 min read
Minimum sum of absolute differences of pairs in a triplet from three arrays
Given three arrays a[], b[] and c[] of sizes A, B and C respectively, the task is to find the minimum possible value of abs(a[i] - b[j]) + abs(b[j] - c[k]) where 0 ? i ? A, 0 ? j ? B and 0 ? k ? C. Examples: Input: A = 3, B = 2, C = 2, a[] = {1, 8, 5}, b[] = {2, 9}, c[] = {5, 4}Output: 3Explanation:The triplet (a[0], b[0], c[1]), i.e. (1, 2, 4) has
11 min read
Minimum degree of three nodes forming a triangle in a given Graph
Given an undirected graph consisting of N vertices and M edges and an array edges[][], with each row representing two vertices connected by an edge, the task is to find the minimum degree of three nodes forming a triangle in the graph. If there doesn't exist any triangle in the graph, then print "-1". Examples: Input: N = 7, Edges = [[1, 2], [1, 3]
10 min read
Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
Given an array of n distinct positive integers. The task is to find the minimum number of elements to be removed such that no three consecutive elements in the array are either increasing or decreasing. That is, after removal either ai - 1 &gt; ai &lt; ai + 1 or ai - 1 &lt; ai &gt; ai + 1. Examples : Input : arr[] = {5, 2, 3, 6, 1} Output : 1 Given
8 min read
Find minimum sum such that one of every three consecutive elements is taken
Given an array of n non-negative numbers, the task is to find the minimum sum of elements (picked from the array) such that at least one element is picked out of every 3 consecutive elements in the array. Examples : Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {1, 2, 3, 6, 7, 1} Output : 4 We pick 3 and 1 (3 + 1 = 4) Note that there are fol
9 min read
Given a linked list of line segments, remove middle points
Given a linked list of coordinates where adjacent points either form a vertical line or a horizontal line. Delete points from the linked list which are in the middle of a horizontal or vertical line. Examples: Input: (0,10)-&gt;(1,10)-&gt;(5,10)-&gt;(7,10) | (7,5)-&gt;(20,5)-&gt;(40,5) Output: Linked List should be changed to following (0,10)-&gt;(
14 min read
Middle term in the binomial expansion series
Given three integers A, X, and n. The task is to find the middle term in the binomial expansion series. Examples: Input : A = 1, X = 1, n = 6Output : MiddleTerm = 20 Input : A = 2, X = 4, n = 7Output : MiddleTerm1 = 35840, MiddleTerm2 = 71680 Approach (A + X)n = nC0 An X0 + nC1 An-1 X1 + nC2 An-2 X2 + ......... + nCn-1 A1 Xn-1 + nCn A0 Xn Total num
11 min read
Value of k-th index of a series formed by append and insert MEX in middle
Given two integers, n and k. Initially we have a sequence consisting of a single number 1. We need to consider series formed after n steps. In each step, we append the sequence to itself and insert the MEX(minimum excluded)(&gt;0) value of the sequence in the middle. Perform (n-1) steps. Finally, find the value of the k-th index of the resulting se
6 min read
Iterative approach for removing middle points in a linked list of line segments
This post explains the iterative approach of this problem. We maintain two pointers, prev and temp. If these two have either x or y same, we move forward till the equality holds and keep deleting the nodes in between. The node from which the equality started, we adjust the next pointer of that node. Implementation: C/C++ Code // C++ program to remo
9 min read
Sum of middle row and column in Matrix
Given an integer matrix of odd dimensions (3 * 3, 5 * 5). then the task is to find the sum of the middle row &amp; column elements. Examples: Input : 2 5 7 3 7 2 5 6 9 Output : Sum of middle row = 12 Sum of middle column = 18 Input : 1 3 5 6 7 3 5 3 2 1 1 2 3 4 5 7 9 2 1 6 9 1 5 3 2 Output : Sum of middle row = 15 Sum of middle column = 18 Implemen
7 min read
Find middle of singly linked list Recursively
Given a singly linked list and the task is to find the middle of the linked list. Examples: Input : 1-&gt;2-&gt;3-&gt;4-&gt;5 Output : 3 Input : 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6 Output : 4 We have already discussed Iterative Solution. In this post iterative solution is discussed. Count total number of nodes in the list in recursive manner and do hal
5 min read
Sorting array with reverse around middle
Consider the given array arr[], we need to find if we can sort array with the given operation. The operation is We have to select a subarray from the given array such that the middle element(or elements (in case of even number of elements)) of subarray is also the middle element(or elements (in case of even number of elements)) of the given array.
6 min read
Practice Tags :