17
\$\begingroup\$

Sita shortlists 4 gifts priced at r1, r2, r3 and r4 respectively. She wants to buy 3 of these gifts such that it doesn't exceed her budget of x.

Input and Output Format:

Input consists of 5 integers that correspond to r1, r2, r3, r4 and x. Output consists of 2 integers. The first integer corresponds to the minimal amount she would spend for buying 3 gifts. The second integer corresponds to the maximal amount she would spend for buying 3 gifts, staying within the budget.

Sample Input

20 30 10 25 80

Sample Output

55 75
  #include<stdio.h>

 int main()
 {
int x,a[4],i,j,n,temp,big,small,sum=0;

// scanf("%d",&r1);
// scanf("%d",&r2);
// scanf("%d",&r3);
// scanf("%d",&r4);
// scanf("%d",&x);
big = 0;
n=4;
for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
}
scanf("%d",&x);
for(i=0;i<n;i++)
{
    for(j=i;j<(n);j++)
    {
        if(a[i]>a[j]){
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    sum+=a[i];
}
//sum = a[0]+a[]
small = sum-a[3];

if((sum-a[0])<=x)
big = sum-a[0];
else if((sum-a[1])<=x)
big = sum-a[1];
else if((sum-a[2])<=x)
big = sum-a[2];
else
big = small;


if(small<=x){
    printf("%d",small);
}

if(big == small){
    return 0;
}

if(big<=x){
    printf("\n%d",big);
}
return 0;
}
\$\endgroup\$
  • 2
    \$\begingroup\$ The code is working 100%.Thanks for your suggestion @BrunoCosta. I thought i could achieve it without having to use array. \$\endgroup\$ – Ankush Singh Jun 8 '16 at 17:18
  • 1
    \$\begingroup\$ I brought this question to meta \$\endgroup\$ – Bruno Costa Jun 9 '16 at 18:13
  • 1
    \$\begingroup\$ To avoid any confusion, I'd recommend adding a note within the question itself that it passes the test cases and you are wondering how to improve the code. \$\endgroup\$ – Simon Forsberg Jun 9 '16 at 19:08
7
\$\begingroup\$

First impressions

Obviously, the code needs to be indented properly. (Is that really how you wrote the code, or an artifact of the posting process? The easiest way to post code on Stack Exchange is to paste it, select it all, and hit CtrlK on the entire block.) In addition, you need to pick a consistent brace style. (Your if-statements use K&R braces, but the function definition and the for-loops use a different convention.)

Don't omit "optional" braces:

if((sum-a[0])<=x)
big = sum-a[0];
else if((sum-a[1])<=x)
big = sum-a[1];
else if((sum-a[2])<=x)
big = sum-a[2];
else
big = small;

By omitting braces, someday, you will contribute to a coding mishap, and it will be your fault. (If you use K&R braces, the braces will hardly take up any extra space.)

In modern C, you should avoid overwhelming declarations like this:

int x,a[4],i,j,n,temp,big,small,sum=0;

I have no idea what these variables are, or where they will be significant. Since the challenge used terminology r and x, I would use those names instead of making up new ones.

Validation and output

Near the end, you wrote:

if(big == small){
    return 0;
}

That check doesn't make much sense to me. First of all, the specification calls for two numbers to be output. You shouldn't decide to omit one of them. (Note that if big is over budget, then so is small.) In the absence of any guidance on how to handle these special cases, I wouldn't bother checking for these conditions, except perhaps to write a comment or assertion.

Algorithm

The messiest part of the program is this bubble-sort-and-sum loop:

for(i=0;i<n;i++)
{
    for(j=i;j<(n);j++)
    {
        if(a[i]>a[j]){
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    sum+=a[i];
}

I don't think that sorting is necessary to solve this challenge, but if it were, I would recommend writing a function to do sorting.

Suggested solution

#include <stdio.h>

int sum(int a[], size_t len) {
    …
}

int max(int a[], size_t len) {
    …
}

int main() {
    // Read input r1, r2, r3, r4, and x.  Note that r[0] holds r1.
    int r[4], x;
    for (int i = 0; i < sizeof(r) / sizeof(r[0]); i++) {
        if (!scanf("%d", &r[i])) return 1;
    }
    if (!scanf("%d", &x)) return 1;

    // Sum of all four
    int total = sum(r, sizeof(r) / sizeof(r[0]));

    // Sum of three smallest r (assumed to be within budget)
    int least = total - max(r, sizeof(r) / sizeof(r[0]));

    int most = least;
    for (int i = 0; i < 4; i++) {
        int other_sum = total - r[i];
        if (most < other_sum && other_sum <= x) {
            most = other_sum;
        }
    }
    printf("%d %d\n", least, most);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.