-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOdes2.java
1750 lines (1413 loc) · 47.7 KB
/
COdes2.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
*****************************************************Alice Apple Tree************************************
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int apple = sc.nextInt();
int cnt = 0, sum = 0;
while (sum < apple) {
cnt++;
sum += (12 * cnt * cnt);
}
System.out.println((8 * (cnt)));
}
}
*************************************************************Binary Palindrome************************
import java.util.*;
public class Main {
public static boolean isBinaryPalindrome(int x) {
int reversed = 0;
int original = x;
while (x > 0) {
reversed <<= 1; // Left shift by 1 bit
reversed |= (x & 1); // Add the least significant bit of 'x' to'reversed'
x >>= 1; // Right shift by 1 bit
}
return reversed == original;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (isBinaryPalindrome(x)) {
System.out.println(x + " has a binary palindrome representation.");
} else {
System.out.println(x + " does not have a binary palindrome representation.");
}
}
}
********************************************************BOOths************************************
import java.util.Scanner;
public class Main {
public static int multiply(int n1, int n2) {
int m = n1;
int r = n2;
int A = n1;
int S = -n1;
int P = 0;
int count = Integer.SIZE;
//System.out.print(count);
while (count > 0) {
if ((r & 1) == 1) {
P += A;
S += m;
}
A <<= 1;
S <<= 1;
count--;
r >>= 1;
}
return P;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter two integer numbers -");
int n1 = scan.nextInt();
int n2 = scan.nextInt();
int result = multiply(n1, n2);
System.out.println("\n\nResult : " + n1 + " * " + n2 + " = " + result);
}
}
*********************************************************** Block Swap **************************************************************
import java.util.*;
class Main{
// Swapping r elements from starting of index a with r elements starting at index b
public static void swap(int arr[], int a, int b, int r){
for(int i = 0 ; i < r ; i++){
int temp = arr[a + i];
arr[a + i] = arr[b + i];
arr[b + i] = temp;
}
}
// Left rotating the array elements
public static void leftRotate(int arr[], int r){
int n = arr.length;
// If the no of elements to rotate = 0 or equal to size of array
if(r == 0 || r == n) return;
int i = r;
int j = n - r;
// Perform block swaps till the size of 2 subarrays is not equal
while (i != j){
// A's size is less
if(i < j){
swap(arr, r-i, r+j-i, i);
j = j - i;
}
// B's size is less
else{
swap(arr, r-i, r, j);
i = i - j;
}
}
// Finally at the end, block swap elements of A and B
swap(arr, r-i, r, i);
}
// Main function
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter size of the array");
int n = s.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements of the array");
for (int i = 0; i < n; i++) arr[i] = s.nextInt();
System.out.println("Enter the number of rotations");
int no_of_rotations = s.nextInt();
leftRotate(arr, no_of_rotations);
System.out.println("Array Elements after rotating : ");
for(int i = 0 ; i < n ; i++){
System.out.print(arr[i] + " ");
}
}
}
*********************************************************** Block Swap **************************************************************
import java.util.*;
class Main{
// Swapping r elements from starting of index a with r elements starting at index b
public static void swap(int arr[], int a, int b, int r){
for(int i = 0 ; i < r ; i++){
int temp = arr[a + i];
arr[a + i] = arr[b + i];
arr[b + i] = temp;
}
}
// Left rotating the array elements
public static void leftRotate(int arr[], int r){
int n = arr.length;
// If the no of elements to rotate = 0 or equal to size of array
if(r == 0 || r == n) return;
int i = r;
int j = n - r;
// Perform block swaps till the size of 2 subarrays is not equal
while (i != j){
// A's size is less
if(i < j){
swap(arr, r-i, r+j-i, i);
j = j - i;
}
// B's size is less
else{
swap(arr, r-i, r, j);
i = i - j;
}
}
// Finally at the end, block swap elements of A and B
swap(arr, r-i, r, i);
}
// Main function
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter size of the array");
int n = s.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements of the array");
for (int i = 0; i < n; i++) arr[i] = s.nextInt();
System.out.println("Enter the number of rotations");
int no_of_rotations = s.nextInt();
leftRotate(arr, no_of_rotations);
System.out.println("Array Elements after rotating : ");
for(int i = 0 ; i < n ; i++){
System.out.print(arr[i] + " ");
}
}
}
------------------------------bulbs-----------------------------------
import java.util.*;
public class Main {
public static ArrayList<Integer> toggleBulbs(int n) {
ArrayList<Integer> arr = new ArrayList<>();
int i = 1;
while ((i * i) <= n) {
arr.add(i * i);
i++;
}
return arr;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
ArrayList<Integer> result = toggleBulbs(n);
System.out.println("Bulbs to toggle: " + result);
}
}
****************************** Chinese Remainder Theorem ********************************
import java.util.Scanner;
public class Main {
static int calculate(int size, int div[], int rem[]) {
int j, x = 1;
while (true) {
for (j = 0; j < size; j++) {
if (x % div[j] != rem[j])
break;
}
if (j == size)
return x;
x++;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of divisors: ");
int size = sc.nextInt();
int[] div = new int[size];
System.out.println("Enter the divisors:");
for (int i = 0; i < size; i++) {
div[i] = sc.nextInt();
}
System.out.println("Enter the remainders:");
int[] rem = new int[size];
for (int i = 0; i < size; i++) {
rem[i] = sc.nextInt();
}
int result = calculate(size, div, rem);
System.out.println("The number is: " + result);
sc.close();
}
}
*******************************************************************COMBINATION ******************************************************
import java.util.*;
public class CombinationExample {
static int fact(int number) {
int f = 1;
int j = 1;
while (j <= number) {
f = f * j;
j++;
}
return f;
}
public static void main(String args[]) {
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(9);
numbers.add(12);
numbers.add(19);
numbers.add(61);
numbers.add(19);
int n = numbers.size();
int r = 2;
int result = fact(n) / (fact(r) * fact(n - r));
System.out.println("The combination value for the numbers list is: " + result);
}
}
********************************************************************************conversions*********************
--string to int
String str = "123";
int number = Integer.parseInt(str);
-----------------int to string
int x =77;
String str = String.valueOf(x)
-----------------------int to binary----------
int num = 42;
String binaryString = Integer.toString(num, 2);
-----------------------------------------------------binary to int
String binaryString = "101010";
int num = Integer.parseInt(binaryString, 2);
*******************************************************************distinct sorted permutations with duplicates allowed****************
import java.util.Arrays;
import java.util.Scanner;
public class Main {
// Calculating factorial of a number
private static int factorial(int n) {
int f = 1;
for (int i = 1; i <= n; i++)
f = f * i;
return f;
}
// Method to print the array
private static void print(char[] temp) {
for (int i = 0; i < temp.length; i++)
System.out.print(temp[i]);
System.out.println();
}
private static void nextPermutation(char[] temp, int n) {
int i;
for (i = n - 1; i > 0; i--) {
if (temp[i] > temp[i - 1]) {
break;
}
}
// Base case: no next permutation
if (i == 0) {
return;
}
int min = i;
int j, x = temp[i - 1];
for (j = i + 1; j < n; j++) {
if ((temp[j] < temp[min]) && (temp[j] > x)) {
min = j;
}
}
char temp_to_swap = temp[i - 1];
temp[i - 1] = temp[min];
temp[min] = temp_to_swap;
Arrays.sort(temp, i, n);
// Print the String
print(temp);
nextPermutation(temp, n);
}
private static void printAllPermutations(String s) {
char temp[] = s.toCharArray();
Arrays.sort(temp);
print(temp);
int total = factorial(temp.length);
for (int i = 1; i < total; i++) {
nextPermutation(temp, temp.length);
}
}
// Driver Code
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
String s = inp.next();
printAllPermutations(s);
}
}
************************ EULERS PHI **********************************************
package CAT1;
import java.util.*;
public class Euler_phi {
// Returns the value of Euler's totient function phi(n)
public static int phi(int n) {
int result = n; // Initialize result as n
// Check for all prime factors of n and subtract their multiples from result
for (int p = 2; p * p <= n; p++) {
if (n % p == 0) { // p is a prime factor of n
while (n % p == 0) { // Remove all multiples of p from n
n /= p;
}
result -= result / p;
}
}
// If n has a prime factor greater than sqrt(n), then add its contribution
if (n > 1) {
result -= result / n;
}
return result;
}
// Main method to test the program
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int phi_n = phi(n);
System.out.println("phi(" + n + ") = " + phi_n);
sc.close();
}
}
******************************************************** EUCLIDEAN GCD *****************************************************
import java.util.*;
public class Main {
public static int GCD(int number1, int number2) {
if (number2 == 0) {
return number1;
}
return GCD(number2, number1 % number2);
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
int number_1 = inp.nextInt();
int number_2 = inp.nextInt();
int gcd = GCD(number_1, number_2);
System.out.println("G.C.D of "+" "+number_1+" & "+number_2 +" is: "+gcd);
int lcm = (number_1 * number_2) / gcd;
System.out.println("L.C.M of "+" "+number_1+" & "+number_2 +" is: "+lcm);
}
}
****************************************************************** Hour Glass in Matrix **********************************************
// Java program to find maximum
// sum of hour glass in matrix
import java.io.*;
import java.util.Scanner;
public class Rough{
// Returns maximum sum of
// hour glass in ar[][]
static int findMaxSum(int [][]mat,int R,int C)
{
if (R < 3 || C < 3){
System.out.println("Not possible");
System.exit(0);
}
// Here loop runs (R-2)*(C-2)
// times considering different
// top left cells of hour glasses.
int max_sum = Integer.MIN_VALUE;
for (int i = 0; i < R - 2; i++)
{
for (int j = 0; j < C - 2; j++)
{
// Considering mat[i][j] as top
// left cell of hour glass.
int sum = (mat[i][j] + mat[i][j + 1] +
mat[i][j + 2]) + (mat[i + 1][j + 1]) +
(mat[i + 2][j] + mat[i + 2][j + 1] +
mat[i + 2][j + 2]);
// If previous sum is less than
// current sum then update
// new sum in max_sum
max_sum = Math.max(max_sum, sum);
}
}
return max_sum;
}
static public void main (String[] args)
{
Scanner inp = new Scanner(System.in);
int R = inp.nextInt();
int C = inp.nextInt();
int [][]mat = new int[R][C];
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
mat[i][j]=inp.nextInt();
}
}
int res = findMaxSum(mat,R,C);
System.out.println("Maximum sum of hour glass = "+ res);
}
}
***************************************************************HAMILTONIAN CYCLE **************************************************************
package FAT;
import java.util.Scanner;
public class HamiltonianCycle {
final int V = 5;
int path[];
boolean isSafe(int v, int graph[][], int path[], int pos) {
if (graph[path[pos - 1]][v] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;
return true;
}
boolean hamCycleUtil(int graph[][], int path[], int pos) {
if (pos == V) {
if (graph[path[pos - 1]][path[0]] == 1)
return true;
else
return false;
}
for (int v = 1; v < V; v++) {
if (isSafe(v, graph, path, pos)) {
path[pos] = v;
if (hamCycleUtil(graph, path, pos + 1))
return true;
path[pos] = -1;
}
}
return false;
}
void hamCycle(int graph[][]) {
path = new int[V];
for (int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if (!hamCycleUtil(graph, path, 1)) {
System.out.println("\nSolution does not exist");
return;
}
printSolution(path);
}
void printSolution(int path[]) {
System.out.println("Solution Exists: Following is one Hamiltonian Cycle");
for (int i = 0; i < V; i++)
System.out.print(" " + path[i] + " ");
System.out.println(" " + path[0]);
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
HamiltonianCycle hamiltonian = new HamiltonianCycle();
int[][] graph1 = new int[hamiltonian.V][hamiltonian.V];
int[][] graph2 = new int[hamiltonian.V][hamiltonian.V];
System.out.println("Enter the adjacency matrix for graph1:");
for (int i = 0; i < hamiltonian.V; i++) {
for (int j = 0; j < hamiltonian.V; j++) {
graph1[i][j] = scanner.nextInt();
}
}
System.out.println("Enter the adjacency matrix for graph2:");
for (int i = 0; i < hamiltonian.V; i++) {
for (int j = 0; j < hamiltonian.V; j++) {
graph2[i][j] = scanner.nextInt();
}
}
scanner.close();
hamiltonian.hamCycle(graph1);
hamiltonian.hamCycle(graph2);
}
}
*******************************************************************Josephus Problem**************************************************
import java.util.*;
public class Main {
static int Josephus(ArrayList<Integer> person, int k, int index) {
if (person.size() == 1) {
return person.get(0);
}
index = ((index + k) % person.size());
person.remove(index);
return Josephus(person, k, index);
}
static int solve(int N, int K) {
K = K - 1;
int index = 0;
ArrayList<Integer> person = new ArrayList<>();
for (int i = 1; i <= N; i++) {
person.add(i);
}
return Josephus(person, K, index);
}
public static void main(String args[]) {
Scanner inp = new Scanner(System.in);
int n = inp.nextInt();
int k = inp.nextInt();
int ans = solve(n,k);
System.out.println(ans);
}
}
----------------------------------- "Knight's Tour" using backtracking.----------------------------------------
import java.util.Scanner;
public class Main {
static int[] x = {1, 2, 2, 1, -1, -2, -2, -1};
static int[] y = {2, 1, -1, -2, -2, -1, 1, 2};
static boolean isSafe(int a, int b) {
return (a >= 0 && a < 8 && b >= 0 && b < 8);
}
static boolean traverse(int posX, int posY, int count, int[][] arr, boolean[][] visited) {
arr[posX][posY] = count;
visited[posX][posY] = true;
if (count == 64)
return true;
if (count > 64)
return false;
for (int i = 0; i < 8; i++) {
int x_axis = posX + x[i];
int y_axis = posY + y[i];
if (isSafe(x_axis, y_axis) && !visited[x_axis][y_axis] && traverse(x_axis, y_axis, count + 1, arr, visited)) {
return true;
}
}
arr[posX][posY] = 0;
visited[posX][posY] = false;
return false;
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("Test Case: ");
int t = inp.nextInt();
while (t-- > 0) {
int posX, posY;
System.out.print("Enter the position: ");
posX = inp.nextInt();
posY = inp.nextInt();
int count = 1;
int[][] arr = new int[8][8];
boolean[][] visited = new boolean[8][8];
if (traverse(posX, posY, count, arr, visited)) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
} else {
System.out.println("Not possible to cover");
}
}
}
}
*****************************************************************karatsuba Multiply******************************************
import java.util.Scanner;
public class Main {
public static long karatsubaMultiply(long x, long y) {
if (x < 10 || y < 10) {
return x * y;
}
int n = Math.max(Long.toString(x).length(), Long.toString(y).length());
int half = (n + 1) / 2;
long a = x / (long) Math.pow(10, half);
long b = x % (long) Math.pow(10, half);
long c = y / (long) Math.pow(10, half);
long d = y % (long) Math.pow(10, half);
long ac = karatsubaMultiply(a, c);
long bd = karatsubaMultiply(b, d);
long adbc = karatsubaMultiply(a + b, c + d) - ac - bd;
return (long) (ac * Math.pow(10, 2 * half) + adbc * Math.pow(10, half) + bd);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number (x): ");
long x = scanner.nextLong();
System.out.print("Enter the second number (y): ");
long y = scanner.nextLong();
long product = karatsubaMultiply(x, y);
System.out.println("Product: " + product);
scanner.close();
}
}
*************************************************** Leaders in the array*************************************
import java.util.Scanner;
public class Main {
// Method to find leaders in the array
static void findLeaders(int arr[], int size) {
// Logic Implementation
int rightMaximum = arr[size - 1];
// Here we have started loop from size-2
// as the rightmost element is always a leader
System.out.print(rightMaximum + " ");
for (int i = size - 2; i >= 0; i--) {
if (arr[i] > rightMaximum) {
rightMaximum = arr[i];
System.out.print(rightMaximum + " ");
}
}
}
// Driver code
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int m = scanner.nextInt();
int array[] = new int[m];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < m; i++) {
array[i] = scanner.nextInt();
}
System.out.println("Leaders in the array are:");
findLeaders(array, m);
scanner.close();
}
}
*******************************************************************Maneuvering a cave**************************************************
import java.util.*;
import java.math.*;
public class Main {
static int count =0,m,n;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
m= sc.nextInt();
n= sc.nextInt();
findWays(1,1);
System.out.println(count);
}
static void findWays(int i, int j) {
if(i>m || j>n)
return;
if(i==m && j==n){
count++;
}
findWays(i, j+1);
findWays(i+1,j);
}
}
*********************************************************************Maximum length of the sequence with 1s(Flip)*****************************
import java.util.Scanner;
public class Main{
public static int singleFlipMaxOnes(int number) {
if (~number == 0)
return 32;
int curr = 0, prev = 0, max_size = 0;
while (number != 0) {
if ((number & 1) == 1)
curr++;
else if ((number & 1) == 0) {
prev = (number & 2) == 0 ? 0 : curr;
curr = 0;
}
max_size = Math.max(prev + curr, max_size);
number >>= 1;
}
return max_size + 1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an unsigned integer number: ");
int number = scanner.nextInt();
int max_length = singleFlipMaxOnes(number);
System.out.println("Maximum length of the sequence with 1s: " + max_length);
scanner.close();
}
}
**************************************************************************Maximum product*****************************************
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
int max = a[0], m = a[0], min = a[0], temp = 0;
for (int i = 1; i < n; i++) {
if (a[i] > 0) {
max = Math.max(a[i], max * a[i]);
min = Math.min(a[i], min * a[i]);
} else if (a[i] == 0) {
max = min = 0;
} else {
temp = max;
max = Math.max(a[i], min * a[i]);
min = Math.min(a[i], temp * a[i]);
}
m = Math.max(m, max);
}
System.out.println("Maximum product = " + m);
scanner.close();
}
}
************************************************Max Equilibrium Sum***********************************************************
import java.util.Scanner;
public class Rough {
public static int getMaxEquilibriumSumOptimized(int[] arr) {
int totalSum = 0;
int leftSum = 0;
int maxSum = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
totalSum += arr[i];
}
for (int i = 0; i < arr.length; i++) {
totalSum -= arr[i];
if (leftSum == totalSum && leftSum > maxSum) {
maxSum = leftSum;
}
leftSum += arr[i];
}
return maxSum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
scanner.close();
int maxSum = getMaxEquilibriumSumOptimized(arr);
System.out.println("Max Equilibrium Sum : " + maxSum);
}
}
**************************************************************** Majority element ******************************************************
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
// Function to find Majority element in an array
static void findMajority(int arr[], int n) {
int maxCount = 0;
int index = -1; // sentinels
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j])
count++;
}
// update maxCount if count of current element is greater
if (count > maxCount) {
maxCount = count;
index = i;
}
}
// if maxCount is greater than n/2, return the corresponding element
if (maxCount > n / 2)
System.out.println("Majority Element: " + arr[index]);
else
System.out.println("No Majority Element");
}
// Driver code
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
// Function calling
findMajority(arr, n);
scanner.close();
}
}
*******************************************************************Maneuvering a cave**************************************************
import java.util.*;
import java.math.*;
public class Main {
static int count =0,m,n;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
m= sc.nextInt();
n= sc.nextInt();
findWays(1,1);
System.out.println(count);
}
static void findWays(int i, int j) {
if(i>m || j>n)
return;
if(i==m && j==n){
count++;
}
findWays(i, j+1);
findWays(i+1,j);
}
}
*************************************************************** moveHyphenToBeginning ***************************************************************
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner inp =new Scanner(System.in);
String originalString = inp.nextLine();
String transformedString = moveHyphenToBeginning(originalString);
System.out.println(transformedString);
}
public static String moveHyphenToBeginning(String string) {
if (string.contains("-")) {
int hyphenIndex = string.indexOf("-");
return "-" + string.substring(0, hyphenIndex) + string.substring(hyphenIndex + 1);
} else {