-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment Solutions.java
1703 lines (1278 loc) · 30.5 KB
/
Assignment Solutions.java
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
Focal length of Spherical Mirror
static int focal_length(int R, char Mirror)
{
int f=R/2;
if((R%2==1) && Mirror==')'){f++;}
if(Mirror == ')'){f=-f;}
return f;
}
Cuboid Perimeter
static int Perimeter(int L, int B, int H){
return 4*(L+B+H);
}
Nobita and Profit
static int Profit(int C, int S){
return S-C;
}
Shinchan and Kazama
static int Time(int A, int B, int S){
if(B>A){
return (B-A)/S;
}
return (A-B)/S;
}
Simple Sum
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class AssignmentSolutions {
public static void main (String[] args) {
// Your code here
int a;
int b;
int c;
Scanner scn = new Scanner(System.in);
a = scn.nextInt();
b = scn.nextInt();
c = scn.nextInt();
System.out.println(a+b+c);
}
}
Fizzbuzz / Newton School
static void NewtonSchool(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){System.out.print("NewtonSchool ");}
else if(i%5==0){System.out.print("School ");}
else if(i%3==0){System.out.print("Newton ");}
else{System.out.print(i+" ");}
}
}
Max Integer
static int MaxInteger(int a ,int b, int c){
if(a>=b && a>=c){return a;}
if(b>=a && b>=c){return b;}
return c;}
Triangle
*****
****
***
**
*
Outer Loop:
Row: 1 ,2 ,3,4,5: for (i=0; i<5; i++)
i = 0,1,2,3,4
Inner Loop:
Column: 5,4,3,2,1 for (j=n-i; j>=1; j--)
j = 5-0 = 5 = 5-i
j = 5-1 = 4 = 5-i
j = 5-2 = 3 = 5-i
j = 5-3 = 2 = 5-i
j = 5-4 = 1 = 5-i
static void Triangle()
{
int n =5;
int i=n;
int j=0;
for (i=0; i<n; i++)
{
for (j=n-i; j>=1; j--)
{
System.out.print("*");
}
System.out.println("");
}
}
Pattern Printing
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
static void pattern(int n)
{
int i=n;
int j=0;
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
System.out.print(j + " ");
}
System.out.println("");
}
}
Basic Calculator
static int calculator(char ch, int a, int b)
{
if(ch == '+')
return a+b;
else if(ch == '-')
return a-b;
else if(ch == '*')
return a*b;
else return a/b;
}
Armstrong Number
static boolean isArmstrong(int N)
{
int num = N;
int sum = 0;
while(N > 0)
{
int digit = N%10;
sum += digit*digit*digit;
N = N/10;
}
if(num == sum)
return true;
else return false;
}
Single Digit
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
int digit_sum =0;
int num = s.nextInt();
int rem = 0;
while (num > 0 || digit_sum > 9)
{
if (num == 0)
{
num = digit_sum; // 15
digit_sum = 0;
}
rem = num % 10; //5 , 4, 3, 2,1 ---> num = 15, 5
digit_sum = digit_sum + rem; //5, 9, 12, 14, 15 --> digit_sum = 0+5 = 5+1 = 6
num = num / 10; // 1234, 123, 12, 1, 0 ---> num= 1
}
System.out.println(digit_sum);
}
}
Buildings
// TC: O(N)
// SC: O(1)
// Author: @devangs
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
class Main {
public static void main (String[] args) {
// Your code here
Scanner scn = new Scanner(System.in);
int N = scn.nextInt();
long arr[] = new long[N];
for(int i=0; i<N; i++){
arr[i] = scn.nextLong();
}
long currentTallest =0;
int count =0;
for(int i=0; i<N; i++){
if(arr[i] > currentTallest){
count++;
currentTallest = arr[i];
}
}
System.out.println(count);
}
}
Simple Arrangement
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int arr[] = new int[n];
int i=0;
for (i=0; i<n; i++)
arr[i] = s.nextInt();
for (i=0; i<n; i++)
{
System.out.print(arr[arr[i]] + " ");
}
}
}
Average me
// TC: O(N)
// SC: O(N)
// Author: @devangs
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int A[] = new int[n];
for(int i=0;i<n;i++){
A[i]=sc.nextInt();
}
long sum=0; // MUST Use long to Avoid Overflow
for(int i=0;i<n;i++){
sum = sum + A[i];
// sum += A[i]; - SHORTHAND
}
long average = sum/n;
System.out.print(average);
}
}
/*
int will always contain floor value of positive Decimal
int p = 4.8
p = 4
int q = -4.8
q = -4
floor (4.8) = 4
floor(-4.8) = -5
Constraint:-
1 <= n <= 100000: Size of Array
1 <= a[i] <= 100000: Values of Array
n values each of 100000
Max sum = 100000 * 100000 = 10^10 = 10 Billions
int: -2 Bn to + 2 Bn
Use int: OVERFLOW- Wrong Answer (WA) - Garbage Value
Use long: Pass: Upto 4 Trillions
int: 4 Bytes
long: 8 Bytes
long long int: 10 Bytes
1 <= n <= 5
1 <= a[i] <= 100
Max value of sum = 100 + 100 + 100 + 100 + 100
= 100 * 5 = 500 (MAX)
*/
Alternate Sum product
// TC: O(N)
// SC: O(1)
// AUthor: @devangs
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int sum=0, product=1;
int[] arr = new int[N];
for(int i=0; i<N; i++){
arr[i]=sc.nextInt();
}
for(int j=0;j<N;j++){
if((j+1)%2==0){ // EVEN
sum = sum+arr[j];
}
else{ // ODD
product = product*arr[j];
}
}
System.out.print(sum+" ");
System.out.print(product);
}
}
Replace element
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for (int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
for (int i=0;i<n;i++){
if(i==0){
System.out.print(arr[i]*arr[i+1]+" ");
}
else if(i==n-1){
System.out.print(arr[i]*arr[i-1]+" ");
}else{
System.out.print(arr[i-1]*arr[i+1]+" ");
}
}
// Your code here
}
}
Max numbers
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner input = new Scanner(System.in);
int tc = input.nextInt();
long first = 0, second = 0, third = 0;
long n, i;
long arr[] = new long[100000];
while(tc>0)
{
first = 0;
second = 0;
third = 0;
n = (int) input.nextLong();
// int arr[] = new int[a];
for(i = 0; i<n; i++){
arr[i] = (int) input.nextLong();
}
for(i = 0; i<n; i++){
if(arr[i] > first){
third = second;
second = first;
first = arr[i];
}
else if(arr[i] > second){
third = second;
second = arr[i];
}
else if(arr[i] > third){
third = arr[i];
}
}
System.out.println(first+" "+second+" "+third);
tc--;
}
}
}
Max Sum Column
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int a[][] = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}
int sum=0;
int ans=0;
for(int i=0;i<n;i++){
sum=0;
for(int j=0;j<m;j++){
sum+=a[j][i];
}
if(sum>ans){ans=sum;}
}
System.out.print(ans);
}
}
Diagonal Sum
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i,j;
int mat[][] = new int[n][n];
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
mat[i][j] = in.nextInt();
long principal = 0, secondary = 0;
for (i = 0; i < n; i++)
{
principal += mat[i][i];
secondary += mat[i][n - i - 1];
}
System.out.println(principal + " " + secondary);
}
}
Good cells
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m= sc.nextInt();
int a[][]= new int[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
int cnt=0;
for(int i=1;i<n-1;i++)
{
for(int j=1;j<m-1;j++)
{
if(a[i-1][j]==1 && a[i+1][j]==1 && a[i][j-1]==1 && a[i][j+1]==1)
{
cnt++;
}
}
}
System.out.print(cnt);
}
}
A Boolean Matrix Problem
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main
{
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
int n,m,i,j;
while(t>0)
{
n= s.nextInt();
m = s.nextInt();
int [][]a = new int[n][m];
boolean []b = new boolean[n];
for(i=0;i<n;i++)
{
b[i]=false;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
a[i][j] = s.nextInt();
if(a[i][j]==1)
{
b[i]=true;
}
}
}
for(i=0;i<n;i++)
{
if(b[i])
{
for(j=0;j<m;j++)
{
System.out.print("1 ");
}
}
else
{
for(j=0;j<m;j++)
{
System.out.print("0 ");
//cout<<0<<" ";
}
}
System.out.println();
//cout<<endl;
}
t--;
}
}
}
@channel
Q: Row with maximum 1's
CODE:
/*
TC: O(N*logN)
SC: O(N*M)
Author: @devangs
*/
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
class Main
{
static int first(int arr[], int low, int high)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || (arr[mid - 1] == 0)) && arr[mid] == 1)
return mid;
else if (arr[mid] == 0)
return first(arr, (mid + 1), high);
else
return first(arr, low, (mid - 1));
}
return -1;
}
static int rowWithMax1s(int mat[][], int m , int n)
{
int max_row_index = 0, max = -1;
int i, index;
for (i = 0; i < m; i++) {
index = first(mat[i], 0, n - 1);
if (index != -1 && n - index > max) {
max = n - index;
max_row_index = i;
}
}
return max_row_index;
}
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
int i,j;
int mat[][] = new int[m][n];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = in.nextInt();
}
}
System.out.println(rowWithMax1s(mat,m,n));
}
}
Fibonacci Numbers
static long Fibonacci(long n)
{
if (n == 0)
{ return 0;}
else if(n==1){
return 1;
}
return Fibonacci(n-1) + Fibonacci(n-2);
}
Sum of digits
static long Sum(long n)
{
if(n==0)
{
return 0;
}
return n%10+Sum(n/10);
}
Sum of Product of Digits of a given number
public static int sumOfProductOfDigits(int n1, int n2)
{
if(n1 < 10 && n2 < 10)
return n1*n2;
return (n1%10)*(n2%10) + sumOfProductOfDigits(n1/10, n2/10);
}
Factorial - Recursion
static int Factorial(int N)
{
if(N==0){
return 1;}
return N*Factorial(N-1);
}
Power function
import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main (String[] args)throws Exception {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String str[] = read.readLine().trim().split(" ");
double X = Double.parseDouble(str[0]);
int N = Integer.parseInt(str[1]);
System.out.println(String.format("%.2f", myPow(X, N)));
}
}
public static double myPow(double x, int n) {
if (n == Integer.MIN_VALUE)
n = - (Integer.MAX_VALUE - 1);
if (n == 0)
return 1.0;
else if (n < 0)
return 1 / myPow(x, -n);
else if (n % 2 == 1)
return x * myPow(x, n - 1);
else {
double sqrt = myPow(x, n / 2);
return sqrt * sqrt;
}
}
}
Searching an element in a sorted array
static int isPresent(long arr[], int n, long k)
{
int left = 0;
int right = n-1;
int res = -1;
while(left<=right){
int mid = (left+right)/2;
if(arr[mid] == k){
res = 1;
break;
}else if(arr[mid] < k){
left = mid + 1;
}else{
right = mid - 1;
}
}
return res;
}
Candy Crush
int cost(int n){
if(n == 0) return 0;
int g = (n-1)/3 + 1;
return g*g + cost(n-g);
}
Q: Minimum Element in Sorted and Rotated Array
import java.util.*;
import java.io.*;
import java.lang.*;
class Main{
public static void main (String[] args) {
//code
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int j=0;j<t;j++){
int al = s.nextInt();
int a[] = new int[al];
for(int i=0;i<al;i++){
a[i] = s.nextInt();
}
binSearchSmallest(a);
}
}
public static void binSearchSmallest(int a[]) {
int s=0;
int e = a.length - 1;
int mid = 0;
while(s<=e){
mid = (s+e)/2;
if(a[s]<a[e]){
System.out.println(a[s]);
return;
}
if(a[mid]>=a[s]){
s=mid+1;
}
else{
e=mid;
}
if(s == e){
System.out.println(a[s]);
return;
}
}
}
}
Q: Square Root of a Number Using Binary Search
int squareRoot(int n)
{
int start = 1;
int end = n;
while(start<=end)
{
int mid = (start+end)/2;
if(mid*mid == n)
return mid; // perfect square
if(mid*mid < n)
start = mid+1;
if(mid*mid > n) // 3*3>8
end = mid-1; //3-1 = 2
}
return end; // res = i-1 after for loop
}
Javascript
Insertion Sort in Java
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
Easy sorting
function easySorting(arr)
{
for(let i = 1; i < 5; i++)
{
let str = arr[i];
let j = i-1;
while(j >= 0 && (arr[j].toString().localeCompare(str)) > 0 )
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = str;
}
return arr;
}
Lucky Sevens
function lucky_sevens(arr) {