-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Count absolute distincts in sorted array
- Loading branch information
1 parent
e5048dc
commit 57f5311
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>coding.challenge</groupId> | ||
<artifactId>CountDistinctAbsoluteSortedArray</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>13</maven.compiler.source> | ||
<maven.compiler.target>13</maven.compiler.target> | ||
</properties> | ||
<name>CountDistinctAbsoluteSortedArray</name> | ||
</project> |
58 changes: 58 additions & 0 deletions
58
Chapter10/CountDistinctAbsoluteSortedArray/src/main/java/coding/challenge/Arrays.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package coding.challenge; | ||
|
||
public final class Arrays { | ||
|
||
private Arrays() { | ||
throw new AssertionError("Cannot be instantiated"); | ||
} | ||
|
||
public static int findAbsoluteDistinct(int[] m) { | ||
|
||
if (m == null) { | ||
return -1; | ||
} | ||
|
||
int count = m.length; | ||
|
||
int left = 0; | ||
int right = m.length - 1; | ||
|
||
while (left < right) { | ||
|
||
// remove duplicates elements from the left and right | ||
while (left < right && m[left] == m[left + 1]) { | ||
count--; | ||
left++; | ||
} | ||
|
||
while (right > left && m[right] == m[right - 1]) { | ||
count--; | ||
right--; | ||
} | ||
|
||
// only one element is left | ||
if (left == right) { | ||
break; | ||
} | ||
|
||
int sum = m[left] + m[right]; | ||
|
||
// decrease the distinct count if zero sum pair is encountered | ||
if (sum == 0) { | ||
|
||
count--; | ||
|
||
left++; | ||
right--; | ||
} else if (sum < 0) { | ||
|
||
left++; | ||
} else { | ||
|
||
right--; | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Chapter10/CountDistinctAbsoluteSortedArray/src/main/java/coding/challenge/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package coding.challenge; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
|
||
int[] m = {-3, -3, -2, -1, -1, -1, 0, 1, 1, 1, 2, 2, 3, 5, 6, 7, 7}; | ||
|
||
int result = Arrays.findAbsoluteDistinct(m); | ||
|
||
System.out.println("Array: " + java.util.Arrays.toString(m)); | ||
System.out.print("The number of distinct absolute values is: " | ||
+ result); | ||
} | ||
} |