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.
//package solve_problems;/* * Cuddle time * Vu Nguyen Phuong Chi * Miss you */importjava.math.BigInteger;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collections;importjava.util.Comparator;importjava.util.HashMap;importjava.util.HashSet;importjava.util.Map;importjava.util.Scanner;importjava.util.Set;publicclassSolve_problems{staticintrunningTime(int[]arr){intshifts=0;for(inti=1;i<arr.length;i++){intkey=arr[i];intj=i-1;// Shift elements to the right until the correct position for the key is foundwhile(j>=0&&arr[j]>key){arr[j+1]=arr[j];j--;shifts++;}// Place the key in its correct positionarr[j+1]=key;}returnshifts;}publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intN=sc.nextInt();int[]arr=newint[N];for(inti=0;i<N;i++){arr[i]=sc.nextInt();}System.out.println(runningTime(arr));sc.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 →