Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Beautiful 3 Set problem solution

YASH PAL, 31 July 2024

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.

HackerRank Beautiful 3 Set problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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("%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 --;
		}
	}
}

{“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("%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;
}

{“mode”:”full”,”isActive”:false}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes