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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:55:30+00:00 2026-06-03T02:55:30+00:00

I have such function applyDiff(List orders, List ordersToAdd, int[] ordersToRemove) { } This function

  • 0

I have such function

applyDiff(List orders, List ordersToAdd, int[] ordersToRemove) {
}

This function should add orders from orderToAdd to orders and remove some orders from orders, indexes of orders to be removed are passed in ordersToRemove array.

The problem is: every time order from ordersToAdd is inserted into orders somewhere at position pos, all indexes from orderToRemove that a greather than pos must be increased at 1.

So should I dinamically modify ordersToRemove array?

What is general “algorithm” or modifing a collection when I should at the same time add-remove elements and I have indexes of elements to be removed?

Note I can not break this task at two (orders adding, orders removing) because the order is very important and function inside it decides in what order orders should be added and removed.

  • 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-03T02:55:31+00:00Added an answer on June 3, 2026 at 2:55 am

    You have not specified explicitly how do you determine at which positions exactly you insert, neither what is the semantics of int[] ordersToRemove (though for the latter I assume it is indices of objects to remove considering your notes). However I can still suggest an “algorithm” and you will apply it accordingly:

    Introduce one more variable in you method indexIncrease. Initialize it to 0. Every time you add to ordersToAdd increase this variable. Every time you consider a value from int[] ordersToRemove do not just consider this value, but rather consider ordersToRemove[i] + indexIncrease. Thus you will get your updated value from ordersToRemove without the need of iterating of over the whole array and increasing the values after each addition.

    Note that applying this “alogrithm” you ensure that the list orders is always in its current state, but the ordersToRemove will not be updated at all. hopefully this will work for you.

    EDIT As per the comments and the completely changed problem description:

    • You can spare the looping through all the elements of ordersToRemove if you use binary index tree. With it every modification becomes O(log n) in complexity, where n is the total number of orders that will exist.
    • considering the input limitations you give the index tree is a lot worse than the brute force solution you already have: the number of elements to iterate every time is neglectably small, the performance drawback is neglectable and the index tree is too much of spurious complexity.

    As seen overall: I advise you to stick with the current solution.

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

Sidebar

Related Questions

For ex. i have c# COM object with such function: int GetString([In, Out, MarshalAs(UnmanagedType.LPStr)]ref
I have such template functions: template<class R> list<R> f(const boost::function<R()>&); template<class R, class A0>
I have such example. function Bar() { this.barVal = BarValue; } function Foo() {
in c++ I have such function extern C _declspec(dllexport) uint8* bufferOperations(uint8* incoming, int size)
Let's suppose I have such handler: ed.onNodeChange.add(function(ed, cm, e) { }); How to get
I have such function, that adds a grid of droppables: function AddClassroomDrops(grid, weeks, days,
I have such code: function allValid() { $('input').each(function(index) { if(something) { return false; }
I'm looking for a way to have a function such as: myFunction({Key, value}, {Key2,
To use such great function as ConvertAll() , I have to convert IList to
i have many events such as $('.hidefile').click(function (event) { On page load and on

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.