0% found this document useful (0 votes)
715 views22 pages

Striver's Pattern Sheet

Uploaded by

ANSHUL KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
715 views22 pages

Striver's Pattern Sheet

Uploaded by

ANSHUL KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Striver's Pattern Sheet

public class Solution {


public static void nForest(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
§

public class Solution {


public static void nForest(int n) {
int nst = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nst; j++) {
System.out.print("* ");
}
nst = nst + 1;
System.out.println();
}
}
}

public class Solution {


public static void nTriangle(int n) {
int num;
int nst = 1;
for (int i = 1; i <= n; i++) {
num = 1;
for (int j = 1; j <= nst; j++) {
System.out.print(num + " ");
num++;
}
nst = nst + 1;
System.out.println();
}
}
}
§
§
public class Solution {
public static void nTriangle(int n) {
int num;
int nst = 1;
for (int i = 1; i <= n; i++) {
num = i;
for (int j = 1; j <= nst; j++) {
System.out.print(i + " ");
}
nst = nst + 1;
System.out.println();
}
}
}
§
public class Solution {
public static void seeding(int n) {
int nst = n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nst; j++) {
System.out.print("* ");
}
nst = nst - 1;
System.out.println();
}
}
}
§

public class Solution {


public static void nNumberTriangle(int n) {
int nst = n;
int num;
for (int i = 1; i <= n; i++) {
num = 1;
for (int j = 1; j <= nst; j++) {
System.out.print(num + " ");
num++;
}
nst = nst - 1;
System.out.println();
}
}
}

public class Solution {


public static void nStarTriangle(int n) {
int nst = 1;
int nsp = (n * 2 - 1) / 2; // or `nsp = n - 1;` also co
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nsp; j++) {
System.out.print(" ");
}
for (int j = 1; j <= nst; j++) {
System.out.print("*");
}
nst = nst + 2;
nsp = nsp - 1;
System.out.println();
}
}
}
§
public class Solution {
public static void nStarTriangle(int n) {
int nst = n * 2 - 1;
int nsp = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nsp; j++) {
System.out.print(" ");
}
for (int j = 1; j <= nst; j++) {
System.out.print("*");
}
nst = nst - 2;
nsp = nsp + 1;
System.out.println();
}
}
}
§
public class Solution {
public static void nStarDiamond(int n) {
int nst = 1;
int nsp = (n * 2 - 1) / 2;
for (int i = 1; i <= 2 * n; i++) {

//edge case for n + 1 row


if (i == n + 1) {
for (int x = 1; x <= n * 2 - 1; x++) {
System.out.print("*");
}
System.out.println();
continue;
}

for (int j = 1; j <= nsp; j++) {


System.out.print(" ");
}
for (int j = 1; j <= nst; j++) {
System.out.print("*");
}
if (i <= (n * 2 - 1) / 2) {
nst = nst + 2;
nsp = nsp - 1;
} else {
nst = nst - 2;
nsp = nsp + 1;
}
System.out.println();
}
}
}

public class Solution {


public static void nStarTriangle(int n) {
int nst = 1;
for (int i = 1; i <= n * 2 - 1; i++) {
for (int j = 1; j <= nst; j++) {
System.out.print("*");
}
if (i <= (n * 2 - 1) / 2) {
nst = nst + 1;
} else {
nst = nst - 1;
}
System.out.println();
}
}
}
§
§
public class Solution {
public static void nBinaryTriangle(int n) {
int nst = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nst; j++) {
if ((i + j) % 2 == 0) {
System.out.print(1 + " ");
} else {
System.out.print(0 + " ");
}
}
nst = nst + 1;
System.out.println();
}
}
}
§
§
public class Solution {
public static void numberCrown(int n) {
int nst = 1;
int nsp = n * 2 - 2;
int num;

for (int i = 1; i <= n; i++) {


num = 1;
for (int j = 1; j <= nst; j++) {
System.out.print(num + " ");
num++;
}
for (int j = 1; j <= nsp; j++) {
System.out.print(" ");
}
num = i;
for (int j = 1; j <= nst; j++) {
System.out.print(num + " ");
num--;
}
nsp = nsp - 2;
nst = nst + 1;
System.out.println();
}
}
}
§

public class Solution {


public static void nNumberTriangle(int n) {
int nst = 1;
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nst; j++) {
System.out.print(num + " ");
num++;
}
nst = nst + 1;
System.out.println();
}
}
}
§
§
public class Solution {
public static void nLetterTriangle(int n) {
int nst = 1;
char ch;
for (int i = 1; i <= n; i++) {
ch = 'A';
for (int j = 1; j <= nst; j++) {
System.out.print(ch + " ");
ch++; // ch = ch + 1 is wrong.
}
nst = nst + 1;
System.out.println();
}
}
}
§
public class Solution {
public static void nLetterTriangle(int n) {
int nst = n;
char ch;
for (int i = 1; i <= n; i++) {
ch = 'A';
for (int j = 1; j <= nst; j++) {
System.out.print(ch + " ");
ch++;
}
nst = nst - 1;
System.out.println();
}
}
}
§

public class Solution {


public static void alphaRamp(int n) {
int nst = 1;
char ch = 'A';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nst; j++) {
System.out.print(ch + " ");
}
nst = nst + 1;
ch++;
System.out.println();
}
}
}

public class Solution {


public static void alphaHill(int n) {
int nst = 1;
int nsp = n - 1;
char ch;
for (int i = 1; i <= n; i++) {
ch = 'A';
for (int j = 1; j <= nsp; j++) {
System.out.print(" ");
}
for (int j = 1; j <= nst; j++) {
System.out.print(ch + " ");
if (j <= nst / 2) {
ch++;
} else {
ch--;
}
}
nst = nst + 2;
nsp = nsp - 1;
System.out.println();
}
}
}

public class Solution {


public static void alphaTriangle(int n) {
int nst = 1;
char ch;

for (int i = 1; i <= n; i++) {


ch = 'A';
ch+=(n - 1);
for (int j = 1; j <= nst; j++) {
System.out.print(ch + " ");
ch--;
}
nst = nst + 1;
System.out.println();
}
}
}
§
§
public class Solution {
public static void symmetry(int n) {
int nst = n;
int nsp = 0;
for (int i = 1; i <= 2 * n; i++) {

if (i == n + 1) {
System.out.print("* ");
for (int x = 1; x <= n * 2 - 2; x++) {
System.out.print(" ");
}
System.out.print("* ");

System.out.println();
continue;
}

for (int j = 1; j <= nst; j++) {


System.out.print("* ");
}
for (int j = 1; j <= nsp; j++) {
System.out.print(" ");
}
for (int j = 1; j <= nst; j++) {
System.out.print("* ");
}
if (i <= (n * 2 - 1) / 2) {
nst = nst - 1;
nsp = nsp + 2;
} else {
nst = nst + 1;
nsp = nsp - 2;
}
System.out.println();
}
}
} §

public class Solution {


public static void symmetry(int n) {
int nst = 1;
int nsp = n + 1;

for (int i = 1; i <= n * 2 - 1; i++) {


for (int j = 1; j <= nst; j++) {
System.out.print("* ");
}
for (int j = 1; j <= nsp; j++) {
System.out.print(" ");
}
for (int j = 1; j <= nst; j++) {
System.out.print("* ");
}
if (i <= (n * 2 - 1) / 2) {
nst = nst + 1;
nsp = nsp - 2;
} else {
nst = nst - 1;
nsp = nsp + 2;
}
System.out.println();
}
}
}
§

public class Solution {


public static void getStarPattern(int n) {
int nsp = n - 2;
for (int i = 1; i <= n; i++) {
//treat first and last row as edge case
if (i == 1 || i == n) {
for (int j = 1; j <= n; j++) {
System.out.print("*");
}
System.out.println();
continue;
}
System.out.print("*");
for (int j = 1; j <= nsp; j++) {
System.out.print(" ");
}
System.out.print("*");

System.out.println();
}
}
}
§

n = 4
public class Solution {
public static void getNumberPattern(int n) {
int edges = 0;
int middle = n * 2 - 1;
int middleStart = n;

for (int i = 1; i <= n * 2 - 1; i++) {


int start = n; // start means edgeStart
for (int j = 1; j <= edges; j++) {
System.out.print(start);
start--;
}

for (int j = 1; j <= middle; j++) {


System.out.print(middleStart);
}

start++;
for (int j = 1; j <= edges; j++) {
System.out.print(start);
start++;
}
if (i <= (n * 2 - 1) / 2) {
edges = edges + 1;
middle = middle - 2;
middleStart = middleStart - 1;
} else {
edges = edges - 1;
middle = middle + 2;
middleStart = middleStart + 1;
}
System.out.println();
}
}
}
§
§

You might also like