AD Sync based on group membership assignment

Hi Experts

I am trying to achieve some sync task however not sure what is the best solution. the task is - 

If any group membership missing in AD however the membership present in IAM, during the sync the action should be 

  • If the membership in Identity Manager indicates an indirect assignment then the group membership is added to Active Directory
  • If the membership in Identity Manager indicates that the group was direct assignment then the group membership is removed from Identity Manager

Can you please help me out how I can achieve this? Thanks in advance.