Pattern Printing question asked in CGI Coding Round
Last Updated :
13 Mar, 2023
Write a program that receives a number as input and prints it in the following format as shown below.
Examples :
Input : n = 3
Output :
1*2*3*10*11*12
--4*5*8*9
----6*7
Input : n = 4
Output :
1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11
Asked in CGI coding round
Approach: The approach is to see the problem, not as a single task but three tasks which, on combining, complete the main task. The three tasks are printing the left-half of the pattern, printing dashes(-), and printing the right-half of the pattern. Combining all three tasks, we would be able to print the pattern.
left-half of pattern
1*2*3*
--4*5*
----6*
A function printdashes() to print the "-".
right-half of
pattern
10*11*12
*8*9
7
Below is the implementation.
C++
// C program to print the given pattern
#include <stdio.h>
// utility function to print "-" in every
// row. This will take care of printing
// "-" in the start of every row
void printdashes(int k)
{
int i;
for (i = 1; i <= k; i++)
printf("-");
}
// function to print the pattern
void pattern(int n){
// variables for vertical left half
/*
1*2*3*
--4*5*
----6*
*/
int row, column, dashes = 0;
int i, j, dash_counter = 0;
int value = 1;
// variables for vertical right half
/*
10*11*12
*8*9
7
*/
int k, l, decrementor = 0;
int column_decrementor = 0;
int support = n - 1;
int temp = ((n * n) + 1);
int temp1 = (n * 2) - 1;
int z = temp;
int tracker;
for (i = 1; i <= n; i++) {
printdashes(dash_counter);
// This part will take care of the vertical
// left half of the pattern
for (j = 1; j <= (2 * n) - dash_counter; j++) {
// Printing the "*" in even positions
if (j % 2 == 0)
printf("*");
else {
printf("%d", value);
value++;
}
}
// This part will take care of the vertical
// right half of the pattern
for (k = 1; k <= (temp1 - decrementor); k++) {
// Printing the "*" in even positions
if (k % 2 == 0)
printf("*");
else {
if (k == 1)
tracker = temp;
printf("%d", temp);
temp++;
}
}
decrementor += 2;
temp = tracker - support;
support--;
// In every row, the number of dash counts
// is increased by 2
dash_counter += 2;
printf("\n");
}
}
// driver program
int main()
{
int n = 3;
pattern(n);
return 0;
}
Java
// Java program to print the given pattern
class GFG {
// utility function to print "-" in every
// row. This will take care of printing
// "-" in the start of every row
static void printdashes(int k)
{
int i;
for (i = 1; i <= k; i++)
System.out.print("-");
}
// function to print the pattern
static void pattern(int n) {
// variables for vertical left half
/*
1*2*3*
--4*5*
----6*
*/
int row, column, dashes = 0;
int i, j, dash_counter = 0;
int value = 1;
// variables for vertical right half
/*
10*11*12
*8*9
7
*/
int k, l, decrementor = 0;
int column_decrementor = 0;
int support = n - 1;
int temp = ((n * n) + 1);
int temp1 = (n * 2) - 1;
int z = temp;
int tracker = 0;
for (i = 1; i <= n; i++) {
printdashes(dash_counter);
// This part will take care of the vertical
// left half of the pattern
for (j = 1; j <= (2 * n) - dash_counter; j++) {
// Printing the "*" in even positions
if (j % 2 == 0)
System.out.print("*");
else {
System.out.print(value);
value++;
}
}
// This part will take care of the vertical
// right half of the pattern
for (k = 1; k <= (temp1 - decrementor); k++) {
// Printing the "*" in even positions
if (k % 2 == 0)
System.out.print("*");
else {
if (k == 1)
tracker = temp;
System.out.print(temp);
temp++;
}
}
decrementor += 2;
temp = tracker - support;
support--;
// In every row, the number of dash counts
// is increased by 2
dash_counter += 2;
System.out.print("\n");
}
}
// Driver code
public static void main(String arg[]) {
int n = 3;
pattern(n);
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to print
# the given pattern
# utility function to
# print "-" in every
# row. This will take
# care of printing
# "-" in the start of every row
def printdashes(k):
for i in range(1,k+1):
print("-",end="")
# function to print the pattern
def pattern(n):
# variables for vertical left half
'''
1*2*3*
--4*5*
----6*
'''
dashes = 0
dash_counter = 0
value = 1
# variables for vertical right half
'''
10*11*12
*8*9
7
'''
decrementor = 0
column_decrementor = 0
support = n - 1
temp = ((n * n) + 1)
temp1 = (n * 2) - 1
z = temp
for i in range(1,n+1):
printdashes(dash_counter)
# This part will take
# care of the vertical
# left half of the pattern
for j in range(1,(((2 * n) - dash_counter)+1)):
# Printing the "*" in even positions
if (j % 2 == 0):
print("*",end="")
else:
print(value,end="")
value=value+1
# This part will take
# care of the vertical
# right half of the pattern
for k in range(1,((temp1 - decrementor)+1)):
# Printing the "*" in even positions
if (k % 2 == 0):
print("*",end="")
else:
if (k == 1):
tracker = temp
print(temp,end="")
temp=temp + 1
decrementor =decrementor + 2
temp = tracker - support
support=support - 1
# In every row, the number of dash counts
# is increased by 2
dash_counter =dash_counter + 2
print("")
# driver program
n = 3
pattern(n)
# This code is contributed
# by Anant Agarwal.
C#
// C# program to print the given pattern
using System;
class GFG {
// utility function to print "-" in every
// row. This will take care of printing
// "-" in the start of every row
static void printdashes(int k)
{
int i;
for (i = 1; i <= k; i++)
Console.Write("-");
}
// function to print the pattern
static void pattern(int n) {
// variables for vertical left half
/*
1*2*3*
--4*5*
----6*
*/
int i, j, dash_counter = 0;
int value = 1;
// variables for vertical right half
/*
10*11*12
*8*9
7
*/
int k, decrementor = 0;
int support = n - 1;
int temp = ((n * n) + 1);
int temp1 = (n * 2) - 1;
int tracker = 0;
for (i = 1; i <= n; i++) {
printdashes(dash_counter);
// This part will take care of the vertical
// left half of the pattern
for (j = 1; j <= (2 * n) - dash_counter; j++) {
// Printing the "*" in even positions
if (j % 2 == 0)
Console.Write("*");
else {
Console.Write(value);
value++;
}
}
// This part will take care of the vertical
// right half of the pattern
for (k = 1; k <= (temp1 - decrementor); k++) {
// Printing the "*" in even positions
if (k % 2 == 0)
Console.Write("*");
else {
if (k == 1)
tracker = temp;
Console.Write(temp);
temp++;
}
}
decrementor += 2;
temp = tracker - support;
support--;
// In every row, the number of dash counts
// is increased by 2
dash_counter += 2;
Console.WriteLine();
}
}
// Driver code
public static void Main() {
int n = 3;
pattern(n);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to print the given pattern
// function to print the pattern
function pattern($n)
{
// variables for vertical left half
$dashes = 0;
$dash_counter = 0;
$value = 1;
// variables for vertical right half
$decrementor = 0;
$column_decrementor = 0;
$support = $n - 1;
$temp = (($n * $n) + 1);
$temp1 = ($n * 2) - 1;
$z = $temp;
for ($i = 1; $i <= $n; $i++)
{
// loop for printing dash
for ($dd = 1; $dd <= $dash_counter;
$dd++)
printf("-");
// This part will take care of the
// vertical left half of the pattern
for ($j = 1; $j <= (2 * $n) -
$dash_counter; $j++)
{
// Printing the "*" in even
// positions
if ($j % 2 == 0)
printf("*");
else {
printf($value);
$value++;
}
}
// This part will take care
// of the vertical right half
// of the pattern
for ($k = 1; $k <= ($temp1 -
$decrementor); $k++)
{
// Printing the "*" in even
// positions
if ($k % 2 == 0)
printf("*");
else {
if ($k == 1)
$tracker = $temp;
printf($temp);
$temp++;
}
}
$decrementor += 2;
$temp = $tracker - $support;
$support--;
// In every row, the number of
// dash counts is increased by 2
$dash_counter += 2;
printf("\n");
}
}
// Driver code
$n = 3;
pattern($n);
// This code is contributed by mits
?>
JavaScript
<script>
// javascript program to print the given pattern
// utility function to print "-" in every
// row. This will take care of printing
// "-" in the start of every row
function printdashes(k)
{
var i;
for (i = 1; i <= k; i++)
document.write("-");
}
// function to print the pattern
function pattern(n) {
// variables for vertical left half
/*
1*2*3*
--4*5*
----6*
*/
var row, column, dashes = 0;
var i, j, dash_counter = 0;
var value = 1;
// variables for vertical right half
/*
10*11*12
*8*9
7
*/
var k, l, decrementor = 0;
var column_decrementor = 0;
var support = n - 1;
var temp = ((n * n) + 1);
var temp1 = (n * 2) - 1;
var z = temp;
var tracker = 0;
for (i = 1; i <= n; i++) {
printdashes(dash_counter);
// This part will take care of the vertical
// left half of the pattern
for (j = 1; j <= (2 * n) - dash_counter; j++) {
// Printing the "*" in even positions
if (j % 2 == 0)
document.write("*");
else {
document.write(value);
value++;
}
}
// This part will take care of the vertical
// right half of the pattern
for (k = 1; k <= (temp1 - decrementor); k++) {
// Printing the "*" in even positions
if (k % 2 == 0)
document.write("*");
else {
if (k == 1)
tracker = temp;
document.write(temp);
temp++;
}
}
decrementor += 2;
temp = tracker - support;
support--;
// In every row, the number of dash counts
// is increased by 2
dash_counter += 2;
document.write("<br>");
}
}
// Driver code
var n = 3;
pattern(n);
// This code is contributed by 29AjayKumar
</script>
Output1*2*3*10*11*12
--4*5*8*9
----6*7
Time Complexity: O(n2)
Auxiliary Space: O(1), As constant extra space is used.
Another approach :
C++
// C++ program to print the given pattern
#include <iostream>
using namespace std;
// function to print the pattern
void printPattern(int row)
{
int x = 1;
int z = (row * row) + 1;
int col = row == 1 ? 1 : (row * 4) - 1;
for(int i = 1; i <= row; i++)
{
int t = z;
for(int j = 1; j <= col - ((i - 1) * 2); j++)
{
if ((i * 2) - 2 >= j)
{
cout << "-";
}
else
{
if(col == 1)
{
cout << x;
}
else if(j <= col/2 && j % 2 == 1)
{
cout << x;
x++;
}
else if(j > col/2 && j % 2 == 1)
{
cout << t;
t++;
}
else
{
cout << "*";
}
}
}
z = (z - row) + i;
cout << "\n";
}
}
// Driver code
int main()
{
int row = 3;
printPattern(row);
return 0;
}
// This code is contributed by shubhamsingh10
C
// C program to print the given pattern
#include<stdio.h>
// function to print the pattern
void printPattern(int row)
{
int x = 1;
int z = (row * row) + 1;
int col = row == 1 ? 1 : (row * 4) - 1;
for(int i = 1; i <= row; i++)
{
int t = z;
for(int j = 1; j <= col - ((i - 1) * 2); j++)
{
if ((i * 2) - 2 >= j)
{
printf("-");
}
else
{
if(col == 1)
{
printf("%d", x);
}
else if(j <= col/2 && j % 2 == 1)
{
printf("%d", x);
x++;
}
else if(j > col/2 && j % 2 == 1)
{
printf("%d", t);;
t++;
}
else
{
printf("*");
}
}
}
z = (z - row) + i;
printf("\n");
}
}
// Driver code
int main()
{
int row = 3;
printPattern(row);
return 0;
}
// This code is contributed by ankurmishra1794
Java
// Java program to print the given pattern
class GFG
{
// function to print the pattern
static void printPattern(int row)
{
int x = 1;
int z = (row * row) + 1;
int col = row == 1 ? 1 : (row * 4) - 1;
for(int i = 1; i <= row; i++)
{
int t = z;
for(int j = 1; j <= col -((i - 1) * 2); j++)
{
if ((i * 2) - 2 >= j)
{
System.out.print("-");
}
else
{
if(col == 1)
{
System.out.print(x);
}
else if(j <= col/2 && j % 2 == 1)
{
System.out.print(x);
x++;
}
else if(j > col/2 && j % 2 == 1)
{
System.out.print(t);
t++;
}
else
{
System.out.print("*");
}
}
}
z = (z - row) + i;
System.out.print("\n");
}
}
// Driver code
public static void main(String[] args)
{
int row = 3;
printPattern(row);
}
}
/* This code is contributed by PrinciRaj1992 */
Python3
# Python3 program to print the given pattern
# Function to print the pattern
def printPattern(row):
x = 1
z = (row * row) + 1
if row == 1:
col = 1
else:
col = (row * 4) - 1
for i in range(1, row + 1):
t = z
for j in range(1, col - ((i - 1) * 2) + 1):
if ((i * 2) - 2 >= j):
print("", end = "-")
else:
if (col == 1):
print(x, end = "")
elif (j <= col / 2 and j % 2 == 1):
print(x, end = "")
x += 1
elif (j > col / 2 and j % 2 == 1):
print(t, end = "")
t += 1
else:
print("*", end = "")
z = (z - row) + i
print()
# Driver code
row = 3
printPattern(row)
# This code is contributed by shivani
C#
// C# program to print the given pattern
using System;
class GFG
{
// function to print the pattern
static void printPattern(int row)
{
int x = 1;
int z = (row * row) + 1;
int col = row == 1 ? 1 : (row * 4) - 1;
for(int i = 1; i <= row; i++)
{
int t = z;
for(int j = 1; j <= col -((i - 1) * 2); j++)
{
if ((i * 2) - 2 >= j)
{
Console.Write("-");
}
else
{
if(col == 1)
{
Console.Write(x);
}
else if(j <= col/2 && j % 2 == 1)
{
Console.Write(x);
x++;
}
else if(j > col/2 && j % 2 == 1)
{
Console.Write(t);
t++;
}
else
{
Console.Write("*");
}
}
}
z = (z - row) + i;
Console.Write("\n");
}
}
// Driver code
public static void Main(String[] args)
{
int row = 3;
printPattern(row);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// javascript program to print the given pattern
// function to print the pattern
function printPattern(row)
{
var x = 1;
var z = (row * row) + 1;
var col = row == 1 ? 1 : (row * 4) - 1;
for(var i = 1; i <= row; i++)
{
var t = z;
for(var j = 1; j <= col -((i - 1) * 2); j++)
{
if ((i * 2) - 2 >= j)
{
document.write("-");
}
else
{
if(col == 1)
{
document.write(x);
}
else if(j <= col/2 && j % 2 == 1)
{
document.write(x);
x++;
}
else if(j > col/2 && j % 2 == 1)
{
document.write(t);
t++;
}
else
{
document.write("*");
}
}
}
z = (z - row) + i;
document.write("<br>");
}
}
// Driver code
var row = 3;
printPattern(row);
// This code is contributed by 29AjayKumar
</script>
Output1*2*3*10*11*12
--4*5*8*9
----6*7
Time Complexity: O(n2)
Auxiliary Space: O(1), As constant extra space is used.
Another approach :
C++
#include <bits/stdc++.h>
using namespace std;
void pattern(int n) {
int size = n * (n + 1);
// prev1 will be used to keep track of last number
// printed in left half of pattern
int prev1 = 0;
// prev2 will be used to keep track of last number
// printed in right half of pattern
int prev2 = size;
for (int i = 0; i < n; i++) {
// print the '-'
for (int j = 0; j < 2 * i; j++) cout << "-";
// l1 to store numbers of left half to be printed
vector<int> l1;
for (int j = prev1 + 1; j <= prev1 + n - i; j++) l1.push_back(j);
// l2 to store numbers of right half to be printed
vector<int> l2;
for (int j = prev2 - (n - i) + 1; j <= prev2; j++) l2.push_back(j);
// combine l1 and l2 and print the list separated by *
for (int j = 0; j < l1.size(); j++) cout << l1[j] << "*";
for (int j = 0; j < l2.size(); j++) cout << l2[j] << "*";
cout << endl;
// decrease prev2 and increase prev1
prev2 -= (n - i);
prev1 += (n - i);
}
}
// driver program
int main() {
int n = 3;
pattern(n);
return 0;
}
// This code is contributed by poojaagarwal2.
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void pattern(int n)
{
int size = n * (n + 1);
// prev1 will be used to keep track of last number
// printed in left half of pattern
int prev1 = 0;
// prev2 will be used to keep track of last number
// printed in right half of pattern
int prev2 = size;
for (int i = 0; i < n; i++) {
// print the '-'
for (int j = 0; j < 2 * i; j++)
System.out.print("-");
// l1 to store numbers of left half to be
// printed
List<Integer> l1 = new ArrayList<>();
for (int j = prev1 + 1; j <= prev1 + n - i; j++)
l1.add(j);
// l2 to store numbers of right half to be
// printed
List<Integer> l2 = new ArrayList<>();
for (int j = prev2 - (n - i) + 1; j <= prev2;
j++)
l2.add(j);
// combine l1 and l2 and print the list
// separated by *
for (int j = 0; j < l1.size(); j++)
System.out.print(l1.get(j) + "*");
for (int j = 0; j < l2.size(); j++)
System.out.print(l2.get(j) + "*");
System.out.println();
// decrease prev2 and increase prev1
prev2 -= (n - i);
prev1 += (n - i);
}
}
public static void main(String[] args)
{
int n = 3;
pattern(n);
}
}
Python3
def pattern(n):
size = n*(n+1)
# prev1 will be used to keep track of last number
# printed in left half of pattern
prev1 = 0
# prev2 will be used to keep track of last number
# printed in right half of pattern
prev2 = size
for i in range(n):
# print the '-'
print('-'*(2*i), end = '')
# l1 to store numbers of left half to be printed
l1 = [j for j in range(prev1+1, prev1+n-i+1)]
# l2 to store numbers of right half to be printed
l2 = [j for j in range(prev2-(n-i)+1,prev2+1)]
# combine l1 and l2 and print the list separated by *
print(*l1+l2, sep = '*')
# decrease prev2 and increase prev1
prev2 -= (n-i)
prev1 += (n-i)
# driver program
n = 3
pattern(n)
# This code is contributed
# by Akash Jain (ultrainstinct).
C#
//C# code for the above approach
using System;
using System.Collections.Generic;
class GFG
{
public static void pattern(int n)
{
int size = n * (n + 1);
// prev1 will be used to keep track of last number
// printed in left half of pattern
int prev1 = 0;
// prev2 will be used to keep track of last number
// printed in right half of pattern
int prev2 = size;
for (int i = 0; i < n; i++)
{
// print the '-'
for (int j = 0; j < 2 * i; j++)
Console.Write("-");
// l1 to store numbers of left half to be
// printed
List<int> l1 = new List<int>();
for (int j = prev1 + 1; j <= prev1 + n - i; j++)
l1.Add(j);
// l2 to store numbers of right half to be
// printed
List<int> l2 = new List<int>();
for (int j = prev2 - (n - i) + 1; j <= prev2; j++)
l2.Add(j);
// combine l1 and l2 and print the list
// separated by *
for (int j = 0; j < l1.Count; j++)
Console.Write(l1[j] + "*");
for (int j = 0; j < l2.Count; j++)
Console.Write(l2[j] + "*");
Console.WriteLine();
// decrease prev2 and increase prev1
prev2 -= (n - i);
prev1 += (n - i);
}
}
public static void Main(string[] args)
{
int n = 3;
pattern(n);
}
}
JavaScript
function pattern( n) {
let size = n * (n + 1);
// prev1 will be used to keep track of last number
// printed in left half of pattern
let prev1 = 0;
// prev2 will be used to keep track of last number
// printed in right half of pattern
let prev2 = size;
for (let i = 0; i < n; i++) {
// print the '-'
for (let j = 0; j < 2 * i; j++)
console.log( "-");
// l1 to store numbers of left half to be printed
let l1=[];
for (let j = prev1 + 1; j <= prev1 + n - i; j++)
l1.push(j);
// l2 to store numbers of right half to be printed
let l2=[];
for (let j = prev2 - (n - i) + 1; j <= prev2; j++)
l2.push(j);
// combine l1 and l2 and print the list separated by *
for (let j = 0; j < l1.length; j++)
console.log( l1[j] + "*");
for (let j = 0; j < l2.length; j++)
console.log( l2[j] + "*");
console.log("<br>");
// decrease prev2 and increase prev1
prev2 -= (n - i);
prev1 += (n - i);
}
}
// driver program
let n = 3;
pattern(n);
// This code is contributed by ratiagrawal.
Output1*2*3*10*11*12*
--4*5*8*9*
----6*7*
Time Complexity: O(n2)
Auxiliary Space: O(1)
As constant extra space is used.
Another approach :
C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n = 4;
int temp_number = (n * n) + n;
int counter = 1;
// loop through each row of the pattern
for (int i = 0; i < n; i++) {
vector<int> temp_list;
// loop through each column in the current row
for (int j = 0; j < n - i; j++) {
// generate two numbers and add them to the
// temp_list
temp_list.push_back(counter);
temp_list.push_back(temp_number - counter + 1);
counter += 1;
}
// sort the numbers in the current row in ascending
// order
sort(temp_list.begin(), temp_list.end());
// print the appropriate number of dashes before the
// row
for (int k = 0; k < i; k++) {
cout << "--";
}
// print the numbers in the current row, separated
// by asterisks
for (int num : temp_list) {
cout << num;
if (num != temp_list.back()) {
cout << "*";
}
}
cout << endl;
}
return 0;
}
// This code is contributed by divyansh2212
Java
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args)
{
int n = 4;
int temp_number = (n * n) + n;
int counter = 1;
// loop through each row of the pattern
for (int i = 0; i < n; i++) {
ArrayList<Integer> temp_list
= new ArrayList<Integer>();
// loop through each column in the current row
for (int j = 0; j < n - i; j++) {
// generate two numbers and add them to the
// temp_list
temp_list.add(counter);
temp_list.add(temp_number - counter + 1);
counter += 1;
}
// sort the numbers in the current row in
// ascending order
Collections.sort(temp_list);
// print the appropriate number of dashes before
// the row
for (int k = 0; k < i; k++) {
System.out.print("--");
}
// print the numbers in the current row,
// separated by asterisks
for (int num : temp_list) {
System.out.print(num);
if (num
!= temp_list.get(temp_list.size()
- 1)) {
System.out.print("*");
}
}
System.out.println();
}
}
}
Python3
n = 4
temp_number = (n*n)+n
counter = 1
for i in range(n):
temp_list = []
for j in range(n-i):
temp_list.append(counter)
temp_list.append(temp_number-counter+1)
counter += 1
temp_list.sort()
temp_list = [str(each) for each in temp_list]
[print("--", end="") for k in range(i)]
print("*".join(temp_list))
JavaScript
let n = 4;
let temp_number = n * n + n;
let counter = 1;
// loop through each row of the pattern
for (let i = 0; i < n; i++) {
let temp_list = [];
// loop through each column in the current row
for (let j = 0; j < n - i; j++) {
// generate two numbers and add them to the temp_list
temp_list.push(counter);
temp_list.push(temp_number - counter + 1);
counter += 1;
}
// sort the numbers in the current row in ascending order
temp_list.sort(function (a, b) { return a - b; });
// print the appropriate number of dashes before the row
let temp = ""; for (let k = 0; k < i; k++) {
temp = temp+ "--";
}
// print the numbers in the current row, separated by asterisks
for (let num of temp_list) {
temp= temp+ num.toString();
if (num != temp_list[temp_list.length - 1]) {
temp = temp +"*";
}
} console.log(temp);
}
C#
// C# code implementation for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
static public void Main()
{
// Code
int n = 4;
int temp_number = (n * n) + n;
int counter = 1;
// loop through each row of the pattern
for (int i = 0; i < n; i++) {
List<int> temp_list = new List<int>();
// loop through each column in the current row
for (int j = 0; j < n - i; j++) {
// generate two numbers and add them to the
// temp_list
temp_list.Add(counter);
temp_list.Add(temp_number - counter + 1);
counter += 1;
}
// sort the numbers in the current row in
// ascending order
temp_list.Sort();
// print the appropriate number of dashes before
// the row
for (int k = 0; k < i; k++) {
Console.Write("--");
}
// print the numbers in the current row,
// separated by asterisks
for (int idx = 0; idx < temp_list.Count;
idx++) {
Console.Write(temp_list[idx]);
if (temp_list[idx] != temp_list.Last()) {
Console.Write("*");
}
}
Console.WriteLine();
}
}
}
// This code is contributed by karthik.
Output1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11
Time Complexity: O(n2)
Auxiliary Space: O(1)
As constant extra space is used.
Similar Reads
Computer Fundamentals Tutorial This Computer Fundamentals Tutorial covers everything from basic to advanced concepts, including computer hardware, software, operating systems, peripherals, etc. Why Learn Computer FundamentalsYour computer can solve complex problem in milliseconds!Helps you understand how computers work and solve
4 min read
Fundamental
Computer HardwareComputer hardware refers to the physical components of a computer that you can see and touch. These components work together to process input and deliver output based on user instructions. In this article, weâll explore the different types of computer hardware, their functions, and how they interact
10 min read
What is a Computer Software?Computer Software serves as the backbone of all digital devices and systems. It is an integral part of modern technology. Unlike hardware which comprises physical components, software is intangible and exists as a code written in programming language. This article focuses on discussing computer soft
8 min read
Central Processing Unit (CPU)The Central Processing Unit (CPU) is like the brain of a computer. Itâs the part that does most of the thinking, calculating, and decision-making to make your computer work. Whether youâre playing a game, typing a school assignment, or watching a video, the CPU is busy handling all the instructions
6 min read
Input DevicesInput devices are important parts of a computer that help us communicate with the system. These devices let us send data or commands to the computer, allowing it to process information and perform tasks. Simply put, an input device is any tool we use to give the computer instructions, whether it's t
11 min read
Output DevicesOutput devices are hardware that display or produce the results of a computer's processing. They convert digital data into formats we can see, hear, or touch. The output device may produce audio, video, printed paper or any other form of output. Output devices convert the computer data to human unde
9 min read
Memory
Computer MemoryMemory is the electronic storage space where a computer keeps the instructions and data it needs to access quickly. It's the place where information is stored for immediate use. Memory is an important component of a computer, as without it, the system wouldnât operate correctly. The computerâs opera
9 min read
What is a Storage Device? Definition, Types, ExamplesThe storage unit is a part of the computer system which is employed to store the information and instructions to be processed. A storage device is an integral part of the computer hardware which stores information/data to process the result of any computational work. Without a storage device, a comp
11 min read
Primary MemoryPrimary storage or memory is also known as the main memory, which is the part of the computer that stores current data, programs, and instructions. Primary storage is stored in the motherboard which results in the data from and to primary storage can be read and written at a very good pace.Need of P
4 min read
Secondary MemorySecondary memory, also known as secondary storage, refers to the storage devices and systems used to store data persistently, even when the computer is powered off. Unlike primary memory (RAM), which is fast and temporary, secondary memory is slower but offers much larger storage capacities. Some Ex
7 min read
Hard Disk Drive (HDD) Secondary MemoryPrimary memory, like RAM, is limited and volatile, losing data when power is off. Secondary memory solves this by providing large, permanent storage for data and programs.A hard disk drive (HDD) is a fixed storage device inside a computer that is used for long-term data storage. Unlike RAM, HDDs ret
11 min read
Application Software
MS Word Tutorial - Learn How to Use Microsoft Word (2025 Updated)Microsoft Word remains one of the most powerful word processing program in the world. First released in 1983, this word processing software has grown to serve approximately 750 million people every month. Also, MS Word occupies 4.1% of the market share for productivity software.With features like re
9 min read
MS Excel Tutorial - Learn Excel Online FreeExcel, one of the powerful spreadsheet programs for managing large datasets, performing calculations, and creating visualizations for data analysis. Developed and introduced by Microsoft in 1985, Excel is mostly used in analysis, data entry, accounting, and many more data-driven tasks.Now, if you ar
11 min read
What is a Web Browser and How does it Work?The web browser is an application software used to explore the World Wide Web (WWW). It acts as a platform that allows users to access information from the Internet by serving as an interface between the client (user) and the server. The browser sends requests to servers for web documents and servic
4 min read
What is a Excel SpreadsheetExcel works like other spreadsheet programs but offers more features. Each Excel file is called a workbook, which contains one or more worksheets. You can start with a blank workbook or use a template.A worksheet is a grid of 1,048,576 rows and 16,384 columns, over 17 billion cells, for entering and
7 min read
System Software
Programming Languages
C Programming Language TutorialC is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Python Tutorial - Learn Python Programming LanguagePython is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Java TutorialJava is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. Known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Syntax and s
10 min read
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.Client Side: On the client side, JavaScript works
11 min read