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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:42:49+00:00 2026-06-14T01:42:49+00:00

I am making a program which finds the maximum-length subarray whose sum is closest

  • 0

I am making a program which finds the maximum-length subarray whose sum is closest to 0.
Actually i was trying to solve this question: http://opc.iarcs.org.in/index.php/problems/DEVIOUS , but when i submit my code, it gives correct answer to only 50% of test cases.
My code is working fine, and even giving correct answer for my own test cases, so i need a test case to prove my code/algorithm wrong, so that i can find the mistake in my code.

This is my algorithm,

  1. Create an array ‘temp’ equal to the size of the input array, initialize all the elements of temp such that, temp[i] = sum of all elements up to input[i]
  2. Sort the array temp, find ‘i’ in temp such that temp[i-1] – temp[i] is closest to 0

Here is my code to solve the problem from the link above.

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <limits.h>

using namespace std;

struct element{
    int index;
    int value;
};

bool compare(element a, element b){
    return a.value < b.value;
}

int main(){
    int n;
    cin >> n;
    int array [n];
    element elements[n];
    int sum = 0;

    for(int i = 0 ;i < n; i ++){
        cin >> array[i];
        sum = sum + array[i];
        elements[i].index = i;
        elements[i].value = sum;
    }

    sort(elements,elements+n,compare);

    int diff = INT_MAX;
    int i1 = 0,i2 = 0;
    for(int i = 1;i < n;i ++){
        int temp = elements[i].value - elements[i-1].value;
        if(temp < diff){
            diff = temp;
            i1 = elements[i].index;
            i2 = elements[i-1].index;
        }
        else if(temp == diff){
            if(abs(elements[i].index - elements[i-1].index) > abs(i1-i2)){
                i1 = elements[i].index;
            i2 = elements[i-1].index;
            }
        }
    }

    diff = 0;
    int s,e;
    if(i1 >= i2){
        e = i1, s = i2+1;
    }else{
        s = i1+1, e = i2;
    }

    for(int i = s;i <= e ; i++){
        diff = diff+array[i];
    }
    cout << diff << endl;

    cout << s+1 << " " << e+1;

    return 0;
}

EDIT: I found a mistake in my code and edited it, it now gives correct answer for 80% of the inputs

  • 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-14T01:42:50+00:00Added an answer on June 14, 2026 at 1:42 am

    In other words, if the profits/losses at the stations are p1, p2, …,
    pN the minister would like to handover a sequence i, i+1, …, j such
    that the absolute value of pi + pi+1 + … + pj is minimized. If there
    is more than one sequence with this minimum absolute value then he
    would like to hand over the longest one

    I think your algorithm is slightly wrong:

    temp[i] = sum of all elements up to input[i]
    Sort the array temp

    The start of the sequence may well be another position than the start of the array – it can be a sub-sequence don’t forget.

    -1 | -2 | 10 | 5 |-10 | 6
    
       |    |    | x | x  | x
    

    edit2: Removed wrong explanation now I looked at the code –

    if(temp < diff){
            diff = temp;
            i1 = elements[i].index;
            i2 = elements[i-1].index;
        }
    

    You do have a preference for -ve numbers (loss). Could it be that the misses are where there is a nearer number to zero because it is either slightly less -ve or +ve (but where the abs value is smaller)?

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

Sidebar

Related Questions

The program I am making has several views in which the user is trying
I am currently making a program which is supposed to prompt the user to
So I'm making a program which is supposed to print a horizontal histogram of
I'm making a program which create a RAW socket in order to read all
I am making a program with which to save banking information (i.e., account, password,
I am making a timetabling program which does one to one matches from SubjectTeacherPeriod
I'm making a program in Java which will automatically move all of the .png
I m making an rmi client server based program which is suppose to pass
I'm making a Java program that accesses a website, which depends on handing out
I'm making a program in which many weird shapes are drawn onto a canvas.

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.