检查给定的数组是否可以重新排列,使得均值等于中值
给定排序的浮点数组arr[] 。检查是否可以重新排列arr[]使其均值等于中值。
例子:
Input: arr[] = {1.0, 3.0, 6.0, 9.0, 12.0, 32.0}
Output: Yes
Explanation: The mean of given array is (1.0 + 3.0 + 6.0 + 9.0 + 12.0 + 32.0) / 6 = 10.5.
Rearranging given array as {1.0, 3.0, 9.0, 12.0, 6.0, 32.0}, here median is (9.0 + 12.0) / 2 = 10.5
Input: arr[] = {8.0, 13.0, 15.0}
Output: No
方法:给定的问题可以通过使用二分搜索和两个指针方法来解决。如果arr[]的大小是奇数,则意味着中位数是可以使用二分搜索来搜索的单个元素。如果数组大小是偶数,则可以使用两个指针方法,因为中位数将由两个元素组成。请按照以下步骤解决给定的问题。
- 初始化一个变量说,意思是存储arr[]的平均值。
- 检查arr[]的大小是偶数还是奇数。
- 如果arr[]的大小是偶数
- 使用两个指针方法搜索平均值 = mean的两个元素。
- 将两个指针初始化为i=0, j=n-1 。
- 执行两个指针方法来搜索arr[]中的中位数。
- 如果arr[]的大小是奇数
- 应用二分查找来查找arr[]中是否存在均值。
- 如果arr[]的大小是偶数
- 如果找到平均值,则返回Yes ,否则返回No 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return true or false if
// size of array is odd
bool binarySearch(float arr[], int size, float key)
{
int low = 0, high = size - 1, mid;
while (low <= high) {
// To prevent overflow
mid = low + (high - low) / 2;
// If element is greater than mid, then
// it can only be present in right subarray
if (key > arr[mid])
low = mid + 1;
// If element is smaller than mid, then
// it can only be present in left subarray
else if (key < arr[mid])
high = mid - 1;
// Else the element is present at the middle
// then return 1
else
return 1;
}
// when element is not present
// in array then return 0
return 0;
}
// Function to return true or false
// if size of array is even
bool twoPointers(float arr[], int N, float mean)
{
int i = 0, j = N - 1;
while (i < j) {
// Calculating Candidate Median
float temp = (arr[i] + arr[j]) / 2;
// If Candidate Median if greater
// than Mean then decrement j
if (temp > mean)
j--;
// If Candidate Median if less
// than Mean then increment i
else if (temp < mean)
i++;
// If Candidate Median if equal
// to Mean then return 1
else
return 1;
}
// when No candidate found for mean
return 0;
}
// Function to return true if Mean
// can be equal to any candidate
// median otherwise return false
bool checkArray(float arr[], int N)
{
// Calculating Mean
float sum = 0;
for (int i = 0; i < N; i++)
sum += arr[i];
float mean = sum / N;
// If N is Odd
if (N & 1)
return binarySearch(arr, N, mean);
// If N is even
else
return twoPointers(arr, N, mean);
}
// Driver Code
int main()
{
float arr[] = { 1.0, 3.0, 6.0, 9.0, 12.0, 32.0 };
int N = sizeof(arr) / sizeof(arr[0]);
if (checkArray(arr, N))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
import java.io.*;
public class GFG{
// Function to return true or false if
// size of array is odd
static boolean binarySearch(float []arr, int size, float key)
{
int low = 0, high = size - 1, mid;
while (low <= high) {
// To prevent overflow
mid = low + (high - low) / 2;
// If element is greater than mid, then
// it can only be present in right subarray
if (key > arr[mid])
low = mid + 1;
// If element is smaller than mid, then
// it can only be present in left subarray
else if (key < arr[mid])
high = mid - 1;
// Else the element is present at the middle
// then return 1
else
return true;
}
// when element is not present
// in array then return 0
return false;
}
// Function to return true or false
// if size of array is even
static boolean twoPointers(float []arr, int N, float mean)
{
int i = 0, j = N - 1;
while (i < j) {
// Calculating Candidate Median
float temp = (arr[i] + arr[j]) / 2;
// If Candidate Median if greater
// than Mean then decrement j
if (temp > mean)
j--;
// If Candidate Median if less
// than Mean then increment i
else if (temp < mean)
i++;
// If Candidate Median if equal
// to Mean then return 1
else
return true;
}
// when No candidate found for mean
return false;
}
// Function to return true if Mean
// can be equal to any candidate
// median otherwise return false
static boolean checkArray(float []arr, int N)
{
// Calculating Mean
float sum = 0;
for (int i = 0; i < N; i++)
sum += arr[i];
float mean = sum / N;
// If N is Odd
if ((N & 1)!=0)
return binarySearch(arr, N, mean);
// If N is even
else
return twoPointers(arr, N, mean);
}
// Driver Code
public static void main(String []args)
{
float []arr = { 1.0f, 3.0f, 6.0f, 9.0f, 12.0f, 32.0f };
int N = arr.length;
if (checkArray(arr, N))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by AnkThon.
Python3
# python program for the above approach
# Function to return true or false if
# size of array is odd
def binarySearch(arr, size, key):
low = 0
high = size - 1
while (low <= high):
# To prevent overflow
mid = low + (high - low) // 2
# If element is greater than mid, then
# it can only be present in right subarray
if (key > arr[mid]):
low = mid + 1
# If element is smaller than mid, then
# it can only be present in left subarray
elif (key < arr[mid]):
high = mid - 1
# Else the element is present at the middle
# then return 1
else:
return 1
# when element is not present
# in array then return 0
return 0
# Function to return true or false
# if size of array is even
def twoPointers(arr, N, mean):
i = 0
j = N - 1
while (i < j):
# Calculating Candidate Median
temp = (arr[i] + arr[j]) / 2
# If Candidate Median if greater
# than Mean then decrement j
if (temp > mean):
j = j - 1
# If Candidate Median if less
# than Mean then increment i
elif (temp < mean):
i = i + 1
# If Candidate Median if equal
# to Mean then return 1
else:
return 1
# when No candidate found for mean
return 0
# Function to return true if Mean
# can be equal to any candidate
# median otherwise return false
def checkArray(arr, N):
# Calculating Mean
sum = 0
for i in range(0, N):
sum += arr[i]
mean = sum / N
# If N is Odd
if (N & 1):
return binarySearch(arr, N, mean)
# If N is even
else:
return twoPointers(arr, N, mean)
# Driver Code
if __name__ == "__main__":
arr = [1.0, 3.0, 6.0, 9.0, 12.0, 32.0]
N = len(arr)
if (checkArray(arr, N)):
print("Yes")
else:
print("No")
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return true or false if
// size of array is odd
static bool binarySearch(float []arr, int size, float key)
{
int low = 0, high = size - 1, mid;
while (low <= high) {
// To prevent overflow
mid = low + (high - low) / 2;
// If element is greater than mid, then
// it can only be present in right subarray
if (key > arr[mid])
low = mid + 1;
// If element is smaller than mid, then
// it can only be present in left subarray
else if (key < arr[mid])
high = mid - 1;
// Else the element is present at the middle
// then return 1
else
return true;
}
// when element is not present
// in array then return 0
return false;
}
// Function to return true or false
// if size of array is even
static bool twoPointers(float []arr, int N, float mean)
{
int i = 0, j = N - 1;
while (i < j) {
// Calculating Candidate Median
float temp = (arr[i] + arr[j]) / 2;
// If Candidate Median if greater
// than Mean then decrement j
if (temp > mean)
j--;
// If Candidate Median if less
// than Mean then increment i
else if (temp < mean)
i++;
// If Candidate Median if equal
// to Mean then return 1
else
return true;
}
// when No candidate found for mean
return false;
}
// Function to return true if Mean
// can be equal to any candidate
// median otherwise return false
static bool checkArray(float []arr, int N)
{
// Calculating Mean
float sum = 0;
for (int i = 0; i < N; i++)
sum += arr[i];
float mean = sum / N;
// If N is Odd
if ((N & 1)!=0)
return binarySearch(arr, N, mean);
// If N is even
else
return twoPointers(arr, N, mean);
}
// Driver Code
public static void Main()
{
float []arr = { 1.0f, 3.0f, 6.0f, 9.0f, 12.0f, 32.0f };
int N = arr.Length;
if (checkArray(arr, N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
Yes
时间复杂度:O(N),当 N 为偶数时。
O(logN),当 N 为奇数时。
辅助空间:O(1)。