Two interesting problems.

Revision en1, by riddle279, 2022-03-10 16:25:04

I have come across two problems but could not build the logic to implement them.

Question 1:

given an array a of n elements [a1,a2,a3,...,an]. To calculate f(i) first sorting elements from 1 to i in non-decreasing order, then use the formula f(i)= =(1*a[1])+(2*a[2])+(3*a[3])+..+(i*a[i]). Compute (f(1)+f(2)+f(3)+...+f(n))mod(10^9+7).

constraints: 1<=n<=10^5 1<=a[i]<=10^6

Sample case: a[]=[4,3,2,1] output:65

explanation:

f(1)=1*4=4 f(2)=1*3+2*4=11 f(3)=1*2+2*3+3*4=20 f(4)=1*1+2*2+3*3+4*4=30

so output is 65%(10^9+7)=65.

Question 2:

Given an array a of n elements [a0,a1,a2,...an-1]. The value of array is defined square of sum of even positioned elements minus odd positioned elements. Find the maximum possible value of any subarray of a[].

constraints: 1<=n<=10^5 -10^6<=a[i]<=10^6

sample case: a[]=[-1,-4,2] output: 36

explanation: consider the subarray[-4,2] the value is(-4-2)^2=36.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English riddle279 2022-03-16 01:16:52 4 (published)
en1 English riddle279 2022-03-10 16:25:04 967 Initial revision (saved to drafts)