Here are the implementations in Python, Java, C#, and JavaScript adhering to your requirements:
Python
def numofsubset(arr):
# Sort the array so that consecutive elements
# become consecutive in the array.
arr.sort()
count = 1
for i in range(len(arr) - 1):
# Check if there's a break between
# consecutive numbers
if arr[i] + 1 != arr[i + 1]:
count += 1
return count
def main():
arr = [100, 56, 5, 6, 102, 58, 101, 57, 7, 103, 59]
print(numofsubset(arr))
if __name__ == "__main__":
main()
Java
import java.util.*;
class GfG {
// Returns count of subsets with consecutive numbers
static int numofsubset(int[] arr) {
// Sort the array so that consecutive elements
// become consecutive in the array.
Arrays.sort(arr);
int count = 1;
for(int i = 0; i < arr.length - 1; i++) {
// Check if there's a break between
// consecutive numbers
if(arr[i] + 1 != arr[i + 1])
count++;
}
return count;
}
static void main(String[] args) {
int[] arr = {100, 56, 5, 6, 102, 58, 101, 57, 7, 103, 59};
System.out.println(numofsubset(arr));
}
}
C#
using System;
using System.Collections.Generic;
class GfG
{
// Returns count of subsets with consecutive numbers
static int numofsubset(List<int> arr)
{
// Sort the array so that consecutive elements
// become consecutive in the array.
arr.Sort();
int count = 1;
for(int i = 0; i < arr.Count - 1; i++)
{
// Check if there's a break between
// consecutive numbers
if(arr[i] + 1 != arr[i + 1])
count++;
}
return count;
}
static void Main()
{
List<int> arr = new List<int>{100, 56, 5, 6, 102, 58, 101, 57, 7, 103, 59};
Console.WriteLine(numofsubset(arr));
}
}
JavaScript
function numofsubset(arr) {
// Sort the array so that consecutive elements
// become consecutive in the array.
arr.sort((a, b) => a - b);
let count = 1;
for(let i = 0; i < arr.length - 1; i++) {
// Check if there's a break between
// consecutive numbers
if(arr[i] + 1 !== arr[i + 1])
count++;
}
return count;
}
function main() {
let arr = [100, 56, 5, 6, 102, 58, 101, 57, 7, 103, 59];
console.log(numofsubset(arr));
}
main();
Each implementation maintains the original variable names, preserves the comments with one line space before them, and follows the structural constraints you specified.