-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfStepsToReduceANumberInBinaryRepresentationToOne.cs
More file actions
151 lines (135 loc) · 4.55 KB
/
NumberOfStepsToReduceANumberInBinaryRepresentationToOne.cs
File metadata and controls
151 lines (135 loc) · 4.55 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.IO;
namespace CodeForecs
{
//https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/
class NumberOfStepsToReduceANumberInBinaryRepresentationToOne
{
public static void Solution()
{
string str = File.ReadAllText(@"C:\Users\guptraju\source\repos\CodeForecs\InputFiles\NumberOfStepsToReduceANumberInBinaryRepresentationToOne.txt");
Console.WriteLine(str.Length);
Stopwatch watch = new Stopwatch();
watch.Start();
//Console.WriteLine(new NumberOfStepsToReduceANumberInBinaryRepresentationToOne().NumSteps(str));
watch.Stop();
//Console.WriteLine(watch.Elapsed.TotalSeconds);
watch.Reset();
watch.Start();
//Console.WriteLine(new NumberOfStepsToReduceANumberInBinaryRepresentationToOne().NumSteps1(str));
watch.Stop();
//Console.WriteLine(watch.Elapsed.TotalSeconds);
watch.Reset();
watch.Start();
Console.WriteLine(new NumberOfStepsToReduceANumberInBinaryRepresentationToOne().NumSteps2(str));
watch.Stop();
Console.WriteLine(watch.Elapsed.TotalSeconds);
}
public int NumSteps(string s)
{
StringBuilder sb = new StringBuilder(s);
return NumSteps(sb, sb.Length);
}
public int NumSteps(StringBuilder sb, int size)
{
//Console.WriteLine(sb.ToString().Substring(0, size));
//Console.WriteLine(Convert.ToInt32(sb.ToString().Substring(0, size), 2).ToString());
if (size == 1)
{
if (sb[size - 1] == '1')
return 0;
return 1;
}
int steps = 0;
while (size != 0 && sb[size - 1] == '0')
{
steps++;
size--;
}
//Console.WriteLine(steps);
size = AddOneToStringBuilder(sb, size) ? size + 1 : size;
return size == 1 ? steps + NumSteps(sb, size) : 1 + steps + NumSteps(sb, size);
}
public bool AddOneToStringBuilder(StringBuilder sb, int size)
{
if (size == 1)
{
return false;
}
while (size >= 1)
{
if (sb[size - 1] == '1')
{
sb[size - 1] = '0';
size--;
}
else
{
sb[size - 1] = '1';
return false;
}
}
if (size <= 0)
{
sb.Insert(0, '1');
}
return true;
}
//Alternate solution using BigInteger
public int NumSteps1(string s)
{
//transform into an int
if (s.Length == 0 || s == null)
return 0;
System.Numerics.BigInteger value = ConvertToBigInt(s);
//check the first bit, add one if it is odd, divide by 2 if it is even
int times = 0;
while (value != 1)
{
if (value % 2 == 1)
value++;
else if (value % 2 == 0)
value /= 2;
times++;
}
return times;
}
private System.Numerics.BigInteger ConvertToBigInt(string s)
{
System.Numerics.BigInteger bi = 0;
foreach (var c in s)
{
bi = bi * 2 + (c == '1' ? 1 : 0);
}
return bi;
}
//the fastest solution
public int NumSteps2(string s)
{
int steps = 0;
int carry = 0;
for (int i = s.Length - 1; i > 0; i--)
{
if (s[i] - '0' + carry == 0)
{
steps++;
carry = 0;
}
else if (s[i] - '0' + carry == 1)
{
steps += 2; // after adding 1 and / 2
carry = 1; // example: 1 + 1 -> 10 -> 10 >> 1 -> carry 1 over
}
else
{
steps++; // current digit is 1, adding carry over 1 is 10 -> only do >> 1 and carry 1 over to the left
carry = 1;
}
}
return steps + carry;
}
}
}