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 :



title-img


                            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

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →