Skip to content

Commit 9a95ee8

Browse files
author
Vinay Patel
committed
Tricks and some other pattern added
1 parent b577c85 commit 9a95ee8

File tree

1 file changed

+234
-1
lines changed

1 file changed

+234
-1
lines changed

Pattern/Patterns.java

Lines changed: 234 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
// Never Asked by product based company like ( MAANG ) .
2+
// Sometimes Asked by service based company.
3+
4+
// Patterns ----> (Nested Loops)
5+
6+
// Trick to solve any pattern question
7+
8+
// 1) for the outer loop , count the number of lines.
9+
10+
// 2) for the inner loop , focus on the colums & connent them somehow to the rows.
11+
12+
// 3) print "*" inside the inner for loop.
13+
14+
// 4) Observe symmetry [ Optimal ]
15+
16+
17+
118
class Patterns {
219
public static void main(String[] args) {
320
// pattern1(5);;
@@ -13,7 +30,15 @@ public static void main(String[] args) {
1330
// pattern11(5);
1431
// pattern12(5);
1532
// pattern13(5);
16-
pattern14(5);
33+
// pattern14(5);
34+
// pattern15(5);
35+
// pattern16(5);
36+
// pattern17(5);
37+
// pattern18(5);
38+
// pattern19(5);
39+
// pattern20(5);
40+
// pattern21(5);
41+
// pattern22(4);
1742

1843
}
1944

@@ -279,5 +304,213 @@ public static void pattern14(int n){
279304
}
280305

281306
}
307+
public static void pattern15(int n){
308+
309+
// ABCDE
310+
// ABCD
311+
// ABC
312+
// AB
313+
// A
314+
315+
for (int i = 0; i < n; i++) {
316+
for (char ch = 'A'; ch <= 'A'+(n-i-1); ch++) {
317+
System.out.print(ch);
318+
}
319+
System.out.println();
320+
}
321+
322+
}
323+
324+
public static void pattern16(int n){
325+
326+
// A
327+
// BB
328+
// CCC
329+
// DDDD
330+
// EEEEE
331+
332+
for (int i = 0; i < n; i++) {
333+
char ch = (char) ('A' + i);
334+
for (int j = 0; j <= i; j++) {
335+
System.out.print(ch);
336+
}
337+
System.out.println();
338+
}
339+
340+
}
341+
342+
public static void pattern17(int n){
343+
344+
// A
345+
// ABA
346+
// ABCBA
347+
// ABCDCBA
348+
// ABCDEDCBA
349+
350+
for (int i = 0; i < n; i++) {
351+
// space
352+
for(int j=0;j<n-i-1;j++){
353+
System.out.print(" ");
354+
}
355+
// star
356+
char ch = 'A';
357+
int breakpoint = (2*i+1)/2;
358+
for(int j=1;j<=2*i + 1;j++){
359+
System.out.print(ch);
360+
// ch++;
361+
if(j<=breakpoint)ch++;
362+
else ch--;
363+
}
364+
// space
365+
for(int j=0;j<n-i-1;j++){
366+
System.out.print(" ");
367+
}
368+
System.out.println();
369+
}
370+
371+
}
372+
373+
public static void pattern18(int n){
374+
375+
// E
376+
// DE
377+
// CDE
378+
// BCDE
379+
// ABCDE
380+
381+
for (int i = 0; i < n; i++) {
382+
for(char ch=(char) ('E'-i);ch<='E';ch++){
383+
System.out.print(ch);
384+
}
385+
System.out.println();
386+
}
387+
388+
}
389+
390+
public static void pattern19(int n){
391+
392+
// **********
393+
// **** ****
394+
// *** ***
395+
// ** **
396+
// * *
397+
// * *
398+
// ** **
399+
// *** ***
400+
// **** ****
401+
// **********
402+
403+
int iniSpace=0;
404+
for (int i = 0; i < n; i++) {
405+
// star
406+
for(int j=1;j<=n-i;j++){
407+
System.out.print("*");
408+
}
409+
// space
410+
for(int j=0;j<iniSpace;j++){
411+
System.out.print(" ");
412+
}
413+
// star
414+
for(int j=1;j<=n-i;j++){
415+
System.out.print("*");
416+
}
417+
iniSpace += 2;
418+
System.out.println();
419+
}
420+
421+
422+
iniSpace=2*n -2;
423+
for(int i=1;i<=n;i++){
424+
// star
425+
for(int j=1;j<=i;j++){
426+
System.out.print("*");
427+
}
428+
// space
429+
for(int j=0;j<iniSpace;j++){
430+
System.out.print(" ");
431+
}
432+
// star
433+
for(int j=1;j<=i;j++){
434+
System.out.print("*");
435+
}
436+
iniSpace -= 2;
437+
System.out.println();
438+
439+
}
440+
}
441+
442+
public static void pattern22(int n) {
443+
444+
// 4444444
445+
// 4333334
446+
// 4322234
447+
// 4321234
448+
// 4322234
449+
// 4333334
450+
// 4444444
451+
452+
for (int i = 0; i < 2*n-1; i++) {
453+
for (int j = 0; j < 2*n-1; j++) {
454+
int top=i;
455+
int left = j;
456+
int right = (2*n- 2) -j;
457+
int down = (2*n -2) - i;
458+
System.out.print(n-Math.min(Math.min(top, down),Math.min(left, right)));
459+
}
460+
System.out.println();
461+
}
462+
}
463+
464+
public static void pattern21(int n) {
465+
466+
// *****
467+
// * *
468+
// * *
469+
// * *
470+
// *****
471+
472+
for(int i=0;i<n;i++){
473+
for(int j=0;j<n;j++){
474+
if(i==0 || j == 0|| i== n-1 || j==n-1){
475+
System.out.print("*");
476+
}
477+
else System.out.print(" ");
478+
}
479+
System.out.println();
480+
}
481+
482+
}
483+
484+
public static void pattern20(int n) {
485+
486+
// * *
487+
// ** **
488+
// *** ***
489+
// **** ****
490+
// **********
491+
// **** ****
492+
// *** ***
493+
// ** **
494+
// * *
495+
496+
497+
int spaces = 2*n - 2;
498+
for (int i = 1; i <=2*n-1; i++) {
499+
int star = i;
500+
if(i>n) star = 2*n -i;
501+
for (int j = 1; j <= star; j++) {
502+
System.out.print("*");
503+
}
504+
for(int j=1;j<=spaces;j++){
505+
System.out.print(" ");
506+
}
507+
for (int j = 1; j <= star; j++) {
508+
System.out.print("*");
509+
}
510+
System.out.println();
511+
if(i<n) spaces -= 2;
512+
else spaces +=2;
513+
}
514+
}
282515

283516
}

0 commit comments

Comments
 (0)