quanlidavid
BAN USERimport java.util.Arrays;
/*Given an array of N integers with +ve & -ve numbers. Find the maxproduct of 3 numbers ? & Test Cases*/
public class Q4 {
int maxProduct(int[] numbers) {
Arrays.sort(numbers);
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
int left = numbers[0] * numbers[1] * numbers[numbers.length - 1];
int right = numbers[numbers.length - 1] * numbers[numbers.length - 2]
* numbers[numbers.length - 3];
int[] max = { left, right };
Arrays.sort(max);
System.out.println("Max = " + max[max.length - 1]);
return 0;
}
public static void main(String[] args) {
int[] numbers = { 2, 5, 6, 1, 3, 0, -6, -2 };
int[] numbers1 = { -8, 2, 5, 6, 1, 3, 0, -6, -2 };
int[] numbers2 = { 2, 5, 6, 1, 3, 6, 3 };
int[] numbers3 = { -2, -5, -6, -1, -3, -6, -3 };
int[] numbers4 = { 2, 5, 6, 1, 3, -6 };
int[] numbers5 = { 2, 6, 1, 3, 0, -6, -2 };
Q4 q4 = new Q4();
q4.maxProduct(numbers);
q4.maxProduct(numbers1);
q4.maxProduct(numbers2);
q4.maxProduct(numbers3);
q4.maxProduct(numbers4);
q4.maxProduct(numbers5);
}
}
// Test
// A. no number 0:
// 1. all +ve. --right is max.
// 2. all -ve. --right is max.
// B. have number 0:
// 3. 0 is on the left, and have >=2 -ve. --right or (2 left multiply 1 right)
// is max.
// 4. 0 is on the right, have <=2 +ve. --right(0) or (2 left multiply 1 right)
// is max.
public class Q7 {
- quanlidavid April 27, 2012static public char firstC(String s) {
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; i++) {
int flag = 0;
for (int j = 0; j < cs.length; j++) {
if (cs[i] == cs[j]) {
flag++;
}
if (flag > 2) {
break;
}
}
if (flag == 1) {
return cs[i];
}
}
return 0;
}
public static void main(String[] args) {
String s = "sadjkwedfks";
System.out.println(Q7.firstC(s));
}
}