-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cs
64 lines (59 loc) · 2.4 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
60
61
62
63
64
/*
Problem: https://www.hackerrank.com/challenges/dynamic-array/problem
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts (Key points in algorithm) :
- Straight-forward question. Just follow the instructions in the problem statement to reach to solution.
- No optimization possible as such.
Gotchas:
<None>
Time Complexity: O(q) //where q is number of queries.
Space Complexity: O(n) //Space wise there are two things which are taking up space:
1. seqList which is keeping pointer to n sequences. It contributes O(n) space.
2. Sequences will also take up space of order O(n) in worst case if all q queries are
of type 1.
//So, at very raw level the space complexity is O(2n) ~ O(n)
*/
using System;
using System.Collections.Generic;
class Solution
{
static void Main(string[] args)
{
List<int> seq;
var userInput = Console.ReadLine();
var userInputSplits = userInput.Split(' ');
var numberOfSequences = int.Parse(userInputSplits[0]);
var numberOfQueries = int.Parse(userInputSplits[1]);
var seqList = new List<List<int>>(new List<int>[numberOfSequences]);
var lastAns = 0;
for (var i = 0; i < numberOfQueries; i++)
{
userInput = Console.ReadLine();
userInputSplits = userInput.Split(' ');
var queryType = int.Parse(userInputSplits[0]);
var x = int.Parse(userInputSplits[1]);
var y = int.Parse(userInputSplits[2]);
var seqIndex = (x ^ lastAns) % numberOfSequences;
switch (queryType)
{
case 1:
if (seqList[seqIndex] != null)
seqList[seqIndex].Add(y);
else
{
seq = new List<int>();
seq.Add(y);
seqList[seqIndex] = seq;
}
break;
case 2:
seq = seqList[seqIndex];
lastAns = seq[y % seq.Count];
Console.WriteLine(lastAns);
break;
}
}
}
}