Sum of sub-array at 10:51

how is the sum of sub-array with elements 4 and 5 zero?

hello @wtakashish
see if we are getting same modulo in prefix sum i,e

…a . . . … … . . a

then that means sum of elements between those two same modulo is divisible by n.
becuase then only
(a+something) %n = a%n
implies something%n=0

so let say we have
…a … a…a…a…
now here if i pick any two a then sum in between them will always be divisible by n.

so if p is the frequency of a in prefix sum array then p*(p-1)/2 such subarray will be there.

so there 4 is how many subarray are there with prefix sum %n equals to 4 . and then using above logic we are calculating total possible subarrays

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

import java.util.*;
public class Main {
public static void main (String args[]) {

Scanner scn=new Scanner(System.in);
	
int val1=0,val2=0;


	int n=scn.nextInt();	
	int arr1[]=new int[n];
	int k=arr1.length-1;

	for (int i=0;i<arr1.length;i++)
	{
arr1[i]=scn.nextInt();

	int res=(int) ((arr1[i])*(Math.pow(10,k)));

//System.out.println(res);
k–;

	val1=val1+res;
	
	}

// System.out.println(val1);
//
int m=scn.nextInt();
int arr2[]=new int[m];
int l=arr2.length-1;
for (int i=0;i<arr2.length;i++)
{
arr2[i]=scn.nextInt();

int res=(int) ((arr2[i])*(Math.pow(10,l)));

		l--;
		
		val2=val2+res;
		
		}
	int fin=val1+val2;
	//System.out.print(fin);
	String num=String.valueOf(fin);		
	

	for (int i=0;i<num.length();i++)
		{
			System.out.print(num.charAt(i)+", ");}
			
		System.out.print("END");

}
}