Running Time of Algorithms

Sort by

recency

|

277 Discussions

|

  • + 0 comments
    def runningTime(arr):
        # Write your code here
        shifts = 0
        n = len(arr)
        
        for i in range(1, n):
            key = arr[i]
            j = i - 1
            while j >= 0 and key < arr[j]:
                arr[j+1] = arr[j]
                j -= 1
                shifts += 1
            arr[j+1] = key
        return shifts
    
  • + 0 comments

    Here is my python sollution

    def runningTime(a):
        dem =  0 
        for i in range(1,len(a)):
            for j in range(i-1,-1,-1):
                if a[i] < a[j] :
                    a[j],a[i] = a[i],a[j]
                    i -=1
                    dem += 1
        return dem
    
  • + 0 comments

    Did anyone try BASH solution it keep getting me Time limit exceeded?

  • + 0 comments

    Here is problem solution in python java c++ c and Javascript - https://programmingoneonone.com/hackerrank-running-time-of-algorithms-problem-solution.html

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/pAWOIEQemtc

    int runningTime(vector<int> arr) {
        int i,j;
        int value;
        int result = 0;
        for(i=1;i<arr.size();i++)
        {
            value=arr[i];
            j=i-1;
            while(j>=0 && value<arr[j])
            {
                arr[j+1]=arr[j];
                j=j-1;
                result ++;
            }
            arr[j+1]=value;
        }
        return result;
    }