A non-empty array A consisting of N numbers is given. The array is sorted in non-decreasing order. The absolute distinct count of this array is the number of distinct absolute values among the elements of the array.
For example, consider array A such that:
A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6The absolute distinct count of this array is 5, because there are 5 distinct absolute values among the elements of this array, namely 0, 1, 3, 5 and 6.
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A consisting of N numbers, returns absolute distinct count of array A.
For example, given array A such that:
A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6the function should return 5, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647];
- array A is sorted in non-decreasing order.
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
// write your code in Java SE 8
if (A.length==0) return 0;
int i=0, j=A.length-1, count = 0;
int prev=(A[i]<-A[j])?A[i]:-A[j];
if (prev==Integer.MIN_VALUE) count++;
prev = Integer.MIN_VALUE;
while (i<j){
// System.out.println(i+", "+j+", "+prev);
if (A[i]==A[j]) return count+1;
if (A[j]<=0){
if (A[i]!=prev) count++;
prev = A[i];
i++;
}
else if (A[i]>=0){
if (A[j]!=-prev) count++;
prev = -A[j];
j--;
}
else {
if (A[i]<-A[j]){
if (A[i]!=prev) count++;
prev = A[i];
i++;
}
else {
if (-A[j]!=prev) count++;
prev = -A[j];
j--;
}
}
}
if (A[i]!=prev && -A[i]!=prev) count++;
return count;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
// write your code in Java SE 8
if (A.length==0) return 0;
int i=0, j=A.length-1, count = 0;
int prev=(A[i]<-A[j])?A[i]:-A[j];
if (prev==Integer.MIN_VALUE) count++;
prev = Integer.MIN_VALUE;
while (i<j){
// System.out.println(i+", "+j+", "+prev);
if (A[i]==A[j]) return count+1;
if (A[j]<=0){
if (A[i]!=prev) count++;
prev = A[i];
i++;
}
else if (A[i]>=0){
if (A[j]!=-prev) count++;
prev = -A[j];
j--;
}
else {
if (A[i]<-A[j]){
if (A[i]!=prev) count++;
prev = A[i];
i++;
}
else {
if (-A[j]!=prev) count++;
prev = -A[j];
j--;
}
}
}
if (A[i]!=prev && -A[i]!=prev) count++;
return count;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
// write your code in Java SE 8
if (A.length==0) return 0;
int i=0, j=A.length-1, count = 0;
int prev=(A[i]<-A[j])?A[i]:-A[j];
if (prev==Integer.MIN_VALUE) count++;
prev = Integer.MIN_VALUE;
while (i<j){
// System.out.println(i+", "+j+", "+prev);
if (A[i]==A[j]) return count+1;
if (A[j]<=0){
if (A[i]!=prev) count++;
prev = A[i];
i++;
}
else if (A[i]>=0){
if (A[j]!=-prev) count++;
prev = -A[j];
j--;
}
else {
if (A[i]<-A[j]){
if (A[i]!=prev) count++;
prev = A[i];
i++;
}
else {
if (-A[j]!=prev) count++;
prev = -A[j];
j--;
}
}
}
if (A[i]!=prev && -A[i]!=prev) count++;
return count;
}
}
The solution obtained perfect score.