data-structures · intermediate · ~15 min

Validate a coloring

Check a coloring constraint.

Challenge

int is_valid_coloring(const int *adj,int n,const int *color);

Given a color per vertex, return 1 if it is a proper coloring (no edge joins two equal colors), else 0.

Input format

adj n×n symmetric; color length n.

Output format

1 if proper, else 0.

Constraints

Colors may be any ints.

Starter code

#include <stddef.h>
/* Given a color per node, return 1 if it is a proper coloring (no edge joins two same-colored nodes), else 0. */
int is_valid_coloring(const int *adj,int n,const int *color){ (void)adj;(void)n;(void)color; return 1; }

Common mistakes

Checking only one direction of the matrix (harmless here, but scan all edges).

Edge cases to handle

No edges → always valid.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.