DEV Community

Cover image for (Solution) Must Do Pattern Problems before starting DSA - Striver DSA Course
Vaibhav sisodiya
Vaibhav sisodiya

Posted on • Edited on

(Solution) Must Do Pattern Problems before starting DSA - Striver DSA Course

In this post, I have given the solutions to the pattern worksheet of DSA course by Striver. Codes are written in JAVA but to get c++ code just copy the code inside main function and replace the print statement of java with that of C++.

I hope you will like it. Do let me know if you want more such solutions.


  • Pattern 1
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 0; i<count; i++){
            for(int j = 0; j<count; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 2
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 0; i<count; i++){
            for(int j = 0; j<=i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 3
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j<=i; j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 4
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j<=i; j++){
                System.out.print(i);
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 5
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j >= i; j--){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 6
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j >= i; j--){
                System.out.print(count-j+1);
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 7
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j > i; j--){
                System.out.print(" ");
            }
            for(int j = 1; j <= 2*i-1; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 8
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j < i; j++){
                System.out.print(" ");
            }
            for(int j = 2*(count - i)+1; j >0; j--){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 9
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j > i; j--){
                System.out.print(" ");
            }
            for(int j = 1; j <= 2*i-1; j++){
                System.out.print("*");
            }
            System.out.println();
        }

        for(int i = 1; i<=count; i++){
            for(int j = 1; j < i; j++){
                System.out.print(" ");
            }
            for(int j = 2*(count - i)+1; j >0; j--){
                System.out.print("*");
            } 
            System.out.println();
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 10
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 0; i<count; i++){
            for(int j = 0; j<=i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i = 1; i<=count; i++){
            for(int j = count; j >= i; j--){
                System.out.print("*");
            }
            System.out.println();
        }     
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 11
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        int one = 1;
        for(int i = 0; i<count; i++){
            if(i%2!=0) one = 0;
            else one = 1;
            for(int j = 0; j<=i; j++){
                System.out.print(one + " ");
                if(one==1) one = 0;
                else one = 1;
            }
            System.out.println();
        }

    }
}

Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 12
public class Program
{
    public static void main(String[] args) {
        int count = 4;
        for(int i = 1; i<=count; i++){
            for(int j=1; j<=i;j++){
                System.out.print(j);
            }
            for(int j=1; j<=2*(count-i);j++){
                System.out.print(" ");
            }
            for(int j=i; j>0;j--){
                System.out.print(j);
            }
            System.out.println();
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 13
public class Program
{
    public static void main(String[] args) {
        int count = 5; int num=1;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j<=i; j++){
                System.out.print(num++ + " ");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 14
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            char alp = 'A';
            for(int j = 1; j<=i; j++){
                System.out.print(alp++);
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 15
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            char alp = 'A';
            for(int j = count; j >= i; j--){
                System.out.print(alp++);
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 16
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        char alp = 'A';
        for(int i = 1; i<=count; i++){
            for(int j = 1 ; j <= i; j++){
                System.out.print(alp);
            }
            System.out.println();
            alp++;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 17
public class Program
{
    public static void main(String[] args) {
        int count = 4;
        for(int i = 1; i<=count; i++){
            char alp = 'A';
            for(int j = count ; j > i; j--){
                System.out.print(" ");
            }
            for(int j = 1 ; j <= i; j++){
                System.out.print(alp++);
            }
            --alp;
            for(int j = 1 ; j < i; j++){
                System.out.print(--alp);
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 18
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            int alp = 65 + count - 1;
            for(int j = i-1 ; j >= 0; j--){
                int result = alp-j;
                System.out.print( (char) result);
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 19
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = count; j >= i; j--){
                System.out.print("*");
            }
            for(int j = 1; j < 2*i-1; j++){
                System.out.print(" ");
            }
            for(int j = count; j >= i; j--){
                System.out.print("*");
            }
            System.out.println();
        }
         for(int i = 1; i<=count; i++){
            for(int j = 1; j <= i; j++){
                System.out.print("*");
            }
            for(int j = 2*(count - i)-1; j >=0; j--){
                System.out.print(" ");
            }
            for(int j = 1; j <= i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 20
public class Program
{
    public static void main(String[] args) {
        int count = 5;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j <= i; j++){
                System.out.print("*");
            }
            for(int j = 2*(count - i); j>0; j--){
                System.out.print(" ");
            }
            for(int j = 1; j <= i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i = 1; i<count; i++){
            for(int j = count; j > i; j--){
                System.out.print("*");
            }
            for(int j = 1; j <= 2*i; j++){
                System.out.print(" ");
            }
            for(int j = count; j > i; j--){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 21
public class Program
{
    public static void main(String[] args) {
        int count = 4;
        for(int i = 1; i<=count; i++){
            for(int j = 1; j<=count; j++){
                if(i==1 || i==count || j==1 || j==count){
                    System.out.print("*");
                } else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

  • Pattern 22
public class Program
{
    public static void main(String[] args) {
        int count = 4;
        for(int i = 1; i<=count; i++){
            for(int j=count; j>count-i;j--){
                System.out.print(j+" ");
            }
            for(int j=1; j<=2*(count-i)-1;j++){
                System.out.print(count-i+1+" ");
            }
            for(int j=count-i+1; j<=count;j++){
                if(j==1) continue;
                System.out.print(j+" ");
            }
            System.out.println();
        }
        for(int i = count-1; i>0; i--){
            for(int j=count; j>count-i;j--){
                System.out.print(j+" ");
            }
            for(int j=1; j<=2*(count-i)-1;j++){
                System.out.print(count-i+1+" ");
            }
            for(int j=count-i+1; j<=count;j++){
                if(j==1) continue;
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output


The sheet is completed. Share your reactions with me.

Signing off,
This is VAIB

Top comments (1)

Collapse
 
anandbaraik profile image
Anand-Baraik

Great work. Will be helpful for many people!
Keep sharing.