I request you to kindly explain me how to approach this question in simple language

Is question ko GFG me 2 approaches se explain kiya gya hai. maine naive and efficient dono approaches smjhne ki koshish ki pr mujhe naive approach me find all subset of size K tk smjh aaya pr mujhe isme permutation kaise niklengi aur array jiske k size ke subset nikalne hain vo array kaise bnegi yeh smjh nhi aaya.
And efficient approach me mujhe Find the total count of even and odd numbers in the given range [low, high]. yeh smj aaya lekin iske niche jo formula
likha hai vo smj me nhi aaya.( even_sum = (prev_eveneven_count) + (prev_oddodd_count) and odd_sum = (prev_evenodd_count) + (prev_oddeven_count).)
it will be a great help if any mentor can guide me how to implement these 2 approaches. :pensive:

@Prabhleen_sheenu, I will highly suggest you to try the question print all the permutation of a string first then it will much easier for you to understand
let’s discuss the first approach this approach is similarto the question of permutations of string so what you doing is for every number you re either picking that elements or not picking that element, if you are picking the element than you push it to the array if there are no element left you compute the sum, now in this question you have the constraint that we only have to consider the permutation of size k so if the size is k then u compute the sum and if sum is even then u return 1 else 0. ask me any doubt you have in this if you are clear with this then we will move to optimized solution

1 Like

Thank you so much sir.
Sir,my first doubt is -
For example, in permutation of a string we take input = abc and output= abc acb bac bca cab cba .How to take input in this case ?

here you can find subsets of the string and for every subset of size k with even sum you can return k!
say you have 4 number a b c d
and k=3
so one of the subset of size k is a b c if a+b+c is even then we know that all permutations of abc is valid as we are just changing arrangements so the sum is unchanged

1 Like

sorry to bother you. :sweat_smile:
but as in this ques Input: low = 4, high = 5, k = 3
mujhe vo array kaise pta chlegi jiske jiske phle subsets nikal ke baad me permutation nikalni hai…

Do i have to think logic in this direction?

that array would be all the numbers in the given range for eg if low = 4 high =5 the array would be {4 5} and for low =9 high=15 the array would be {9 10 11 12 13 14 15 }

image
image

sir but hm array= 4,5 se k=3 ka subset kaise nikal skte hain?

questions main mentioned hai ki (a number can be chosen multiple times) means ham ek element ko kai baar le sakte hai

Ji sir. Pr mujhe k repeating subsets nikalna smj nhi aarha. Jo naive approach me point 1 me k subsets nikalne ki approach hai usko kaise modify krenge…

hi @Prabhleen_sheenu, check this code
here i have provided the code for finding subsets of size k with repeated elements

1 Like

Awesome :slight_smile: :slight_smile:
Thank you so much sir.


sorry.my logic of finding sum is processing one by one element of out array… :sweat_smile: how can i modify this code.

And even by using this accumulate function i am getting segmentation fault


sorry.my logic of finding sum is processing one by one element of out array… :sweat_smile: how can i modify this code.

And even by using this accumulate function i am getting segmentation fault

you are not computing sum correctly, you should initialize sum outside the loop as in your code sum is initializing again and again to 0 also you need to check is sum is even outside the for loop as first you have to compute the sum of all elements and then check if its even or not

1 Like

Finally its done. Thanks a lot :slight_smile: