C++ Learning Community Forum
August 01, 2010, 02:34:35 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to find the k-th element of a subrange of an array/vector?  (Read 441 times)
Qbx
N00b!!1
*
Posts: 10


View Profile
« on: November 18, 2009, 08:13:44 PM »

Namely, given an array arr[1..n] of different integer numbers,
your program must answer a series of questions Q(i, j, k) in the form:
what would be the k-th number in arr[i..j] segment, if this segment was sorted?

Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


int main() {
    int arr[] = { 13, 9, 3, 8, 11, 42, 5, 6, 4, 19, 77, 1, 7 };

    vector<int> a(arr, arr + sizeof(arr) / sizeof(arr[0]));

return 0;
}

Any ideas to find it in the fastest way?
Logged
charlie
Is the meadow on fire?
Dr. of C++ology
****
Posts: 730


CsGYh8AacgY


View Profile
« Reply #1 on: December 04, 2009, 12:02:43 AM »

Sort, then use k (or k-1) as the index?
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!