Click here to Skip to main content
15,446,510 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
n a cinema theatre named 'w', with `n' seats, whenever the theatre is full (i.e., when all the seats are occupied), theatre administartion will be gifting a viewer with a surprise gift and the lucky viewer will be chosen through the following process, called as the 'Character allottment process'.

Character allotment process :

1. Starting from the first seat, allot a character of `w' ( starting from the first character) to each seat. That is, allot the first character of w to the seat no.1, allot the second character of `w' to the seat no.2 and so on.

2. If all the characters of `w' are allotted, then continue the allotment from the first symbol of `w' again.

3. After allotting a character to the last seat, continue the allotment process by allotting the next character (character, next to the character allotted to the last seat earlier) from the last seat

4. Continue allotting the characters till the first seat.

5. In the process, all the seats would have two characters allotted to each seat, one character during the forward allottment process (when the characters are allotted from the first seat to the last seat) and another during the backward allottement process. (when the characters are allotted from the last seat to the first seat)

6. The first seat which gets the same character during both the forward allottment process and the backward allottment process is the lucky seat.

7. The person occupying the luck seat is given the surprise gift.

For eg, Name of the theatre : good. Total number of seats : 10 During the forward allottment, seats are allotted as : 1-g, 2-o, 3-o, 4-d,5-g, 6-o, 7-o, 8-d, 9-g, 10-o

and during reverse allotment process 10-o, 9-d, 8-g, 7-o, 6-o, 5-d, 4-g, 3-o, 2-o, 1-d. The first seat to get the same characters in both the allottment is the seat no : 10. The person who occupied the seat no. 10 is the lucky winner.

Given the name of the theatre `w', the total number of seats in the theatre, write an algorithm and the code to identify the lucky seat. Print -1 if no seat number could get the same letters.

Input format :

Name of the theatre: w

Total number of seats in the theatre: n

Output format :

The seat number of the lcky seat.

What I have tried:

C++
#include<iostream>
using namespace std;
int main()
{
 string w; cin >> w;
int n; cin >> n;
char* arr = new char[n+1];
int ctr = 0;
for (int i = 1; i <= n; i++) 
{
arr[i] = w[ctr++];
ctr %= w.length();
}
for(int i = n;i>0;i--){
    if (arr[i]==w[ctr++]){
      cout << i;
      return 1;
    }
    ctr %= w.length();
    }
  return 0;
}
Posted
Updated 1-Jan-22 6:21am
v5
Comments
Patrice T 1-Jan-22 11:00am    
What is the question/problem ?
OriginalGriff 1-Jan-22 11:23am    
I don't think he wants to do his own homework this year ... :sigh:
Patrice T 1-Jan-22 11:27am     CRLF
Just asking, to be fair. By the way, the question look like a challenge requirement.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include<iostream>

using namespace std;
int main() {
  string w;
  cin >> w;
  int n;
  cin >> n;
  char * arr = new char[n + 1];
  int ctr = 0;
  for (int i = 1; i <= n; i++) {
    arr[i] = w[ctr++];
    ctr %= w.length();
  }
  for (int i = n; i > 0; i--) {
    if (arr[i] == w[ctr++]) {
      cout << i;
      return 1;
    }
    ctr %= w.length();
  }
  return 0;
}

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 
C++
char* arr = new char[n+1];
...
...

delete [] arr;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900