forked from chitwang/iitk-sem1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candy-crush.c
68 lines (43 loc) · 2.06 KB
/
candy-crush.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*Your friendly neighborhood shopkeeper has decided to reward your excellent performance at JEE and give you some candies. She gives you a list of 10 integers (not all may be positive) and tells you to select two locations in the list (i.e. two numbers between 1 and 10 -- the first number being smaller than or equal to the second number).
Once you tell her these two locations, the shopkeeper will to give as many candies as the sum of all the numbers occurring between the locations indicated by you (including the locations specified by you). Write an algorithm to calculate what is the maximum number of candies you can get this way. Your input will be a list of 10 integers and your output should be the maximum number of candies you can get, the starting location, and the ending location, all separated by a single space with no trailing spaces at the end.
Caution
Be careful about extra/missing lines and extra/missing spaces.
Be careful that array locations start from zero whereas in your output, you should give locations that start from one, just as we do in day to day conversations.
Constraints
All numbers lie between -1000 and 1000.
Sample Input
1 3 5 2 6 -7 1 1 -3 1
Sample Output:
17 1 5
Explanation : If you select the locations 1 and 5, then you get will get 1+3+5+2+6 = 17 candies since the numbers between the first and fifth locations (including both ends) in the list are 1, 3, 5, 2, 6. You can verify that this is the maximum number of candies you can get.*/
// solution:
#include <stdio.h>
int main()
{
int arr[10];
int max = 0;
int a;
int b;
for (int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
for (int i = 0; i < 10; i++)
{
for (int j = i; j < 10; j++)
{
int sum = 0;
for (int p = i; p <= j; p++)
{
sum += arr[p];
if (sum > max)
{
max = sum;
a = i + 1;
b = j + 1;
}
}
}
}
printf("%d %d %d", max, a, b);
}