Brute Force Algorithm in C

Advanced
6 min read
Updated: Nov 2026
1 example

Trying Every Possibility

Brute force (exhaustive search) generates every candidate solution and tests each one. It is the simplest to write and reason about, always correct when implemented properly, and often the right choice for small inputs or as a reference to test faster algorithms. Its cost grows quickly: pairs are O(n^2), subsets O(2^n), permutations O(n!).

Remember
for each candidate:
    if candidate is valid: use it

Cheat Sheet

Keep these facts handy while you work with Brute Force Algorithm in C in C.

TopicDetail
PairsO(n^2)
SubsetsO(2^n)
PermutationsO(n!)
UseSmall inputs, reference solutions

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Divide and Conquer in C.

Example 1 — Find a pair and count the checks

Target 26 in 2, 7, 11, 15.

C
#include <stdio.h>

int main(void) {
    int a[] = {2, 7, 11, 15};
    int n = 4, target = 26, checks = 0;

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            checks++;
            if (a[i] + a[j] == target) {
                printf("pair: %d + %d = %d (checked %d pairs)\n", a[i], a[j], target, checks);
                return 0;
            }
        }
    }
    printf("no pair found after %d checks\n", checks);
    return 0;
}

How It Works

  1. Pairs are tried in the order (2,7), (2,11), (2,15), (7,11), (7,15), (11,15).
  2. Only the last pair matches, so all six are checked.
  3. For n items the loop checks n(n-1)/2 pairs in the worst case.

Edge Cases & Pitfalls

Check these before relying on Brute Force Algorithm in C in real programs.

scale

Ignoring growth

A solution that checks all subsets is unusable beyond about 25 items.

early

Not stopping early

Return as soon as you find a valid answer if only one is needed.

Key Takeaways

  • Try everything.
  • Simple and correct.
  • Great as a baseline.

One line: Brute force (exhaustive search) generates every candidate solution and tests each one.

Frequently Asked Questions

An approach that systematically tries all possible candidates and checks each one for validity.
When the input is small, when you need a simple correct reference, or when no better algorithm is known.
Continue with "Divide and Conquer in C" next. Split a problem into independent halves and combine the results. The C Algorithm Techniques hub lists every topic in order.

Next: Divide and Conquer in C

Split a problem into independent halves and combine the results.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful