-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwo Algorithm
More file actions
192 lines (152 loc) · 4.68 KB
/
AddTwo Algorithm
File metadata and controls
192 lines (152 loc) · 4.68 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
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class OrdArray {
private int[] a; // ref to array a
private int nElems; // number of data items
//-----------------------------------------------------------
public OrdArray(int max) { // constructor
a = new int[max]; // create array
nElems = 0;
}
//-----------------------------------------------------------
public int size() {
return nElems;
}
//-----------------------------------------------------------
public int find(int searchKey) {
int lowerBound = 0;
int upperBound = nElems-1;
int curIn;
while(true) {
curIn = (lowerBound + upperBound ) / 2;
if (a[curIn]==searchKey)
return curIn; // found it
else if (lowerBound > upperBound)
return nElems; // can't find it
else { // divide range
if (a[curIn] < searchKey)
lowerBound = curIn + 1; // it's in upper half
else
upperBound = curIn - 1; // it's in lower half
} // end else divide range
} // end while
} // end find()
//-----------------------------------------------------------
public void insert(int value) { // put element into array
int j;
for (j=0; j<nElems; j++) // find where it goes
if (a[j] > value) // (linear search)
break;
for (int k=nElems; k>j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
//-----------------------------------------------------------
public boolean delete(int value) {
int j = find(value);
if (j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move bigger ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//-----------------------------------------------------------
public void display() { // displays array contents
for (int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i=0; i<nElems; i++){
sb.append(a[i]+" ");
}
return sb.toString();
}
//-----------------------------------------------------------
public int[] merge(OrdArray arr) {
// YOUR CODES
OrdArray third = new OrdArray(arr.nElems+nElems);
for(int i=0; i<nElems;i++) {
third.insert(a[i]);
}
for(int i=0; i<arr.nElems;i++) {
third.insert(arr.a[i]);
}
return third.a;
}
public int[] addTwo(int target) {
// YOUR CODES
int begin= 0;
int end = nElems-1;
int[] indexes= new int[2];
int dummy[]= {-1,-1};
while(begin<end) {
int sum = a[begin]+a[end];
if(sum==target) {
indexes[0]=begin;
indexes[1]=end;
break;
} else if( sum<target) {
begin++;
}
else {
end--;
}
}
if(indexes[0]==0 && indexes[1]==0) {
return dummy;
}
else {
return indexes;
}
// For compilation, you need to change it.
}
public static void main(String[] args){
// YOUR CODES to test the merge() method.
// The following are the codes to test the addTwo() method
OrdArray arr = new OrdArray(50);
arr.insert(12);
arr.insert(10);
arr.insert(24);
arr.insert(0);
arr.insert(6);
arr.insert(5);
arr.insert(7);
arr.insert(2);
arr.display();
//Creating two ordered arrays
// int[] secondarr= {2,4,6,8,10,10};
OrdArray firstarr = new OrdArray(50);
firstarr.insert(1);
firstarr.insert(3);
firstarr.insert(5);
firstarr.insert(5);
firstarr.insert(7);
firstarr.insert(11);
firstarr.insert(9);
// firstarr.display();
OrdArray secondarr = new OrdArray(50);
secondarr.insert(2);
secondarr.insert(6);
secondarr.insert(8);
secondarr.insert(4);
secondarr.insert(10);
secondarr.insert(10);
System.out.println(Arrays.toString(secondarr.merge(firstarr)));
// secondarr.display();
// arr.merge(firstarr);
Scanner console = new Scanner(System.in);
System.out.print("Please enter a target number: ");
int target = console.nextInt();
System.out.println("The two elements that can add up to " + target + " are at indexes: " + Arrays.toString(arr.addTwo(target)));
console.close();
}
} // end class OrdArray