[c++][Problems] Window Problem

Hey there!, its been quite for a while, hasn’t it?. well i got something, So my Professor gave me a Problem to solve Today, i thought you might find it interesting too, my solution is in C++, yours can be in any language, tell everyone in the comment, if you find another solution.

The Problem is:

You have been given Number of open applications(or say windows), then current application position and you’ll be given a range of useful applications (application that should not be closed), and rest application should be closed. time taken to step each window is 1 second, The useful windows will be in continues form . If unuseful windows are in continues form then the time to close all of them is only 1 second.

find the time taken to close all unuseful windows
Example:

INPUT

No. of windows: 5
range of useful windows:
2
4

current position: 3
OUTPUT
time taken is : 6

Example :

// Created by Dinesh Solanki on 04-02-2019.
#include <iostream>
using namespace std;
int start,end,time=0,windows,pos,temp;
bool fromStart=false,atEnd=false,rightDone=false,leftDone=false;
void goRight(int &time)
{
    if(!atEnd)
    {   temp=pos;
        while(temp++!=(end+1))
        {
            time++;
        }
        if(!leftDone && !fromStart)
            time+=time;
        //time+=(windows-end)+(windows-end);
    }
    rightDone=true;
}
void goLeft(int &time)
{
    if(!fromStart)
    {
        temp = pos;
        while(temp--!=start-1)
            time++;
        if(!rightDone && !atEnd)
            time+=time;
    }
    leftDone=true;
}
int main() {
    cout<<"\nEnter no of Open windows:";
    cin>>windows;
    cout<<"\nEnter Start and end of range(useful windows):";
    cin>>start>>end;
    cout<<"\nEnter current position:";
    cin>>pos;
    if(end==windows)
        atEnd=true;
    if(start==1)
        fromStart=true;
    if(pos-start<end-pos)
    {
        goLeft(time);
        goRight(time);
    }
    else
    {
        goRight(time);
        goLeft(time);
    }
    //time+=(start-1)+(start-1);
    cout<<"Time taken to close: "<<time<<" Second(s).";
    return 0;
}

Leave a comment