DEV Community

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

Posted on • Edited on

23 1

(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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
anandbaraik profile image
Anand-Baraik

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

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay