-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
240 lines (220 loc) · 7.59 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace array_data_algorthims
{
public class Program
{
static void Main(string[] args)
{
//int[] ar = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//int result = simpleArraySum(ar);
//int[] arr = { 203, 204 ,205 ,206 ,207 ,208 ,203 ,204 ,205 ,206 };
//int[] brr = { 203, 204 ,204 ,205 ,206 ,207 ,205 ,208 ,203 ,206 ,205 ,206 ,204 };
//int[] result = missingArrayFromInput(arr, brr);
//foreach (int a in result)
// Console.WriteLine("/n {0}", a);
//int[] ar = { 1, -2, 3, 4, 5, -6, -7, 8, 0, 0 };
//plusMinus(ar);
Console.ReadLine();
}
# region -- Simple Array
// Simple array Sum Function goes here.
public static int simpleArraySum(int[] ar)
{
/* SimpleArraySum function below.. */
int result = 0;
if (ar.Length > 0)
{
foreach (int eachElement in ar)
{
result += eachElement;
}
}
return result;
}
#endregion
# region -- Sum Of Diagonals
// Given a square matrix, calculate the absolute difference between the sums of its diagonals.
public static int diagonalDifference(List<List<int>> arr)
{
int diag1Sum = 0; //TL > BR
int diag2Sum = 0; //TR > BL
int col = 0;
for (int row = 0; row < arr.Count; row++)
{
diag1Sum += arr[row][col]; //[2][2] = -12
diag2Sum += arr[row][arr[row].Count - 1 - col]; //[2][3 - 1 - 2]= [0] == 10
col++;
}
return Math.Abs(diag1Sum - diag2Sum);
}
#endregion
# region -- MinMaxSum
// Min Max Sum Given Array
public static void minMaxSumGivenArray(int[] arr)
{
#region
// input = {1, 2, 3, 4, 5} - Calculate the sum of integers, display Min and Max of sum.
// int[] arr = { 1, 2, 3, 4, 5 }; // {7, 69, 2, 221, 7864}
List<long> result = new List<long>();
for (int i = 0; i < arr.Length; i++)
{
long sum = 0;
for (int j = 0; j < arr.Length; j++)
{
if (i != j)
{
sum += arr[j];
}
}
result.Add(sum);
}
Console.WriteLine("{0} {1}", result.Min(), result.Max());
#endregion
#region
// short and best answer for getting min and max sum.
Array.Sort(arr);
long min = 0, max = 0;
for (int i = 0, j = arr.Length - 1; i < arr.Length - 1; i++, j--)
{
max = max + arr[j];
min = min + arr[i];
}
Console.WriteLine("{0} {1}", min, max);
Console.ReadLine();
#endregion
}
#endregion
# region -- Tallest Duplicates
// Count how many candles are tallest in a given array.
public static int birthdayCakeCandles(List<int> candles)
{
int noOfCandles = 0, max = 0;
if (candles.Count > 0)
{
max = candles.Max();
for (int i = 0; i < candles.Count; i++)
{
if (max == candles[i])
noOfCandles++;
}
}
return noOfCandles;
}
#endregion
#region -- Plus Minus
// Plus Minus- Zero count and divide by the array.length.
static void plusMinus(int[] arr)
{
int positive = 0, negative = 0, zero = 0;
if (arr.Length > 0)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 0)
{
zero++;
}
else if (arr[i] < 0)
{
negative++;
}
else
{
positive++;
}
}
Console.WriteLine("{0}\n {1}\n {2}\n", (float)positive / arr.Length, (float)negative / arr.Length, (float)zero / arr.Length);
}
}
#endregion
public static int[] missingArrayFromInput(int[] arr, int[] brr)
{
// int[] c = arr.Concat(brr).Distinct().ToArray();
//int[] same = arr.Intersect(brr).ToArray(); ;
//int[] diff = arr.Union(brr).Except(same).ToArray();
//int[] c = new int[] { diff[0], same[0], same[1], diff[1] };
Array.Sort(arr);
Array.Sort(brr);
List<int> index = new List<int>();
for (int i = 0, j = 0; i < brr.Length; i++, j++)
{
int checkNo = 0;
if (j == arr.Length)
{
j--;
checkNo = brr[i];
}
else if (arr[j] != brr[i])
{
checkNo = brr[i];
}
bool isDuplicate = index.Exists(x => x == checkNo);
if (!isDuplicate)
if(checkNo != 0)
index.Add(checkNo);
}
return null;
}
#region - Staircase --upward --downward --triangle
// Complete the staircase function below.
static void staircase(int n)
{
//Staircase --upward
if (n > 0)
{
for (int i = 1; i <= n; i++)
Console.WriteLine(new String('#', i).PadLeft(n, ' '));
}
//Staircase --downward
if (n > 0)
{
for (int i = 1; i <= n; i++)
Console.WriteLine(new String('#', i).PadRight(n, ' '));
}
#region -- another way of making triangle.
int height = 6;
int count = height;
for (int row = 0; row < height; row++)
{
for (int column = 0; column < count; column++)
{
Console.Write("#");
}
Console.WriteLine();
count--;
}
#endregion
}
#endregion
#region - Smalles positive integer --min --max --binary search
// find the smallest missing positive integer in an sorted array.
static void findSmallestMissingPositiveNumber(int[] n)
{
int[] nums = new int[] {1, 2, 3, 4, 5, 6, 7 }; // output : 8
int left = 0, right = nums.Length - 1;
int answer = findSmallestMissing(nums, left, right);
Console.WriteLine(answer);
}
static static int findSmallestMissing(int[] nums, int left, int right)
{
// base condition
if (left > right) {
return left;
}
int mid = left + (right - left) / 2;
// if the mid-index matches with its value, then the mismatch
// lies on the right half
if (nums[mid] == mid) {
return findSmallestMissing(nums, mid + 1, right);
}
else {
// mismatch lies on the left half
return findSmallestMissing(nums, left, mid - 1);
}
}
#endregion
}
}