In this HackerRank Beautiful 3 Set problem solution, we have given n, find any beautiful set having a maximum number of elements. Then print the cardinality of S on a new line, followed by |S| lines where each line contains 3 space-separated integers describing the respective values of x, y, and z.

## Problem solution in Python.

```if __name__ == '__main__':
n = int(input().strip())
k = (2 * n) // 3
print(k + 1)
y = 2 * k - n
x = n - 2 * y
for i in range(y + 1):
print(f"{i} {x + i} {n - x - 2 * i}")
for i in range(k - y):
print(f"{y + i + 1} {i} {n - y - 1 - 2 * i}")
```

{"mode":"full","isActive":false}

## Problem solution in Java.

```import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int max = n/3*2;
if (n%3==2) {
max++;
}
bw.write(String.valueOf(max+1));
bw.newLine();
int first = max;
for (int i = (max+1)/2; i >= 0; i--) {
bw.write(String.valueOf(first+" "+i+" "+(n-i-first)));
bw.newLine();
first--;
}
int sn = n;
if (n%3==1) {
sn--;
}
for (int i = sn-first-1; first >= 0; i--) {
bw.write(String.valueOf(first+" "+i+" "+(n-i-first)));
bw.newLine();
first--;
}
bw.close();
}
}

```

{"mode":"full","isActive":false}

## Problem solution in C++.

```#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
int n;
scanf("%d", &n);
int bel = (n - 1) / 3 + 1,ans = bel * 2 - 1 + (n - 1) % 3;
printf("%d\n", ans);
if (ans & 1)
{
int mx = n - (ans - 1);
for(int i = ans - 1;i >= 0;i --)
{
if (mx < 0) mx = ans - 1;
printf("%d %d %d\n", i , mx, n - i - mx);
mx --;
}
} else
{
printf("0 0 %d\n", n);
int mx = n - (ans - 1);
for(int i = ans - 1;i;i --)
{
if (mx <= 0) mx = ans - 1;
printf("%d %d %d\n", i , mx,n - i - mx);
mx --;
}
}
}
```

{"mode":"full","isActive":false}

## Problem solution in C.

```//Tanuj Khattar
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>

#define gu getchar_unlocked
#define pu putchar_unlocked
#define LL long long int
#define ULL unsigned long long int
#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)<(b)?(b):(a))
#define ROUNDOFFINT(d) d = (int)((double)d + 0.5)
#define PLUSWRAP(index,n) index = (index+1)%n
#define MINUSWRAP(index,n) index = (index + n -1)%n

#define INF 1000000000    //1 billion (10^9)

#define FLUSHN while(gu()!='\n')
#define FLUSHS while(gu()!=' ')

int cmpfunc(const void *a,const void *b)
{
return *(int *)a - *(int *)b;
}

int main()
{
int n;
si(n);
int ans=2*n;
ans/=3;
ans++;
dout(ans);
if(n%3!=2)
{
int i;
int b=n/3;
b++;
for(i=0;i<ans;i=i+1)
{
printf("%d %d %d\n",i,b,n-i-b);
b=(b+1)%ans;
}
}
else
{
int fl=0;
int b=2+n-ans;
int i;
for(i=0;i<ans;i++)
{
printf("%d %d %d\n",i,b,n-i-b);
b=b+1;
if(b==ans+1)
b=0;
}
}
return 0;
}
```

{"mode":"full","isActive":false}