user7620502
user7620502

Reputation:

taking input of large no in c

Our initial numbers are 1,2 ,3 ,4 , and 5 . We can calculate the following sums using four of the five integers:

If we sum everything except 1 , our sum is . 2+3+4+5;

and same as rest

As you can see, the minimal sum is min and the maximal sum is max. Thus, we print these minimal and maximal sums as two space-separated integers on a new line.

Hints: Beware of integer overflow! Use 64-bit Integer.

This is my problem but it failed to test some values....i think it is the datatype issut but i have taken the longest no i can

Fails for this input: 769082435 210437958 673982045 375809214 380564127

Help me out here.

int main()
{
     int n=5;
    long long  unsigned int *arr = malloc(sizeof(int) * n);

       long long  unsigned int ar[n],min=0,max=0,sum=0;


    for(int i=0;i<n;i++)
    {
    scanf("%lld",&arr[i]);
    }


    for(int i=0;i<n;i++)
    {
        sum= arr[i]+sum;
    }

    for(int i=0;i<n;i++)
    {

    ar[i] = sum -arr[i];

    }




    for(int i=0; i<n ;i++)
    {
            min=ar[0];

        if(min> ar[i])
           min =ar[i];

    }

     for(int i=0; i<n ;i++)
    {
            max=ar[0];

        if(max< ar[i])
           max =ar[i];

    }

    printf("%lld %lld",min,max);
    return 0;
}

Upvotes: 0

Views: 183

Answers (2)

Pablo
Pablo

Reputation: 13580

You are allocating the wrong number of bytes:

long long  unsigned int *arr = malloc(sizeof(int) * n);

you want to create an array of long long unsigned int, but you only allocate space for n ints. The correct malloc call would be

long long  unsigned int *arr = malloc(n * sizeof *arr);

As for the other error, coderredoc's answer tell you more about these, no point in repeating them here.

Upvotes: 0

user2736738
user2736738

Reputation: 30926

You need to allocated memory for proper type of variable namely unsigned long long not int. To get rid of these silly mistakes you can use this simple way -

long long  unsigned int *arr = malloc(sizeof *arr * n);

Here you don't have to worry about the type of what is being pointed to. It is determined without you explicitly mentioning it.

Inside the for loop you are setting min to ar[0] again and again. This is not right. Do it exactly once before entering loop. Same goes with max variable too. The for loop will be (similarly for the max variable also)

unsigned long long min =ar[0];
for(int i=0; i<n ;i++)
{
    if(min > ar[i])
       min = ar[i];
}

Upvotes: 1

Related Questions