Skip to content

CoreTask#9: ClusterCompression - Submit Your Code #34

@ErSKS

Description

@ErSKS

Define a cluster in an integer array to be a maximum sequence of elements that are all the same value. For example, in the array {3, 3, 3, 4, 4, 3, 2, 2, 2, 2, 4} there are 5 clusters, {3, 3, 3}, {4, 4}, {3}, {2, 2, 2, 2} and {4}. A cluster-compression of an array replaces each cluster with the number that is repeated in the cluster. So, the cluster compression of the previous array would be {3, 4, 3, 2, 4}. The first cluster {3, 3, 3} is replaced by a single 3, and so on. Write a function named clusterCompression with the function signature int[ ] clusterCompression(int[ ] a) The function returns the cluster compression of the array a. The length of the returned array must be equal to the number of clusters in the original array! This means that someplace in your answer you must dynamically allocate the returned array.

  • {0, 0, 0, 2, 0, 2, 0, 2, 0, 0} => {0, 2, 0, 2, 0, 2, 0} {18} => {18} {} => {}
  • {-5, -5, -5, -5, -5} => {-5} {1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1} => {1, 2, 1}
  • {8, 8, 6, 6, -2, -2, -2} => {8, 6, -2}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions