forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
87 lines (75 loc) · 2.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Problem: https://www.hackerrank.com/challenges/between-two-sets
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
1. Get the highest number present in set A. Let's call it maxA.
2. Get the lowest number present in set B. Let's call it minB.
3. Let the count of common Xs between set A and B be c. Initially set it to 0.
4. Initialize a counter, co to 1.
5. Run a loop while maxA <= minB
5.1 Initialize a boolean, b to true.
5.2 check if even a single element in set A is found which is not a factor of maxA then set b to false.
5.3 if b is true then check if even a single element in set B is found for which maxA is not a factor then set b to false.
5.4 If b is true then increment c by 1.
5.5 increment co by 1.
5.6 Set maxA to maxA * co.
5.7 Continue iterating the loop until the termination condition is met.
6. print c on a new line.
Time Complexity: O(x(n+m)) //where x = (max(m) - min(n))/min(n)
//Little tricky as the while loop is not purely iterative incrementing by 1 each time.
Space Complexity: O(1) //number of dynamically allocated variables remain constant for any number of elements in set A or B.
*/
using System;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
//No need to capture number of elments in set A and B as I use foreach loop to iterate them.
Console.ReadLine();
var a_temp = Console.ReadLine().Split(' ');
var setA = Array.ConvertAll(a_temp, Int32.Parse);
var b_temp = Console.ReadLine().Split(' ');
var setB = Array.ConvertAll(b_temp, Int32.Parse);
int total = getTotalX(setA, setB);
Console.WriteLine(total);
}
static int getTotalX(int[] a, int[] b)
{
var totalXs = 0;
var maximumA = a.Max(); //Time-complexity O(n)
var minimumB = b.Min(); //Time-complexity O(m)
var counter = 1;
var multipleOfMaxA = maximumA;
while (multipleOfMaxA <= minimumB)
{
var factorOfAll = true;
foreach (var item in a) //Time complexity O(n)
{
if (multipleOfMaxA % item != 0)
{
factorOfAll = false;
break;
}
}
if (factorOfAll)
{
foreach (var item in b) //Time complexity O(m)
{
if (item % multipleOfMaxA != 0)
{
factorOfAll = false;
break;
}
}
}
if (factorOfAll)
totalXs++;
counter++;
multipleOfMaxA = maximumA * counter; //Here counter is the x factor which contributes to O(x(n+m)) complexity.
}
return totalXs;
}
}