Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/com/interview/array/ArrayAddition.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

public class ArrayAddition {

public int[] add(int arr1[], int arr2[]){
public int[] add(int arr1[], int arr2[]) {
int l = Math.max(arr1.length, arr2.length);
int[] result = new int[l];
int c=0;
int i = arr1.length-1;
int j= arr2.length-1;
int r=0;
l--;
while(i >=0 && j >=0){
while (i >=0 && j >=0) {
r = arr1[i--] + arr2[j--] + c;
c = r/10;
result[l--] = r%10;
}
while(i>=0){
while (i>=0) {
r = arr1[i--] + c;
c = r/10;
result[l--] = r%10;
}
while(j>=0){
while (j>=0) {
r = arr2[j--] + c;
c = r/10;
result[l--] = r%10;
}
if(c != 0){
if (c != 0) {
int[] newResult = new int[result.length+1];
for(int t= newResult.length-1; t> 0; t--){
for (int t= newResult.length-1; t> 0; t--) {
newResult[t] = result[t-1];
}
newResult[0] = c;
Expand All @@ -36,13 +36,13 @@ public int[] add(int arr1[], int arr2[]){
return result;
}

public static void main(String args[]){
public static void main(String args[]) {

int arr1[] = {9,9,9,9,9,9,9};
int arr2[] = {1,6,8,2,6,7};
ArrayAddition aa = new ArrayAddition();
int result[] = aa.add(arr1, arr2);
for(int i=0; i < result.length; i++){
for (int i=0; i < result.length; i++) {
System.out.print(" " + result[i]);
}
}
Expand Down