Printing the patterns using only one loop

31,191

Solution 1

I want optimization. The time complexity of two loops will be O(n^2) whereas for only one loop it will be O(n). And O(n) < O(n^2).

You realise that 99.999% of the time will be spent updating the console. If you want to save time, don't write anything. The time taken to loop is trivial by comparison.

BTW The number of stars you produce will be O(N^2) so the time complexity with be O(N^2) whether you use 1, 2 or 3 loops.

Solution 2

The following code will print Diamond shape using single loop. You can change the value of size variable accordingly.

import java.util.Arrays;

class DiamondShapeUsingSingleLoop{
    public static void main(String args[]){
        int size = 5;
        for(int rowNumber = -size +1 ; rowNumber < size ; rowNumber++){
            char row[] = new char[2*size - Math.abs(rowNumber) - 1];
            Arrays.fill(row,0,Math.abs(rowNumber),' ');
            Arrays.fill(row,Math.abs(rowNumber), row.length,'*');
            System.out.println(String.valueOf(row));
        }
    }
}

Output is following :

    *                                                                                                                                                           
   ***                                                                                                                                                          
  *****                                                                                                                                                         
 *******                                                                                                                                                        
*********                                                                                                                                                       
 *******                                                                                                                                                        
  *****                                                                                                                                                         
   ***                                                                                                                                                          
    *

Solution 3

    *
   * *
  * * *
 * * * *
* * * * *

Write some requirements - then the solution becomes clear:

  • on iteration 0, print 1 "* " sequence at position 4
  • on iteration 1, print 2 "* " sequences at position 3
  • on iteration 2, print 3 "* " sequences at position 2
  • on iteration 3, print 4 "* " sequences at position 1
  • on iteration 4, print 5 "* " sequences at position 0
Share:
31,191
coder005
Author by

coder005

Updated on August 10, 2021

Comments

  • coder005
    coder005 over 2 years

    I have printed this pattern using one loop:

    *
    **
    ***
    ****
    *****
    
    String s = "";
    for (i = 1; i <= n; ++i) {
        s += "*";
        System.out.println(s);
    }
    

    Now i want how to print following patterns using only one loop.

    1)
        *
       * *
      * * *
     * * * *
    * * * * *
    
    2)
      * * * * * 
       * * * *
        * * *
         * *
          *
    
    3)
       1 2 3 4 5
       1 2 3 4
       1 2 3 
       1 2
       1
    

    and other similar patterns using only one loop, I have done all of them using more than one loop.