Print a ‘Y’ shaped pattern from asterisks in N number of lines.
Examples:
Input: N = 12
Output:* *
* *
* *
* *
* *
* *
* *
*
*
*
*
*
Input: 8
Output:* *
* *
* *
* *
* *
*
*
*
Approach:
Follow the steps to solve this problem:
- Initialize two variable s = N / 2 and t = N / 2.
- Traverse a loop on i from 0 till N-1
- Check if i > s
- Traverse a loop on j from 0 till s-1 and print ” ” i.e., space
- Else,
- Iterate on j from 0 till i-1 and print ” ” i.e., space
- Then print “*” i.e., asterisk
- Iterate on k from 0 till 2*t-1 and print ” ” i.e., space
- Decrement t by 1.
- Print ” *” at the end of each iteration.
Below is the implementation of the above approach:
C++
// C++ code for the above approach#include <iostream>using namespace std;int main(){ // Given integer N int N = 12; // Initialize s and t int s = N / 2; int t = s; // Traverse from 0 till N for (int i = 0; i < N; i++) { if (i > s) { for (int j = 0; j < s; j++) cout << " "; } else { for (int j = 0; j < i; j++) cout << " "; cout << "*"; for (int k = 0; k < 2 * t; k++) cout << " "; // Decrement t t--; } cout << " *" << endl; } return 0;} |
Java
// Java code for the above approachimport java.io.*;class GFG { public static void main(String[] args) { // Given integer N int N = 12; // Initialize s and t int s = N / 2; int t = s; // Traverse from 0 till N for (int i = 0; i < N; i++) { if (i > s) { for (int j = 0; j < s; j++) System.out.print(" "); } else { for (int j = 0; j < i; j++) System.out.print(" "); System.out.print("*"); for (int k = 0; k < 2 * t; k++) System.out.print(" "); // Decrement t t--; } System.out.println(" *"); } }}// This code is contributed by Rohit Pradhan |
Python3
# Given integer NN = 12# Initialize s and ts = N / 2t = s# Traverse from 0 till Nfor i in range(0, N): if (i > s): for j in range(0, int(s)): print(" ", end = "") else: for j in range(0, i): print(" ", end = "") print("*", end = "") for k in range(0, (2*int(t))): print(" ", end = "") # Decrement t t = t - 1 print(" *") # This code is contributed by akashish__ |
C#
// C# code to implement the approachusing System;using System.Collections.Generic;class GFG{ public static void Main() { // Given integer N int N = 12; // Initialize s and t int s = N / 2; int t = s; // Traverse from 0 till N for (int i = 0; i < N; i++) { if (i > s) { for (int j = 0; j < s; j++) Console.Write(" "); } else { for (int j = 0; j < i; j++) Console.Write(" "); Console.Write("*"); for (int k = 0; k < 2 * t; k++) Console.Write(" "); // Decrement t t--; } Console.WriteLine(" *"); } }}// This code is contributed by code_hunt. |
Javascript
<script>// JavaScript implementation of the approach// Given integer N let N = 12; // Initialize s and t let s = Math.floor(N / 2); let t = s; // Traverse from 0 till N for (let i = 0; i < N; i++) { if (i > s) { for (let j = 0; j < s; j++) document.write(" "); } else { for (let j = 0; j < i; j++) document.write(" "); document.write("*"); for (let k = 0; k < 2 * t; k++) document.write(" "); // Decrement t t--; } document.write(" *" + "<br/>"); }// This code is contributed by sanjoy_62.</script> |
* *
* *
* *
* *
* *
* *
* *
*
*
*
*
*
Time Complexity: O(N2), for using nested loops.
Auxiliary Space: O(1), as constant extra space is required.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
