🎉 Festival Deal: Save Big on Professional Courses with Real Projects – Apply Coupons FEST300OFF FEST500OFF
→ Click Here ←

Brute Force Approach  


Introduction
  • It is a simple technique that tries all possible solutions to find the correct one.
  • It does not use any smart tricks or optimizations.
  • It checks each option step by step until the answer is found.
  • In one line, we can say that brute-force tries everything and picks the correct one.
  • Advantages:
    • Very simple to understand.
    • Easy to write code. Example: for(int i = 0; i < n; i++) { ... }
    • Always gives a correct result.
    • Good to build logic for optimization later.
  • Disadvantages:
    • Very slow for large inputs.
    • Time complexity is high (often O(n²), O(2ⁿ), O(n!)).
    • Not suitable for real-time or large applications.
    • Wastes time checking unnecessary possibilities.
Ways to Achieve Brute Force:
  1. Single loops:
    • Checks each element one by one
    • Example: Finding maximum in an array
      public class MaxInArray
      {
          public static void main(String[] args)
          {
              int[] arr = {5, 12, 3, 19, 7};
              
              // Assume first element is max
              int max = arr[0];
      
              // Check every element
              for(int i = 1; i < arr.length; i++)
              {
                  if(arr[i] > max)
                  {
                      max = arr[i];
                  }
              }
      
              System.out.println("Maximum element is: " + max);
          }
      }
  2. Nested loops:
    • Checks all combinations like pairs/triples
    • Example: Finding pairs with given sum
      public class PairWithSum
      {
          public static void main(String[] args)
          {
              int[] arr = {2, 7, 4, 5, 1, 3};
              int target = 6;
      
              System.out.println("Pairs with given sum " + target + " are:");
      
              // Brute force approach: check all pairs
              for(int i = 0; i < arr.length; i++)
              {
                  for(int j = i + 1; j < arr.length; j++)
                  {
                      if(arr[i] + arr[j] == target)
                      {
                          System.out.println(arr[i] + " + " + arr[j] + " = " + target);
                      }
                  }
              }
          }
      }
  3. Recursion:
    • Generates all possibilities step by step
    • Example: Solving N-Queens problem
      public class NQueens
      {
          public static void main(String[] args)
          {
              int N = 4; // Size of chessboard
              int[] board = new int[N]; // board[i] = column of queen in row i
              solveNQueens(board, 0, N);
          }
      
          // Recursive function to place queens row by row
          public static void solveNQueens(int[] board, int row, int N)
          {
              if(row == N)
              {
                  printBoard(board, N); // All queens placed successfully
                  return;
              }
      
              // Try all columns in current row
              for(int col = 0; col < N; col++)
              {
                  if(isSafe(board, row, col))
                  {
                      board[row] = col; // Place queen
                      solveNQueens(board, row + 1, N); // Move to next row
                  }
              }
          }
      
          // Check if placing queen at (row, col) is safe
          public static boolean isSafe(int[] board, int row, int col)
          {
              for(int i = 0; i < row; i++)
              {
                  // Same column or diagonal attack
                  if(board[i] == col || Math.abs(board[i] - col) == row - i)
                  {
                      return false;
                  }
              }
              return true;
          }
      
          // Print the board
          public static void printBoard(int[] board, int N)
          {
              for(int i = 0; i < N; i++)
              {
                  for(int j = 0; j < N; j++)
                  {
                      if(board[i] == j) System.out.print("Q ");
                      else System.out.print(". ");
                  }
                  System.out.println();
              }
              System.out.println();
          }
      }
    and many more such problems...

Brute-Force Approach Task:

Try below programs using Brute-Force Approach in Java:

  1. WAP to Generate All Subsets (Power Set) of a Set
  2. WAP to Count All Inversions in an Array
  3. WAP to Solve the Traveling Salesman Problem (Brute Force)
  4. WAP to Find All Permutations of a String
  5. WAP to Check if a String is a Palindrome by Checking All Substrings