-2

This program works fine but I am getting error when giving a larger number as inputs. The link for this problem is https://www.hackerearth.com/practice/data-structures/arrays/multi-dimensional/practice-problems/algorithm/monk-and-operations/description/

#include<stdio.h>
#include<stdlib.h>
#define MAX(a,b) (((a)>(b))?(a):(b))
int main(){
    int m,n,i,j,matrsum,matcsum;
    scanf("%d %d",&m,&n);
    int a[m][n],v1,v2,v3,v4;
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    scanf("%d %d %d %d",&v1,&v2,&v3,&v4);
     for(i=0;i<m;i++){
        int rsum=0,risum=0,rusum=0;
        for(j=0;j<n;j++){
            rsum=rsum+abs(a[i][j]);
            risum+=abs(a[i][j]+v1);
            rusum+=abs(v2);
        }
        matrsum+=MAX(rsum,MAX(risum,rusum));
    }
    for(i=0;i<n;i++){
        int csum=0,cisum=0,cusum=0;
        for(j=0;j<m;j++){
            csum=csum+abs(a[j][i]);
            cisum+=abs(a[j][i]+v3);
            cusum+=abs(v4);
        }
        matcsum+=MAX(csum,MAX(cisum,cusum));
    }
    printf("%d ",MAX(matrsum,matcsum));  
} 

Example Input(Worst Case) 2 2
227790993 -762740567
57519048 -247119797
266086776 904261341 -872001509 212276239

Parthu
  • 13
  • 3
  • 1
    `int` is a signed integer, on most platforms it's 32 bits which means it has a range of about plus/minus two billion. If you're only dealing with unsigned numbers use `unsigned` and you have a range from zero to a little over four billion. For even larger numbers, there's `unsigned long long` which is guaranteed to be at least 64 bits wide, and can handle very large numbers. – Some programmer dude Jan 17 '20 at 10:41
  • 1
    Questions should contain all necessary information within them. Links should be supplemental, not necessary. Edit the question to include the sizes of the arrays and magnitudes of the elements and sums. – Eric Postpischil Jan 17 '20 at 10:46

1 Answers1

1

You could use long int instead of int if you are only working with signed integers.

The size of the long type is at least 4 bytes (32 bits). The minimum value is −2,147,483,647 and the maximum value is at least +2,147,483,647.

There is a pretty infromative post you can check here.What is the difference between "long", "long long", "long int", and "long long int" in C++?

alex01011
  • 1,670
  • 2
  • 5
  • 17