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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:10:38+00:00 2026-06-05T22:10:38+00:00

Here is a question that I encountered during an Interviewstreet codesprint . I was

  • 0

Here is a question that I encountered during an Interviewstreet codesprint.
I was unable to find a a solution or even think in its direction. I’d be thankful if someone could help me find the soultion, or explain me how the problem neeeds to be dealt with.

Given numbers 1, 2, 3, .., N, arrange them in a order such that the
sum of product of adjecent numbers is maximized.

For example: if N = 3, and we order them as ( 1, 2, 3 ), the sum of
products is 1*2 + 2*3 = 8 and if we order them as ( 1, 3 ,2 ) the sum
of products is 1*3 + 3*2 = 9.

Input format :

First line of the input contains T, the number of test-cases. Then
follow T lines, each containing an integer N.

Output format :

For each test case print the maximum sum of product of adjacent
numbers.

Sample input :

2 2 4

Sample output :

2 23

Explanation :

In first test case given permutation is ( 1, 2 ). So maximum sum of
product is 1*2. In Second test case the numbers are (1,2,3,4).
Arrangement 1,3,4,2 has sum of product of adjacent numbers as
1*3+3*4+4*2 = 23. No other arrange has sum of product of adjacent
numbers more than 23.

Constraints :

1 <= T <= 10 1 <= N <= 200000

  • 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-05T22:10:40+00:00Added an answer on June 5, 2026 at 10:10 pm

    The maximum sum-of-adjacent-products comes when the largest value is in the middle of the sequence, and the successively lower values alternate to its left and right. That is, your sequence for a given value n would be […, n-3, n-1, n, n-2, n-4, …] (or the reverse of this, which will have the same sum of products).

    So, leaving out the input-parsing bits, here’s the heart of the algorithm (in Python, but easily translated to other languages):

    def maximumSumOfAdjacentProducts(n):
        if n == 1: # special case needed for a one element sequence
            return 1
    
        sumOfProducts = n * (n-1) # this pair is the "center" of the sequence
    
        for i in range(n-2, 0, -1): # iterate downward from n-2 to 1
            sumOfProducts += i*(i+2) # each adjacent pair is separated by 2
    
        return sumOfProducts
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a question that I don't quite understand: The command, system(pause); is taught to
Here is a question that I have had for some time but never actually
Here's a question that comes up again and again in C++ class implementation. I'm
Sort of a methods/best practices question here that I am sure has been addressed,
The method in question that I want to use is gem and sourced here
I've been a long time browser here, but never have had a question that
Basic question here - I have many lines of code that look something like:
The question here is: If a thread that acquiring an exclusive lock to an
my question is that I have made multimap.Here is the partial code. if(binary_search(final.begin() ,
I was browsing StackOverflow when I encountered this question. Here the author mentions his/her

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.