Beautiful 3 Set – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
C++ Beautiful 3 Set HackerRank Solution
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
int main() {
int N;
while(~scanf("%d", &N)) {
int K = 2 * N / 3;
while((K + 1) + (K + 1) / 2 <= N + 1) ++ K;
//i0..N-i
int P = K + (K % 2 == 0);
vector<int> pos(P);
int t = P / 2;
rep(i, P)
pos[i] = i % 2 == 0 ? P - 1 - i / 2 : t - 1 - i / 2;
if(K % 2 == 0) {
rep(i, P) {
if(pos[i] > N - i) {
pos.erase(pos.begin() + i);
break;
}
}
}
vector<bool> vis(N * 2 + 1, false);
bool ok = true;
rep(i, K) {
ok &= 0 <= pos[i] && pos[i] <= N - i;
ok &= !vis[i + pos[i]];
vis[i + pos[i]] = true;
}
if(!ok) { cerr << "not ok" << endl; return 1; }
printf("%d\n", K);
rep(i, K)
printf("%d %d %d\n", i, pos[i], N - i - pos[i]);
}
return 0;
}
Java Beautiful 3 Set HackerRank Solution
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class F {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
/*
* 01234
* 23401
* 42031
[[0, 1, 2, 3, 4, 5, 6]]
[[3, 4, 5, 6, 0, 1, 2]]
[[6, 4, 2, 0, 5, 3, 1]]
*
*
[[0, 1, 2, 3, 4, 5, 6, 7]]
[[0, 4, 5, 6, 7, 1, 2, 3]]
[[11, 6, 4, 2, 0, 5, 3, 1]]
*
[[0, 1, 2, 3, 4, 5, 6]]
[[0, 1, 4, 5, 6, 2, 3]]
[[10, 8, 4, 2, 0, 3, 1]]
[[0, 1, 2, 3, 4]]
[[0, 1, 3, 4, 2]]
[[7, 5, 2, 0, 1]] *
*/
int n = ni();
if(n % 3 == 0){
out.println(n/3*2+1);
for(int i = 0;i < n/3*2+1;i++){
out.print(i + " ");
out.print((i+n/3)%(n/3*2+1) + " ");
out.println((2*(n/3*2+1)-1-2*i)%(n/3*2+1));
}
}else if(n % 3 == 2){
out.println(n/3*2+2);
int m = n/3*2+2;
for(int i = 0;i < m;i++){
out.print(i + " ");
out.print((i == 0 ? 0 : (i+n/3-1)%(m-1)+1) + " ");
out.println((i == 0 ? n : (m-2*i+2*(m-1))%(m-1)));
}
}else if(n % 3 == 1){
out.println(n/3*2+1);
int m = n/3*2+1;
for(int i = 0;i < m;i++){
out.print(i + " ");
out.print((i <= 1 ? i : (i+n/3-3)%(m-2)+2) + " ");
out.println((i <= 1 ? n-2*i : (m-3+4-2*i+2*(m-2))%(m-2)));
}
}
}
void run() throws Exception
{
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new F().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
Python 3 Beautiful 3 Set HackerRank Solution
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)
Python 2 Beautiful 3 Set HackerRank Solution
n = int(raw_input())
triples = []
if n == 1:
triples = [(0,0,1)]
else:
beststart = -1
count = 0
for start in range((n+1)/2):
odds = min(start+1, n+1-start)
evens = min(n-start-odds+1,(n-start)/2)
if odds + evens > count:
count = odds+evens
beststart = start
o = min(beststart+1, n+1-beststart)
e = min(n-beststart-o+1,(n-beststart)/2)
for i in range(o):
triples.append((beststart-i, n-beststart-i, 2*i))
oe = beststart+1
if (n + 1 - oe) % 2 == 1:
oe += 1
for i in range(e):
triples.append((oe+i, i, n-oe-2*i))
print len(triples)
a,b,c = set(), set(), set()
for x in triples:
print x[0],x[1],x[2]
assert x[0]+x[1]+x[2] == n
a.add(x[0])
b.add(x[1])
c.add(x[2])
assert len(a) == len(triples) and len(b) == len(triples) and len(c) == len(triples)
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below