-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySum.java
More file actions
114 lines (92 loc) · 1.82 KB
/
ArraySum.java
File metadata and controls
114 lines (92 loc) · 1.82 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
package coding;
public class ArraySum {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int arr[]= {7,9,3,5};
// int arr2[]= {1,8,5,2,7};
// int res[]=sumOfArray(arr, arr2);
// for(int i=0;i<res.length;i++) {
// System.out.print(res[i]+" ");
// }
System.out.println(strPal("amama"));
}
public static int[] sumOfArray(int arr1[],int arr2[]) {
int res[]=new int[Math.max(arr1.length, arr2.length)+1];
int carry=0;
int i=arr1.length-1;
int j=arr2.length-1;
int counter=res.length-1;
while(i>=0 && j>=0) {
int temp =arr1[i]+arr2[j]+carry;
if(temp>9) {
res[counter]=temp-10;
carry=1;
}
else {
res[counter]=temp;
carry=0;
}
j--;i--;
counter--;
}
while(i>=0) {
int temp =arr1[i]+carry;
if(temp>9) {
res[counter]=temp-10;
carry=1;
}
else {
res[counter]=temp;
carry=0;
}
i--;
counter--;
}
while(j>=0) {
int temp =arr2[j]+carry;
if(temp>9) {
res[counter]=temp-10;
carry=1;
}
else {
res[counter]=temp;
carry=0;
}
counter--;
j--;
}
res[counter]=carry;
return res;
}
public static int strPal(String str) {
int count=0;
for (int i = 0; i < str.length(); i++) {
for (int j = i+1; j < str.length(); j++) {
String temp=str.substring(i,j);
if(IsPalindrome(temp)) {
count++;
System.out.println(temp);
}
}
}
return count;
}
/**
public static boolean isPallindrome(String str) {
int j=str.length()-1;
for (int i = 0; i < j; i++,j--) {
if(str.charAt(i)!= str.charAt(j)) {
return false;
}
}
return true;
}
*/
public static boolean IsPalindrome(String str) {
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
if (str.charAt(i) != str.charAt(j))
return false;
}
return true;
}
}