**Beautiful 3 Set**

### Problem Statement :

You are given an integer n. A set, S, of triples (xi,yi,zi) is beautiful if and only if: 0 <= xi,yi,zi xi+yi+zi = n, for all i : 1 <= i <= |S| Let X be the set of different xi's in S, Y be the set of different yi's in S, and Z be the set of different zi in S. Then |X| = |Y| = |Z| = |S|. The third condition means that all xi's are pairwise distinct. The same goes for yi and zi. Given , find any beautiful set having a maximum number of elements. Then print the cardinality of (i.e., ) on a new line, followed by lines where each line contains space-separated integers describing the respective values of , , and . Input Format A single integer, n. Constraints 1 <= n <= 300 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.

### Solution :

` ````
Solution in C :
In C++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
if (N == 1){
cout << "1\n0 0 1\n";
} else if (N == 2){
cout << "2\n0 0 2\n1 1 0\n";
} else if (N == 3){
cout << "3\n0 1 2\n2 0 1\n1 2 0\n";
} else if (N == 4){
cout << "3\n0 0 4\n1 1 2\n2 2 0\n";
} else {
if (N % 3 == 0){
int h = N/3*2+1;
cout << h << "\n";
for (int i=0; i<h; i++){
cout << i << ' ' << (N/3+i)%h << ' ' << (N - i - (N/3+i)%h) << '\n';
}
} else if (N % 3 == 2){
int h = N-N/3;
cout << h << "\n0 0 " << N << "\n";
for (int i=1; i<h; i++){
int m = i <= h/2 ? (i + N/3) : (i - N/3 - 1);
cout << i << ' ' << m << ' ' << (N-i-m) << "\n";
}
} else {
int h = N-N/3;
cout << h << "\n0 0 " << N << "\n1 1 " << N-2 << "\n";
for (int i=1; i<=N/3; i++){
cout << i+1 << ' ' << i+N/3 << ' ' << N - (i+1) - (i+N/3) << '\n';
}
for (int i=1; i<N/3; i++){
cout << i+N/3+1 << ' ' << i+1 << ' ' << N - (i+N/3+1) - (i+1) << '\n';
}
}
}
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int max = n/3*2;
if (n%3==2)
max++;
int sn = n;
if (n%3==1)
sn--;
System.out.println(max+1);
int first = max;
for (int i = (max+1)/2; i >= 0; i--) {
System.out.println(first+" "+i+" "+(n-i-first));
first--;
}
for (int i = sn-first-1; first >= 0; i--) {
System.out.println(first+" "+i+" "+(n-i-first));
first--;
}
}
}
In C :
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, k, r, **S, i, q;
scanf("%d",&n);
k = (2*n)/3;
r = (2*n)%3;
S = malloc(sizeof(int*)*k);
switch ( r ) {
case 0:
for (i=0; i<=k/2; i++) {
S[i] = malloc(sizeof(int)*3);
S[i][0] = i;
S[i][1] = k-2*i;
S[i][2] = k/2+i;
}
for (i=k/2+1; i<=k; i++) {
S[i] = malloc(sizeof(int)*3);
S[i][0] = i;
S[i][1] = 2*(k-i)+1;
S[i][2] = i-k/2-1;
}
break;
case 2:
for (i=0; i<=k/2; i++) {
S[i] = malloc(sizeof(int)*3);
S[i][0] = i;
S[i][1] = k-2*i;
S[i][2] = k/2+i+1;
}
for (i=k/2+1; i<=k; i++) {
S[i] = malloc(sizeof(int)*3);
S[i][0] = i;
S[i][1] = 2*(k-i)+1;
S[i][2] = i-k/2;
}
break;
case 1:
q = k/2;
for (i=0; i<=q; i++) {
S[i] = malloc(sizeof(int)*3);
S[i][0] = i;
S[i][1] = k-2*i;
S[i][2] = q+i+1;
}
for (i=q+1; i<=k; i++) {
S[i] = malloc(sizeof(int)*3);
S[i][0] = i+1;
S[i][1] = 2*(k-i);
S[i][2] = i-q-1;
}
break;
default:
break;
}
printf("%d\n",k+1);
for (i=0; i<=k; i++)
printf("%d %d %d\n", S[i][0], S[i][1], S[i][2]);
return 0;
}
In Python3 :
n = int(input().strip())
low = n // 3
high = 2 * n // 3
print(high + 1)
for i in range(low+1):
print(i, 2*(low-i), n+i-2*low)
for j in range(low+1, high+1):
print(j, 2*(high-j)+1, n+j-1-2*high)
```

## View More Similar Problems

## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →## Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →## Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →## QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →## Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →## Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →