OA Tiktok
Anonymous User
188

A platform has n servers, where the data stored on the ft server is represented by the array memory[I). To manage the server efficiently you can
perform the following operation any (possibly zero) number of times:
Choose an index idx, such that 1<= idx <= n/2.
Swap the data of the pair of servers that are equidistant from the beginning and the ending of the array memory and have a distance less than or equal to idx
The total working efficiency of the servers is calculated as the sum of the product of the data present in each server with the index of that server.

Given an integer n, and an array memory, find the maximum possible total working efficiency that we can get, since the total working efficiency can be very large print in modulo 10^9+7.\

• 2 < n <= 10^5
• n is always an even integer.
• 1 <= memory[i] <= 10^5

Sample:1 [2,4,1,3]

Operation idx	memory	  total working efficient
			1	2	   [3,1,4,2]	(1*3 + 2*1 + 3*4 + 4*2) = 25
			2	1	   [2,1,4,3]	(1*2 + 2*1 + 3*4 + 4*3) = 28

Here, we can perform 2 Operations and the maximum possible total working
efficiency we can get is 28 modulo 10^9+7, which is equal to 28 itself. If we perform
any other operation after performing Operation 1 and Operation 2, the total
working efficiency will not increase more than 28.
Output : 28

Sample 2
memory = [5,4,1,5,3,2]
Output 81


Operation	idx	 memory	         total working efficient
			1	  2	    [2,3,1,5,4,5]  1*2 + 2*3 + 3*1 + 4*5 + 5*4 + 6*5 = 81

Sample 3 memory = [5,1,4,2,4,1,2,3]
output 114


Operation	idx	 memory	                 total working efficient
				1	3	[3,2,1,2,4,4,1,5]	(1*3 + 2*2 + 3*1 +  4*2 + 5*4 + 6*4 + 7**1 + 8**5)  = 109
				2	2	[5,1,1,2,4,4,2,3]	(1*5 + 2*1 +3*1 +   4*2 + 5*4  + 6*4 + 7*2 + 8*3) = 100
				3	1	[3,1,1,2,4,4,2,5]	(1*3 + 2*1 + 3*1 +  4*2 + 5*4  +  6*4 + 7*2+ 8*5) = 114

I am trying to code above but not there where I am going wrong! everytime 1 test case is not working.
Anyone?

import java.util.Arrays;
public class MaximizeTotalWorkingEfficiency {

public static int calculateEfficiency(int[] arr) {
final int mod = 1000000007;
long result = 0;
for (int i = 0; i < arr.length; i++) {
// System.out.println("executed ");
// System.out.println("arr------ " + arr[i]);
result = (result + (i + 1) * arr[i]) % mod;
}
// System.out.println("Result is " + result);
return (int) result;

}
public static int maximizeTotalWorkingEfficiency(int length, int[] memory)
{
int n = memory.length;
int Efficiency = 0;
int new_Efficiency = 0;
for (int idx = n/2; idx >= 1; idx--)
{
// System.out.println("idx is " + idx);
for(int j=0; j < idx; j++)
{
// System.out.println("j is " + j);
// if((Math.min(idx,n-1-idx)) <= idx){
int temp = memory[j];
memory[j] = memory[memory.length - 1 - j];
memory[memory.length - 1 - j] = temp;
// }

}
new_Efficiency = calculateEfficiency(memory);
Efficiency = Math.max(new_Efficiency,Efficiency);
}
return Efficiency;
}

public static void main(String[] args) {
int[] memory1 = {2,4,1,3};
System.out.println("Sample 1 Output: " + maximizeTotalWorkingEfficiency(4, memory1));
System.out.println("");

int[] memory2 = {5, 4, 1, 5, 3, 2};
System.out.println("Sample 2 Output: " + maximizeTotalWorkingEfficiency(6, memory2));
System.out.println("");

int[] memory3 = {5, 1, 4, 2, 4, 1, 2, 3};
System.out.println("Sample 3 Output: " + maximizeTotalWorkingEfficiency(8, memory3));
}
}

Comments (8)