We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
static int runningTime(int[] arr) {
// Complete this function
int i, key, j,count=0;
for (i = 1; i < arr.length; i++)
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
count++;
}
arr[j+1] = key;
}
return(count);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int arr_i = 0; arr_i < n; arr_i++){
arr[arr_i] = in.nextInt();
}
int result = runningTime(arr);
System.out.println(result);
in.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Running Time of Algorithms
You are viewing a single comment's thread. Return to all comments →
Easy java code
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
for (i = 1; i < arr.length; i++) { key = arr[i]; j = i-1;
} return(count);
}