Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 14, 2016 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/096ebc5bc1769b83b38ec6eeaabbc7c5 to your computer and use it in GitHub Desktop.
Save jianminchen/096ebc5bc1769b83b38ec6eeaabbc7c5 to your computer and use it in GitHub Desktop.
Array Construction - HackerRank - study code, score 80 (max_score) - one of only two solutions
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static bool[, ,] w;
static void Main(String[] args) {
int _tc_ = int.Parse(Console.ReadLine());
while (_tc_-- > 0) {
Array_Construction();
}
}
static int n, s, k;
static void Array_Construction() {
var tmp = Console.ReadLine().Split(' ');
n = int.Parse(tmp[0]);
s = int.Parse(tmp[1]);
k = int.Parse(tmp[2]);
w = new bool[n, s + 1, k + 1];
int[] A = new int[n];
if (construct(A, 0, 0, 0) == 1)
Console.WriteLine(string.Join(" ", A));
else
Console.WriteLine(-1);
}
static int construct(int[] A, int sum, int diffsum, int p) {
if (p == n) {
if (sum == s && diffsum == k) return 1;
return 0;
}
if (w[p, sum, diffsum]) return -1; else w[p, sum, diffsum] = true;
int i = 0;
if (p != 0) i = A[p - 1];
for (; i <= s; i++) {
if (sum + i * (n - p) > s || diffsum + (i * p - sum) * (n - p) > k) return 0;
A[p] = i;
var z = construct(A, sum + i, diffsum + i * p - sum, p + 1);
//if (z == -1) return 0;
if (z == 1) return 1;
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment