Code- https://ide.codingblocks.com/s/406835
Can you give me hint how can I optimize this code?
I’ve got a couple of questions regarding this code.
When do we really use set? I mean what is set used for? Isn’t it same as an array storage?
cin>>n;
int a[n];
Shouldn’t this give error or warning? I have read that we shouldn’t declare arrays like this.
@PranavM16
set is basically used when we want ti check the existence of the element.
and no there is no error in this a[n] because it is not used when in constraints the size of the array can be greater then the size 10^6 because we cannot declare the array with greater then this locally(in the main).
if you have any doubt you can ask here:
Happy Learning!!
ans+=((j-i+1)*(j-i))/2;
ans=ans%1000000007;
mp.erase(a[i]);
not able to understand this part
Hello @PranavM16 if you will dry run then you will get what this ‘ans+=((j-i+1)*(j-i))/2;’ means.
ans=ans%1000000007;
it is mentioned in the ouput format to print the answer after taking modulo.
Hello @PranavM16 if you will dry run then you will get what this ‘ans+=((j-i+1)*(j-i))/2;’ means.
Does this formulae helps us to calculate the number of possible subarrays/ permutations within a particular range?
If yes, then how were you able to think about this formulae? Just observing the pattern?
Yes this will help in the same only.
this is the logic development by solving questions you will get.
if we know all elements in a subarray arr[i…j] are distinct, sum of all lengths of distinct element subarrays in this sub array is ((j-i+1)*(j-i+2))/2.
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.