Checking if 3 numbers are an arithmetic progression Mod note: This question is being discussed on meta


I tried to create a method, that given 3 int numbers and a bool returns true if they are in strict increasing order (by the same amount) like (1, 2, 3), (3, 7, 11) or (0, 4, 8). Except if equalOk is set to true, then equality of two adjacent elements is allowed, such as (5,5,7) or (3,3,3) which should also return true;.

Is there any redundant code in my solution and how could it be done simpler?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InOrderEqual
{
    class Program
    {
        static void Main(string[] args)
        {
            InOrderEqual(2, 5 , 11, false);  //true  
            InOrderEqual(0, 5, 10, false);  //true
            InOrderEqual(5, 4, 3, true);   //false
            InOrderEqual(5, 5, 7, true);   //true
            InOrderEqual(1, 2, 3, true);   //true
            InOrderEqual(12, 12, 12, false); // false
            InOrderEqual(5, 8, 11, false); // true
            Console.ReadLine();
        }
        public static bool InOrderEqual(int a, int b, int c, bool equalOk)
        {
            bool isEqual = false;
            if (b == a || (a == b &amp;&amp; a == c))
            {
                isEqual = true;
                if (equalOk == true)
                {
                    Console.WriteLine(true);
                    return true;
                }
                else Console.WriteLine(false);
                return false;
            }


            else if (( a + a) - b == (b + b) - c|| b - a == c - b &amp;&amp; c &gt; b &amp;&amp; b &gt; a)
            {
                Console.WriteLine(true);
                return true;
            }

            Console.WriteLine(false);
            return false;
        }
    }
}
The requirements are that (1) b - a == c - b and that (2) a &lt; b &lt; c, except when equalOk is true, in which case (3) a &lt;= b &lt;= c. If (1) is true, then it is sufficient to just check a &lt; b for (2) and a &lt;= b for (3).

private bool InOrderEqual(int a, int b, int c, bool equalOk) {
    if (b - a != c - b) return false; // (1)
    if (equalOk) return a &lt;= b; // (3)
    return a &lt; b; // (2)
}


Originally, equidistance was not an explicit requirement.

Original answer:

The requirements are that a &lt; b &lt; c, except when equalOk is true, in which case a &lt;= b &lt;= c. There's no need for anything more complex than this:

private bool InOrderEqual(int a, int b, int c, bool equalOk) {
    if (equalOk) return a &lt;= b &amp;&amp; b &lt;= c;
    return a &lt; b &amp;&amp; b &lt; c;
}