Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Beautiful 3 Set Problem Solution

Yashwant Parihar, June 13, 2023August 1, 2024

In this post, we will solve HackerRank Beautiful 3 Set Problem Solution.

You are given an integer n. A set. S, of triples (x, y, z) is beautiful if and only if:

  • 0 < xi, Yi, Zi
  • Xi + Yi + zi = n, Vi: 1 ≤ i ≤ |S|
  • Let X be the set of different x,’s in S. Y be the set of different y’s in S. and Z be the set of different z, in S. Then |X = Y = Z = |S|
    The third condition means that all,’s are pairwise distinct. The same goes for y, and zi. Given n. find any beautiful set having a maximum number of elements. Then print the cardinality of S (i.e.. |S) on a new line, followed by S❘ lines where each line contains 3 space-separated integers describing the respective values of a y₁, and z₁.

Input Format

A single integer, n.

Output Format
On the first line, print the cardinality of S (i.e.. [S]). For each of the S subsequent lines, print three space-separated numbers per line describing the respective values of xi, yi, and zi, for triple i in S.

Sample Input

3

Sample Output

3
0 1 2
2 0 1
1 2 0
HackerRank Beautiful 3 Set Problem Solution
HackerRank Beautiful 3 Set Problem Solution

Beautiful 3 Set Problem C Solution

//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;
}

Beautiful 3 Set Problem C++ Solution

#include <bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(),(x).end()

template<typename T> inline bool checkmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; }
template<typename T> inline bool checkmax(T &a, T b) { if (b > a) { a = b; return 1; } return 0; }

typedef long long ll;

vector<vector<int>> check(int s, int n) {
    for (int p = 0; p < 2; ++p) {
        vector<bool> v(n + 1, 0);
        vector<vector<int>> ans(s + 1);
        bool ok = true;
        int j = s;
        for (int i = 0; i < 2; ++i) {
            int y = (i + p) % 2;
            while (y <= s && j >= 0) {
                int z = n - j - y;
                if (z < 0) {
                    ok = 0;
                    break;
                }
                if (v[z]) {
                    ok = 0;
                    break;
                }
                v[z] = true;
                ans[j] = vector<int>({j, y, z});
                y += 2;
                j--;
            }
            if (!ok) {
                break;
            }
        }
        if (ok) {
            return ans;
        }
    }
    return vector<vector<int>>();
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> ans(1, vector<int>({0, 0, n}));
    for (int i = 1; i <= n; ++i) {
        vector<vector<int>> tmp = check(i, n);
        if (tmp.size()) {
            ans = tmp;
        }
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i][0] << ' ' << ans[i][1] << ' ' << ans[i][2] << endl;
    }
}

Beautiful 3 Set Problem C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
    static void Solve(int n)
    {
        int[,] mat = new int[n + 1, n + 1];
        for (int x = 0; x <= n; x++)
        {
            for (int y = 0; y <= n; y++)
            {
                mat[x, y] = n - x - y;
                //Console.Write("{0,2} ",mat[x,y]);
            }
            //Console.WriteLine();
        }
        int max = 0, idx = 0, m1, m2;
        for (int x = 0; x <= n; x++)
        {
            m1 = mat[x, 0] / 2 + 1;
            m2 = mat[0, m1] / 2 + 1;
            if (x >= m2 && mat[x, 0] % 2 != mat[0, m1] % 2)
            {
                if (m1 + m2 > max)
                {
                    max = m1 + m2;
                    idx = x;
                }
            }
        }
        Console.WriteLine(max);
        m1 = mat[idx, 0] / 2 + 1;
        for (int y = 0; y < m1; y++)
        {
            Console.WriteLine("{0} {1} {2}", y, idx + y, n - idx - 2 * y);
        }
        m2 = mat[0, m1] / 2 + 1;
        for (int x = 0; x < m2; x++)
        {
            Console.WriteLine("{0} {1} {2}", m1 + x, x, n - 2 * x - m1);
        }
    }
    static void Main(String[] args) {
        int n = Convert.ToInt32(Console.ReadLine());
        Solve(n);
    }
}

Beautiful 3 Set Problem Java Solution

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

Beautiful 3 Set Problem JavaScript Solution

function processData(input) {
  let k = Math.floor(2*input/3+1)
  let x=[],y=[],z=[]
  console.log(k)
  for(let i = 0, j=Math.floor(input/3 + input%3); i < k; ++i, ++j){
      if((i+j)>input) j=(j+1)%2
      if(!x.includes(i) && !y.includes(j)){
        x.push(i)
        y.push(j)
        z.push(input-(i+j))
      }
  }
  for(let i = 0;i<x.length;i++){
    console.log(x[i]," ",y[i]," ",z[i])
  }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Beautiful 3 Set Problem Python Solution

n = int(input().strip())
i = n * 2 // 3
print (i+1)
x=(i+1)//2
for j in range(x+1):
    print(j,x+j,n-x-2*j)

for j in range(x+1,i+1):
    print(j,j-x-1,n-2*j+x+1)
  
    
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post
  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes