Skip to main content
Question Protected by gnat
Tweeted twitter.com/#!/StackProgrammer/status/406050099392167936
added 239 characters in body
Source Link
7Aces
  • 449
  • 1
  • 5
  • 18

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (sum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame i.e., O(n log n) or less?

EDIT: ConsiderThe best approach so far was to sort the problem givenarray, find where a+b smaller than k then add indice of a to solution. But the particular condition Xprocess still has to go through n-1 'b's to check for each of them. It's better but still doesn't improve the worst case.

Also, in this caseisn't homework. I came across this while preparing for a competition.

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (sum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame i.e., O(n log n) or less?

EDIT: Consider the problem given the particular condition X, in this case

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (sum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame i.e., O(n log n) or less?

The best approach so far was to sort the array, find where a+b smaller than k then add indice of a to solution. But the process still has to go through n-1 'b's to check for each of them. It's better but still doesn't improve the worst case.

Also, this isn't homework. I came across this while preparing for a competition.

deleted 4 characters in body
Source Link
7Aces
  • 449
  • 1
  • 5
  • 18

I came across a problem where I need to evaluate all pair combinations in an array and check if they satisfy a condition. I was wondering what the algorithmic approach should be to the problem.

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (say sumsum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame i.e., O(n log n) or less.?

EDIT: Consider the problem given the particular condition X, in this case

I came across a problem where I need to evaluate all pair combinations in an array and check if they satisfy a condition. I was wondering what the algorithmic approach should be to the problem.

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (say sum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame i.e., O(n log n) or less.

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (sum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame i.e., O(n log n) or less?

EDIT: Consider the problem given the particular condition X, in this case

deleted 8 characters in body
Source Link
Joachim Sauer
  • 11k
  • 3
  • 54
  • 45

I came across a problem where I need to evaluate all pair combinations in an array and check if they satisfy a condition. I was wondering what the algorithmic approach should be to the problem.

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (say sum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame iei.e., O(n log n) or less.

Thanks!

I came across a problem where I need to evaluate all pair combinations in an array and check if they satisfy a condition. I was wondering what the algorithmic approach should be to the problem.

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (say sum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame ie., O(n log n) or less.

Thanks!

I came across a problem where I need to evaluate all pair combinations in an array and check if they satisfy a condition. I was wondering what the algorithmic approach should be to the problem.

Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (say sum of the two elements < an arbitrary number, k) .

The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame i.e., O(n log n) or less.

Source Link
7Aces
  • 449
  • 1
  • 5
  • 18
Loading