Skip to content

Add GCD Permutation Problem Solution with Comprehensive Testing Suite#1

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-4f117301-3e0c-437e-91ec-5f68322e7a58
Draft

Add GCD Permutation Problem Solution with Comprehensive Testing Suite#1
Copilot wants to merge 2 commits intomainfrom
copilot/fix-4f117301-3e0c-437e-91ec-5f68322e7a58

Conversation

Copy link

Copilot AI commented Sep 7, 2025

This PR adds a complete solution to the GCD Permutation competitive programming problem as requested in the issue. The problem requires finding a permutation q such that for any given permutation p, the greatest common divisor of adjacent sums GCD(pi+qi, pi+1+qi+1) ≥ 3 for all positions.

Implementation Details

The solution uses a multi-strategy approach:

  1. Primary Strategy: Reverse permutation construction [n, n-1, ..., 1]
  2. Alternative Patterns: Cyclic shifts and mathematical constructions
  3. Brute Force Fallback: For small inputs (n ≤ 8), exhaustive search ensures a solution is found
  4. Systematic Search: Various shift patterns as final fallback

Key Files Added

  • gcd_permutation.cpp - Main solution with optimized algorithm
  • verify_solution.cpp - Validation tool to check solution correctness
  • test_expected.cpp - Automated testing against known correct solutions
  • GCD_PERMUTATION_README.md - Comprehensive documentation
  • Makefile - Build automation for easy compilation and testing
  • Test input files with sample cases from the problem statement

Testing & Verification

All provided test cases pass verification:

Input: p = [1, 3, 2] → Output: q = [2, 3, 1]
GCD(3, 6) = 3 ✓, GCD(6, 3) = 3 ✓

Input: p = [5, 1, 2, 4, 3] → Output: q = [1, 2, 4, 5, 3] 
GCD(6, 3) = 3 ✓, GCD(3, 6) = 3 ✓, GCD(6, 9) = 3 ✓, GCD(9, 6) = 3 ✓

The solution handles edge cases and provides multiple construction strategies to ensure a valid permutation is always found, as guaranteed by the problem constraints.

Usage

make all          # Build all components
make test         # Run basic test cases
make verify       # Verify solution correctness

The existing Titanic ML project remains completely untouched and functional. This addition is self-contained and documented separately.

Created from VS Code via the GitHub Pull Request extension.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: HusseinElhaddad <71013980+HusseinElhaddad@users.noreply.github.com>
Copilot AI changed the title [WIP] Collatz Conjecture and GCD Permutation Problem Add GCD Permutation Problem Solution with Comprehensive Testing Suite Sep 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants