-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC1Dot1.java
More file actions
112 lines (89 loc) · 1.83 KB
/
C1Dot1.java
File metadata and controls
112 lines (89 loc) · 1.83 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
package chap01;
public class C1Dot1 {
//
//Write a short Java function that takes an array of int values and determines
//if there is a pair of numbers in the array whose product is odd.
boolean isOdd(int num){
if(num%2!=0)
return true;
else
return false;
}
int findOddPair(int num[]){
int []resultArray;
int length=num.length;
resultArray=new int[length];
int[] retArray = new int[length];
if(length<=2){
if(length==1 || length==0){
return 0;
}else{
if(isOdd(num[0]*num[1])){
return 1;
}else{
return 0;
}
}
}
else{
for (int i=0;i<length;i++){
for(int j=1;j<(length-1);j++){
if(isOdd(num[i]*num[j])){
resultArray[i]=num[i];
resultArray[j]=num[j];
}
}
}
}
//remove 0's from resultArray
int count=0;
int k=0;
while(count<(length)){
if(resultArray[(count)]!=0){
retArray[k]=resultArray[count];
k++;
}
count++;
}
count=0;
k=0;
while(retArray[(k)]!=0 && (k<resultArray.length-1)){
k++;
}
resultArray=new int[k];
while(count<(k)){
resultArray[count]=retArray[count];
count++;
}
return resultArray.length;
}
boolean findOddP(int p[]){
int count=0;
for(int i=0;i<p.length;i++){
if(isOdd(p[i])){
count++;
}
}
if(count>=2)
return true;
else
return false;
}
public static void main(String args[]){
int[] input={1,3};
C1Dot1 c=new C1Dot1();
// for(int k=0;k<c.findOddPair(input).length;k++){
// System.out.println(c.findOddPair(input)[k]);
// }
// if(c.findOddPair(input)>0){
// System.out.println("There is at least one pair of odd products");
// }else{
// System.out.println("no odds");
// }
if(c.findOddP(input)){
System.out.println("There is at least one pair of odd products");
}else{
System.out.println("no odds");
}
}
}