Created
June 11, 2017 18:59
-
-
Save jianminchen/095e5677cd148b45dea119927906d53e to your computer and use it in GitHub Desktop.
Hackerrank parity party - recursive solution - score 0, https://www.hackerrank.com/contests/infinitum18/challenges/parity-party
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
class Solution | |
{ | |
static void Main(String[] args) | |
{ | |
string[] tokens_n = Console.ReadLine().Split(' '); | |
int n = Convert.ToInt32(tokens_n[0]); | |
int a = Convert.ToInt32(tokens_n[1]); | |
int b = Convert.ToInt32(tokens_n[2]); | |
int c = Convert.ToInt32(tokens_n[3]); | |
int result = parityParty(n, a, b, c); | |
Console.WriteLine(result); | |
} | |
/// <summary> | |
/// n - n kinds of candies, n is also total of candies | |
/// a - odd number of candies | |
/// b - even number of candies | |
/// c - don't care of candies | |
/// </summary> | |
/// <param name="n"></param> | |
/// <param name="a"></param> | |
/// <param name="b"></param> | |
/// <param name="c"></param> | |
/// <returns></returns> | |
static int parityParty(int n, int a, int b, int c) | |
{ | |
// Complete this function | |
var odd = new int[a]; | |
var even = new int[b]; | |
var doNotCare = new int[c]; | |
return parityPartyCount(n, a, b, c, odd, even, doNotCare); | |
} | |
private static int parityPartyCount(int n, int a, int b, int c, int[] odd, int[] even, int[] doNotCare) | |
{ | |
int modulo = 7340033; | |
if (n == 0 ) | |
{ | |
bool passBoth = checkOdd(odd) || checkEven(even); | |
if (passBoth) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
int sum = 0; | |
for(int i = 0; i < odd.Length; i++) | |
{ | |
odd[i] += 1; | |
sum += parityPartyCount(n - 1, a, b, c, odd, even, doNotCare); | |
sum = sum % modulo; | |
odd[i] -= 1; | |
} | |
for (int i = 0; i < even.Length; i++) | |
{ | |
even[i] += 1; | |
sum += parityPartyCount(n - 1, a, b, c, odd, even, doNotCare); | |
sum = sum % modulo; | |
even[i] -= 1; | |
} | |
for (int i = 0; i < doNotCare.Length; i++) | |
{ | |
doNotCare[i] += 1; | |
sum += parityPartyCount(n - 1, a, b, c, odd, even, doNotCare); | |
sum = sum % modulo; | |
doNotCare[i] -= 1; | |
} | |
return sum; | |
} | |
/// <summary> | |
/// Satified means that each person should be odd number | |
/// </summary> | |
/// <param name="numbers"></param> | |
/// <returns></returns> | |
private static bool checkOdd(int[] numbers) | |
{ | |
foreach (var number in numbers) | |
{ | |
if( number % 2 == 0) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
private static bool checkEven(int[] numbers) | |
{ | |
foreach (var number in numbers) | |
{ | |
if (number % 2 == 1) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment