Subarray with ditinct elements hashing


how to write output in 10^9+7 format and kindly tell the logic also

I have edited your code, try to submit it now,

Hello @Vibhuti0206

Before knowing what 10^9 + 7 is used for, you need to know what % (Modulo) is used for
The operator % (Modulo) is the used to get the remainder, we get when dividend is divided by the divisor
Example
5 % 3 = 2
11 % 2 = 1

Now you need to print (sum % (10^9 + 7)) in your question.

Why we take modulo ?
Integer can only store values upto +2,147,483,647
We take the mod each time something is added to sum so that sum never reach beyond +2,147,483,647 (Which we then won’t be able to store in the integer variable).

Why 10^9 + 7
10^9 + 7 is used because it is a prime number
We chose a prime number because if its a prime number and if we take mod of a number by Prime the result is generally spaced i.e. the results are very different results in comparison to mod the number by non-prime, that is why primes are generally used for mod.

yukti maam,could you kindly explain how did you adjust ans%(10^9+7) with the line21

how to deal with 10^9+7 in answer?

I hope you understood wht I explained to u over the chat option…

maam the only thting i havenot understood is that you have used (j-i)(j-i+1) in formula whereas the formulas in video that has been taught uses (i-j+1)(i-j+2)??? and in if we use video formula then asnwer goes out of range?

The solution is based on the fact that if we know all elements in a subarray arr[i…j] are distinct, sum of all lengths of distinct element subarrays in this subarray is ((j-i) * (j-i+1))/2 .
As, the possible lengths of subarrays are 1, 2, 3,……, j – i +1. So, the sum will be ((j – i ) * (j – i +1))/2.
On this basis only, formula has been used…

Since you arent replying anything, I am marking this doubt as resolved… You can reopen it if u still face any issues…