1
+ package binod .arrays ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ public class Example {
6
+ public static void main (String [] args ) {
7
+ int [] marks = new int [50 ];
8
+ Scanner sc = new Scanner (System .in );
9
+ marks [0 ] = sc .nextInt ();
10
+ marks [1 ] = sc .nextInt ();
11
+ marks [2 ] = sc .nextInt ();
12
+
13
+ System .out .println ("math " + marks [0 ]);
14
+ System .out .println ("eng " + marks [1 ]);
15
+ System .out .println ("science " + marks [2 ]);
16
+
17
+ }
18
+ }
19
+
20
+ class Example1 {
21
+ public static int linearSearch (int [] num , int k ) {
22
+ for (int i = 0 ; i < num .length ; i ++) {
23
+ if (num [i ] == k ) {
24
+ return i ;
25
+ }
26
+ }
27
+ return -1 ;
28
+ }
29
+
30
+ public static int stringSearch (String [] menus , String item ) {
31
+ for (int i = 0 ; i < menus .length ; i ++) {
32
+ if (menus [i ].equals (item )) {
33
+ return i ;
34
+ }
35
+ }
36
+ return -1 ;
37
+ }
38
+
39
+ public static void main (String [] args ) {
40
+ String [] menu = {"dosa" , "chole bhature" , "somasa" , "chowmin" };
41
+ String item = "somasa" ;
42
+ int [] numbers = {2 , 4 , 6 , 10 , 1 , 5 };
43
+ int key = 10 ;
44
+
45
+ int index = linearSearch (numbers , key );
46
+ if (index == -1 ) {
47
+ System .out .println ("not found" );
48
+ } else {
49
+ System .out .println ("found at" + index );
50
+ }
51
+ int in = stringSearch (menu , item );
52
+ if (in == -1 ) {
53
+ System .out .println ("not found" );
54
+ } else {
55
+ System .out .println ("fount at " + in );
56
+ }
57
+ }
58
+ }
59
+
60
+ //Time complexity = O(n)
61
+ class Example2 {
62
+ public static void update (int [] marks , int nonChangable ) {
63
+ nonChangable = 20 ;
64
+ for (int i = 0 ; i < marks .length ; i ++) {
65
+ marks [i ] = marks [i ] + 1 ;
66
+ }
67
+ }
68
+
69
+ public static void main (String [] args ) {
70
+ int [] marks = {97 , 98 , 99 };
71
+ int nonChangable = 5 ;
72
+ System .out .println (nonChangable );
73
+ update (marks , nonChangable );
74
+ for (int i = 0 ; i < marks .length ; i ++) {
75
+ System .out .print (marks [i ] + " " );
76
+ }
77
+ System .out .println ();
78
+
79
+ }
80
+ }
81
+
82
+ class Example3 {
83
+
84
+ public static int getLargest (int [] num ) {
85
+ int l = Integer .MIN_VALUE ;
86
+ for (int i = 0 ; i < num .length - 1 ; i ++) {
87
+ if (l < num [i + 1 ]) {
88
+ l = num [i ];
89
+ }
90
+ }
91
+ return l ;
92
+ }
93
+
94
+ public static void main (String [] args ) {
95
+ int [] num = {1 , 2 , 6 , 3 , 5 };
96
+ int lar = getLargest (num );
97
+ System .out .println (lar );
98
+
99
+ }
100
+
101
+ }
102
+
103
+ class Example4 {
104
+
105
+ public static int getSmallest (int [] num ) {
106
+ int s = Integer .MAX_VALUE ;
107
+ for (int i = 0 ; i < num .length ; i ++) {
108
+ if (num [i ] < s ) {
109
+ s = num [i ];
110
+ }
111
+ }
112
+ return s ;
113
+
114
+ }
115
+
116
+ public static void main (String [] args ) {
117
+ int [] numbers = {3 , 4 , 7 , 9 , 2 };
118
+ int smallest = getSmallest (numbers );
119
+ System .out .println (smallest );
120
+ }
121
+ }
122
+
123
+ class BinarySearch {
124
+ public static int binarySearch (int [] num , int key ) {
125
+ int start = 0 , end = num .length - 1 ;
126
+ while (start <= end ) {
127
+ int mid = (start + end ) / 2 ;
128
+ //comparisons
129
+ if (num [mid ] == key ) {//found
130
+ return mid ;
131
+ }
132
+ if (num [mid ] < key ) {//right
133
+ start = mid + 1 ;
134
+ } else {//left
135
+ end = mid - 1 ;
136
+ }
137
+ }
138
+ return -1 ;
139
+
140
+ }
141
+
142
+ public static void main (String [] args ) {
143
+ int [] numbers = {2 , 4 , 6 , 8 , 10 , 12 , 14 };
144
+ int key = 10 ;
145
+ System .out .println ("index for key is: " + binarySearch (numbers , key ));
146
+ }
147
+ }
148
+
149
+ class BinarySearch1 {
150
+ public static int binarySearch (int [] num , int key ) {
151
+ int start = 0 ;
152
+ int end = num .length - 1 ;
153
+ while (start <= end ) {
154
+ int mid = (start + end ) / 2 ;
155
+
156
+ if (num [mid ] == key ) {
157
+ return mid ;
158
+ }
159
+ if (key > num [mid ]) {
160
+ //right
161
+ start = mid + 1 ;
162
+
163
+ } else {
164
+ //left
165
+ end = mid - 1 ;
166
+ }
167
+
168
+ }
169
+ return -1 ;
170
+
171
+ }
172
+
173
+ public static void main (String [] args ) {
174
+ int [] numbers = {3 , 2 , 6 , 4 , 9 , 5 };
175
+ int key = 9 ;
176
+ System .out .println ("Index of of $key is " + binarySearch (numbers , key ));
177
+
178
+ }
179
+ }
180
+
181
+ class ReverseArray {
182
+ public static void reverse (int [] num ) {
183
+
184
+ int start = 0 ;
185
+ int end = num .length - 1 ;
186
+
187
+ while (start < end ) {
188
+ //swap elements
189
+ int temp = num [start ];
190
+ num [start ] = num [end ];
191
+ num [end ] = temp ;
192
+
193
+ // move towards center
194
+ start ++;
195
+ end --;
196
+
197
+ }
198
+ }
199
+
200
+ public static void main (String [] args ) {
201
+ int [] numbers = {4 , 2 , 7 , 1 , 6 };
202
+ reverse (numbers );
203
+ for (int n : numbers ) {
204
+ System .out .print (n );
205
+ }
206
+
207
+ }
208
+ }
209
+
210
+ class Pairs {
211
+ public static void getPairs (int [] num ) {
212
+ int tp = 0 ;
213
+ for (int i = 0 ; i < num .length ; i ++) {
214
+ int current = num [i ];
215
+ for (int j = i + 1 ; j < num .length ; j ++) {
216
+ System .out .print ("(" + current + "," + num [j ] + ")" + " " );
217
+ tp ++;
218
+
219
+ }
220
+ System .out .println ();
221
+ }
222
+ /*
223
+ formula to find tp
224
+ sum of nth num : 1+2+3+4+ ...... n
225
+ sum=n(n+1)/2
226
+ */
227
+ System .out .println ("Total pairs: " + tp );
228
+ }
229
+
230
+ public static void main (String [] args ) {
231
+ int [] numbers = {4 , 3 , 6 , 7 , 9 , 2 };
232
+ getPairs (numbers );
233
+ }
234
+ }
235
+
236
+ class SubArray {
237
+ public static void printSubArray (int [] num ) {
238
+ int ts =0 ;
239
+ for (int i = 0 ; i < num .length ; i ++) {
240
+ for (int j = i ; j < num .length ; j ++) {
241
+ int sum =0 ;
242
+ for (int k = i ; k <= j ; k ++) {
243
+ System .out .print (num [k ] + " " );
244
+ sum =sum +num [k ];
245
+
246
+ }
247
+ ts ++;
248
+ System .out .print ("=" +sum );
249
+ System .out .println ();
250
+ }
251
+ System .out .println ();
252
+
253
+ }
254
+ System .out .println ("Total sub array " + ts );
255
+ }
256
+
257
+ public static void main (String [] args ) {
258
+ int [] numbers = {2 , 5 , 4 , 7 , 8 };
259
+ printSubArray (numbers );
260
+
261
+ }
262
+ }
0 commit comments