# Brute-Force Algorithm in C#

A brute force algorithm is used to find a solution by trying all the possible answers and picking the best one.  It is a problem-solving technique.

Brute force algorithms are generally very slow since they do testing a large number of possible answers.

### Advantages of a brute-force algorithm

The following are the advantages of the brute-force algorithm:

• This algorithm finds all the possible solutions, and it also guarantees that it finds the correct solution to a problem.
• This type of algorithm is applicable to a wide range of domains.
• It is mainly used for solving simpler and small problems.
• It can be considered a comparison benchmark to solve a simple problem and does not require any particular domain knowledge.

### Disadvantages of a brute-force algorithm

The following are the disadvantages of the brute-force algorithm:

• It is an inefficient algorithm as it requires solving each and every state.
• It is a very slow algorithm to find the correct solution as it solves each state without considering whether the solution is feasible or not.
• The brute force algorithm is neither constructive nor creative as compared to other algorithms.

``using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BruteForceAlgorithm{    class BruteForceAlgo    {        public delegate bool DelBruteForceAlgo(ref char[] inputs);        static void Main(string[] args)        {            DelBruteForceAlgo testCallback = delegate (ref char[] inputs)            {                var str = new string(inputs);                return (str == "bbc");            };            bool result = BruteForce("abcde", 1, 5, testCallback);            Console.WriteLine(result);        }        public static bool BruteForce(string inputs, int startLength, int endLength, DelBruteForceAlgo testCallback)        {            for (int len = startLength; len <= endLength; ++len)            {                char[] chars = new char[len];                for (int i = 0; i < len; ++i)                    chars[i] = inputs;                if (testCallback(ref chars))                    return true;                for (int i1 = len - 1; i1 > -1; --i1)                {                    int i2 = 0;                    for (i2 = inputs.IndexOf(chars[i1]) + 1; i2 < inputs.Length; ++i2)                    {                        chars[i1] = inputs[i2];                        if (testCallback(ref chars))                            return true;                        for (int i3 = i1 + 1; i3 < len; ++i3)                        {                            if (chars[i3] != inputs[inputs.Length - 1])                            {                                i1 = len;                                goto outerBreak;                            }                        }                    }                outerBreak:                    if (i2 == inputs.Length)                        chars[i1] = inputs;                }            }            return false;        }    }}``
JavaScript
1. JavaScript
Cop
--------------------------------------------------