codeChallenge/SequenceFunc.java
2024-10-23 18:42:15 -04:00

44 lines
1.0 KiB
Java

import java.util.Arrays;
import java.util.Scanner;
/**
* SequenceFunc
*/
public class SequenceFunc {
private static final int MOD = 1000000007;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the number of elements: ");
int n = scan.nextInt();
System.out.println("Please enter the elements of the Array");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
}
System.out.println("The array has the following elements: ");
for (int x : arr) {
System.out.print(x + "\t");
}
// to store the last result
// let's create a temporary array
long result = 0;
int[] temp = new int[n];
for (int i = 1; i <= n; ++i) {
System.arraycopy(arr, 0, temp, 0, i);
Arrays.sort(temp, 0, i);
long fi = 0;
for (int j = 0; j < i; j++) {
fi = (fi + (long) (j + 1) * temp[j]) % MOD;
}
result = (result + fi) % MOD;
}
System.out.println(result);
scan.close();
}
}