-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-of-two-arrays.java
More file actions
58 lines (44 loc) · 1.86 KB
/
diff-of-two-arrays.java
File metadata and controls
58 lines (44 loc) · 1.86 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
// Difference Of Two Arrays solution
import java.io.*;
import java.util.*;
public class Main{
// Function to print the difference of two arrays
public static void diffOfArrays(int arr1[], int size1, int arr2[], int size2){
int diff[] = new int[size2]; // size of larger array, arr2
int carry = 0;
for(int i=size2-1, j=size1-1, k=diff.length-1; i>=0; i--, j--, k--){
// handing if no number present in array1 to get subtracted
int n1 = j >=0 ? arr1[j] : 0;
if( arr2[i]+carry >= n1){
diff[k]= arr2[i] + carry - n1;
carry = 0;
}
else{ // when array2 value can't subtract array1
diff[k] = arr2[i] + carry + 10 - n1;
carry = -1;
}
}
// handling preceding zeroes that should not be printed
int index= 0;
while(index<=diff.length && diff[index]==0){
index++; // finding index no of first non-zero number
}
// printing the difference of arrays
while(index < diff.length){
System.out.println( diff[index++] );
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Array 1
int n1 = Integer.parseInt(br.readLine()); // size of array 1
int a1[] = new int[n1];
for(int i=0; i<n1; i++) a1[i] = Integer.parseInt(br.readLine());
// Array 2
int n2 = Integer.parseInt(br.readLine()); // size of array 2
int a2[] = new int[n2];
for(int i=0; i<n2; i++) a2[i] = Integer.parseInt(br.readLine());
// Calling function to subtract the arrays
diffOfArrays(a1, n1, a2, n2);
}
}