Limited budget party

TLE int last test case: https://ide.codingblocks.com/#/s/15636
ques link: https://hack.codingblocks.com/contests/c/452/1239

Your time complexity is O(n^2).So it wont run for larger values of n.
Think of doing in single for loop i.e O(n).
This snippet will help you.

void subArraySum(int arr[], int n, int sum) {
	int curr_sum = arr[0], start = 0, i;

    for (i = 1; i <= n; i++) {

		while (curr_sum > sum && start < i - 1) {
			curr_sum = curr_sum - arr[start];
			start++;
		}

		if (curr_sum == sum) {
			cout<<"YES";
			return;
		}

		if (i < n)
			curr_sum = curr_sum + arr[i];

	}

	cout<<"NO";
	return;
}

Ask again if you need further help.

1 Like

please explain this in detail

Hey Akshay, can you please mention what you haven’t understand so we can explain that to you.