Addition Under Modulo

PHOTO EMBED

Sat Feb 05 2022 12:20:21 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #additionundermodulo #mathematics #gfg #geeksforgeeks

import java.util.*;
import java.io.*;

class Solution {
    public static long sumUnderModulo(long a, long b){
        
        long M = 1000000007;
        // a+b mod M = (a mod M + b mod M)mod M
        return  (a % M + b % M)%M;
    }   
}

class GFG
{
    public static void main(String args[])throws IOException
    {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        
        //taking testcases
        int t = Integer.parseInt(read.readLine());
        
        while(t-- > 0) {
            String[] str = read.readLine().trim().split(" ");
            
            //taking input a and b
            Long a = Long.parseLong(str[0]);
            Long b = Long.parseLong(str[1]);
            
            //calling method sumUnderModulo()
            System.out.println(new Solution().sumUnderModulo(a,b));
        }
    }
}
content_copyCOPY

9. Addition Under Modulo Given two numbers a and b, find the sum of a and b. Since the sum can be very large, find the sum modulo 10^9+7. Example 1: Input: a = 9223372036854775807 b = 9223372036854775807 Output: 582344006 Explanation: 9223372036854775807 + 9223372036854775807 = 18446744073709551614. 18446744073709551614 mod (10^9+7) = 582344006 Example 2: Input: a = 1000000007 b = 1000000007 Output: 0 Explanation: 1000000007 + 1000000007 = (2000000014) mod (10^9+7) = 0 Your Task: You don't need to read input or print anything. Your task is to complete the function sumUnderModulo() that takes a and b as input parameters and returns sum of a and b under modulo 10^9+7. Expected Time Complexity: O(1) Expected Auxilliary Space: O(1) Constraints: 1 <= a,b <= 2^63 - 1

https://practice.geeksforgeeks.org/problems/addition-under-modulo/1/?track=DSASP-Mathematics&batchId=190