#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROWS 3
#define COLS 3
// Function to find the maximum number in the matrix
int findMaxValue(int matrix[ROWS][COLS]) {
int maxVal = matrix[0][0];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (matrix[i][j] > maxVal) {
maxVal = matrix[i][j];
}
}
}
return maxVal;
}
// Function to check if a row contains unique numbers
bool isRowUnique(int row[], int len, int maxVal) {
bool *uniqueNumbers = (bool*)calloc(maxVal + 1, sizeof(bool)); // Allocate memory dynamically
if (!uniqueNumbers) { // Check memory allocation
printf("Memory allocation failed!\n");
return false;
}
for (int i = 0; i < len; i++) {
if (uniqueNumbers[row[i]]) {
free(uniqueNumbers); // Free allocated memory before returning
return false; // Found a repetition
}
uniqueNumbers[row[i]] = true;
}
free(uniqueNumbers); // Free allocated memory after use
return true;
}
// Function to check if all matrix rows are unique
bool areMatrixRowsUnique(int matrix[ROWS][COLS]) {
int maxVal = findMaxValue(matrix); // Get max number for dynamic allocation
for (int i = 0; i < ROWS; i++) {
if (!isRowUnique(matrix[i], COLS, maxVal)) {
return false; // If any row has repetitions, return false
}
}
return true; // All rows are unique
}
int main() {
// Matrix with large numbers - More general solution
int matrix[ROWS][COLS] = {
{31000, 23898, 18370},
{12004, 56230, 37123},
{18370, 89231, 18370} // This row contains a repetition
};
if (areMatrixRowsUnique(matrix)) {
printf("All rows contain unique numbers.\n");
} else {
printf("Some rows contain repeated numbers.\n");
}
return 0;
}
/*
run:
Some rows contain repeated numbers.
*/