Maximum number of Valid Pairs satisfying conditions k*(j – i) != (arr[j] – arr[i])/k
Last Updated :
07 Dec, 2023
Given an array arr[] of size N and an integer k, the task is to find the maximum number of valid pair of indices (i, j) such that i < j and k*(j – i) != (arr[j] – arr[i])/k.
Example:
Input: arr[] = {4, 1, 5, 3}, k = 2
Output: 5
Explanation: All possible pair is valid except {arr[1] , arr[2]}, because given condition doesn’t meet : 2(2 – 1) == (5 – 1) / 2.
Input: arr[] = {1, 2, 3, 4, 5}, k = 1
Output: 0
Explanation: There are no valid pairs.
Approach:
- The condition for a valid pair is when k*(j – i) != (arr[j] – arr[i])/k
- This condition can also be expressed as (j*k2 – arr[j]) !=(i*k2 – arr[i]).
- To find valid pairs, we’ll use a map to keep track of the count of elements with the value (i*k2 – arr[i]) up to the i th index.
- For the i th element, the number of valid pairs is calculated as
Total pairs up to i th index – count of elements with value (i*k2 – arr[i]) up to i th index.
Steps to solve this problem:
- Create a map for storing the value of (ik2 – arr[i])
- Create a variable result for storing the count of all valid pairs
- Iterate over the given array
- Store the count of all the previous elements with value (i*k2 – arr[i]) into a variable count, as this will act as all invalid pairs with the element at the i th index.
- Add all valid pairs by removing the invalid pairs into the result i.e. ( result += i – count).
- Increment the count of (ik2 – arr[i]) into the map for every ith index.
- Return the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long validPairs(vector< int >& arr, int k)
{
unordered_map< int , int > unmap;
long long result = 0;
for ( int i = 0; i < arr.size(); i++) {
long long count = unmap[i * k * k - arr[i]];
result += i - count;
unmap[i * k * k - arr[i]]++;
}
return result;
}
int main()
{
vector< int > arr = { 4, 1, 5, 3 };
int k = 2;
cout << validPairs(arr, k);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static long validPairs( int [] arr, int k)
{
Map<Integer, Integer> unmap = new HashMap<>();
long result = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
long count
= unmap.getOrDefault(i * k * k - arr[i], 0 );
result += i - count;
unmap.put(
i * k * k - arr[i],
unmap.getOrDefault(i * k * k - arr[i], 0 )
+ 1 );
}
return result;
}
public static void main(String[] args)
{
int [] arr = { 4 , 1 , 5 , 3 };
int k = 2 ;
System.out.println(validPairs(arr, k));
}
}
|
Python3
def validPairs(arr, k):
unmap = {}
result = 0
for i in range ( len (arr)):
count = unmap.get(i * k * k - arr[i], 0 )
result + = i - count
unmap[i * k * k - arr[i]] = unmap.get(i * k * k - arr[i], 0 ) + 1
return result
arr = [ 4 , 1 , 5 , 3 ]
k = 2
print (validPairs(arr, k))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static long ValidPairs(List< int > arr, int k)
{
Dictionary< long , long > map = new Dictionary< long , long >();
long result = 0;
for ( int i = 0; i < arr.Count; i++)
{
long count = map.GetValueOrDefault(i * k * k - arr[i], 0);
result += i - count;
if (map.ContainsKey(i * k * k - arr[i]))
{
map[i * k * k - arr[i]]++;
}
else
{
map.Add(i * k * k - arr[i], 1);
}
}
return result;
}
static void Main( string [] args)
{
List< int > arr = new List< int > { 4, 1, 5, 3 };
int k = 2;
Console.WriteLine(ValidPairs(arr, k));
}
}
|
Javascript
function validPairs(arr, k) {
const unmap = new Map();
let result = 0;
for (let i = 0; i < arr.length; i++) {
const count = unmap.get(i * k * k - arr[i]) || 0;
result += i - count;
unmap.set(i * k * k - arr[i], (unmap.get(i * k * k - arr[i]) || 0) + 1);
}
return result;
}
function main() {
const arr = [4, 1, 5, 3];
const k = 2;
console.log(validPairs(arr, k));
}
main();
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...