//Microsoft (R) Visual C# Compiler version 3.4.0-beta4-19562-05 (ff930dec)
//Copyright (C) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
public static void Main(string[] args)
int[] arr = new int[] { 1, 1, 2, 3};
int diff = 1;
//Formula or trick to find the count of subsets with given difference
// s1 - s2 = diff
// s1 - (sum - s1) = diff
// s1 - sum + s1 = diff
// 2s1 - sum = diff
// 2s1 = diff + sum
// s1 = (diff + sum) / 2;
int s1 = (diff + arr.Sum()) / 2;
Console.WriteLine(CountSubsetSum (arr, s1) + " subsets found with given difference " + diff);
}
private static int CountSubsetSum (int[] arr, int sum)
int[,] t = new int[arr.Length+1, sum+1];
for (int i=0; i < t.GetLength(0); i++)
for (int j=0; j < t.GetLength(1); j++)