🎉 New Year Offer: Already Upto 70% OFF + Extra 26% OFF — Use One Coupon for All Courses → NEWYEAR26 ( Limited Time Offer ) << Professional Courses >>

Brute Force Approach  


Brute Force Approach
  • It is a programming method that tries all possible solutions to find the correct or optimal one.
  • It focuses on simple logic and direct comparison rather than optimization.
  • Note: This approach does not rely on advanced techniques such as two pointers, sliding window, or hashing; instead, it uses nested loops or multiple iterations to explore every possibility.
  • It generally involves multiple nested loops.
  • Its time complexity is generally O(n²) or higher.
  • The brute force method is easy to understand and implement but inefficient for large input sizes.
  • Some basic programs that can be solved using the brute force approach are:
    1. WAP to check whether the number is prime or not.
    2. WAP to check whether a number is an Armstrong number or not
Array Programs based on Brute-Force Approach
  1. WAP to find all pairs of elements whose sum equals a given number
    • 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:");
      
              boolean found = false; // flag to track if any pair is found
      
              // 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);
                          found = true; // mark as found
                      }
                  }
              }
      
              if (!found)
              {
                  System.out.println("No pairs found with the given sum.");
              }
          }
      }
  2. WAP to find the union of two arrays (all unique elements from both arrays combined together)
    • public class UnionOfArrays
      {
          public static void main(String[] args)
          {
              int[] arr1 = {1, 2, 3, 4, 5};
              int[] arr2 = {3, 4, 5, 6, 7};
      
              // Create a new array to store union elements
              int[] union = new int[arr1.length + arr2.length];
              int k = 0;
      
              // Copy all elements from arr1
              for (int i = 0; i < arr1.length; i++)
              {
                  union[k++] = arr1[i];
              }
      
              // Add elements from arr2 that are not already present in arr1
              for (int i = 0; i < arr2.length; i++)
              {
                  boolean isDuplicate = false;
      
                  for (int j = 0; j < arr1.length; j++)
                  {
                      if (arr2[i] == arr1[j])
                      {
                          isDuplicate = true;
                          break;
                      }
                  }
      
                  if (!isDuplicate)
                  {
                      union[k++] = arr2[i];
                  }
              }
      
              System.out.println("Union of the two arrays:");
              for (int i = 0; i < k; i++)
              {
                  System.out.print(union[i] + " ");
              }
          }
      }
Brute-Force Approach Task:
  • Try below programs using Brute-Force Approach in Java:
    1. WAP to calculate the sum and average of array elements
    2. WAP to find the maximum and minimum element in an array
    3. WAP to Merge Two Arrays into One
    4. WAP to Move Zeros to End of an Array
    5. WAP to search for an element in an array (Linear or Binary Search)

Advantages:
  1. Simple and easy to understand: Logic is straightforward and requires no advanced concepts.
  2. Easy to implement: Often written with basic for or while loops.
  3. Always produces correct results, since it checks every possible case.
  4. Helpful for beginners to build fundamental problem-solving logic before learning optimizations.
Disadvantages:
  1. Very slow for large inputs because it checks all combinations or possibilities.
  2. High time complexity — often O(n²), O(2ⁿ), or even O(n!).
  3. Inefficient for real-time or large-scale applications.
  4. Does not use optimization, so it performs many unnecessary calculations.