Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

import java.util.Arrays;

The document contains a Java class named ArrayStats that manages an integer array. It includes a method to count the number of groups of consecutive identical elements of a specified size and a method to return the string representation of the array. The class is designed to be initialized with an integer array and provides functionality to analyze the array's structure.

Uploaded by

gouraiahnaga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

import java.util.Arrays;

The document contains a Java class named ArrayStats that manages an integer array. It includes a method to count the number of groups of consecutive identical elements of a specified size and a method to return the string representation of the array. The class is designed to be initialized with an integer array and provides functionality to analyze the array's structure.

Uploaded by

gouraiahnaga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Arrays;
import java.util.Scanner;

public class ArrayStats {


private int[] array; // Declare the array as an instance variable

// Constructor to initialize the array


public ArrayStats(int[] arr) {
this.array = arr;
}

// Method to count the number of groups of size "size"


public int getNumGroupsOfSize(int size) {
int cnt = 0; // To count groups of the specified size
int currentCount = 1; // To count consecutive same elements

// Iterate through the array starting from the second element


for (int i = 1; i < array.length; i++) {
if (array[i] == array[i - 1]) {
currentCount++; // Increase the count if the current element is
the same as the previous one
} else {
// Check if the group size is exactly "size"
if (currentCount == size) {
cnt++; // Increment the count of groups of the given size
}
currentCount = 1; // Reset count for a new value
}
}

// After the loop, check the last group


if (currentCount == size) {
cnt++;
}

return cnt;
}

// Method to return the string representation of the array


public String toString() {
return Arrays.toString(array);
}
}

You might also like