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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:27:07+00:00 2026-05-25T15:27:07+00:00

i have an array which contains only 2 types of numbers(x and x-1) eg:-

  • 0

i have an array which contains only 2 types of numbers(x and x-1) eg:- {5,5,4,4,5,5,5} and i am given a range like 12-14(inclusive). i already know the length of the array is constant 7 and i also know how many elements of each type there are in an array(count)

now i need to find if there is any combination of elements in the array whose sum falls into that range.

All i need is the number of elements in the subset whose sum falls in that range.

i have solved this problem by using brute force in the following way but it is very in efficient.

here count is the number of x-1’s in the array

for(int i=0;i<=7-count;i++){
             for(int j=0;j<=count;j++){
                 if(x*(i)+(x-1)*j>=min && x*(i)+(x-1)*j<=max){
                 output1=i+j;
             }
             }
         }

could some one plz tell me if there is a better way of solving this

example:-

the array given is {5,5,4,4,5,5,5} and the range given is 12-14.

so i would pick {5,5,4} subset whose sum is 14 and so the answer to the number of elements in the subset will be 3.{5,4,4} can also be picked in this solution

  • 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-05-25T15:27:08+00:00Added an answer on May 25, 2026 at 3:27 pm

    You can improve your brute force by using some analysis.

    with N being the array length and n being the result:

    0 <= n <=N
    0 <= j <= count
    0 <= i <= N - count
    n = i + j -> j <= n
    
    sum = x * i + (x - 1) * j = x * n - j
    
    min <= x * n - j <= max -> x * n - max <= j <= x * n - min
    min <= x * n - j -> n >= (min + j) / x >= min / x
    x * n - j <= max -> n <= (max + j) / x <= (max + count) / x
    

    summing up you can use your cycle but with other range:

    for (int n = min / x; n <= min (N, (max + count) / x); n++)
    {
        for (int j = max (0, x * n - max); j <= min (count, x * n - min, n); j++)
        {
            sum = x * n - j;
            if (sum >= min && sum <= max)
            {
                output1 = n;
            }
        }
    }
    

    P.S.: here’s some picture that may help to understand the idea
    graph http://i.zlowiki.ru/110917_768e5221.jpg

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

Sidebar

Related Questions

I have a array called $A which contains only non zero positive numbers. Now
I have an array which contains 12 numbers and I want to save this
I have a cell array which contains 7 matrices of varying column and length
I have this ingredients array which contains multiple variables (like name, icon, value etc.).
I have an array which contains 2 types of items (lets call them type
I have an array which contains a set of UIImage in byte forms. --My
I have an array which contains another array Would I notate it this way?
I have an array which contains around 50-200 hyperlinks. How can I pass this
I have an array which contains sets of three similar named items; however, sometimes
Am I missing something in the Array documentation? I have an array which contains

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.