Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Beautiful 3 Set problem solution

YASH PAL, 31 July 202425 January 2026

In this HackerRank Beautiful 3 Set problem solution, you are 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.

HackerRank Beautiful 3 Set problem solution

HackerRank Beautiful 3 Set 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}")


Beautiful 3 Set 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();
    }
}


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("%dn", 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 %dn", i , mx, n - i - mx);
			mx --;
		}
	} else
	{
		printf("0 0 %dn", n);
		int mx = n - (ans - 1);
		for(int i = ans - 1;i;i --)
		{
			if (mx <= 0) mx = ans - 1;
			printf("%d %d %dn", i , mx,n - i - mx);
			mx --;
		}
	}
}

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("%dn",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lldn",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 %dn",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 %dn",i,b,n-i-b);
b=b+1;
if(b==ans+1)
b=0;
}
}
return 0;
}


Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes