fork(8) download
  1. /**
  2.  * For the solution i have approached as follows for eg:
  3.  * we have given array a=[4,3,2]
  4.  * so if we see the permutation we would get as such:
  5.  *
  6.  * 4*4
  7.  * 4*3+3*3
  8.  * 3*3
  9.  * 3*2+2*2
  10.  * 4*2+3*2+2*2
  11.  * 2*2
  12.  *
  13.  * we will arrange in such a way that how many times 4 will repeat ,how many times 3 will repeat and so on
  14.  * so we would get something like this
  15.  *
  16.  * 4(4)+3(4+3+3)+2(4+3+3+2+2+2)
  17.  * on further solving we will get
  18.  * 4(1*4)+3(1*4+2*3)+2(1*4+2*3+3*2)
  19.  *
  20.  *
  21.  *
  22.  * so i have created two arrays one which will contain the original elements 4,3,2 named input1
  23.  * and another array named arr which will keep the counts of 4,3,2
  24.  * then we will multiply the elements of arr and input1 and add it to get the output
  25.  *
  26.  *
  27.  *
  28.  
  29.  */
  30. import java.util.*;
  31. class Ideone {
  32. public static long answer=0;
  33. public static int longvalue=1000000007;
  34. public static void main(String args[])
  35. {
  36. Scanner s=new Scanner(System.in);
  37. int n=s.nextInt();
  38. int a[]=new int[n];
  39. for(int i=0;i<n;i++)
  40. a[i]=s.nextInt();
  41. long start=System.currentTimeMillis();
  42. int output=possibleways(a);
  43. System.out.println(output);
  44. long stop=System.currentTimeMillis();
  45. System.out.println(stop-start);
  46. }
  47. private static int possibleways(int[] input1) {
  48. long[] arr=new long[input1.length];
  49. arr[0]=input1[0];
  50. answer=0;
  51. for(int i=1;i<input1.length;i++)
  52. {
  53. int temp=((i+1)*input1[i]);
  54. arr[i]=arr[i-1]+temp;
  55.  
  56. }
  57. for(int i=0;i<arr.length;i++)
  58. {
  59. long temp=(input1[i]*arr[i]);
  60. answer=(answer+temp)%longvalue;
  61. }
  62.  
  63. return (int)answer%longvalue;
  64. }
  65.  
  66. }
  67.  
Success #stdin #stdout 0.1s 2841600KB
stdin
3 1 2 3
stdout
53
0