给定一个长度为N的数组arr[]和一个整数K ,任务是计算对(i, j)的数量,使得i < j和arr[i] > K * arr[j] 。
例子:
Input: arr[] = {5, 6, 2, 5}, K = 2
Output: 2
Explanation: The array consists of two such pairs:
(5, 2): Index of 5 and 2 are 0, 2 respectively. Therefore, the required conditions (0 < 2 and 5 > 2 * 2) are satisfied.
(6, 2): Index of 6 and 2 are 1, 2 respectively. Therefore, the required conditions (0 < 2 and 6 > 2 * 2) are satisfied.
Input: arr[] = {4, 6, 5, 1}, K = 2
Output: 3
朴素的方法:解决问题的最简单的方法是遍历数组,对于每个索引,找到索引大于它的数字,使得其中的元素乘以K 时小于当前索引处的元素。
请按照以下步骤解决问题:
- 初始化一个变量,比如cnt ,用0来计算所需对的总数。
- 从左到右遍历数组。
- 对于每个可能的索引,比如i ,遍历索引i + 1到N – 1并且如果找到任何元素,比如 arr[j],那么将cnt的值增加1使得arr[j] * K小于arr [我] 。
- 完成数组遍历后,将cnt打印为所需的对数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the count required pairs
int getPairs(int arr[], int N, int K)
{
// Stores count of pairs
int count = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
// Check if the condition
// is satisfied or not
if (arr[i] > K * arr[i + 1])
count++;
}
}
cout << count;
}
// Driver Code
int main()
{
int arr[] = { 5, 6, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
// Function Call
getPairs(arr, N, K);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to find the count required pairs
static void getPairs(int arr[], int N, int K)
{
// Stores count of pairs
int count = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
// Check if the condition
// is satisfied or not
if (arr[i] > K * arr[i + 1])
count++;
}
}
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, 6, 2, 1 };
int N = arr.length;
int K = 2;
// Function Call
getPairs(arr, N, K);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find the count required pairs
def getPairs(arr, N, K):
# Stores count of pairs
count = 0
# Traverse the array
for i in range(N):
for j in range(i + 1, N):
# Check if the condition
# is satisfied or not
if (arr[i] > K * arr[i + 1]):
count += 1
print(count)
# Driver Code
if __name__ == '__main__':
arr = [ 5, 6, 2, 1 ]
N = len(arr)
K = 2
# Function Call
getPairs(arr, N, K)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the count required pairs
static void getPairs(int []arr, int N, int K)
{
// Stores count of pairs
int count = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
// Check if the condition
// is satisfied or not
if (arr[i] > K * arr[i + 1])
count++;
}
}
Console.Write(count);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 5, 6, 2, 1 };
int N = arr.Length;
int K = 2;
// Function Call
getPairs(arr, N, K);
}
}
// This code is contributed by shikhasingrajput
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to merge two sorted arrays
int merge(int arr[], int temp[],
int l, int m, int r, int K)
{
// i: index to left subarray
int i = l;
// j: index to right subarray
int j = m + 1;
// Stores count of pairs that
// satisfy the given condition
int cnt = 0;
for (int l = 0; i <= m; i++) {
bool found = false;
// Traverse to check for the
// valid conditions
while (j <= r) {
// If condition satifies
if (arr[i] >= K * arr[j]) {
found = true;
}
else
break;
j++;
}
// While a[i] > K*a[j] satisfies
// increase j
// All elements in the right
// side of the left subarray
// also satisies
if (found) {
cnt += j - (m + 1);
j--;
}
}
// Sort the two given arrays and
// store in the resultant array
int k = l;
i = l;
j = m + 1;
while (i <= m && j <= r) {
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Elements which are left
// in the left subarray
while (i <= m)
temp[k++] = arr[i++];
// Elements which are left
// in the right subarray
while (j <= r)
temp[k++] = arr[j++];
for (int i = l; i <= r; i++)
arr[i] = temp[i];
// Return the count obtained
return cnt;
}
// Function to partiton array into two halves
int mergeSortUtil(int arr[], int temp[],
int l, int r, int K)
{
int cnt = 0;
if (l < r) {
// Same as (l + r) / 2, but avoids
// overflow for large l and h
int m = (l + r) / 2;
// Sort first and second halves
cnt += mergeSortUtil(arr, temp,
l, m, K);
cnt += mergeSortUtil(arr, temp,
m + 1, r, K);
// Call the merging function
cnt += merge(arr, temp, l,
m, r, K);
}
return cnt;
}
// Function to print the count of
// required pairs using Merge Sort
int mergeSort(int arr[], int N, int K)
{
int temp[N];
cout << mergeSortUtil(arr, temp, 0,
N - 1, K);
}
// Driver code
int main()
{
int arr[] = { 5, 6, 2, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
// Function Call
mergeSort(arr, N, K);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to merge two sorted arrays
static int merge(int arr[], int temp[],
int l, int m, int r, int K)
{
// i: index to left subarray
int i = l;
// j: index to right subarray
int j = m + 1;
// Stores count of pairs that
// satisfy the given condition
int cnt = 0;
for (i = l; i <= m; i++)
{
boolean found = false;
// Traverse to check for the
// valid conditions
while (j <= r)
{
// If condition satifies
if (arr[i] >= K * arr[j])
{
found = true;
}
else
break;
j++;
}
// While a[i] > K*a[j] satisfies
// increase j
// All elements in the right
// side of the left subarray
// also satisies
if (found == true)
{
cnt += j - (m + 1);
j--;
}
}
// Sort the two given arrays and
// store in the resultant array
int k = l;
i = l;
j = m + 1;
while (i <= m && j <= r)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Elements which are left
// in the left subarray
while (i <= m)
temp[k++] = arr[i++];
// Elements which are left
// in the right subarray
while (j <= r)
temp[k++] = arr[j++];
for (i = l; i <= r; i++)
arr[i] = temp[i];
// Return the count obtained
return cnt;
}
// Function to partiton array into two halves
static int mergeSortUtil(int arr[], int temp[],
int l, int r, int K)
{
int cnt = 0;
if (l < r)
{
// Same as (l + r) / 2, but avoids
// overflow for large l and h
int m = (l + r) / 2;
// Sort first and second halves
cnt += mergeSortUtil(arr, temp,
l, m, K);
cnt += mergeSortUtil(arr, temp,
m + 1, r, K);
// Call the merging function
cnt += merge(arr, temp, l,
m, r, K);
}
return cnt;
}
// Function to print the count of
// required pairs using Merge Sort
static void mergeSort(int arr[], int N, int K)
{
int temp[] = new int[N];
System.out.print(mergeSortUtil(arr, temp, 0, N - 1, K));
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 5, 6, 2, 5 };
int N = arr.length;
int K = 2;
// Function Call
mergeSort(arr, N, K);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to merge two sorted arrays
def merge(arr, temp, l, m, r, K) :
# i: index to left subarray
i = l
# j: index to right subarray
j = m + 1
# Stores count of pairs that
# satisfy the given condition
cnt = 0
for l in range(m + 1) :
found = False
# Traverse to check for the
# valid conditions
while (j <= r) :
# If condition satifies
if (arr[i] >= K * arr[j]) :
found = True
else :
break
j += 1
# While a[i] > K*a[j] satisfies
# increase j
# All elements in the right
# side of the left subarray
# also satisies
if (found) :
cnt += j - (m + 1)
j -= 1
# Sort the two given arrays and
# store in the resultant array
k = l
i = l
j = m + 1
while (i <= m and j <= r) :
if (arr[i] <= arr[j]) :
temp[k] = arr[i]
k += 1
i += 1
else :
temp[k] = arr[j]
k += 1
j += 1
# Elements which are left
# in the left subarray
while (i <= m) :
temp[k] = arr[i]
k += 1
i += 1
# Elements which are left
# in the right subarray
while (j <= r) :
temp[k] = arr[j]
k += 1
j += 1
for i in range(l, r + 1) :
arr[i] = temp[i]
# Return the count obtained
return cnt
# Function to partiton array into two halves
def mergeSortUtil(arr, temp, l, r, K) :
cnt = 0
if (l < r) :
# Same as (l + r) / 2, but avoids
# overflow for large l and h
m = (l + r) // 2
# Sort first and second halves
cnt += mergeSortUtil(arr, temp, l, m, K)
cnt += mergeSortUtil(arr, temp, m + 1, r, K)
# Call the merging function
cnt += merge(arr, temp, l, m, r, K)
return cnt
# Function to print the count of
# required pairs using Merge Sort
def mergeSort(arr, N, K) :
temp = [0]*N
print(mergeSortUtil(arr, temp, 0, N - 1, K))
# Driver code
arr = [ 5, 6, 2, 5 ]
N = len(arr)
K = 2
# Function Call
mergeSort(arr, N, K)
# This code is contributed by divyeshrabadiya07.
C#
// C# program for the above approach
using System;
class GFG {
// Function to merge two sorted arrays
static int merge(int[] arr, int[] temp, int l, int m,
int r, int K)
{
// i: index to left subarray
int i = l;
// j: index to right subarray
int j = m + 1;
// Stores count of pairs that
// satisfy the given condition
int cnt = 0;
for (i = l; i <= m; i++) {
bool found = false;
// Traverse to check for the
// valid conditions
while (j <= r) {
// If condition satifies
if (arr[i] >= K * arr[j]) {
found = true;
}
else
break;
j++;
}
// While a[i] > K*a[j] satisfies
// increase j
// All elements in the right
// side of the left subarray
// also satisies
if (found == true) {
cnt += j - (m + 1);
j--;
}
}
// Sort the two given arrays and
// store in the resultant array
int k = l;
i = l;
j = m + 1;
while (i <= m && j <= r)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Elements which are left
// in the left subarray
while (i <= m)
temp[k++] = arr[i++];
// Elements which are left
// in the right subarray
while (j <= r)
temp[k++] = arr[j++];
for (i = l; i <= r; i++)
arr[i] = temp[i];
// Return the count obtained
return cnt;
}
// Function to partiton array into two halves
static int mergeSortUtil(int[] arr, int[] temp, int l,
int r, int K)
{
int cnt = 0;
if (l < r) {
// Same as (l + r) / 2, but avoids
// overflow for large l and h
int m = (l + r) / 2;
// Sort first and second halves
cnt += mergeSortUtil(arr, temp, l, m, K);
cnt += mergeSortUtil(arr, temp, m + 1, r, K);
// Call the merging function
cnt += merge(arr, temp, l, m, r, K);
}
return cnt;
}
// Function to print the count of
// required pairs using Merge Sort
static void mergeSort(int[] arr, int N, int K)
{
int[] temp = new int[N];
Console.WriteLine(
mergeSortUtil(arr, temp, 0, N - 1, K));
}
// Driver code
static public void Main()
{
int[] arr = new int[] { 5, 6, 2, 5 };
int N = arr.Length;
int K = 2;
// Function Call
mergeSort(arr, N, K);
}
}
// This code is contributed by Dharanendra L V
Javascript
2
时间复杂度: O(N 2 )
辅助空间: O(N)
高效方法:思想是使用归并排序的概念,然后根据给定的条件计算对。请按照以下步骤解决问题:
- 初始化一个变量,比如answer ,以计算满足给定条件的对数。
- 重复地将数组划分为两个相等的一半或几乎相等的一半,直到每个分区中剩下一个元素。
- 调用递归函数,计算合并两个分区后满足条件arr[i] > K * arr[j]且i < j 的次数。
- 通过分别为前半部分和后半部分的索引初始化两个变量(例如i和j )来执行它。
- 递增j直到arr[i] > K * arr[j]和j <后半部分的大小。将(j – (mid + 1)) 添加到答案并增加i 。
- 完成上述步骤后,将answer的值打印为所需的对数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to merge two sorted arrays
int merge(int arr[], int temp[],
int l, int m, int r, int K)
{
// i: index to left subarray
int i = l;
// j: index to right subarray
int j = m + 1;
// Stores count of pairs that
// satisfy the given condition
int cnt = 0;
for (int l = 0; i <= m; i++) {
bool found = false;
// Traverse to check for the
// valid conditions
while (j <= r) {
// If condition satifies
if (arr[i] >= K * arr[j]) {
found = true;
}
else
break;
j++;
}
// While a[i] > K*a[j] satisfies
// increase j
// All elements in the right
// side of the left subarray
// also satisies
if (found) {
cnt += j - (m + 1);
j--;
}
}
// Sort the two given arrays and
// store in the resultant array
int k = l;
i = l;
j = m + 1;
while (i <= m && j <= r) {
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Elements which are left
// in the left subarray
while (i <= m)
temp[k++] = arr[i++];
// Elements which are left
// in the right subarray
while (j <= r)
temp[k++] = arr[j++];
for (int i = l; i <= r; i++)
arr[i] = temp[i];
// Return the count obtained
return cnt;
}
// Function to partiton array into two halves
int mergeSortUtil(int arr[], int temp[],
int l, int r, int K)
{
int cnt = 0;
if (l < r) {
// Same as (l + r) / 2, but avoids
// overflow for large l and h
int m = (l + r) / 2;
// Sort first and second halves
cnt += mergeSortUtil(arr, temp,
l, m, K);
cnt += mergeSortUtil(arr, temp,
m + 1, r, K);
// Call the merging function
cnt += merge(arr, temp, l,
m, r, K);
}
return cnt;
}
// Function to print the count of
// required pairs using Merge Sort
int mergeSort(int arr[], int N, int K)
{
int temp[N];
cout << mergeSortUtil(arr, temp, 0,
N - 1, K);
}
// Driver code
int main()
{
int arr[] = { 5, 6, 2, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
// Function Call
mergeSort(arr, N, K);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to merge two sorted arrays
static int merge(int arr[], int temp[],
int l, int m, int r, int K)
{
// i: index to left subarray
int i = l;
// j: index to right subarray
int j = m + 1;
// Stores count of pairs that
// satisfy the given condition
int cnt = 0;
for (i = l; i <= m; i++)
{
boolean found = false;
// Traverse to check for the
// valid conditions
while (j <= r)
{
// If condition satifies
if (arr[i] >= K * arr[j])
{
found = true;
}
else
break;
j++;
}
// While a[i] > K*a[j] satisfies
// increase j
// All elements in the right
// side of the left subarray
// also satisies
if (found == true)
{
cnt += j - (m + 1);
j--;
}
}
// Sort the two given arrays and
// store in the resultant array
int k = l;
i = l;
j = m + 1;
while (i <= m && j <= r)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Elements which are left
// in the left subarray
while (i <= m)
temp[k++] = arr[i++];
// Elements which are left
// in the right subarray
while (j <= r)
temp[k++] = arr[j++];
for (i = l; i <= r; i++)
arr[i] = temp[i];
// Return the count obtained
return cnt;
}
// Function to partiton array into two halves
static int mergeSortUtil(int arr[], int temp[],
int l, int r, int K)
{
int cnt = 0;
if (l < r)
{
// Same as (l + r) / 2, but avoids
// overflow for large l and h
int m = (l + r) / 2;
// Sort first and second halves
cnt += mergeSortUtil(arr, temp,
l, m, K);
cnt += mergeSortUtil(arr, temp,
m + 1, r, K);
// Call the merging function
cnt += merge(arr, temp, l,
m, r, K);
}
return cnt;
}
// Function to print the count of
// required pairs using Merge Sort
static void mergeSort(int arr[], int N, int K)
{
int temp[] = new int[N];
System.out.print(mergeSortUtil(arr, temp, 0, N - 1, K));
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 5, 6, 2, 5 };
int N = arr.length;
int K = 2;
// Function Call
mergeSort(arr, N, K);
}
}
// This code is contributed by AnkThon
蟒蛇3
# Python3 program for the above approach
# Function to merge two sorted arrays
def merge(arr, temp, l, m, r, K) :
# i: index to left subarray
i = l
# j: index to right subarray
j = m + 1
# Stores count of pairs that
# satisfy the given condition
cnt = 0
for l in range(m + 1) :
found = False
# Traverse to check for the
# valid conditions
while (j <= r) :
# If condition satifies
if (arr[i] >= K * arr[j]) :
found = True
else :
break
j += 1
# While a[i] > K*a[j] satisfies
# increase j
# All elements in the right
# side of the left subarray
# also satisies
if (found) :
cnt += j - (m + 1)
j -= 1
# Sort the two given arrays and
# store in the resultant array
k = l
i = l
j = m + 1
while (i <= m and j <= r) :
if (arr[i] <= arr[j]) :
temp[k] = arr[i]
k += 1
i += 1
else :
temp[k] = arr[j]
k += 1
j += 1
# Elements which are left
# in the left subarray
while (i <= m) :
temp[k] = arr[i]
k += 1
i += 1
# Elements which are left
# in the right subarray
while (j <= r) :
temp[k] = arr[j]
k += 1
j += 1
for i in range(l, r + 1) :
arr[i] = temp[i]
# Return the count obtained
return cnt
# Function to partiton array into two halves
def mergeSortUtil(arr, temp, l, r, K) :
cnt = 0
if (l < r) :
# Same as (l + r) / 2, but avoids
# overflow for large l and h
m = (l + r) // 2
# Sort first and second halves
cnt += mergeSortUtil(arr, temp, l, m, K)
cnt += mergeSortUtil(arr, temp, m + 1, r, K)
# Call the merging function
cnt += merge(arr, temp, l, m, r, K)
return cnt
# Function to print the count of
# required pairs using Merge Sort
def mergeSort(arr, N, K) :
temp = [0]*N
print(mergeSortUtil(arr, temp, 0, N - 1, K))
# Driver code
arr = [ 5, 6, 2, 5 ]
N = len(arr)
K = 2
# Function Call
mergeSort(arr, N, K)
# This code is contributed by divyeshrabadiya07.
C#
// C# program for the above approach
using System;
class GFG {
// Function to merge two sorted arrays
static int merge(int[] arr, int[] temp, int l, int m,
int r, int K)
{
// i: index to left subarray
int i = l;
// j: index to right subarray
int j = m + 1;
// Stores count of pairs that
// satisfy the given condition
int cnt = 0;
for (i = l; i <= m; i++) {
bool found = false;
// Traverse to check for the
// valid conditions
while (j <= r) {
// If condition satifies
if (arr[i] >= K * arr[j]) {
found = true;
}
else
break;
j++;
}
// While a[i] > K*a[j] satisfies
// increase j
// All elements in the right
// side of the left subarray
// also satisies
if (found == true) {
cnt += j - (m + 1);
j--;
}
}
// Sort the two given arrays and
// store in the resultant array
int k = l;
i = l;
j = m + 1;
while (i <= m && j <= r)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Elements which are left
// in the left subarray
while (i <= m)
temp[k++] = arr[i++];
// Elements which are left
// in the right subarray
while (j <= r)
temp[k++] = arr[j++];
for (i = l; i <= r; i++)
arr[i] = temp[i];
// Return the count obtained
return cnt;
}
// Function to partiton array into two halves
static int mergeSortUtil(int[] arr, int[] temp, int l,
int r, int K)
{
int cnt = 0;
if (l < r) {
// Same as (l + r) / 2, but avoids
// overflow for large l and h
int m = (l + r) / 2;
// Sort first and second halves
cnt += mergeSortUtil(arr, temp, l, m, K);
cnt += mergeSortUtil(arr, temp, m + 1, r, K);
// Call the merging function
cnt += merge(arr, temp, l, m, r, K);
}
return cnt;
}
// Function to print the count of
// required pairs using Merge Sort
static void mergeSort(int[] arr, int N, int K)
{
int[] temp = new int[N];
Console.WriteLine(
mergeSortUtil(arr, temp, 0, N - 1, K));
}
// Driver code
static public void Main()
{
int[] arr = new int[] { 5, 6, 2, 5 };
int N = arr.Length;
int K = 2;
// Function Call
mergeSort(arr, N, K);
}
}
// This code is contributed by Dharanendra L V
Javascript
2
时间复杂度: O(N*log N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。