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

  • Home
  • SEARCH
  • 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 8343723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:09:12+00:00 2026-06-09T06:09:12+00:00

I am having a Algorithm question, in which numbers are been given from 1

  • 0

I am having a Algorithm question, in which numbers are been given from 1 to N and a number of operations are to be performed and then min/max has to be found among them.

Two operations – Addition and subtraction

and operations are in the form a b c d , where a is the operation to be performed,b is the starting number and c is the ending number and d is the number to be added/subtracted

for example

suppose numbers are 1 to N
and
N =5

1 2 3 4 5

We perform operations as

1 2 4 5

2 1 3 4

1 4 5 6

By these operations we will have numbers from 1 to N as

1 7 8 9 5

-3 3 4 9 5

-3 3 4 15 11

So the maximum is 15 and min is -3

My Approach:
I have taken the lower limit and upper limit of the numbers in this case it is 1 and 5 only stored in an array and applied the operations, and then had found the minimum and maximum.

Could there be any better approach?

  • 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-09T06:09:14+00:00Added an answer on June 9, 2026 at 6:09 am

    I will assume that all update (addition/subtraction) operations happen before finding max/min. I don’t have a good solution for update and min/max operations mixing together.

    You can use a plain array, where the value at index i of the array is the difference between the index i and index (i – 1) of the original array. This makes the sum from index 0 to index i of our array to be the value at index i of the original array.

    Subtraction is addition with the negated number, so they can be treated similarly. When we need to add k to the original array from index i to index j, we will add k to index i of our array, and subtract k to index (j + 1) of our array. This takes O(1) time per update.

    You can find the min/max of the original array by accumulating summing the values and record the max/min values. This takes O(n) time per operation. I assume this is done once for the whole array.

    Pseudocode:

    a[N] // Original array
    d[N] // Difference array
    
    // Initialization
    d[0] = a[0]
    for (i = 1 to N-1)
        d[i] = a[i] - a[i - 1]
    
    // Addition (subtraction is similar)
    add(from_idx, to_idx, amount) {
        d[from_idx] += amount
        d[to_idx + 1] -= amount
    }
    
    // Find max/min for the WHOLE array after add/subtract
    current = max = min = d[0];
    for (i = 1 to N - 1) {
       current += d[i]; // Sum from d[0] to d[i] is a[i]
       max = MAX(max, current);
       min = MIN(min, current);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems implementing an algorithm to read a foreign process' memory. Here
I'm having trouble getting my head around algorithm analysis. I seem to be okay
I'm having trouble finding the right pathfinding algorithm for some AI I'm working on.
I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
What's so difficult about the subject that algorithm designers are having a hard time
I build a parsing algorithm using NSXMLParser. Im having doubt as to what is
I am creating a Stratego Game and am having trouble creating an algorithm that
This question is a little involved. I wrote an algorithm for breaking up a
This question is probably quite different from what you are used to reading here
Short question: How do I automatically detect whether a CSV file has headers in

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.