-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.cs
48 lines (40 loc) · 1.5 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
/*
Problem: https://www.hackerrank.com/challenges/beautiful-days-at-the-movies/problem
C# Language Version: 6.0
.Net Framework Version: 4.5.2
Thoughts :
1. Get i,j,k
2. Initialize a counter bDays to 0
3. Initialize a counter c to i. Start a for loop using counter c until c is less than j.
- Get the absolute difference of i and the number formed by reversing the digits of i. Let the absolute difference be d.
- Get the remainder r when d is divided by k.
- If k is zero then increment bDays by 1.
- Increment c by 1.
- Continue loop until loop termination condition is met.
4. Print bDays.
Time Complexity: O(n) //Absolutely speaking the loop runs i-j times. But O(i-j) is equivalent of O(i)
Space Complexity: O(1) //dynamically allocated variables will remain same irrespective of input.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
var userInput = Console.ReadLine().Split();
var i = int.Parse(userInput[0]);
var j = int.Parse(userInput[1]);
var k = int.Parse(userInput[2]);
var bDays = 0;
for (int c = i; c <= j; c++)
{
var reverseString = new string(c.ToString().Reverse().ToArray());
var reverseNumber = int.Parse(reverseString);
var d = Math.Abs(c - reverseNumber);
var r = d % k;
if (r == 0)
bDays++;
}
Console.WriteLine(bDays);
}
}