I am pretty noobie with C++ and am trying to do some HackerRank challenges as a way to work on that.
Right now I am trying to solve Angry Children problem: https://www.hackerrank.com/challenges/angry-children
Basically, it asks to create a program that given a set of N integer, finds the smallest possible "unfairness" for a K-length subset of that set. Unfairness is defined as the difference between the max and min of a K-length subset.
The way I'm going about it now is to find all K-length subsets and calculate their unfairness, keeping track of the smallest unfairness.
I wrote the following C++ program that seems to the problem correctly:
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int unfairness = -1; int N, K, minc, maxc, ufair; int *candies, *subset; void check() { ufair = 0; minc = subset[0]; maxc = subset[0]; for (int i = 0; i < K; i++) { minc = min(minc,subset[i]); maxc = max(maxc, subset[i]); } ufair = maxc - minc; if (ufair < unfairness || unfairness == -1) { unfairness = ufair; } } void process(int subsetSize, int nextIndex) { if (subsetSize == K) { check(); } else { for (int j = nextIndex; j < N; j++) { subset[subsetSize] = candies[j]; process(subsetSize + 1, j + 1); } } } int main() { cin >> N >> K; candies = new int[N]; subset = new int[K]; for (int i = 0; i < N; i++) cin >> candies[i]; process(0, 0); cout << unfairness << endl; return 0; } The problem is that HackerRank requires the program to come up with a solution within 3 seconds and that my program takes longer than that to find the solution for 12/16 of the test cases. For example, one of the test cases has N = 50 and K = 8; the program takes 8 seconds to find the solution on my machine. What can I do to optimize my algorithm? I am not very experienced with C++.