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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:48:06+00:00 2026-05-28T13:48:06+00:00

This is my code: SortedDictionary<int,int> Numbers = new SortedDictionary<int,int>(); List<int> onlyP = new List<int>(Numbers.Keys);

  • 0

This is my code:

SortedDictionary<int,int> Numbers = new SortedDictionary<int,int>();
List<int> onlyP = new List<int>(Numbers.Keys);
int Inferior = int.Parse(toks[0]);
int Superior = int.Parse(toks[1]);
int count = 0;

int inferiorindex = Array.BinarySearch(Numbers.Keys.ToArray(), Inferior);
if (inferiorindex < 0) inferiorindex = (inferiorindex * -1) - 1;
int superiorindex = Array.BinarySearch(Numbers.Keys.ToArray(), Superior);
if (superiorindex < 0) superiorindex = (superiorindex * -1) - 1;

count = Numbers[onlyP[superiorindex]] - Numbers[onlyP[inferiorindex]];

So what I’m trying to do is this: I’ve got a sorted dictionary with powers as keys, and a normal iteration as values. I’ve to print how many numbers of the keys fit within a specified range.

Example:
Some entries of the dict: [1,1],[4,2],[8,3],[9,4],[16,5],[25,6],[27,7],[32,8]
Limits: 2 and 10
Numbers within 2 – 10 : 4, 8, 9 = 3 numbers.

With BinarySearch I’m trying to quickly find the numbers I want and then substract Potencias[onlyP[superiorindex]] – Potencias[onlyP[inferiorindex]] to find how many numbers are within the range. Unfortunately it’s not working for all the cases, and it sometimes gives less numbers than the actual amount. How can this be fixed? Thanks in advance.

[EDIT] Examples of the problems: If I select limits: 4 and 4… it returns 0, but the answer is 1.
limits: 1 and 10^9 (the whole range) returns 32669… But the answer is 32670.
The algorithm is ignoring powers.

  • 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-28T13:48:07+00:00Added an answer on May 28, 2026 at 1:48 pm

    Finally, having read the documentation. Notice the -1 on the upperIndex conversion and the +1 on the return value, these are important.

    var numbers = new[] { 1, 4, 8, 9, 16, 25, 27, 32 };
    
    var lowerBound = 4;
    var upperBound = 17;
    
    int lowerIndex = Array.BinarySearch(numbers, lowerBound);
    if (lowerIndex < 0) lowerIndex = ~lowerIndex;
    
    // - 1 here because we want the index of the item that is <= upper bound.
    int upperIndex = Array.BinarySearch(numbers, upperBound);
    if (upperIndex < 0) upperIndex = ~upperIndex - 1;
    
    return (upperIndex - lowerIndex) + 1;
    

    Explanation:

    For the lower index we just take the complement because the BinarySearch returns the index of the first item >= lowerBound.

    For the upper index we additionally minus one from the complement because we want the first item <= upperBound (not >= upperBound which is what BinarySearch returns).

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

Sidebar

Related Questions

I have some trivial code that looks like this: SortedDictionary<String, long> d = new
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
HI I am using nested SortedDictionary in my code as SortedDictionary<string, SortedDictionary<string, int>> but
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
This code URL listofFiles = this.getClass().getResource(someDir); File f = new File(listofFiles.toString()); File[] files =
Consider the following code: var myDict = new Dictionary<string, int>(); myDict.Add(Key1, 1); myDict.Add(Key2, 2);
This code fails when I try to debug it using VC2010: char frd[32]=word-list.txt; FILE
This code: #include Backpack.h #include <sstream> #include <algorithm> int Backpack::getCurrentWeight() { int weight =
this code is supposed to list recent calls with recent same nos skipped but

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.