task 23 Bubble in Array

Back to Problem Solutions forum

Sergey Afonin     2016-02-15 17:23:25

This part of the code, which is considered Checksum

data = [9, 77, 248, 2881, 243, 98, 8, 8, 192, 43340, 86656, 72059, 9, 18387, 72, 41, 724, 1, 97540, 719, 65001, 603, 78, 93577, 73947, 93, 2913, 6212, 5, 44269, 20, 75853, 3964, 89753] res = data[0] * 113 % 10000007 for i in range(1,len(data)): res = (res + data[i]) * 113 % 10000007

my answer: 1934077 Expected answer: 6492779

In Problem 17 this code finds all the right

Why? what's the problem? Please help

Quandray     2016-02-15 21:22:08
User avatar

You need the checksum of the array after processing. I don't think your code is giving that.

I suggest you use the example data while testing your code. If your code doesn't give the correct answer with the example data, there is little point trying it with the test data.

Sergey Afonin     2016-02-16 16:56:17

Exactly !!! Thank you so much!!! :))

Dead Forest Foundation     2019-12-13 11:27:38
User avatar

I have the same problem after processing :(

import java.util.*;
public class Main { static int a = 1; static int b = 0; public static void main (String[]args) { double seed = 113;

    double A[] = {1, 4, 3, 2, 6, 5};            //EXAMPLE!
    BS(A);  
    double result = 0;

     for (int i = 0; i<A.length; i++) 
     {
        double temp = (result + A[i]) * seed;
        if(temp >= lim) result = (result + A[i]) * seed % lim;
        else result = (result + A[i]) * seed;
     }
    System.out.println((a) + " " + (long)result);

  } 
  public static void BS(double[] arr)           //bubble arraysort
{
    int c = 0; 
    for(int i = arr.length-1 ; i > 0 ; i--)
    {

        for(int j = 0 ; j < i ; j++)
        {
            if( arr[j] > arr[j+1] )
            {
                c++;
                double tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tmp;
                b++;
            }
        }
        if (c>0) 
        {
            a++;
            c = 0;
        }
    }
}

}

//3 5242536 - Correct answer //3 4246130 - My answer

Please login and solve 5 problems to be able to post at forum