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 6474565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:36:06+00:00 2026-05-25T06:36:06+00:00

This question was asked in interview and deals with recursion/backtracking. Suppose we have two

  • 0

This question was asked in interview and deals with recursion/backtracking. Suppose we have two arrays of booleans:bool* source and bool* target,each of them the same length n(source/target/n are given as arguments). The goal of the question is to transform source to target using operation switch.

  • If there are several transforns: present any one of them
  • If there is no solution: assert that there is no solution

Definition: operation switch(int i, bool* arr) inverts the value at arr[i] and arr[i-1] and arr[i+1] (if these indices are in the range 0…n-1).

In other words, switch operation will usually flip three bits (i and its neighbors), but only two at the ends.

For example:

  • switch(0,arr) will switch the values of arr[0] and arr[1] only
  • switch(n-1,arr) will switch the values of arr[n-1] and arr[n-2] only

Thank you in advance for the suggestions about the algorithm.

  • 1 1 Answer
  • 0 Views
  • 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-25T06:36:07+00:00Added an answer on May 25, 2026 at 6:36 am

    As far as I understand the problem, the main observation is that we never need to perform more than one switch on any position. This is because switching twice is the same as not switching at all, so all even switches are equivalent to 0 switches and all odd switches are as good as 1 switch.

    Another thing is that the order of the switches don’t matter. switching the i-th and i+1-th elements is the same as switching i+1-th and then the i-th. The pattern obtained at the end is the same.

    Using these two observations, we can simply try out all possible ways of applying switch to the n length array. This can be done recursively (i.e. doing a switch at index i/not doing a switch at index i and then trying out i+1) or by enumerating all 2**n n bit bitmask and using them to apply switches until one of them creates the target value.

    Below is my hack at the solution. I have packed the arrays into ints and used them as bitmasks to simplify the operations. This prints out the indices that need to be switched to get the target array and prints “Impossible” if the target is not obtainable.

    #include <cstdio>
    
    int flip(int mask, int bit){
        return mask^(1<<bit);
    }
    
    int switch_(int mask, int index, int n){
        for(int i=-1;i<=+1;i++){
            if ((index+i)>=0 && (index+i)<n) mask=flip(mask,index+i);
        }
        return mask;
    }
    
    int apply(int source, int flips, int n){
        int result=source;
        for(int i=0;i<n;i++){
            if (flips&(1<<i)) result=switch_(result,i,n);
        }
        return result;
    }
    
    void solve(int source, int target, int n){
        bool found=false;
        int current=0;
        int flips=0;
        for(flips=0;flips<(1<<n) && !found;flips++){
            current=apply(source,flips,n);
            found=(current==target);
        }
        if (found){
            flips--;
            for(int i=0;i<n;i++){
                if (flips&(1<<i)) printf("%d ",n-i-1); //prints the indices in descending order
            }
            printf("\n");
        }
        else{
            printf("Impossible\n");
        }
    }
    
    int array2int(int* arr, int n){
        int ret=0;
        for(int i=0;i<n;i++){
            ret<<=1;
            if (arr[i]==1) ret++;
        }
        return ret;
    }
    int main(){
        int source[]={0,0,0,0};
        int target[]={1,1,1,1};
        int n=4;
        solve(array2int(source,n),array2int(target,n),n);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I went to an interview today and was asked this question: Suppose you have
This question was asked at interview. Say I have a contract. [ServiceContract] public interface
This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more
This question was asked at interview.I need to have running total ( only using
One of my friend was asked this question in an interview - You have
This question was asked to me in an interview: Lets say you have a
I was asked this question in an interview. If you had two numbers represented
This was the question asked in interview. Can we call one constructor from another
This is an interview question asked a month ago.... Do session use cookies? If
I was asked this question during an interview. They're both O(nlogn) and yet most

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.