-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cs
59 lines (48 loc) · 1.66 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Problem: https://www.hackerrank.com/challenges/runningtime/problem
C# Language Version: 7.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
This was same as insertion sort problem present at below url:
https://www.hackerrank.com/challenges/insertionsort2/problem
The only additional thing required in here was to maintain a count whenever a shifting took place. I achieved it using a
counter in the nested loop.
From algorithm stand-point the steps remain same as for insertion sort.
Time Complexity: O(n^2) //There is a nested loop.
Space Complexity: O(1) //number of dynamically allocated variables remain constant for any input.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static int runningTime(int[] A)
{
var shiftCount = 0;
var j = 0;
for (var i = 1; i < A.Length; i++)
{
var value = A[i];
j = i - 1;
while (j >= 0 && value < A[j])
{
A[j + 1] = A[j];
shiftCount++;
j = j - 1;
}
A[j + 1] = value;
}
return shiftCount;
}
static void Main(String[] args)
{
//no need of count of elements as we use array's length property.
Console.ReadLine();
var arr_temp = Console.ReadLine().Split(' ');
var arr = Array.ConvertAll(arr_temp, int.Parse);
var result = runningTime(arr);
Console.WriteLine(result);
}
}