Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 884673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:47:08+00:00 2026-05-15T12:47:08+00:00

I need to swap first n elements from two non repeating sequences(arrays), where n

  • 0

I need to swap first n elements from two non repeating sequences(arrays), where n is a random integer.

Seq1: 1 4 5 6 9 8 2 3 7

Seq2: 3 9 1 2 8 7 4 5 6

If n = 4

Seq1: 3 9 1 2 | 9 8 2 3 7

Seq2: 1 4 5 6 | 8 7 4 5 6

Now i need to repair the sequence by replacing the repeated numbers after ‘|’.

How to do this?

This is my effort..

for(left1 = 0; left1<pivot; left1++)
  {
   for(right1 = pivot; right1 < no_jobs; right1++)
    {
     if(S1->sequence[left1] == S1->sequence[right1])
      {
        for(left2 = 0; left2<pivot; left2++)
        {
          for(right2 = pivot; right2<no_jobs; right2++)
          {
            if(S2->sequence[left2] == S2->sequence[right2])
            {
              swap_temp = S1->sequence[right1];
              S1->sequence[right1] = S2->sequence[right2];
              S2->sequence[right2] = swap_temp;
              break;
            }
          } 
        }
      }
    }
  }
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:47:09+00:00Added an answer on May 15, 2026 at 12:47 pm

    Swapping the first n elements is straightforward using a single for loop.

    for(int i = 0; i < n; i++){
       int tmp = array1[i];
       array1[i] = array2[i];
       array2[i] = tmp;
    }
    

    Now you need to find what has changed in the arrays. You can do this by comparing the parts you swapped.

    int m1 = 0, m2 = 0;
    int missing_array1[n];
    int missing_array2[n];
    
    for(int i = 0; i < n; i++){
        bool found = false;
        for(int j = 0; j < n; j++){
            if(array1[i] == array2[j]){
                found = true;
                break;
            } 
        }
        if(!found){
            missing_array2[m2++] = array1[i];
        }
    }
    
    for(int i = 0; i < n; i++){
        bool found = false;
        for(int j = 0; j < n; j++){
            if(array2[i] == array1[j]){
                found = true;
                break;
            } 
        }
        if(!found){
            missing_array1[m1++] = array2[i];
        }
    }
    

    missing_array2 now contains the numbers that are missing from array2. These are all the numbers that will be duplicated in array1. The same goes for missing_array1. Next you need to scan both arrays and replace the duplicates with the missing numbers.

    while(m1 >= 0){
        int z = 0;
        while(missing_array1[m1] != array2[n + z]){
            z++;
        }
        array2[n + z] = missing_array2[m1--];
    }
    
    while(m2 >= 0){
        int z = 0;
        while(missing_array2[m2] != array1[n + z]){
            z++;
        }
        array1[n + z] = missing_array1[m2--];
    }
    

    In summary, you compare the parts you swapped to find the values that will be missing from each array. These value are also the values that will be duplicated in the opposite array. Then you scan each of the arrays and replace the duplicate values with one of the missing values (I assume you don’t care which of the missing values, as long as all the values are unique.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.