-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
3002 lines (2655 loc) · 81.5 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
using hackerrank;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using hackerrank.csharpCertification;
using System.Numerics;
public static class Extensions
{
public static void AddSorted<T>(this List<T> list, T value)
{
int x = list.BinarySearch(value);
list.Insert((x >= 0) ? x : ~x, value);
}
}
public class MaxAndValue
{
public int Value { get; set; }
public int Max { get; set; }
public MaxAndValue(int m, int v)
{
Max = m;
Value = v;
}
public MaxAndValue()
{
}
}
class Solution
{
static void Main(string[] args)
{
//largestRectangle(new List<int>() { 1, 2, 3, 4, 5 });
//int w = Convert.ToInt32(Console.ReadLine().Trim());
//int h = Convert.ToInt32(Console.ReadLine().Trim());
//int isVerticalCount = Convert.ToInt32(Console.ReadLine().Trim());
//List<bool> isVertical = new List<bool>();
//for (int i = 0; i < isVerticalCount; i++)
//{
// bool isVerticalItem = Convert.ToInt32(Console.ReadLine().Trim()) != 0;
// isVertical.Add(isVerticalItem);
//}
//int distanceCount = Convert.ToInt32(Console.ReadLine().Trim());
//List<int> distance = new List<int>();
//for (int i = 0; i < distanceCount; i++)
//{
// int distanceItem = Convert.ToInt32(Console.ReadLine().Trim());
// distance.Add(distanceItem);
//}
//List<long> result = getMaxArea(w, h, isVertical, distance);
//Console.WriteLine(String.Join(" ", result));
//foreach (var result in results)
//{
// Console.WriteLine(result);
//}
//Console.WriteLine(result);
//var r = solutionT1("area,land\n3722,CN\n6612,RU\n3855,CA\n3797,USA", "area");
//int T = Convert.ToInt32(Console.ReadLine().Trim());
//var results = new List<string>();
//for (int TItr = 0; TItr < T; TItr++)
//{
// string w = Console.ReadLine();
// string result = biggerIsGreater(w);
// results.Add(result);
// //Console.WriteLine(result);
//}
//Console.WriteLine("---------------Resultssssssssss----------------------");
//foreach (var result in results)
//{
// Console.WriteLine(result);
//}
//lilysHomework(new List<int>() { 12, 15, 13, 11 });
//minNotOccurPositiveNumber(new List<int>() { 1, 2, 3 }.ToArray());
//BoundedSlice(2, new List<int>() { 3, 5, 7, 6, 3 }.ToArray());
//BoundedSlice(0, new List<int>() { 1000000000 , 1000000000 }.ToArray());
//var a = new Something();
//Console.WriteLine(OddOccurrencesInArray(new List<int> { 9, 3, 9, 3, 9, 7, 9 }.ToArray()));
//Console.WriteLine(binary_gap(32));
//Console.WriteLine(cyclic_rotation(new int[] { }, 3));
//Console.WriteLine(frog_jmp(10, 85, 30));
//Console.WriteLine(perm_missing_elem(new int[] { 2, 3, 1, 5}));
//Console.WriteLine(tape_equilibrium(new int[] { 3, 1, 2, 4, 3 }));
//Console.WriteLine(perm_check(new int[] { 4, 1 }));
//Console.WriteLine(passing_cars(new int[] { 0, 0, 1, 1, 0 }));
//Console.WriteLine(frog_river_one(5, new int[] { 1, 3, 1, 4, 2, 3, 5, 4 }));
//Console.WriteLine(distinct_sort(new int[] { 2, 1, 1, 2, 3, 1 }));
//Console.WriteLine(max_product_of_three(new int[] { -3, 1, 2, -2, 5, 6 }));
//Console.WriteLine(triangle(new int[] { 10, 2, 5, 1, 8, 20 }));
//Console.WriteLine(brackets("{[()()]}"));
//Console.WriteLine(brackets("{([)()]"));
//Console.WriteLine(fish(new int[] { 4, 3, 2, 1, 5 }, new int[] { 0, 1, 0, 0, 0 }));
//Console.WriteLine(nesting("(()(())())"));
//Console.WriteLine(dominator(new int[] { 3, 4, 3, 2, 3, -1, 3, 3 }));
//Console.WriteLine(equi_leader(new int[] {-1,-1}));
//Console.WriteLine(max_profit(new int[] { 23171, 21011, 21123, 21366, 21013, 21367 }));
//Console.WriteLine(max_slice_sum(new int[] { 3, 2, -6, 4, 0 }));
//Console.WriteLine(count_factors(25));
//Console.WriteLine(min_perimeter_rectangle(30));
//Console.WriteLine(chocolates_by_numbers(12, 8));
//Console.WriteLine(abs_distinct(new int[] { -2147483648, 0 }));
//Console.WriteLine(count_distinct_slices(6, new int[] { 3, 4, 5, 5, 2}));
//Console.WriteLine(count_triangles(new int[] { 10, 2, 5, 1, 8, 12 }));
//Console.WriteLine(max_nonoverlapping_segments(new int[] { 1, 3, 7, 9, 9 }, new int[] { 5, 6, 8, 9, 10 }));
//Console.WriteLine(tie_ropes(4, new int[] { 1, 2, 3, 4, 1, 1, 3 }));
//Console.WriteLine(challenge_bug_wars(new int[] { 0, 1, 0, 1, 1, 1, 0 }, new int[] { 1, 2, 3, 4, 5, 6, 7 }));
//Console.WriteLine(Flipgame(new int[] { 1, 1 }, new int[] { 1, 2 }));
//Console.WriteLine(MinReorder(6, new int[][] { new int[] { 0, 1 }, new int[] { 1, 3 }, new int[] { 2, 3 }, new int[] { 4, 0 }, new int[] { 4, 5 } }));
//Console.WriteLine(MinReorder(4, new int[][] { new int[] { 0, 1 }, new int[] { 2, 0 }, new int[] { 3,2 } }));
Console.ReadLine();
}
static int MinReorder(int n, int[][] connections)
{
int reorder = 0;
var connectionsList = connections.ToList();
var connected = new List<int[]>();
var subConnections = connectionsList.Where(x => x[1] == 0).ToList();
connectionsList = connectionsList.Where(x => x[1] != 0).ToList();
connected.AddRange(subConnections);
int i = 0;
while (connectionsList.Count() > 0)
{
if (!connected.Any(x => x[0] == connectionsList[i][1]))
{
connected.Add(new int[] { connectionsList[i][1], connectionsList[i][0] });
connectionsList.RemoveAt(i);
reorder++;
}
else
{
connected.Add(new int[] { connectionsList[i][0], connectionsList[i][1] });
connectionsList.RemoveAt(i);
}
}
return reorder;
}
static int calcRecursiveConnections(int goal, int[][] connections)
{
int result = 0;
var subConnections = connections.Where(x => x[1] == goal);
//result += subConnections.Count();
foreach (var item in subConnections)
{
int t = item[0];
item[0] = item[1];
item[1] = item[0];
result++;
//result += calcRecursiveConnections(item, connections);
}
return result;
}
//static int Flipgame(int[] fronts, int[] backs)
//{
// int min = 2001;
// for (int i = 0; i < fronts.Length; i++)
// {
// int temp = Math.Min(fronts[i], backs[i]);
// if (fronts[i] < backs[i] && temp < min)
// {
// int t = fronts[i];
// fronts[i] = backs[i];
// backs[i] = t;
// break;
// }
// }
// HashSet<int> discard = new HashSet<int>();
// for (int i = 0; i < fronts.Length; i++)
// {
// if(fronts[i] == backs[i])
// {
// discard.Add(backs[i]);
// }
// }
// for (int i = 0; i < backs.Length; i++)
// {
// int temp = Math.Min(fronts[i], backs[i]);
// if (!fronts.Contains(backs[i]) && min > temp && )
// min = backs[i];
// }
// if (min == 2001) return 0;
// return min;
//}
//static int challenge_bug_wars(int[] A, int[] X)
//{
// int count = 1;
// int fuel = 0;
// for (int i = 0; i < X.Length - 1; i++)
// {
// if (A[i] != 0)
// {
// fuel += A[i];
// fuel -= Math.Abs(X[i + 1] - X[i]);
// fuel += A[i + 1];
// if (fuel > 0) count++;
// }
// }
// return count;
//}
static int tie_ropes(int K, int[] A)
{
long tempSum = 0;
int result = 0;
for (int i = 0; i < A.Length; i++)
{
tempSum += A[i];
if (tempSum >= K)
{
tempSum = 0;
result++;
}
}
return result;
}
static int max_nonoverlapping_segments(int[] A, int[] B)
{
int nonOverlap = 0;
int last = -1;
for (int i = 0; i < A.Length; i++)
{
if ((last < 0) || (A[i] > B[last]))
{
last = i;
++nonOverlap;
}
}
return nonOverlap;
}
static int count_triangles(int[] A)
{
int triangles = 0;
Array.Sort(A);
for (int i = 0; i < A.Length - 2; i++)
{
int k = i + 2;
for (int j = i + 1; j < A.Length - 1; j++)
{
while (k < A.Length && A[i] + A[j] > A[k] && A[j] + A[k] > A[i] && A[k] + A[i] > A[j])
k++;
triangles += k - j - 1;
}
}
return triangles;
}
static int count_distinct_slices(int M, int[] A)
{
// i dont understand the question.
return 0;
}
static int abs_distinct(int[] A)
{
HashSet<long> stack = new HashSet<long>();
for (int i = 0; i < A.Length; i++)
{
long abs = Math.Abs((long)A[i]);
if (!stack.Contains(abs))
stack.Add(abs);
}
return stack.Count;
}
static int chocolates_by_numbers(int N, int M)
{
return N / gcd_chocolates_by_numbers(N, M);
}
//greatest common divisor
static int gcd_chocolates_by_numbers(int a, int b)
{
if (a % b == 0) return b;
else return gcd_chocolates_by_numbers(b, a % b);
}
static int min_perimeter_rectangle(int N)
{
int minPerimeter = Int32.MaxValue;
for (int i = 1; i < Math.Sqrt(N); i++)
{
if (N % i == 0)
{
int tempPerimeter = 2 * (i + (N / i));
if (tempPerimeter < minPerimeter) minPerimeter = tempPerimeter;
}
}
var sqrt = Math.Sqrt(N);
if (sqrt % 1 == 0)
{
int sqrtInt = (int)sqrt;
int tempPerimeter = 2 * (sqrtInt + sqrtInt);
if (tempPerimeter < minPerimeter) minPerimeter = tempPerimeter;
}
return minPerimeter;
}
static int count_factors(int N)
{
var result = 0;
for (int i = 1; i < Math.Sqrt(N); i++)
{
if (N % i == 0)
result += 2;
}
// check exact square
if (Math.Sqrt(N) % 1 == 0)
result++;
return result;
}
static int max_slice_sum(int[] A)
{
int max = A[0];
int previousMax = A[0];
for (int i = 1; i < A.Length; i++)
{
previousMax = Math.Max(A[i], previousMax + A[i]);
max = Math.Max(previousMax, max);
}
return max;
}
static int max_profit(int[] A)
{
int maxProfit = 0;
int min = Int32.MaxValue;
for (int i = 0; i < A.Length; i++)
{
min = Math.Min(min, A[i]);
maxProfit = Math.Max(maxProfit, A[i] - min);
}
return maxProfit;
}
static int equi_leader(int[] A)
{
Dictionary<int, int> equiFirst = new Dictionary<int, int>();
for (int i = 0; i < A.Length; i++)
{
if (equiFirst.ContainsKey(A[i]))
equiFirst[A[i]]++;
else
equiFirst[A[i]] = 1;
}
int result = 0;
Dictionary<int, int> equiSecond = new Dictionary<int, int>();
for (int i = 0; i < A.Length - 1; i++)
{
if (equiSecond.ContainsKey(A[i]))
equiSecond[A[i]]++;
else
equiSecond[A[i]] = 1;
if (equiFirst.ContainsKey(A[i]))
equiFirst[A[i]]--;
//else
// equiAll[A[i]] = 0;
var firstLeader = equiFirst.FirstOrDefault(x => x.Value == equiFirst.Values.Max());
var secondLeader = equiSecond.FirstOrDefault(x => x.Value == equiSecond.Values.Max());
if (firstLeader.Key != 0 && secondLeader.Key != 0)
if (firstLeader.Value > equiFirst.Values.Sum() / 2 && secondLeader.Value > equiSecond.Values.Sum() / 2)
if (firstLeader.Key == secondLeader.Key)
result++;
}
return result;
}
static int dominator(int[] A)
{
Dictionary<int, int> count = new Dictionary<int, int>();
for (int i = 0; i < A.Length; i++)
{
if (count.ContainsKey(A[i]))
count[A[i]]++;
else
count[A[i]] = 1;
}
var result = count.Where(x => x.Value > A.Length / 2).FirstOrDefault();
return result.Key == 0 ? -1 : Array.IndexOf(A, result.Key);
}
static int nesting(string S)
{
Stack<string> strings = new Stack<string>();
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("(", ")");
for (int i = 0; i < S.Length; i++)
{
if (S[i].ToString() == "(")
strings.Push("(");
else
{
if (strings.Count > 0 && strings.Peek() == "(")
strings.Pop();
else
return 0;
}
}
if (strings.Count > 0) return 0;
else return 1;
}
static int fish(int[] A, int[] B)
{
Stack<int> alives = new Stack<int>();
for (int i = 0; i < A.Length; i++)
{
if (alives.Count == 0)
alives.Push(i);
else
{
while (alives.Count != 0 && B[i] - B[alives.Peek()] == -1 && A[alives.Peek()] < A[i])
alives.Pop();
if (alives.Count == 0)
{
alives.Push(i);
}
else
{
if (B[i] - B[alives.Peek()] != -1)
alives.Push(i);
}
}
}
return alives.Count;
}
static int brackets(String S)
{
Dictionary<string, string> pairs = new Dictionary<string, string>();
pairs.Add("{", "}");
pairs.Add("[", "]");
pairs.Add("(", ")");
List<string> set = new List<string>();
for (int i = 0; i < S.Length; i++)
{
if (S[i].ToString() == "{" || S[i].ToString() == "[" || S[i].ToString() == "(")
set.Add(S[i].ToString());
else
{
if (set.Count == 0) return 0;
string last = set.LastOrDefault();
if (S[i].ToString() != pairs[last])
return 0;
set.RemoveAt(set.Count - 1);
}
}
if (set.Count > 0) return 0;
else return 1;
}
static int triangle(int[] A)
{
/** https://stackoverflow.com/a/53144072/3110319
* Since the array is sorted A[i + 2] is always greater or equal to previous values
* So A[i + 2] + A[i] > A[i + 1] ALWAYS
* As well ass A[i + 2] + A[i + 1] > A[i] ALWAYS
* Therefore no need to check those. We only need to check if A[i] + A[i + 1] > A[i + 2]?
* Since in case of A[i] + A[i + 1] > MAXINT the code would strike an overflow (ie the result will be greater than allowed integer limit)
* We'll modify the formula to an equivalent A[i] > A[i + 2] - A[i + 1]
* And inspect it there
*/
if (A.Length < 3) return 0;
Array.Sort(A);
for (int i = 0; i < A.Length - 2; i++)
{
if (A[i] + A[i + 1] > A[i + 2])
return 1;
}
return 0;
}
static int max_product_of_three(int[] A)
{
int count = A.Length;
if (count == 3) return A.Aggregate((x, y) => x * y);
var l = A.ToList();
l.Sort();
int max1 = l[count - 3] * l[count - 2] * l[count - 1];
int max2 = l[0] * l[1] * l[count - 1];
if (max1 > max2) return max1;
else return max2;
}
static int distinct_sort(int[] A)
{
return A.Distinct().Count();
}
static int frog_river_one(int X, int[] A)
{
var temp = new HashSet<int>();
for (int i = 0; i < A.Length; i++)
{
if (!temp.Contains(A[i]))
{
temp.Add(A[i]);
if (temp.Count == X) { return i; }
}
}
return -1;
}
static int passing_cars(int[] A)
{
int max = 1000000000;
var eCount = 0;
var pairsCount = 0;
for (int i = 0; i < A.Length; i++)
{
if (A[i] == 0)
{
eCount++;
}
else
{
pairsCount += eCount;
if (pairsCount > max) return -1;
}
}
return pairsCount;
}
public static int perm_check(int[] A)
{
if (A.Length == 0) return 0;
if (A.Length == 1 && A[0] != 1) return 0;
else if (A.Length == 1 && A[0] == 1) return 1;
var l = A.ToList();
l.Sort();
if (l[0] != 1) return 0;
for (int i = 0; i < l.Count - 1; i++)
{
if (l[i + 1] - l[i] != 1) return 0;
}
return 1;
}
public static int tape_equilibrium(int[] A)
{
long first = A[0];
long second = A.Skip(1).Sum();
int min = (int)Math.Abs(first - second);
for (int i = 1; i < A.Length - 1; i++)
{
first += A[i];
second -= A[i];
var diff = (int)Math.Abs(first - second);
if (diff < min) min = diff;
}
return min;
}
public static int perm_missing_elem(int[] A)
{
long n = A.Length + 1;
long sum = (n * (n + 1)) / 2;
long sumA = A.Sum(v => (long)v);
int miss = (int)(sum - sumA);
return miss;
}
public static int frog_jmp(int X, int Y, int D)
{
if ((Y - X) % D == 0) return (Y - X) / D;
else return (Y - X) / D + 1;
}
public static int[] cyclic_rotation(int[] A, int K)
{
if (A.Count() == 0) return A;
List<int> aList = new List<int>(A);
for (int i = 0; i < K; i++)
{
int t = aList.Last();
aList.Insert(0, t);
aList.RemoveAt(aList.Count - 1);
}
return aList.ToArray();
}
public static int binary_gap(int N)
{
int result = 0;
result = Convert.ToString(N, 2)
.Trim('0')
.Split(new[] { '1' })
.Max(x => x.Length);
return result;
}
public static int OddOccurrencesInArray(int[] A)
{
var odd = A.GroupBy(x => x).First(x => x.Count() % 2 == 1).Key;
return odd;
}
static int GetTopSecret(Something something)
{
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var field = something.GetType().GetField("topSecretValue", bindingFlags);
var result = (int)field?.GetValue(something);
return result;
}
public sealed class Something
{
public Something()
{
topSecretValue = 42;
}
private int topSecretValue;
}
static int BoundedSlice(int K, int[] A)
{
int result = 0;
//var resultList = new List<(int, int)>();
for (int i = 0; i < A.Length; i++)
{
int last = i + K > A.Length - 1 ? A.Length - 1 : i + K;
for (int j = i; j <= last; j++)
{
if (Math.Abs(A[i] - A[j]) <= K)
result++;
}
}
return result;
}
static int minNotOccurPositiveNumber(int[] A)
{
var positive = A.Where(x => x > 0).ToArray();
Array.Sort(positive);
int result = 1;
for (int i = 0; i < positive.Length; i++)
{
if (result < positive[i]) return result;
result = positive[i] + 1;
}
return result;
}
static int lilysHomework(List<int> arr)
{
var sortedArr = new List<int>(arr);
sortedArr.Sort();
int count = 0;
var tempArr = new List<int>(arr);
for (int i = 0; i < arr.Count; i++)
{
if (sortedArr[i] == arr[i])
continue;
int index = sortedArr.IndexOf(arr[i]);
int temp = tempArr[index];
tempArr[index] = arr[i];
tempArr[i] = temp;
count++;
}
return count;
}
static void almostSorted(List<int> arr)
{
//almostSorted(new List<int> { 1, 5, 4, 3, 2, 6 });
//almostSorted(new List<int> { 2, 3, 5, 7, 41, 13, 17, 19, 23, 29, 31, 37, 11 });
//almostSorted(new List<int> { 3,1,2 });
int startIndex = -1;
int endIndex = -1;
for (int i = 0; i < arr.Count - 1; i++)
{
if (arr[i] > arr[i + 1])
{
startIndex = i;
break;
}
}
if (startIndex == -1)
{
Console.WriteLine("yes");
return;
}
for (int i = arr.Count - 1; i > 0; i--)
{
if (arr[i] < arr[i - 1])
{
endIndex = i;
break;
}
}
var copyArr = new List<int>(arr);
int temp = arr[startIndex];
arr[startIndex] = arr[endIndex];
arr[endIndex] = temp;
bool trySwap = false;
for (int i = 0; i < arr.Count - 1; i++)
{
if (arr[i] > arr[i + 1])
{
trySwap = true;
break;
}
}
if (trySwap)
{
if (startIndex == 0 && endIndex == arr.Count - 1)
{
Console.WriteLine("no");
return;
}
var reverse = copyArr.GetRange(startIndex, endIndex - startIndex + 1);
reverse.Reverse();
var firstPart = copyArr.GetRange(0, startIndex);
firstPart.AddRange(reverse);
var lastPart = copyArr.GetRange(endIndex + 1, arr.Count - endIndex - 1);
firstPart.AddRange(lastPart);
for (int i = 0; i < firstPart.Count - 1; i++)
{
if (firstPart[i] > firstPart[i + 1])
{
Console.WriteLine("no");
return;
}
}
Console.WriteLine("yes");
Console.WriteLine($"reverse {startIndex + 1} {endIndex + 1}");
}
else
{
Console.WriteLine("yes");
Console.WriteLine($"swap {startIndex + 1} {endIndex + 1}");
return;
}
}
static int queensAttack(int n, int k, int r_q, int c_q, List<List<int>> obstacles)
{
int result = 0;
int cross1 = Math.Min(r_q - 1, n - c_q);
cross1 += Math.Min(n - r_q, c_q - 1);
int cross2 = n - Math.Max(r_q, c_q);
cross2 += Math.Min(r_q, c_q) - 1;
//int cross1 = n - Math.Max(r_q, c_q);
//cross1 += Math.Min(r_q, c_q) - 1;
//int cross2 = Math.Min(n - r_q, r_q - 1);
//cross2 += Math.Min(n - c_q, c_q - 1);
int vertical = n - 1;
int horizontal = n - 1;
int obsTotal = 0;
foreach (var item in obstacles)
{
if (item[1] == c_q)
{
//vertical
if (item[0] > r_q)
{
obsTotal += n - item[0] + 1;
}
else
{
obsTotal += item[0];
}
}
else if (item[0] == r_q)
{
//horizontal
if (item[1] < c_q)
{
obsTotal += item[1];
}
else
{
obsTotal += n - item[1] + 1;
}
}
else if (Math.Abs(r_q - c_q) == Math.Abs(item[0] - item[1]) || Math.Abs(r_q - item[0]) == Math.Abs(c_q - item[1]))
{
//cross
obsTotal += Math.Min(Math.Min(item[0], item[1]) - 1, n - Math.Max(item[0], item[1])) + 1;
}
//else if (Math.Abs(r_q - c_q) == Math.Abs(item[0] - item[1]))
//{
// //cross1
// if (r_q > item[0])
// {
// obsTotal += Math.Min(item[0], item[1]);
// }
// else
// {
// obsTotal += n - Math.Max(item[0], item[1]) + 1;
// }
//}
//else if (Math.Abs(r_q - item[0]) == Math.Abs(c_q - item[1]))
//{
// //cross2
// if(r_q > item[0])
// {
// obsTotal += n - item[0] + 1;
// }
// else
// {
// obsTotal += n - item[1] + 1;
// }
//}
}
result = cross1 + cross2 + vertical + horizontal - obsTotal;
return result;
}
static List<int> absolutePermutation(int n, int k)
{
var dic = new Dictionary<int, int>();
for (int i = 1; i <= n; i++)
{
int diff = i - k;
if (diff > 0 && !dic.ContainsKey(diff))
dic.Add(diff, diff);
else
{
int sum = i + k;
if (sum > n)
{
dic.Clear();
dic.Add(-1, -1);
break;
}
else
dic.Add(sum, sum);
}
}
return dic.Select(x => x.Key).ToList();
}
static string biggerIsGreater(string w)
{
for (int i = w.Length - 1; i > 0; i--)
{
if (w[i].CompareTo(w[i - 1]) > 0)
{
string firstPart = w.Substring(0, i - 1);
string secondPart = w.Substring(i, w.Length - i);
var secondPartOrdered = secondPart.OrderBy(x => x).ToList();
var nextBigger = secondPartOrdered.Select((item, index) => new { item, index }).First(x => w[i - 1].CompareTo(x.item) < 0);
secondPartOrdered[nextBigger.index] = w[i - 1];
var result = firstPart + nextBigger.item + string.Join("", secondPartOrdered);
if (result == w)
return "no answer";
else
return result;
}
}
return "no answer";
}
static int nonDivisibleSubset(int k, List<int> s)
{
var dic = new Dictionary<int, int>();
foreach (var item in s)
{
int remainder = item % k;
if (dic.ContainsKey(remainder))
{
dic[remainder]++;
}
else
{
dic[remainder] = 1;
}
}
int count = 0;