[Hackerrank] Game of Rotation

Source: Internet
Author: User

Question connection: Game of Rotation

Mark is an undergraduate student and he is interested in rotation. a conveyor belt competition is going on in the town which mark wants to win. in the competition, there's a conveyor belt which can be represented as a strip1xnBlocks. each block has a number written on it. the belt keeps rotating in such a way that after each rotation, each block is shifted to left of it and the first block goes to last position.

There is a switch near the conveyer belt which can stop the belt. Each participant ipant wocould be given a single chance to stop the belt and hisPmeanWocould be calculated.

PmeanIs calculated using the sequence which is there on the belt when it stops. The participating ipant having highestPmeanIs the winner. There can be multiple winners.

Mark wants to be among the winners. WhatPmeanHe shoshould try to get which guarantees him to be the winner.

WhereIIs the index of a block at the conveyor belt when it is stopped. indexing starts from 1.

Input Format 
First line containsNDenoting the number of elements on the belt.
Second line containsNSpace separated integers.

Output Format 
Output the requiredPmean

Constraints 
1 ≤ n ≤106
-109 ≤ each number ≤ 109

 

Question: at the beginning, we thought that the index value pairs should be as large as possible, so we sorted the array and then obtained the sum of the Index * values.

The biggest bug of this idea is that the array can only be changed through translation, and cannot be changed at will. Therefore, the array obtained by sorting may not be converted.

The correct method is to simulate n transformations, put the first element at the end each time, and then calculate the new sum. Note that the new sum can be obtained directly from the old sum: new_sum = old_sum-(A1 + A2 +... + an) + N * A [I]; where a [I] is the number put to the end in the current loop. Only its index value is increased by (n-1), and the index values of other elements are reduced by 1.

Note that the int type in Java ranges from-2147483648 ~ 2147483647. In the worst case of the question, the sum of all numbers can reach 1015. Therefore, the long type is used. The range of the Long type is-9223372036854774808 ~ 9223372036854774807, enough.

The Code is as follows:

 1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4  5  6 public class Solution { 7     public static void main(String[] args) { 8         Scanner in = new Scanner(System.in); 9         int n = in.nextInt();10         Long[] num = new Long[n];11         Long array_sum = 0L;12         Long weight = 0L;13         for(int i = 0;i < n;i++){14             num[i]= in.nextLong();15             array_sum += num[i];16             weight += (i+1) * num[i];17         }18         Long max = weight;19         for(int i = 0;i < n;i++){20             weight = weight - array_sum + n*num[i];21             max = Math.max(max, weight);22         }23         24         System.out.println(max);25         26         27         28       }29 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.