πŸŽ‰ New Year Offer: Already Upto 70% OFF + Extra 26% OFF β€” Use One Coupon for All Courses β†’ NEWYEAR26 ( Limited Time Offer ) << Professional Courses >>

Iterative Approach  


Introduction
  • It is a programming method that uses simple repetition β€” loops like for and while β€” to process each element sequentially.
  • Note: This approach does not use recursion; therefore, it improves performance by saving both memory and execution time.
  • It typically uses a single loop (and occasionally two independent loops).
  • Its average time complexity is generally Linear (O(n)) or near-linear (O(n log n)).
  • Some basic programs that can be solved using the iterative approach are:
    1. WAP to print numbers from 1 to n
    2. WAP to find the factorial of a number
    3. WAP to reverse a number
    4. WAP to count the number of digits in a number
    5. WAP to check whether a number is prime
Array Programs based on Iterative Approach
  1. Using Loops for Linear Problems:
    • WAP to find the sum of all elements in an array
      public class SumOfArray
      {
          public static void main(String[] args)
          {
              int[] arr = {4, 7, 1, 3, 6};
              int sum = 0;
      
              for(int i = 0; i < arr.length; i++)
              {
                  sum += arr[i]; // adding each element
              }
      
              System.out.println("Total Sum: " + sum);
          }
      }
  2. Iterating with Conditions:
    • WAP to count even numbers in an array
      public class CountEven
      {
          public static void main(String[] args)
          {
              int[] arr = {2, 5, 8, 11, 14};
              int count = 0;
      
              for(int i = 0; i < arr.length; i++)
              {
                  if(arr[i] % 2 == 0)
                  {
                      count++;
                  }
              }
      
              System.out.println("Total Even Numbers: " + count);
          }
      }
Iterative Approach Task:
  • Try below programs using Iterative 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. Faster and more memory-efficient compared to recursion since it avoids the overhead of recursive function calls.
  2. No stack overflow risk, as it does not use the call stack for repeated function calls.
  3. Easier to debug and trace because the control flow is straightforward and linear.
  4. Better performance for problems involving large input sizes or simple repetitive computations.
Disadvantages:
  1. The code may become lengthy and complex for problems that are naturally recursive (e.g., tree or graph traversal).
  2. Sometimes harder to visualize, as recursion often mirrors the logical structure of certain problems.
  3. Not ideal for divide-and-conquer type problems where recursion provides a more elegant solution.