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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:42:44+00:00 2026-06-04T21:42:44+00:00

I am doing a problem of spoj i tried to do it but I

  • 0

I am doing a problem of spoj i tried to do it but I am always getting TLE(time limit expired )
the question is Hotels . this is my code , please can you tell me the way to optimize it.

#include<stdio.h>

int main()
{
    unsigned long long int a,b,i;
    scanf("%llu %llu",&a,&b);
    unsigned long long int arr[a];
    for(i=0;i<a;i++)
    {
        scanf("%llu",&arr[i]);
    }


        unsigned long long int d;
        unsigned long long int k,z=0;
    for(i=0;i<a;i++)
    {
        d = arr[i];
        for(k=i+1;k<a+1;k++)
        {
            if (d<b)
            {
                if(z<d)
                z = d;
            }
            else
            if(d==b)
            {
                z = d;
                break;
            }
            else
            break;
            d = d + arr[k];
        }
        if(d==b)
        break;
    }
    printf("%llu",z);
    return 0;
}

in this like if input 5 12 , these five are 2 1 3 4 5 to make 12 then i do like this :
first i compare 12 with 2 , then 2+1 , then 2+1+3 and so on after it goes to 5 it compares 12 to 1 , 1+3 , 1+3+4 .. and in this way i am taking only consecutive and break when i find equal to 12 otherwise it goes on till last.

Hitesh

  • 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-04T21:42:45+00:00Added an answer on June 4, 2026 at 9:42 pm

    There is no need to restart again as you said in the explanation after it goes to 5 it compares 12 to 1 , 1+3 , 1+3+4 .. and in this way. Look at your approach carefully, you are recalculating the sums of a subarray again and again. For e.g – You have already calculated sum of subarray 1+3+4 when you were doing 2+1+3+4. Hence there is still some scope of optimization.

    Before telling the exact solution, I want you to try it out first. Also as one more hint refer to the following problem Find subarray with given sum.

    EDIT: Sharing complete solution for future purpose.

    #include<stdio.h>
    
    /* Returns the maximum possible sum less than or equal to the gieven sum */
    int subArraySum(int arr[], int n, int sum)
    {
        /* Initialize curr_sum as value of first element
         and starting point as 0 */
        int curr_sum = arr[0], max_sum = 0, start = 0, i;
    
        /* Add elements one by one to curr_sum and if the curr_sum exceeds the
         sum, then remove starting element */
        for (i = 1; i <= n; i++)
        {
            // If curr_sum exceeds the sum, then remove the starting elements
            while (curr_sum > sum && start < i-1)
            {
                curr_sum = curr_sum - arr[start];
                start++;
            }
    
            //keep track of the maximum sum so far.
            if (max_sum < curr_sum)
                max_sum = curr_sum;
    
            curr_sum = curr_sum + arr[i];
        }
    
        return max_sum;
    }
    

    // Driver program to test above function

    int main()
    {
        int arr[] = {7, 3, 5, 6};
        int n = sizeof(arr)/sizeof(arr[0]);
        int sum = 9;
        printf("Max Sum = %d\n", subArraySum(arr, n, sum));
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing this problem: http://www.codechef.com/problems/FCTRL I have the solution, but, the memory usage is
I'm doing a practice problem and the question asks to create a destructor to
I get this problem while doing some experiment with set . I use a
The Facebook connect code is eluding me a bit. I have no problem doing
i was trying to do this problem but i could not able to get
I am doing a problem from codingbat and I am stuck at this problem.
Okay, so generally I wouldn't have a problem doing this and it would be
I'm doing problem 20 on Project Euler - finding the sum of the digits
I am doing problem 7 of Project Euler. What I am supposed to do
I have problem doing OTA installation for my iPad application. I updated my enterprise

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.