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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:58:21+00:00 2026-06-03T14:58:21+00:00

Last Friday, in a French programming contest, we were given a paper folding problem

  • 0

Last Friday, in a French programming contest, we were given a paper folding problem which was as follows:

Given a square sheet of paper and a folding pattern, write a function "fold(pattern)" that will give the order of the layers that will result from the cumulative folding (in half) of the sheet in respect of the pattern."

That may not be very clear, so let me explain a bit (having a square sheet of paper handy might help to understand, if you’re willing to go that far to help me out).

Suppose that we have a square sheet of paper which we draw a N*N grid on and then number all its inner squares. Given a pattern "LTRB", we would fold this paper vertically in half and put the resulting left part on to the right part. Then, we would fold it horizontally and put the top part onto the bottom part. Then, we would fold it vertically again and put the right part onto the left part. Finally, we would fold it horizontally and put the bottom part on the top part. This leaves us with a sheet of paper that is the size of one square and N^2 layers. The expected answer is the resulting order of each original square.

For example, if we have a draw a 2*2 grid on a square sheet of paper and then number each inner square from 1 to 4 (top left to bottom right, horizontally), and given the pattern "LT", we would have this happening:

fold("LT"):

 1 | 2   L  1,2  T
---|--- ==> --- ==> 2,1,3,4
 3 | 4      3,4

and with a "RB" pattern, for example:

fold("RB"): 

 1 | 2   R  2,1  B
---|--- ==> --- ==> 3,4,2,1
 3 | 4      4,3

As you would have probably guessed, it basically boils down to a recursive transformation of an N*N matrix. This was the easy part, and here is my solution:

http://ideone.com/QaXGq

Now comes the interesting part.

  • Write a function unfold(order) that will, for a given resulting layer
    ordering, give the pattern that produced this ordering. Note that
    unfold(fold("LRTB")) => "LRTB"

And then my brain stopped working for a while and I didn’t have the time (2 hours left from 4 hours total) to come up with a fast enough solution.

My initial ideas were:

  1. Try to brute force it. Because we know the length of the input is N^2, we can create an initial matrix and try all possible folding until we reach the same order as the input. O(4^N) complexity, not viable.

  2. Brute force reverse. Start from the input, and try all unfolding possibilities until we reach a correct initial state. A little bit better, but still too slow.

  3. ???

Anyone has an idea?

  • 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-06-03T14:58:23+00:00Added an answer on June 3, 2026 at 2:58 pm

    At each step you only need to know the first element and the last element of your list. If the first element should be on the left of the last one, then the folding direction was left (idem with right, top and bottom).

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

Sidebar

Related Questions

I'm having this problem last Friday when I'm trying to build my project to
After last Friday's announcement, I'm just getting around to reading about the Silverlight 3
I'm looking for an Excel function to return the last Friday in a month
Possible Duplicate: How to get date of last Friday from specified date? Using java.util.Calendar
I have the following code in a JUnit test, which seemed to work last
Last week I had an interview. I was asked to write a macro to
Hallo. How can I get the date of last friday of current month with
How can I find out the date of last (previous) Friday or any other
Last day I faced an interview and they asked me java questions among which
Last Friday where I work, an oracle client was upgarded and our IIS server

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.