My goal is to use various sorting methods to run 500 iterations of two random(each time) arrays, calculate the runtime and store it into an array. When all 500 iterations are done, I want to average the times and print out the average for each array size at the end, not each time the loop runs. I am currently getting the following error: "Index 500 out of bounds for length 500" I am also getting the same average for each time the loop runs. How can I run an unsorted array each time through my loop? How can I store the value? How can I do it again for the other array size?
My example of bubble Sort is as follows:
public static void main(String[] args) {
{
int[] smallUnsortedArray = createArrayWithRandomInts(99);
bubbleSort(smallUnsortedArray);
int[] largeUnsortedArray = createArrayWithRandomInts(10000);
bubbleSort(largeUnsortedArray);
}
}
/**
* Sorting an array of ints in ascending order using bubbleSort
* Best-Case Complexity: O(n), Average Complexity: O(n^2), Worst-Case Complexity: O(n^2)
* O(n) is achieved in Best-Case (already sorted array) using the alreadySorted flag
* #param array
* #return
*/
static int[] bubbleSort(int[] array)
{
int bubbleSortTime[] = new int[500];
int iterations = 0;
while(iterations < 500) {
int temp;
boolean alreadySorted = true;
long start = System.nanoTime();
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length - 1; j++)
{
if (array[j] > array[j + 1])
{
alreadySorted = false;
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
if (alreadySorted == true)
{
break;
}
;
long end = System.nanoTime();
long bubbleSortRunTime = end - start;
bubbleSortTime[i] = (int) bubbleSortRunTime;
i++;
}
// getting array length
int length = bubbleSortTime.length;
// default sum value.
int sum = 0;
// sum of all values in array using for loop
for (int i = 0; i < bubbleSortTime.length; i++) {
sum += bubbleSortTime[i];
}
double average = sum / bubbleSortTime.length;
System.out.println("Average of bubble sort : "+average + " nano seconds.");
}
iterations++;
}
return array;
}
static int[] createArrayWithRandomInts(int size)
{
int[] array = new int[size];
for (int i = 0; i < size; i++)
{
array[i] = (int) (Math.random() * Math.random() * 100000);
}
return array;
}
}
I had tried it this way(original):
static int[] bubbleSort(int[] array)
{
int bubbleSortTime[] = new int[500];
int x, y;
for (x = 0; x< 500; x++) {
int temp;
boolean alreadySorted = true;
long start = System.nanoTime();
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length - 1; j++)
{
if (array[j] > array[j + 1])
{
alreadySorted = false;
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
if (alreadySorted == true)
{
break;
}
}
long end = System.nanoTime();
long bubbleSortRunTime = end - start;
bubbleSortTime[x] = (int) bubbleSortRunTime;
x++;
}
// getting array length
int length = bubbleSortTime.length;
// default sum value.
int sum = 0;
// sum of all values in array using for loop
for (int i = 0; i < bubbleSortTime.length; i++) {
sum += bubbleSortTime[i];
}
double average = sum / length;
System.out.println("Average of bubble sort : "+average + " nano seconds.");
}
return array;
}
I'm not sure where my logic is breaking down. I think my current build is better than the previous, but I didn't get the out of bounds error with the original.
Your first loop (the "i" loop) should be on bubbleSortTime.length, not array.length. And you don't need the extra i++ at the end of the "i" loop. That will cause you to walk off the end of the array. (ArrayIndexOutOfBounds)
public static int desc(int number) {
// Time complexity = 2n+n2
// space complexity = ?
int i, j, temp;
int array[] = new int[Integer.toString(number).length()];
for (i = 0; i < array.length; i++)
array[i] = Integer.toString(number).charAt(i) - '0';
for (i = 0; i < array.length - 1; i++) {
for (j = 0; j < array.length - i - 1; j++) {
if (array[j] < array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
StringBuilder strNum = new StringBuilder();
for (int k : array) {
strNum.append(k);
}
int finalInt = Integer.parseInt(strNum.toString());
System.out.println(finalInt);
return finalInt;
}
This is my code as per my understanding I am able to calculate time complexity please suggest if the time complexity is correct or not and also help me to calculate the space complexity of this program I am bit confuse on how to calculate of space complexity .
First let's optimize your code from:
int array[] = new int[Integer.toString(number).length()];
for (i = 0; i < array.length; i++)
array[i] = Integer.toString(number).charAt(i) - '0';
to:
String to_number = Integer.toString(number);
int array[] = new int[to_number.length()];
for (i = 0; i < array.length; i++)
array[i] = to_number.charAt(i) - '0';
No need to call Integer.toString(number) multiple times.
Being N the number of digits in the parameter number (i.e., Integer.toString(number).length()) the time complexity can be calculated as follows:
String to_number = Integer.toString(number);
has a time complexity of N and the same for:
for (i = 0; i < array.length; i++)
array[i] = to_number.charAt(i) - '0';
The follow up double loop:
for (i = 0; i < array.length - 1; i++) {
for (j = 0; j < array.length - i - 1; j++) {
if (array[j] < array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
is a well-know for being N(N-1)/2, which can be simplified to a time complexity of O(N^2). The next loop
for (int k : array) {
strNum.append(k);
}
has time complexity of N. Finally,
int finalInt = Integer.parseInt(strNum.toString());
is also N. So the time complexity is N + N + N^2 + N + N, which simplifies to O(N^2) time complexity, also known as quadratic time complexity.
Now let us look at the space complexity:
int array[] = new int[Integer.toString(number).length()];
in this case N, and
for (int k : array) {
strNum.append(k);
}
also N, so the space complexity is 2N, which simplifies to O(N).
My code is supposed to take the random number generated in the random method and sort them but it's only giving me one number.
My program is a random number generator that is supposed to make 1000 numbers that I can sort but my code only inserts one number into the array.
public static void main(String[] args) {
// write
int max = 1000;
int min=0;
int range = max - min + 1;
// generate random numbers within 1 to 10
for (int i = 0; i < 1000; i++) {
int rand = (int) (Math.random () * range) + min;
System.out.println ( rand );
int array[] = {rand};
int size = array.length;
for ( i = 0; i < size - 1; i++) {
int min1 = i;
for (int j = i + 1; j < size; j++) {
if (array[j] < array[min1]) {
min = j;
}
}
int temp = array[min1];
array[min1] = array[i];
array[i] = temp;
}
for (int k = 0; k < size; i++) {
System.out.print(" " + array[i]);
}
}
}
You need to break your program into separate steps:
Insert all the random numbers into the array
Sort the array
Print the contents of the array
Few problems I noticed:
Since you want to generate 1000 numbers from 1-10, max and min should have values of 10 and 1, respectively.
array should be declared before you start inserting values. It should also have a fixed size of 1000.
Your bubble sort algorithm also had some errors which led to incorrect output. If you wish to sort the array from greatest to least instead, simply change the > to < in the condition of the if statement.
I also decided to use Arrays.toString() to print the array instead of the loop.
public static void main(String[] args) {
int max = 10;
int min = 1;
int range = max - min + 1;
int size = 1000;
int[] array = new int[size];
for (int i = 0; i < size; i++) {
int rand = (int) (Math.random() * range + min);
array[i] = rand;
}
int temp = 0;
for (int i = 0; i < size; i++) {
for (int j = 1; j < size - i; j++) {
if (array[j - 1] > array[j]) {
temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
System.out.println(Arrays.toString(array));
}
your code will result an ArrayIndexOutException. below is the code change from your code ,i dont change too much so you can compare them and find your mistakes,wish good :D
public static void main(String[] args) {
int max = 1000;
int min=0;
int range = max - min + 1;
int[] array = new int[range];
// generate random numbers within 1 to 10
for (int i = 0; i < 1000; i++) {
int rand = (int) (Math.random () * range) + min;
array[i] = rand;
}
int size = array.length;
for (int i = 0; i < size; i++) {
int min1 = i;
for (int j = i + 1; j < size; j++) {
if (array[j] < array[min1]) {
min1 = j;//here min1
}
}
int temp = array[min1];
array[min1] = array[i];
array[i] = temp;
}
for (int k = 0; k < size; k++) {
System.out.print(" " + array[k]);
}
}
let me explain it more clearly,in the OP's code there has some questions,two majors:
one:
for ( i = 0; i < size - 1; i++) {
int min1 = i;
for (int j = i + 1; j < size; j++) {
if (array[j] < array[min1]) {
min = j;
}
}
int temp = array[min1];
array[min1] = array[i];
array[i] = temp;
}
will never run,because the array size is 1 ,so the for loop phrase will be ignore without running(mean for(int i = 0; i < 0; i++){....}).
two:
for (int k = 0; k < size; i++) {
System.out.print(" " + array[i]);
}
beacause the array size is 1,so when array[1] will throw index out exception.so the outermost loop will just run once then throw a exception.
:D
I submitted a solution to Tape Equilibrium problem in Codility. [Codility training][1]
The problem is described as follows:
A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
The solution I submitted is:
class Solution {
public int solution(int[] A) {
long d = A[0] - A[A.length-1];
int l = 1;
int r = A.length -2;
while(l <= r) {
if (Math.abs(d + A[l]) < Math.abs(d - A[r])) {
d += A[l];
l++;
}
else {
d -= A[r];
r--;
}
}
return (int) Math.abs(d);
}
}
I achieved 85% accuracy but couldn't get to correct for some use case. Can someone help me to find what's wrong with this solution. Thanks
The following is my 100% solution:
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
// int idx = 0;
int sumPre = A[0];
int sumPost = 0;
for (int i = 1; i < A.length; i++) {
sumPost += A[i];
}
int difMin = Math.abs(sumPost - sumPre);
int tempSub = 0;
for (int i = 1; i < A.length - 1; i++) {
sumPre += A[i];
sumPost -= A[i];
tempSub = Math.abs(sumPost - sumPre);
if (tempSub < difMin) {
difMin = tempSub;
// idx = i+1;
}
}
return difMin;
}
}
I can not find their test input, but I find a weird thing is that when "for(int i = 1; i < A.length - 1; i++) " is changed to " for(int i = 1; i < A.length; i++)", then it will trigger two wrong runs...So it still must be a border value issue.
If any one find a test input can break the validity, please share with us, thanks.
Caution: {1,-1} indeed triggered the problem, since P < N, so at least one element should be left in the right part. -> {1,-1},{} is not a valid solution according to the problem definition.
Problem solved.
C# and Linq version for 100% as of May 2021:
public int solution(int[] A)
{
int left = A[0];
int right = A.Skip(1).Aggregate((c,x)=> c+=x);
int min = Math.Abs(left-right);
for(int i=1; i < A.Length-1; i++)
{
left+=A[i];
right-=A[i];
min = Math.Min(min,Math.Abs(left-right));
}
return min;
}
I also tried and got only 83%. My solution:
class Solution {
public int solution(int[] A) {
int[] leftSums = new int[A.length];
for (int i = 0; i < leftSums.length; i++) {
leftSums[i] = 0;
}
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
leftSums[i] = sum;
}
/*
for (int i = 0; i < leftSums.length; i++) {
if (i == 0) {
System.out.print("Left Sums Array is : [");
}
if (i == leftSums.length - 1) {
System.out.println(leftSums[i] + "]");
}
System.out.print(leftSums[i] + ", ");
}
*/
final int total = sum;
//System.out.println("Total is " + total);
int minDiff = Integer.MAX_VALUE;
int currDiff = 0;
for (int i = 0; i < leftSums.length; i++) {
currDiff = Math.abs(leftSums[i] - (total - leftSums[i]));
if (currDiff < minDiff) {
minDiff = currDiff;
}
}
return minDiff;
}
}
Below are those which failed for correctness.
double
two elements 1.280 s WRONG ANSWER
got 0 expected 2000
small
small elements 1.304 s WRONG ANSWER
got 0 expected 20
I tested myself for 2 elements and it worked for me.
I share my 100% score Java solution:
class Solution {
public int solution(int[] A) {
final int size = A.length;
long sumMin = (int)A[0];
long sumMax = 0;
for (int i = 1; i < size; i++) {
sumMax += (int)A[i];
}
int minDif = (int)Math.abs(sumMax - sumMin);
for (int i = 1; i < size; i++) {
int dif = (int)Math.abs(sumMax - sumMin);
if (dif < minDif) {
minDif = dif;
}
sumMin += A[i];
sumMax -= A[i];
}
return minDif;
}
}
The trick is that looping the array twice your complexity is 2N, which is O(N).
Addition results should be 'long' in order not to have problems with big extremes.
For %83 results, the problem is it says "splits this tape into two non-empty parts". So if you split for A[0], your first array will be empty. So you should start with A[1].
Ruby 100%
def solution(a)
left = a.inject(:+)
right = 0
a[0...-1].inject(Float::INFINITY) do |min, el|
left -=el
right += el
candidate = (right-left).abs
min < candidate ? min : candidate
end
end
You can actually do that, with one loop in C#.
Add Linq:
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
long sum = A.Sum(p => (long)p);
int val1 = Convert.ToInt32(A.GetValue(0));
int val2 = Convert.ToInt32(sum - val1);
int result = Math.Abs(val1 - val2);
for (int i = 1; i < A.Length-1; i++)
{
val1 += Convert.ToInt32(A.GetValue(i));
val2 -= Convert.ToInt32(A.GetValue(i));
if (result > Math.Abs(val1 - val2))
{
result = Math.Abs(val1 - val2);
}
}
return result;
}
Counterexample for user699681: A = {0, 1, 2, -5, 2},
and for Ism: A = {1, -1}.
TapeEquilibrium in C
int solution(int A[], int N) {
// write your code in C90
long int s_r=0,s_l=A[0],sum=A[0];
int i,min=11111111,r;
for(i=1;i<N;i++)
sum+=A[i];
for(i=1;i<N;i++)
{
s_r=sum-s_l;
r=(int)(s_l-s_r);
if(r<0) r=-r;
if(min>r)min=r;
if(min==0)break;
s_l=sum-s_r+A[i];
}
return min;
}
.. Or even a bit shorter to get 100%
public int solution(int[] A) {
int sumMin = A[0];
int sumMax = 0;
for (int i = 1; i < A.length; i++) {
sumMax += A[i];
}
int minDif = Math.abs(sumMin - sumMax);
for (int i = 1; i < A.length - 1; i++) {
sumMin += A[i];
sumMax -= A[i];
minDif = Math.min(minDif, Math.abs(sumMin - sumMax));
}
return minDif;
}
Here's my implementation using Java 8 IntStream to simplify the sum process...
100% Correct, 100% Performance.
import java.util.stream.IntStream;
public class TapeEquilibrium {
public static int diffIndex( int[] A ) {
long lower = 0, diff = 0, higher = IntStream.of( A ).asLongStream().sum(), minDiff = Integer.MAX_VALUE;
for(int i = 0; i < A.length-1; i++) {
lower += A[i];
higher -= A[i];
diff = Math.abs( higher - lower);
if( diff < minDiff ) {
minDiff = diff;
}
}
return (int) minDiff;
}
public static void main( String[] args ) {
int[] A = { 3, 1, 2, 4, 3 };
System.out.println( diffIndex( A ) );
}
}
here is my Solution with 100% correctness & performance
int solution(int A[], int N)
{
int sum,i;
sum=0;
for(i=0;i<N;i++)
{
sum+=A[i];
A[i]=sum;
}
int min_diff=abs(sum-A[0]*2);
for(i=0;i<N-1;i++)
{
int tmp;
tmp=abs(sum-A[i]*2);
if(tmp<min_diff)
min_diff=tmp;
}
return min_diff;
}
Try this one:
Class Solution {
public int solution(int[] A) {
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
int len = A.length;
int min = 1 ;
for(int i=0; i<len; i++){
sum2 += A[i];
}
for(int i=0; i< len-1 ; i++){
sum1 += A[i];
sum3 = sum2-sum1;
if( min > Math.abs(sum1- sum3)){
min = Math.abs( sum1 - sum3);
}
}
return min;
}
}
Here is 100% in scala.
def solution(A: Array[Int]): Int = {
//get the whole sum
val allSum = A.sum
//calculate left and right sum
var sumLeft = A(0)
var sumRight = allSum - sumLeft
//set initial diff for P=1
var minDiff = math.abs(sumLeft-sumRight) //difference
// loop for all P after the initial P position
for(p <- 1 to A.length-2){
//recalculate values
sumLeft += A(p)
sumRight -= A(p)
if(math.abs(sumLeft-sumRight) < minDiff){
// if difference is smaller then save new min diff
minDiff = math.abs(sumLeft-sumRight)
}
}
minDiff
}
Performance: https://codility.com/demo/results/trainingZNZCZN-AGC/
long sumofall = 0, leftsideSum = A[0], rightsidesum=0;
int x,LR = 0;
ArrayList listResult = new ArrayList();
for(x=0;x<A.Length;x++)
{
sumofall+= A[x];
}
for(x=1;x<A.Length;x++)
{
rightsidesum = sumofall-leftsideSum;
LR = (int)(rightsidesum - leftsideSum);
if(LR < 0)
{
LR=-LR;
}
listResult.Add(LR);
leftsideSum+=A[x];
}
listResult.Sort();
return Convert.ToInt32(listResult[0].ToString());
}
I share my 100% solution using Java 8.
public class TapeEquilibrium {
public int tapeEquilibrium(int[] A) {
final int N = A.length;
long minimalSum = (int) A[0];
int[] rightSide = Arrays.copyOfRange(A, 1, N);
long maximalSum = IntStream.of(rightSide).sum();
int minimalDifference = (int) Math.abs(maximalSum - minimalSum);
for (int i = 1; i < N; i++) {
int difference = (int) Math.abs(maximalSum - minimalSum);
minimalDifference = difference < minimalDifference ? difference : minimalDifference;
minimalSum += A[i];
maximalSum -= A[i];
}
return minimalDifference;
}
}
Here is my C# solution. Score 100%
if (A == null || A.Length == 0)
{
return 0;
}
int d1 = 0;
int d2 = A.Sum();
int p = 1;
int x = int.MaxValue;
// Replaced using A.sum();
//for (int i=0; i < A.Length; i++)
//{
// d2 += A[i];
//}
for (int j = 0; j < A.Length; j++)
{
if (j < p)
{
d1 += A[j];
}
int ad = Math.Abs(d1 - (d2 - d1));
x = Math.Min(x, ad);
if (p == A.Length -1) { break; }
p++;
}
return x;
Below is my solution which got 100% . As most of you guys did I first got the sum of the array then go through it while adding up left and right parts and then getting the absolutes of them and putting the results into a map then checking the map for the minimum value .
int totalLeft = 0;
int totalRight = 0;
int total = 0;
int result = 0;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < A.length; i++) {
total += A[i];
}
for (int i = 0; i < A.length - 1; i++) {
totalRight = total - (A[i] + totalLeft);
totalLeft += A[i];
result = Math.abs(totalLeft - totalRight);
map.put(i, result);
}
return Collections.min(map.values());
TapeEquilibrium in Swift 4
public func solution(_ A : inout [Int]) -> Int {
let P = 1
var splitIndex = P
var firstPartSum = A[splitIndex - 1]
var secondPartSum = Array(A[splitIndex..<A.count]).reduce(0, +)
var minimalDifference = abs(firstPartSum - secondPartSum)
if minimalDifference == 0 {
return minimalDifference
}
splitIndex += 1
while splitIndex < A.count {
firstPartSum += A[splitIndex - 1]
secondPartSum -= A[splitIndex - 1]
let dif = abs(firstPartSum - secondPartSum)
if dif == 0 {
return dif
}
if dif < minimalDifference {
minimalDifference = dif
}
splitIndex += 1
}
return minimalDifference
}
No one posted Javascript solution yet so here is mine with comments:
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Making it shorter.
let len = A.length;
// Definitely need to store, and initialise first value.
let left = new Array(len);
left[0] = A[0];
// Same as above, but initialise for last value.
let right = new Array(len);
right[len - 1] = A[len - 1];
let trackLowest = Number.MAX_SAFE_INTEGER;
// One shot calculate for both at any element (from 'outwards' 'in').
// Note there is 2 elements at least, and we already preset the first
// element, so we start and build from index 1.
for (let i = 1; i < len; ++i) {
left[i] = left[i - 1] + A[i];
right[len - 1 - i] = right[len - i] + A[len - 1 - i];
}
// Once the above is done, it's time to calculate the difference.
// If I am at index 0, then I want sum of index 0 AND left, and sum of index
// 1 and right (note not index 0 also).
// We stop before len - 1 because that's the rules and the sum of right will
// have been out of bounds if we want difference for last index, isn't it?
for (let i = 0; i < len - 1; ++i) {
let smallestDiff = Math.abs(left[i] - right[i + 1]);
if (smallestDiff < trackLowest) {
trackLowest = smallestDiff;
}
}
return trackLowest;
}
Basically sum up as you walk the loop simultaneously for the left and right side.
Once done, just get the difference, that's it. O(n) complexity.
My 100% JavaScript solution with O(N) time complexity (should be pretty self-explanatory):
function solution(A) {
let left = 0;
let right = A.reduce((sum, cur) => sum + cur, 0);
let min = Infinity;
for (let p = 0, len = A.length - 1; p < len; p++) {
left += A[p];
right -= A[p];
min = Math.min(min, Math.abs(left - right));
}
return min;
}
100% in Swift 4 for correctness & performance
Detected Time Complexity: 0(n)
var sumMin = A[0]
var sumMax = 0
for i in 1..<A.count {
sumMax += A[i]
}
var diff = abs(sumMin - sumMax)
for i in 1..<A.count-1 {
sumMin += A[i]
sumMax -= A[i]
diff = min(diff, abs(sumMin - sumMax));
}
return diff
Here mine in Java,
// got 91% because "int totalRight = (Arrays.stream(A).sum() - A[0]);" take too long to load
int totalLeft = A[0];
int totalRight = (Arrays.stream(A).sum() - A[0]);
//int afterMinus = 0;
int min = 0;
min = Math.abs(totalLeft - totalRight);
for(int i=1; i<(A.length-1); i++) {
//for(int j=A.length; j>0; j--) {
totalLeft += A[i];
totalRight -= A[i];
//System.out.println("totalLeft = "+ totalLeft);
//System.out.println("totalRight = "+ totalRight);
if(Math.abs(totalLeft - totalRight) < min) {
//System.out.println("min = "+ min);
min = Math.abs(totalLeft - totalRight);
}
}
return min;
// got 100% because change "int totalRight = (Arrays.stream(A).sum() - A[0]);" into for loop
//int totalSum = Arrays.stream(A).sum();
int totalLeft = A[0];
int totalRight = 0;
//int afterMinus = 0;
int min = 0;
for(int i=1; i<A.length; i++) {
totalRight += A[i];
}
min = Math.abs(totalLeft - totalRight);
for(int i=1; i<(A.length-1); i++) {
//for(int j=A.length; j>0; j--) {
totalLeft += A[i];
totalRight -= A[i];
//System.out.println("totalLeft = "+ totalLeft);
//System.out.println("totalRight = "+ totalRight);
if(Math.abs(totalLeft - totalRight) < min) {
//System.out.println("min = "+ min);
min = Math.abs(totalLeft - totalRight);
}
}
return min;
Well, from 91% to 100%, thanks to #sebadagostino after surfing for almost 2 hours for the logic and hints.