How I can change the required capability for an admin menu without editing the plugin file?

The functioning of the code below depends a lot on how the plugin in question was built.

add_action( 'admin_menu', 'change_capability' );

function change_capability() {

    remove_menu_page(
        $menu_slug,
    );

    add_menu_page(
        $page_title,
        $menu_title,
        $new_capability,
        $menu_slug,
    );

}