-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.java
More file actions
323 lines (237 loc) · 7.64 KB
/
Array.java
File metadata and controls
323 lines (237 loc) · 7.64 KB
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
/* input-output in arrray-----------------------
import java.util.*;
public class Array {
public static void main(String[] args) {
int marks[]= new int[100];
// System.out.println("lengh of array:"+ marks.length);
Scanner sc = new Scanner(System.in);
marks[0] = sc.nextInt();
marks[1] = sc.nextInt();
marks[2] = sc.nextInt();
System.out.println("phy :"+ marks[0]);
System.out.println("chem :"+ marks[1]);
System.out.println("math :"+ marks[2]);
float percentage = (marks[0]+marks[1]+marks[2])/3f;
System.out.println(percentage);
}
}
*/
/* Passing Array as Reference....................
public class Array {
public static void update(int marks[]) {
for (int i = 0; i < marks.length; i++) {
marks[i] = marks[i] + 1;
}
}
public static void main(String[] args) {
int marks[]= {97, 98, 99};
update(marks);
for (int i = 0; i < marks.length; i++) {
System.out.print(marks[i]+" ");
}
}
}
*/
/*
//Liner Search......................... time complexity O(n)
public class Array {
public static int Linear_search(int number[],int key){
for(int i=0; i< number.length;i++){
if(number[i] == key){
return i;
}
}
return -1;
}
public static void main(String[] args) {
int number[]= {2,4,6,8,10,12,14,16};
int key=20;
int index = Linear_search(number, key);
if(index == -1){
System.out.println("NOT found");
}else{
System.out.println("key at index:"+ index);
}
}
}
*/
/* Que: get largest.......................................
public class Array {
public static int getLargest(int num[]){
int largest = Integer.MIN_VALUE; //-INFINITY
int smallest = Integer.MAX_VALUE;
for(int i = 0; i<num.length; i++){
if(largest < num[i]){
largest = num[i];
}
if (smallest > num[i]){
smallest = num[i];
}
}
System.out.println(" smallest value is:"+ smallest);
return largest;
}
public static void main(String[] args) {
int num[]= {1,4,6,3,9};
System.out.println("the largest value is:"+ getLargest(num));
}
}
*/
/*
//Binary Search.............................[TC.= O(logn)]
public class Array {
public static int BinarySearch(int numbers[],int key){
int start = 0; int end = numbers.length-1;
while(start<=end){
int mid = (start+end)/2;
//comparision
if(numbers[mid] == key){
return mid;
}
if(numbers[mid]<key){
start= mid+1;
}
else{
end= mid-1;
}
}
return -1;
}
public static void main(String[] args) {
int numbers[]={2,4,6,8,10,12,14};
int key=2;
System.out.println("index for the no is:"+ BinarySearch(numbers, key));
}
}
*/
/*
//Q:Reverse of a Array................
public class Array {
public static void reverse(int number[]){
int first=0,last=number.length-1;
while(first<last){
int temp = number[last];
number[last] = number[first];
number[first] = temp;
first++;
last--;
}
}
public static void main(String[] args) {
int number[]= {2,4,6,8,10};
reverse(number);
for(int i= 0; i<number.length;i++){
System.out.print(number[i]+" ");
}
}
}
*/
/* //Pairs Print.....................................
public class Array {
public static void pairsprt(int number[]){
for(int i=0;i< number.length;i++){
int curr= number[i];
for(int j= i+1;j<number.length;j++){
System.out.print("("+ curr+","+number[j]+") ");
}
System.out.println();
}
}
public static void main(String[] args) {
int number[]={2,4,6,8,10};
pairsprt(number);
}
}
*/
//* sub array................
/* public class Array {
public static void Subarray(int number[]){
// int ts=0;
for(int i=0;i< number.length;i++){
for(int j=i;j< number.length;j++){
for(int k=i;k<=j;k++){
System.out.print(number[k]+" ");
}
// ts++;
System.out.println();
}
System.out.println();
}
//System.out.println("total subarray ="+ts);
}
public static void main(String[] args) {
int number[]={2,4,6,8,10};
Subarray(number);
}
}
*/
/*✅ 1022. Sum of Root To Leaf Binary Numbers (LeetCode)
This problem gives a binary tree where each node contains either 0 or 1.
Every root-to-leaf path represents a binary number.
We need to return the sum of all those binary numbers in decimal form.
🔎 Key Idea
For every path:
Start from root.
At each node:
current = current * 2 + node.val
(This is equivalent to left shift in binary)
When you reach a leaf node, add the current value to the total sum.
🧠 Why This Works
If path is:
1 → 0 → 1
Binary calculation:
((1 * 2 + 0) * 2 + 1)
= (2 * 2 + 1)
= 5
Which is 101₂ = 5.
💻 Java Solution (DFS) */
class Solution {
public int sumRootToLeaf(TreeNode root) {
return dfs(root, 0);
}
private int dfs(TreeNode node, int current) {
if (node == null) {
return 0;
}
// Form binary number
current = current * 2 + node.val;
// If leaf node, return value
if (node.left == null && node.right == null) {
return current;
}
// Recur for left and right
return dfs(node.left, current) + dfs(node.right, current);
}
}
/*
⏱ Time Complexity
O(N) → Visit each node once.
📦 Space Complexity
O(H) → Recursive stack (H = height of tree)
🚀 Optimization Insight
Instead of:
current = current * 2 + node.val;
You can also use bitwise:
current = (current << 1) | node.val;
This is slightly more optimal and shows strong binary understanding — good for interviews 🔥
*/
/*
//66 plus one
import java.util.Arrays;
public class Solution {
public static int[] plusOne(int[] digits) {
int n = digits.length;
for (int i = n - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
// If all digits were 9
int[] res = new int[n + 1];
res[0] = 1;
return res;
}
}
*/