The reverse order in the array to Java implementation __java

Source: Internet
Author: User

Two digits in an array, and if the previous number is greater than the following number, the two digits form a reverse-order pair. Enter an array to find the total number of reverse pairs in this array p. and outputting the result of p to 1000000007 modulo. That is, the output p%1000000007

public class Solution {public static int inversepairs (int [] array) {if (array==null| |
        array.length==0) {return 0;
        } int[] copy = new Int[array.length];
        for (int i=0;i<array.length;i++) {Copy[i] = Array[i];

    int count = Inversepairscore (array,copy,0,array.length-1);//The value is too large to find the return count;
            private static int Inversepairscore (int[] array,int[] copy,int low,int high) {if (Low==high) {
        return 0;
        int mid = (Low+high) >>1;
        int leftcount = Inversepairscore (array,copy,low,mid)%1000000007;
        int rightcount = Inversepairscore (Array,copy,mid+1,high)%1000000007;
        int count = 0;
        int i=mid;
        int J=high;
        int loccopy = high; while (I>=low&&j>mid) {if (Array[i]>array[j]) {count = J
                -mid; copy[loccopy--]= array[i--];
                if (count>=1000000007)//Numerical value is too large to find more than {count%=1000000007;
            } else {copy[loccopy--] = array[j--];
        } for (; i>=low;i--) {copy[loccopy--]=array[i];
        for (; j>mid;j--) {copy[loccopy--]=array[j];
        for (int s=low;s<=high;s++) {Array[s] = Copy[s];
    Return (Leftcount+rightcount+count)%1000000007; }
}

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.