-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
268 lines (224 loc) · 7.62 KB
/
Program.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
namespace ConsoleApplication2
{
class Program
{
//Divide 2 numbers
internal int DivideTest(int n, int d)
{
if (d == 0)
return -1;
return (n / d);
}
//O(n) time and O(1) space complexity
internal int GetMax(int[] arr)
{
if (arr.Length == 0)
return arr[0];
int maxElement = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (maxElement < arr[i])
maxElement = arr[i];
}
return maxElement;
}
//O(log n) time and O(1) space complexity
internal int BinarySearch(int[] arr, int target, bool iterRecur)
{
int low = 0, high = arr.Length - 1;
if (iterRecur)
res = BinarySearchRecur(arr, low, high, target);
else
res = BinarySearchIterative(arr, low, high, target);
return res;
}
//Recursive BinarySearch
private int BinarySearchUtil(int[] arr, int low, int high, int target)
{
//Base case
if (high < low)
return -1;
int mid = (low + high) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
return BinarySearchUtil(arr, mid + 1, high, target);
else
return BinarySearchUtil(arr, low, mid - 1, target);
}
//Iterative BinarySearch
private int BinarySearchIterative(int[] arr, int low, int high, int target)
{
//Base case with 1 element
if (arr.Length == 1)
return -1;
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
//O(n) time and O(1) space complexity
internal bool LinearSearch(int[] arr, int key)
{
if (arr.Length == 1 && arr[0] == key)
return true;
bool res = false;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == key)
{
//If element found update the res and break;
res = true;
break;
}
}
return res;
}
//O(n) time and O(1) space complexity
internal int GetConsecutiveOnes(int[] arr1)
{
//Null check
if (arr1.Length == 0)
return -1;
//Base case
if (arr1.Length == 1 && arr1[0] == 1)
return 1;
int maxCount = 0;
int count = 1;
int n = arr1.Length;
for (int i = 0; i < n - 1; i++)
{
if (arr1[i] == 1 && arr1[i + 1] == 1)
{
count = count + 1;
}
else if (arr1[i] == 0 && arr1[i + 1] == 1)
{
count = 1;
}
else
{
count = 0;
}
if (maxCount < count)
maxCount = count;
}
return maxCount;
}
//Check if the string is a palindrome in O(n) time complexity
internal bool PalindromeCheck(string str)
{
int startInd = 0;
int endInd = str.Length - 1;
bool res = Palindrome(str, startInd, endInd);
return res;
}
private bool Palindrome(string str, int startInd, int endInd)
{
//Null check
if (str.Length == 0)
return true;
int i = startInd;
int j = endInd;
while (i < j)
{
if (str[i] != str[j])
return false;
i++;
j--;
}
return true;
}
//Check if array contains 2 numbers that add upto a given sum
internal bool CheckTwoSum(int[] arr, int sum)
{
//Null check
if (arr.Length == 0)
return false;
if (arr.Length == 2 && (arr[0] + arr[1]) == sum)
return true;
Array.Sort(arr);
int left = 0, right = arr.Length - 1;
while (left < right)
{
if ((arr[left] + arr[right]) == sum)
return true;
if (arr[left] + arr[right] < sum)
left++;
else
right--;
}
return false;
}
//Check if 3 numbers add upto a given sum in an array
internal bool CheckThreeSum(int[] arr, int sum)
{
//Null check
if (arr.Length == 0)
return false;
Array.Sort(arr);
for (int i = 0; i < arr.Length - 2; i++)
{
int left = i + 1;
int right = arr.Length - 1;
while (left < right)
{
if ((arr[i] + arr[left] + arr[right]) == sum)
return true;
if ((arr[i] + arr[left] + arr[right]) < sum)
left++;
else
right--;
}
}
return false;
}
static void Main(string[] args)
{
Program obj = new Program();
//Divide 2 elements
int val = obj.DivideTest(4, 4);
Console.WriteLine("{0}", val);
//Get max element in an array
int[] arr = { 1, 2, 3 };
int element = obj.GetMax(arr);
Console.WriteLine("{0}", element);
//Check for a given element in a sorted using Binary search
int[] arr = new int[] { 1, 2, 3, 4, 5 };
int key1 = 1;
bool iterRecur = false;
int res = obj.BinarySearch(arr, key1, iterRecur);
Console.WriteLine("Target element found at {0}", res);
// Check for a given element in a sorted using Linear search
bool res = obj.LinearSearch(arr, 2);
Console.WriteLine("Target element found at {0}", res);
//Get max count of 1's in an array
int[] arr2 = { 1, 1, 0, 1, 1, 1 };
int maxCount = obj.GetConsecutiveOnes(arr2);
Console.WriteLine("{0}", maxCount);
//Check if the string is a palindrome
string str = "malayalam";
bool result = obj.PalindromeCheck(str);
Console.WriteLine("{0}", result);
//Check if array contains 2 numbers that add upto a given sum
int[] arr3 = { 6, 5, 4, 3, 2, 1, 9, 8, 7 };
int sum3 = 18;
bool result1 = obj.CheckTwoSum(arr3, sum3);
Console.WriteLine("{0}", result1);
Console.ReadKey();
//Check if array contains 3 numbers that add upto a given sum
int[] arr4 = { 3, 2, 1, 4, 8 };
int sum4 = 13;
bool result2 = obj.CheckThreeSum(arr4, sum4);
Console.WriteLine("{0}", result2);
}
}
}