Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the coding logic for the question ? thier are n shops, each shop will give a coupen with some number on it. if you take a coupen from a shop then you cant take coupen from the next k shops. at the end you should posses the max total of coupens.
Input:- n(no of shops) k(shops to skip)
(Values of coupen you get at a shop respectively)
Output:- sum(max coupens collected)

Sample input
10 2
120
3
7
9
150
0
7
10
6
Answer 120+150+10

What I have tried:

Ive tried for the logic but no luck
Posted
Updated 22-Jul-18 4:12am
v2

Start by reading the input and storing it.
Then process the input repeatedly, trying each combination.
For example, if you take the first shops coupon, then you cannot take a coupon from the next two, but you can take the fourth shops coupon if you want - 7. If you do, then you will have 2 coupons in total. So one combination is 0, 3 (because indexes start at zero)
If you don't take the forth shop coupon, you could take the fifth: 9
So you will end up with combinations
0, 3
0, 4
0, 5
0, 6, 7
0, 6, 8
0, 6, 9
1
2, 6, 7
2, 6, 8
2, 6, 9
...
Your job is to work out what the longest such combination may be: 3 in this case.

That's the "brute force and ignorance" approach - and while it will work, it won't be particularly efficient, particularly for large datasets. It may be that your test will require a better approach, such as a weighted graph: Longest path problem - Wikipedia[^] but that will be a lot more complex to implement.

Either way, you'll get no code: this is a test of your abilities, not mone!
 
Share this answer
 
v2
Quote:
How do I wite the code for stalls and coupens coding questions (tcs digital advanced coding questions)?

First of all, in this kind of challenges, every word matters. So it is a good idea to give exact wording and/or a link to original webpage.
Quote:
Ive tried for the logic but no luck

This is a challenge assigned to you by you. Your goal is to find out if you can solve the problem or not. Since you didn't solved the challenge, it means that you need to sharpen your kills. Studying data structures and algorithms is a good start, Stepwise refinement is a good idea too.
Structured Programming.pdf[^]
Quote:
Ive tried for the logic but no luck

Take a sheet of paper and a pencil and try to solve the problem by hand, no computer. Solve other datasets. Make it an algorithm.
More or less you have to check all combination of shops and keep best result.

[Update]
The code you posted in Solution 3 is wrong:
- first reason, you never reset curr_val to 0 as you test different paths.
- "i couldn't make it go beyond 10" is also an indication that the code is wrong.
 
Share this answer
 
v2
well when i wrote the test even i couldn't figure it out,but after coming home,when i tried,it worked here the solution:
C++
#include <stdio.h>

int main(void) {
	int n,k,arr[20],i,j,curr_val=0,max_val=0,l,c;
	scanf("%d,%d",&n,&k);
for(i=0;i<n;i++){
    scanf("%d",&arr[i]);
}
j=0;
while(j<n){
    l=0;
while(l<n){
    c=0;
for(i=j;i<n;){
    ++c;
    if(c==1){
 if(j-k-1>0)
     curr_val+=arr[j-k-1];
    }
 curr_val+=arr[i];
if(i==j)
 i+=l+k+1;
 else
 i+=k+1;
}
if(curr_val>max_val){
    max_val=curr_val;
}
curr_val=0;
l++;
}
j++;
}
printf("%d",max_val);
	return 0;
}

i couldn't make it go beyond 10 so most probably it will work good for input of 10 values
 
Share this answer
 
v2
Comments
Patrice T 22-Jul-18 10:52am    
is it your solution, or your work so far ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900