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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:17:11+00:00 2026-06-15T08:17:11+00:00

Given an array x of size n, construct an array y of size n,

  • 0

Given an array x of size n, construct an array y of size n, where yi = sum(a) - xi.

How can this be done in O(n) using constant space and without using the subtraction operator? I can’t figure this one out. No bitwise operations can be used to mimic subtraction. I know the key here is with the additional constant space by making use of some data structure but how can this be done with the O(n) restriction? Making an array that holds the sums of all combinations except xi would require O(n^2).

  • 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-15T08:17:12+00:00Added an answer on June 15, 2026 at 8:17 am

    So, we have array x, and we must make array y. Let’s assume that our array x have size 5 (just for example). So let’s write values of each element of y’ array.

    y[0] =      + x[1] + x[2] + x[3] + x[4];
    y[1] = x[0] +      + x[2] + x[3] + x[4];
    y[2] = x[0] + x[1] +      + x[3] + x[4];
    y[3] = x[0] + x[1] + x[2]        + x[4];
    y[4] = x[0] + x[1] + x[2] + x[3]       ;
    

    Do you see this diagonal line? It divides y to left part and right part, where each next element of yLeft is sum of previous yLeft element and some element of x array. The same situation with right part of y (just reversed)

    Code, C#:

    var x = new int[] { 4, 6, 2, 4, -2, 4, 0 };
    var y = new int[x.Length];
    
    y[0] = 0;
    y[y.Length - 1] = 0;
    
    var curYLeft = 0;
    for ( int i = 1; i < x.Length; i++ ) {
        curYLeft += x[i - 1];
        y[i] = curYLeft;
    }
    
    var curYRight = 0;
    for ( int i = x.Length - 1 - 1; i >= 0; i-- ) {
        curYRight += x[i + 1];
        y[i] += curYRight;
    }
    
    for ( int i = 0; i < x.Length; i++ ) {
        Console.WriteLine("{0} {1}", x.Sum() - x[i], y[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You're given an array of size n, containing arbitrary boolean values. What is the
I was reading a book when I found that array size must be given
Given two sorted arrays: A and B . The size of array A is
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Given an Array A of n subArrays Sn, how can I select the Array
question origin Given an unsorted array of size n containing objects with ids of
Given an Array arr of size 100000, each element 0 <= arr[i] < 100
I have an interview question that I can't seem to figure out. Given an
Given an array, unsigned char q[32]=1100111... , how can I generate a 4-bytes bit-set,
Given an array of size N I need to find the min number of

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.