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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:26:35+00:00 2026-06-13T21:26:35+00:00

I want to solve this CodeChef challenge: Suppose We are given an array A

  • 0

I want to solve this CodeChef challenge:

Suppose We are given an array A of N(of range 100,000) elements. We are to find the count of all pairs of 3 such elements 1<=Ai,Aj,Ak<=30,000 such that

Aj-Ai = Ak- Aj and i < j < k

In other words Ai,Aj,Ak are in Arithmetic Progression. For instance for Array :

9 4 2 3 6 10 3 3 10

so The AP are:

{2,6,10},{9,6,3},{9,6,3},{3,3,3},{2,6,10} 

So the required answer is 5.

My Approach

What I tried is take 30,000 long arrays named past and right. Initially right contains the count of each 1-30,000 element.

If we are at ith position past stores the count of array value before i and right stores the count of array after i. I simply loop for all possible common difference in the array. Here is the code :

right[arr[1]]--;

for(i=2;i<=n-1;i++)
{
    past[arr[i-1]]++;
    right[arr[i]]--;
    k=30000 - arr[i];
    if(arr[i] <= 15000)
        k=arr[i];
    for(d=1;d<=k;d++)
    {
        ans+= right[arr[i] + d]*past[arr[i]-d] + past[arr[i] + d]*right[arr[i]-d];
    }
    ans+=past[arr[i]]*right[arr[i]];
}

But this gets me Time Limit Exceeded. Please help with a better algorithm.

  • 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-13T21:26:37+00:00Added an answer on June 13, 2026 at 9:26 pm

    You can greatly cut execution time if you make a first pass over the list and only extract number pairs that it is possible to have an 3 term AP between (difference is 0 mod 2). And then iterating between such pairs.

    Pseudo C++-y code:

    // Contains information about each beginning point
    struct BeginNode {
      int value;
      size_t offset;
      SortedList<EndNode> ends;  //sorted by EndNode.value
    };
    
    // Contains information about each class of end point
    struct EndNode {
      int value;
      List<size_t> offsets; // will be sorted without effort due to how we collect offsets
    };
    
    struct Result {
      size_t begin;
      size_t middle;
      size_t end;
    };
    
    SortedList<BeginNode> nodeList;
    foreach (auto i : baseList) {
      BeginNode begin;
      node.value = i;
      node.offset = i's offset; //you'll need to use old school for (i=0;etc;i++) with this
      // baseList is the list between begin and end-2 (inclusive)
      foreach (auto j : restList) { 
        // restList is the list between iterator i+2 and end (inclusive)
        // we do not need to consider i+1, because not enough space for AP
        if ((i-j)%2 == 0) { //if it's possible to have a 3 term AP between these two nodes
          size_t listOffset = binarySearch(begin.ends);
          if (listOffset is valid) {
            begin.ends[listOffset].offsets.push_back(offsets);
          } else {
            EndNode end;
            end.value = j;
            end.offsets.push_back(j's offset);
            begin.ends.sorted_insert(end);
          }
        }
      }
      if (begin has shit in it) {
        nodeList.sorted_insert(begin);
      }
    }
    // Collection done, now iterate over collection
    
    List<Result> res;
    foreach (auto node : nodeList) {
      foreach (auto endNode : node.ends) {
        foreach (value : sublist from node.offset until endNode.offsets.last()) {
          if (value == average(node.value, endNode.value)) {
            // binary_search here to determine how many offsets in "endNode.offsets" "value's offset" is less than.
            do this that many times:
              res.push_back({node.value, value, endNode.value});
          }
        }
      }
    }
    
    return res;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to solve the following optimization problem: Non-Latex: Given x and mu, find
I am new to prolog and i want to solve this problem. Suppose I
i want help how to solve this sql problem. suppose i have 3 tables
I want to solve this issue in query @ for Ex : id ip
I'm trying for hours now to solve this problem.. So.. I want to use
I've been searching for days to solve this, but with no success. I want
I want to solve this puzzle but the file resides on remote server. How
I have a little problem: I want to solve this problem with OCaml, so
I want json output order by id(child id) Can I solve this problem ?
I don't know how to solve this. I want to change the (DOM)source code

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.