Make A and B equal (START53)

Problem Code:
MAKEABEQUAL

Contest Code:START53D
Submission byrashmi1729
Submitted: 11 hours ago

Status:
Correct Answer

Language : C
(NOTE : Try a few times yourself before moving to the solution)


#include <stdio.h>

int main(void) {
	int t;
	scanf("%d\t",&t);
	
	while(t--)
	{
	    int n;
	    scanf("%d\n",&n);
	    int a[n],b[n];
	    for(int i = 0; i < n; i++)
	    {
	        scanf("%d ",&a[i]);
	    }
	    
	    for(int i = 0; i < n; i++)
	    {
	        scanf("%d ",&b[i]);
	    }
	    
	    long long int sum_a = 0, sum_b = 0;
	    for(int i = 0; i < n; i++)
	    {
	        sum_a+=a[i];
	        sum_b+=b[i];
	    }
	    
	    if(sum_a == sum_b)
	    {
	        long long int max_abs_diff = 0;
	        for(int i = 0; i < n; i++)
	        {
	            max_abs_diff += (a[i] > b[i]) ? 0 : (b[i] - a[i]);
	            
	        }
	        
	        printf("%lld\n",max_abs_diff);
	    }
	    else
	    {
	        printf("-1\n");
	    }
	}
	return 0;
}



No comments:

Post a Comment