-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDecrementBinaryCounter.java
More file actions
30 lines (27 loc) · 1015 Bytes
/
Copy pathDecrementBinaryCounter.java
File metadata and controls
30 lines (27 loc) · 1015 Bytes
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
package amortizedAnalysis.binarycounter;
import java.util.Arrays;
public class DecrementBinaryCounter {
public static void main(String[] args){
int[] binary = {1, 0, 0, 1}; // initialize the binary array to 1001
System.out.println(Arrays.toString(binary)); // prints [1, 0, 0, 1]
decrement(binary); // decrements the binary array
System.out.println(Arrays.toString(binary)); // prints [1, 0, 0, 0]
decrement(binary);
System.out.println(Arrays.toString(binary)); // prints [0, 1, 1, 1]
decrement(binary);
System.out.println(Arrays.toString(binary)); // prints [0, 1, 1, 0]
decrement(binary);
System.out.println(Arrays.toString(binary)); // prints [0, 1, 0, 1]
}
public static void decrement(int[] a){
int k = a.length;
for(int i = k-1; i >= 0 ; i--){
if(a[i] == 0){
a[i] = 1;
}else{
a[i] = 0;
return;
}
}
}
}