Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Party to friends Or Not

Ashutosh has N friends and he wants to give a party to his C friends on his birthday. Ashutosh knows the amount he will have to spend on each of his N friends in party. Given the expense amount of each of the N friends, comment if it is possible for Ashutosh to give a party to his C friends if he has just R rupees.

Input Method

First line contains three space separated integers: N, C and R respectively

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Second line contains N space separated integers which represents the amount he needs to spend on each of his N friends.

Constraints

N <= 1000

C is lesser than N

R < 10000

Output
Output Method

If it is possible to give party to C friends, print "Party"

Else print "Sad".

Sample Input 1

5 3 24
6 4 21 20 13

Sample Output 1

Party

function solve(N,C,R,arr){
    //console.log(N,C,R,arr)
    var flag=false;
    function partyOrNot(N,C,R,arr,position,newarr){
        var sum=0;
        for(var j=0;j<newarr.length;j++){
            sum+=newarr[j];
        }
        
        if(newarr.length==C && sum<=R){
            flag=true;
            return;
        }
        if(sum>=R||newarr.length>=C){
            return;
        }
        else{
            for(var i=position;i<N;i++){
                newarr.push(arr[i]);
                partyOrNot(N,C,R,arr,i+1,newarr);
                newarr.pop();
            }
            return;
        }
    }
    
    
    partyOrNot(N,C,R,arr,0,[]);
    //console.log(flag);
    if(flag){
        console.log("Party")
    }
    else{
        console.log("Sad")
    }
}

I tried this but not get correct answer some test cases passed but still partial accepted.

>Solution :

You Just need to remove = from this condition.

 if(sum>R||newarr.length>C){
            return;
 }

After this it will work fine.

  • Use can solve this problem using sorting. first sort the complete array
  • And take Sum variable and Start the Loop 0 to n
for(let i=0;i<n;i++){
   sum+=arr[i];
   if(sum<=R){
     print "Party"
   }else{
     print "Sad"
   }
}
  • This will work as well Thanks!
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading