Codility Lesson3 1. Minavgtwoslice

Source: Internet
Author: User

Task description

A non-empty zero-indexed array a consisting of N integers is given. A pair of integers (P, Q), such that 0≤p < Q < N, was called a slice of array A (notice that the slice con Tains at least-elements). The average of a slice (P, Q) is the sum of a[p] + a[p + 1] + ... + a[q] divided by the length of the slice. To be precise, the average equals (A[p] + a[p + 1] + ... + a[q])/(q−p + 1).

For example, array A such that:

    A[0] = 4    a[1] = 2    a[2] = 2    a[3] = 5    a[4] = 1    a[5] = 5    a[6] = 8

Contains the following example slices:

  • Slice (1, 2), whose average is (2 + 2)/2 = 2;
  • Slice (3, 4), whose average is (5 + 1)/2 = 3;
  • Slice (1, 4), whose average is (2 + 2 + 5 + 1)/4 = 2.5.

The goal is to find the starting position of a slice whose average is minimal.

Write a function:

Class Solution {public int solution (int[] A);}

That is, given a non-empty zero-indexed array a consisting of N integers, returns the starting position of the slice with the Minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.

For example, given array A such that:

    A[0] = 4    a[1] = 2    a[2] = 2    a[3] = 5    a[4] = 1    a[5] = 5    a[6] = 8

The function should return 1, as explained above.

Assume that:

  • N is an integer within the range [2.. 100,000];
  • Each element of within the range [−10,000. ].

Complexity:

  • Expected worst-case time complexity is O (N);
  • Expected worst-case space complexity is O (N), beyond input storage (not counting the storage required for input arguments) .

Elements of input arrays can be modified.

classSolution { Public intSolution (int[] A) {//Write your code in Java SE 8        intleft = 0;//Use of pointers left and right to traverse the array        intright = 1; intLen =a.length; intsum = a[0] + a[1]; intSumleft = 0; DoubleAVG = 0; DoubleMin = 10001; intres = 0;  while(Left < len-1 && Right <Len) {avg= (sum-sumleft)/(Double) (Right-left + 1);//Int/int can not get double if donot convert            if(Avg <min) {min=avg; Res=Left ; }            if(A[left] > Avg && left < right-1) {//When move left pointer can decrease the AVG then move it.left++; Sumleft+ = A[left-1]; Continue            //must have this, or may appear the right grow to Len in next step and left have no chance to update. }            if(Right < len-1 && A[right+1] < avg)//When move right pointer can decrease the AVG then move it.|| left = = right-1) {//Make sure threr are not "piece/0"right++; if(Right <len) sum+=A[right]; } Else{ Left++;//if not move leftSumleft + = A[left-1]; }        }        returnRes; }}

Codility Lesson3 1. Minavgtwoslice

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.