Create a prefect diamond from the elements of the given String
Last Updated :
26 Jul, 2023
Given a string str, the task is to print the perfect diamond from the elements of the given string.
Examples:
Input: str = ” geeksforgeeks”
Output:
g
e e
k s f
o r
g
eliminate ‘eeks’ because it is not required to make a perfect diamond from the given string
Input: str = “good”
Output:
g
o o
d
Approach: This can be solved with the following idea:
We need a perfect square number equal to or lower than the length of the string because the sum of all elements in diamond space is a perfect square.
Steps involved in the implementation of the code:
- Find the length of the string and check whether it is a perfect square of a number or not.
- If not a perfect square then finds the just lower perfect square and store it in a variable say len and n containing the square root of that number.
- Take a variable ch that traverses the given string and print the elements
- We require two nested loops.
- First to make an upper triangle of a diamond and another is to print the lower triangle of a diamond.
- In both the nested loop there are further loops one is for printing the front spaces and another is to print the character.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void perfectDiamond(string str)
{
int num = str.length();
int n = sqrt (num);
int len = n * n;
int ch = 0;
for ( int i = 1; i <= n; i++) {
for ( int k = n - 1; k >= i; k--) {
cout << " " ;
}
for ( int j = 1; j <= i; j++) {
cout << str[ch] << " " ;
ch++;
}
cout << endl;
}
for ( int i = 1; i < n; i++) {
for ( int k = 1; k <= i; k++) {
cout << " " ;
}
for ( int j = n - 1; j >= i; j--) {
cout << str[ch] << " " ;
ch++;
}
cout << endl;
}
}
int main()
{
string str = "geeksforgeeks" ;
perfectDiamond(str);
return 0;
}
|
Java
import java.lang.Math;
public class Main {
public static void perfectDiamond(String str) {
int num = str.length();
int n = ( int ) Math.sqrt(num);
int len = n * n;
int ch = 0 ;
for ( int i = 1 ; i <= n; i++) {
for ( int k = n - 1 ; k >= i; k--) {
System.out.print( " " );
}
for ( int j = 1 ; j <= i; j++) {
System.out.print(str.charAt(ch) + " " );
ch++;
}
System.out.println();
}
for ( int i = 1 ; i < n; i++) {
for ( int k = 1 ; k <= i; k++) {
System.out.print( " " );
}
for ( int j = n - 1 ; j >= i; j--) {
System.out.print(str.charAt(ch) + " " );
ch++;
}
System.out.println();
}
}
public static void main(String[] args) {
String str = "geeksforgeeks" ;
perfectDiamond(str);
}
}
|
Python3
import math
def perfectDiamond( str ):
num = len ( str )
n = int (math.sqrt(num))
len_str = n * n
ch = 0
for i in range ( 1 , n + 1 ):
for k in range (n - 1 , i - 1 , - 1 ):
print ( " " , end = "")
for j in range ( 1 , i + 1 ):
print ( str [ch], end = " " )
ch + = 1
print ()
for i in range ( 1 , n):
for k in range ( 0 , i):
print ( " " , end = "")
for j in range (n - 1 , i - 1 , - 1 ):
print ( str [ch], end = " " )
ch + = 1
print ()
if __name__ = = '__main__' :
str = "geeksforgeeks"
perfectDiamond( str )
|
C#
using System;
class Program
{
static void perfectDiamond( string str)
{
int num = str.Length;
int n = ( int )Math.Sqrt(num);
int len = n * n;
int ch = 0;
for ( int i = 1; i <= n; i++)
{
for ( int k = n - 1; k >= i; k--)
{
Console.Write( " " );
}
for ( int j = 1; j <= i; j++)
{
Console.Write(str[ch] + " " );
ch++;
}
Console.WriteLine();
}
for ( int i = 1; i < n; i++)
{
for ( int k = 1; k <= i; k++)
{
Console.Write( " " );
}
for ( int j = n - 1; j >= i; j--)
{
Console.Write(str[ch] + " " );
ch++;
}
Console.WriteLine();
}
}
static void Main()
{
string str = "geeksforgeeks" ;
perfectDiamond(str);
}
}
|
Javascript
function perfectDiamond(str) {
var num = str.length;
var n = Math.sqrt(num);
var len_str = n * n;
var ch = 0;
for ( var i = 1; i <= n; i++) {
for ( var k = n - 1; k >= i - 1; k--) {
process.stdout.write( " " );
}
for ( var j = 1; j <= i; j++) {
process.stdout.write(str[ch] + " " );
ch += 1;
}
console.log();
}
for ( var i = 1; i < n; i++) {
for ( var k = 0; k < i; k++) {
process.stdout.write( " " );
}
for ( var j = n - 1; j >= i - 1; j--) {
process.stdout.write(str[ch] + " " );
ch += 1;
}
console.log();
}
}
var str = "geeksforgeeks" ;
perfectDiamond(str);
|
Time complexity: O(n*n*n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...