How to solve this problem for non-contiguous range?Prateek wants to give a party to his N friends on his birthday, where each friend is numbered from 1 to N. His friends are asking for a gift to come to the party, instead of giving him one. The cost of the gifts are given in the array Value where ith friend asks for a gift which has a cost Cost[i].But, Prateek has only X amount of money to spend on gifts and he wants to invite his friends which are in continuous range such that sum of the cost of the gifts of those friends will be exactly equal to X.If he can invite his friends, who can satisfy the above condition then, print YES otherwise print NO.Input:The first line contains a single integer T, denoting the number of test cases. In each test case, the following input will be present: - The next line contains two space-separated integers N and X, where N represents the number of friends and X represents amount of money which Prateek can spend on gifts.- Next N line contains N integers, where i'th line contains i'th integer, which represents the Cost[i] .OuputOutput exactly T lines, each containing the answer to the corresponding test case . Here is how I solved for the contiguous range.#include <iostream>using namespace std;bool checkCost(int *a,int l,int r,int sum,int n,int x){    if(r<=n)    {        if(sum==x)            return true;        if(sum<x)            return checkCost(a,l,r+1,sum+a[r],n,x);        else            return checkCost(a,l+1,r,sum-a[l],n,x);    }    return false;}int main(){    int t;    cin >> t;    while(t--)    {        int n,x;        cin >> n >> x;        int a[n];        for(int i=0; i<n; i++)        {            cin >> a[i];        }        if(checkCost(a,0,0,0,n,x))            cout<<YES<<endl;        else            cout<<NO<<endl;    }    return 0;}
HackerEarth Prateek and his Friends non-contiguous
c++;algorithm;array