-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
11824 lines (6343 loc) · 148 KB
/
Main.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
import java.util.*;
import java.io.*;
/*/
* Date: 15/12/2021
*
* Data types: primitive and non primitive types - DONE
* Operators- Done
* Code for Operators - Done
*
*
* Date: 16/12/2021
*
* Print Arrays - DONE
* Loops - For, While, Do While- DONE
* If, Else, Switch Case
* Questions on Arrays - max and min, sum of all values, multiplication of all values - DONE
*
*
* */
public class Main
{
static int linearSearch(int[] arr, int key)
{
for(int i=0;i<arr.length;i++)
{
if(arr[i]==key)
return i;
}
return -1;
}
static void printArray(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
static int binarySearch(int[] arr, int key)
{
/*
[1,2,3,4,5]
middle element= 3
low = 0, high = n-1= 4
middle index = 2 = (0+4)/2
arr[mid] = 3
Right: No need for left and middle, search in mid+1 to high
Left: No need for right and middle, search in low to mid-1
*/
int low = 0;
int high = arr.length-1;
int mid;
// -2 Bn to +2 Bn
// low = 1.5 Bn, high = 1.5 Bn
// low = 15000000000000, high = 15000000000000
while(low<=high)
{
mid=(low+high)/2; // 2- Can Cause Overflow
mid = low + (high-low)/2; // - No Overflow
if(arr[mid]==key) // key = 3, return 2
return mid;
else if(arr[mid] < key) // key = 5, 3<5, Right
low = mid+1; // Focus: [4 5]
else // key = 1, 3>1, Left
high = mid-1; // Focus: [1 2]
}
return -1;
}
static int sum_of_digits(int num)
{
int sum = 0;
int rem =0;
while (num>0)
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
return sum;
}
static int factorial(int n)
{
int fact = 1;
for (int i =2; i<=n; i++) // Bottom Up Approach
fact = fact*i;
//for (int i =n; i>=2; i--) // Top Down approach
//fact = fact*i;
return fact;
}
static int sumofArray(int[] arr, int n)
{
int i=0;
int sum =0;
for (i=0; i<n; i++)
{
// sum = sum + arr[i]; // O(sum)- Creating a copy of sum every time
sum += arr[i]; // O(1) - No Copy Created
// 0 + a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9]
}
return sum;
}
public static void main(String[] args)
{
char a = 'a';
float c = 12.5f;
double d = 32.5;
long e = 1000L;
byte f = 20;
Boolean g = true;
short h = 5;
int p = 10;
int q = 7;
//System.out.println("Values are: " + a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g + " " + h);
//System.out.println(str);
//System.out.println(p+q + " " + (p-q) + " " + p*q + " " + p/q + " " + p%q);
//System.out.println(str + str2);
//System.out.println((p>q) + " " + (p<q) + " " + (p>=q) + " " + (p<=q) + " " + (p==q) + " " + (p!=q));
++p;
--q;
//System.out.println(p + " " + q);
p--;
q++;
//System.out.println(p + " " + q);
int arr[] = {1,2,3,4,5,6,7,8,9,10};
//a[i]: i = 0 to n-1
int n = arr.length;
int i = 1;
for (i = 0; i < n; i++)
{
//System.out.print(arr[i] + " ");
}
///ystem.out.println(" ");
//System.out.println("Sum is:" + sumofArray(arr, n));
//System.out.println(a[0] + a[1] + a[2] + a[3] + a[4] .....+ a[n-1]);
int mul = 1;
for (i=0; i<n; i++)
{
mul *= arr[i];
// mul = mul * arr[i];
}
//System.out.println("Mul is:" + mul);
//System.out.println(factorial(5));
//System.out.println(factorial(6));
//System.out.println("Sum of Digits: " + sum_of_digits(12345));
//System.out.println("Sum of Digits: " + sum_of_digits(8724282));
//System.out.println("Sum of Digits: " + sum_of_digits(1));
// Unsorted Array
int arr2[] = {5,2,-1,-6,4,3,8,9};
int key = -6;
//System.out.println(linearSearch(arr2, key));
// Sorted Array
int arr3[] = {1,2,3,4,5};
key = 7;
//System.out.println(binarySearch(arr3, key));
i = 1;
do
{
++i;
// System.out.print(i + " ");
} while (i<=10);
i = 1;
while(i<10)
{
//System.out.print(i + " ");
++i;
}
int [][]mat = {{1,2}, {3,4}, {5,6}}; // Initialization
int row = mat.length;
int col = mat[0].length;
/*
1 2 3: mat[0]
4 5 6: mat[1]
mat.length = no of rows = 2
mat[0].length or mat[1].length = no of cols = 3
*/
int j=1;
for (i=0; i<row; i++) // m = rows
{
for (j=0; j<col; j++) // n = columns
{
// System.out.print(mat[i][j] + " ");
}
// System.out.println();
}
int sumofMatrix = 0;
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
sumofMatrix += mat[i][j];
}
}
//System.out.println("Sum of Matrix: " + sumofMatrix);
String str = "devang sharma";
// Indexing: 0-d, 1-e, 2-v, 3-a, 4-n, 5-g, 6-"", 7-s
System.out.println(str);
// str.length() = 6
System.out.println(str.length());
// () - function, [] - array
// str.charAt(idx)
//System.out.println(str.charAt(0));
// String s = sc.next(); From One Line
// String s = sc.nextLine(); next Line
// str.contains("seq") - True or False
//System.out.println(str.contains("dev"));
// .concat() - Concatenation
//str.concat(" Java");
//System.out.println(str);
//System.out.println(str.concat(" Java"));
//System.out.println(str + " Java");
// str.equals("Java") - True or False
//System.out.println(str.equals("Java"));
//System.out.println(str == "Java");
//"JAVA", "java" : OP - True
//System.out.println(str.equalsIgnoreCase("DEvAnG ShARmA"));
//"devang" - "DEVANG"
System.out.println(str.toUpperCase());
// char: 'a'-'z', 0-9, 'A'-'Z', Special Character
//String str2 = "DevA%*^5678@@*(*@(*@ ShAr";
//System.out.println(str2.toUpperCase());
//System.out.println(str2.toLowerCase());
//str = "devang sharma";
System.out.println(str.indexOf("a"));
System.out.println(str.indexOf("dev"));
System.out.println(str.indexOf("van"));
System.out.println(str.indexOf("k"));
System.out.println(str.indexOf("deo"));
// 3 Methods for Search in a String
// indexOf(), contains() - O(1) Operations
// Travel the String and compare char by char - O(N)
String str2 = " devang sharma ";
System.out.println(str2.trim());
System.out.println(str2.replace("devang", "Newton"));
}
}
----------------------------------------------------------------
Date: 14/12/2021
Java: Compiled Language
Installed: JDK (Java Development Kit)
JDK = JVM + JRE
JVM = Java Virtual Machine - Compiler
JRE = Java Run Time Environment
"Write Once, Run Everywhere"
javac Main.java: Compile - Empty Line- Successful
Prints Error if any Error
java Main.java: RUN
Variables: Changes value (Roll No.)
Constants: Can't change value (Name)
int a = 10;
System.out.println("Value of a is: " + a);
// System - Class in Java: Console (Environment)
//- IDE, Online Editor
// out -> Output
// println -> Print on Next Line
// print -> Print on same line
a = 20;
System.out.println("New Value of a is: " + a);
// const int a = 25; - CPP, Py, Go, Perl
final int p = 10; // Java
System.out.println("Value of p is: " + p);
//p = 20;
//System.out.println("New Value of p is: " + p);
DATA TYPE:
Data Type is Identity of any variable or constant
Date: 15/12/2021
2 Types:
(1) Primitive Data Types:
Number, Decimal, Boolean, Char
- char: a-z, A-Z, 0-9: 2 Bytes
char: 'a'-'z', 'A'-'Z', '0'-'9', ',', ;, !: 2 Bytes
char: ASCII: 0-127, 128-255
a: 97
z: 97+25=
A:
Z:
All Programming Language -> Ascii Code -> 1 byte of data: char-> 1 Byte
Java -> UTF-8 (Unicode Values) -> 2 Bytes -> char: 2 Bytes
- char: a-z, A-Z, 0-9: 2 Bytes
'0'- char, 0- int
- byte: 1 Byte (-128 to 127)
- short: 2 Bytes (-2^15 to 2^15-1) = (-32768 to 32767)
- int: 4 Bytes (-2^31 to 2^31-1)
- long: 8 Bytes (-2^63 to 2^63-1)
- float: 4 Bytes (Decimals: 242.65)
- double: 8 Bytes (Decimals: 242.65)
- Boolean: true/false- 1 Bit (0/1) - 0: FALSE, 1: TRUE
int a = 15;
float 223.25 = 223.2500 (4 Digits Precision)
double 223.25 = 223.25000000 (8 Digits Precision)
(2) Non Primitive Data Types:
- String
- Array
- Interfaces
Computer: 0 or 1 (Binary/Bits)
0/1: 1 Bit of data
1 Byte = 8 bits (11111111)
1 KB = 1024 bytes
1 MB = 1024 KB
1 GB = 1024 MB
1 TB = 1024 GB (Terrabye)
1 PB = 1024 TB (PentaByte)
1 ZB = 1024 PB (ZettaByte)
OPERATORS:
(1) Mathematical Operators:
+: Addition
-: Subtraction
*: Multiplication
/: Division (Quotient)
%: Modulus (Remainder)
12 = 7*1 + 5
D = d*q + r
12: Dividend
7: Divisor
1: Quotient
5: Remainder
12 % 7 = 5
Operator Overloading:
One Operator behaves differently with different data types. For
Eg:
+ :
int: Add the Numbers
String: Concatenate the Strings
5 + 2 = 7 : Add
"dev" + "ang" = "devang" : Concatenate/Merge
(2) Relational Operators: true/false
<: Less Than
>: Greater Than
<=: Less Than or Equal To
>=: Greater Than or Equal To
==: Equal To
!=: Not Equal To
Note:
In Programming,
= : Assignment
==: Equality
!=: Inequality
int a = 10
int b = 20
10 < 20: true
10 > 20: false
if (a == b): false
if (a != b): true
(3) Unary Operators:
One Operand
int a = 10;
--a; // 9
Pre-increment/decrement: ++, --
Increases/Decreases the value by 1 before and then use it
a = 10;
++a; //11
--a; //10
Post-increment/decrement: a++, a--
Use it and then increment/decrement the value
a++; //11
a--; //10
(4) Logical Operators:
&&: AND
||: OR
! : NOT
AND:
1 && 1 = 1
1 && 0 = 0
0 && 1 = 0
0 && 0 = 0
OR:
1 || 1 = 1
1 || 0 = 1
0 || 1 = 1
0 || 0 = 0
NOT:
!0 = 1
!1 = 0
DSA: Data Structure and Algorithms
20 Litres of Water
5 Buckets, Capacity: 10 L
1st Way:
B1- 10L, B2-10L, Rest Empty
2nd Way:
B1-4L, B2-4L, B3-4L, B4-4L, B5-4L
Bucket: Storing Water
Ways: Ways to Store Water
Water: Data
Bucket: Data Structure
Ways: Algorithms
(1) Data Structures:
- Used to Store Data
Array, Linked List, Stacks, Queues, Graphs, Trees, RBT, BST
(2) Algorithms:
- Step by Step Process to Solve the question using Data Structures
Dynamic Programming, Binary Search, Backtracking, Greedy, Recursion
ARRAY:
10 integers
int a,b,c,d,e,f,g,h,i,j;
1000 integers
Collection of "similar type of data types" stored in a "contiguous location"
[1 2 3 4 5] - YES- Array of Integers
[1 2 p 3 4 q 5] - NO
['a' 'b' 'c' 'd'] - Array of Characters
["devang" "sharma" "newton" "school"] - Array of Strings
3 Gifts:
1 Bear: 1 Ft
2 Bear: 2 Ft
3 Bear: 5 Ft
Pack in 1 Package - Height of package ?
= Max(5,2,1) = 5
type name[size];
int a[5];
char a[5];
String a[10];
float a[10];
int a[] = {1,2,3,4,5};
System.out.println(a.length); //5
Size = 5
Indexing: 0 to size-1 : 0 to 4
0 Index: 1, a[0] = 1
1 Index: 2, a[1] = 2
2 Index: 3, a[2] = 3
3 Index: 4, a[3] = 4
4 Index: 5, a[4] = 5
a[5] = ?- Error- Out of Bound Index
Contiguous/Continuous Location:
int a[] = {1,2,3,4,5};
Index: 0 to 4
int: 4 Bytes
Address of a[0] = 1- Base Address of Array: 1000: ASSUME
Address of a[1] = 1000 + 1*4 = 1004
Address of a[2] = 1000 + 2*4 = 1008
Address of a[3] = 1000 + 3*4 = 1012
................................................................
Address of a[k] = Base Address + k*4
----- 1 ----------- -------- 2 -------- ---- 3 --------
1000 1001 1002 1003 1004 1005 1006 1007 1008
Date: 16/12/2021
(1) Array: DONE
(2) Loops:
Q: WAP to Print Numbers from 1 to 10 in Java
int a[] = {1,2,3,4,5...10}; - 40 Bytes in Memory
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
....
System.out.println("1");
System.out.println("2");
System.out.println("3");
....
LOOP:
Same Thing on Repetition
Movie: Inception, Edge of Tomorrow
(1) For Loop
for (initialisation ; condition ; increment/decrement)
{
//Code to be executed
}
for (int i=0; i<10; i++)
{
System.out.println(i);
}
System.out.println(i); //10
Dry Run:
0 1 2 3 4 5 6 7 8 9
for (int i=0; i<=10; i++)
{
System.out.print(i + " ");
}
System.out.println(i); //11
OP:
0 1 2 3 4 5 6 7 8 9 10
for (int i=1; ;++i)
{
System.out.println(i);
}
OP:
1 2 3 4 5 6 7 8 9 10.........................
int i = 1;
for (; i<10; i++)
{
System.out.println(i);
}
OP: 1-9
for (int i = 1; i<10; )
{
System.out.println(i);
}
OP:
1 1 1 1 1 1 1 1......infinite
int i =5;
for (; ;)
{
System.out.println(i);
}
OP:
5 5 5 5 5 5 5.....infinite
for (int i=0; i<=10; i++)
{
System.out.print(i + "");
}
OP: 0-10
System.out.println(" ");
for(int i=10;i>=0;i--)
{
System.out.print(i + " ");
}
OP: 10 -0
WHILE LOOP:
while (condition)
{
Code to be executed/ Body
increment/decrement
}
int i =1;
while (i<10)
{
System.out.println(i);
i++;
}
OP:
1 2 3 4 5 6 7 8 9
int i =1;
while ()
{
System.out.println(i);
i++;
}
OP:
1 2 3 4 5 6 7 8 9 10.....infinite
int i =1;
while ()
{
System.out.println(i);
}
OP:
1 1 1 1 1 1.......infinite
for, while - Entry Based Loop
DO - WHILE LOOP: Exit Based Loop
do
{
body
increment/decrement
} while (condition);
int i = 1;
do
{
i++;
System.out.println(i);
} while (i<10);
OP:
2 3 4 5 6 7 8 9 10
int i = 1;
do
{
i= 1000;
System.out.println(i);
} while (i<10); // 1000 < 10: False -> EXIT
OP:
1000
Q: Print Sum of All Values in an Array
a[] = {1,2,3,4,5}
OP: 15
Q: WAP to Multiply All Values in an Array
a[] = {1,2,3,4,5}
OP: 1*2* 3*4*5 = 60
Date : 20th December 2021
Mentor: DEVANG SHARMA
Batch: November Batch - 5
Agenda : Assignment and Coding Questions
"Please Type 'Hi' in the Chat Box if you have joined and Can See this Screen".
Functions:
A Piece of Code which is used to perform a SPECIFIC TASK
Why ?
- Better Modularity: Modules
- Easier Implementation
- Single Point of Failure
Syntax:
[return type] name (parameters)
{
body
return value;
}
()- Functions
[]- Arrays
Floor: Bottom
Ceil: TOP
Programming -- English
-5 -4.5 -4 -3 -2 -1 0 1 2 3 4 4.5 4.9 5
-4 < -3
4 > 3
Floor: below: Nearest Left on Number Line
floor(4.5) = 4
floor (4.9) = 4
floor(4.1) = 4
floor(4) = 4
floor(-4.5)= -5
floor (-4.9) = -5
floor (-4.1) = -5
Ceil -> Ceiling: top: Nearest Right on the Number Line
ceil (4.5) = 5
ceil (4.9) = 5
ceil (4.1) = 5
ceil (4) = 4
ceil(-4.5)= -4
ceil (-4.9) = -4
ceil (-4.1) = -4
int n = 9;
int p = n/2; -> 4.5
p = 4
4: integer part, 5: decimal part
floor(9/2) = floor(4.5) = 4
floor(n/2) = n/2, n>0
p == Math.floor(n/2)