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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:03:30+00:00 2026-06-01T10:03:30+00:00

This problem is taken from interviewstreet.com Given array of integers Y=y1,…,yn, we have n

  • 0

This problem is taken from interviewstreet.com

Given array of integers Y=y1,…,yn, we have n line segments such that
endpoints of segment i are (i, 0) and (i, yi). Imagine that from the
top of each segment a horizontal ray is shot to the left, and this ray
stops when it touches another segment or it hits the y-axis. We
construct an array of n integers, v1, …, vn, where vi is equal to
length of ray shot from the top of segment i. We define V(y1, …, yn)
= v1 + … + vn.

For example, if we have Y=[3,2,5,3,3,4,1,2], then v1, …, v8 =
[1,1,3,1,1,3,1,2], as shown in the picture below:

enter image description here

For each permutation p of [1,…,n], we can calculate V(yp1, …,
ypn). If we choose a uniformly random permutation p of [1,…,n], what
is the expected value of V(yp1, …, ypn)?

Input Format

First line of input contains a single integer T (1 <= T <= 100). T
test cases follow.

First line of each test-case is a single integer N (1 <= N <= 50).
Next line contains positive integer numbers y1, …, yN separated by a
single space (0 < yi <= 1000).

Output Format

For each test-case output expected value of V(yp1, …, ypn), rounded
to two digits after the decimal point.

Sample Input

6
3
1 2 3
3
3 3 3
3
2 2 3
4
10 2 4 4
5
10 10 10 5 10
6
1 2 3 4 5 6

Sample Output

4.33
3.00
4.00
6.00
5.80
11.15

Explanation

Case 1: We have V(1,2,3) = 1+2+3 = 6, V(1,3,2) = 1+2+1 = 4, V(2,1,3) =
1+1+3 = 5, V(2,3,1) = 1+2+1 = 4, V(3,1,2) = 1+1+2 = 4, V(3,2,1) =
1+1+1 = 3. Average of these values is 4.33.

Case 2: No matter what the permutation is, V(yp1, yp2, yp3) = 1+1+1 =
3, so the answer is 3.00.

Case 3: V(y1 ,y2 ,y3)=V(y2 ,y1 ,y3) = 5, V(y1, y3, y2)=V(y2, y3, y1) =
4, V(y3, y1, y2)=V(y3, y2, y1) = 3, and average of these values is
4.00.

A naive solution to the problem will run forever for N=50. I believe that the problem can be solved by independently calculating a value for each stick. I still need to know if there is any other efficient approach for this problem. On what basis do we have to independently calculate value for each stick?

  • 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-01T10:03:31+00:00Added an answer on June 1, 2026 at 10:03 am

    As you correctly, noted we can solve problem independently for each stick.

    Let F(i, len) is number of permutations, that ray from stick i is exactly len.
    Then answer is

    (Sum(by i, len) F(i,len)*len)/(n!)

    All is left is to count F(i, len). Let a(i) be number of sticks j, that y_j<=y_i. b(i) – number of sticks, that b_j>b_i.

    In order to get ray of length len, we need to have situation like this.

    B, l...l, O  
       len-1 times
    

    Where O – is stick #i. B – is stick with bigger length, or beginning. l – is stick with heigth, lesser then ith.

    This gives us 2 cases:
    1) B is the beginning, this can be achieved in P(a(i), len-1) * (b(i)+a(i)-(len-1))! ways.
    2) B is bigger stick, this can be achieved in P(a(i), len-1)*b(i)*(b(i)+a(i)-len)!*(n-len) ways.

    edit: corrected b(i) as 2nd term in (mul)in place of a(i) in case 2.

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

Sidebar

Related Questions

I have an unusual problem. Let's consider such models (taken from django docs): class
This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives
I have a simple but strange problem. I compare to strings, (taken from different
I'm writing this question because one little invisible problem has taken from me hours
This example is taken from the Linux System Programming book, page 88. Code: http://pastebin.com/mEfmHbPP
The following problem is taken from Problems on Algorithms (Problem 653): You are given
Hey, This is just a simple exercise from class that I decided to have
I'm having a problem with this query that takes several seconds to complete. I
Problem Hello all! I have this code which takes my jpg image loops through
This is my first post here. I have a problem. I need to take

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.