给定一个仅包含整数的长度为N的数组,任务是打印数组的特殊数字。如果这个数组中的一个数至少能被数组中的一个其他数整除,那么这个数就被称为特殊数。
例子 :
Input : 1 2 3
Output : 2 3
Explanation : both 2 and 3 are divisible by 1.
Input : 2 3 4 6 8 9
Output : 4 6 8 9
Explanation : 2 and 3 are not divisible by any other element. Rest of the element are divisible by at-least 1 element. 6 is divisible by both 2 and 3, 4 divisible by 2, 8 divisible by 2 and 4 both, 9 divisible by 3.
Input : 3 5 7 11
Output :
Explanation : all elements are relatively prime so no special number.
一个简单的解决方案是遍历所有元素,然后检查每个元素是否可以被任何其他元素整除。此解决方案的时间复杂度为 O(n 2 )
当存在许多值不是很大的元素时,另一种效果更好的解决方案。将所有数组元素存储到哈希中并找出数组中的最大元素,然后到最大元素找出给定数字的倍数,然后如果数组元素的倍数在哈希中,则该数字可被至少一个数组元素整除.为了删除重复值,我们将值存储到集合中,因为如果数组有 2、3 和 6,那么只有 6 可以被数组的至少一个元素整除,2 和 3 都可以整除 6,因此 6 将只存储一次。
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find special numbers
void divisibilityCheck(int arr[], int n)
{
// Storing all array elements in a hash
// and finding maximum element in the array
unordered_set s;
int max_ele = INT_MIN;
for (int i = 0; i < n; i++) {
s.insert(arr[i]);
// Update the maximum element of the array
max_ele = max(max_ele, arr[i]);
}
// Traversing the array elements and storing the array
// multiples that are present in s in res
unordered_set res;
for (int i = 0; i < n; i++) {
// Check for non-zero values only
if (arr[i] != 0) {
// Checking the factors of current element
for (int j = arr[i] * 2; j <= max_ele; j += arr[i]) {
// If current factor is already part
// of the array then store it
if (s.find(j) != s.end())
res.insert(j);
}
}
}
// For non-distinct elmments
// To store the frequency of elements
unordered_map mp;
for (int i = 0; i < n; i++)
mp[arr[i]]++;
unordered_map::iterator it;
vector ans;
for (it = mp.begin(); it != mp.end(); it++) {
// If frequency is at least 2
if (it->second >= 2) {
if (res.find(it->first) == res.end()) {
// If frequency is greater than 1 and
// the number is not divisible by
// any other number
int val = it->second;
// Then we push the element number of
// times it is present in the vector
while (val--)
ans.push_back(it->first);
}
}
// If frequency is greater than 1 and the number
// is divisible by any other number
if (res.find(it->first) != res.end()) {
int val = it->second;
// Then we push the element number of
// times it is present in the vector
while (val--)
ans.push_back(it->first);
}
}
// Print the elements that are divisible by
// at least one other element from the array
for (auto x : ans)
cout << x << " ";
}
// Driver code
int main()
{
int arr[] = { 2, 3, 8, 6, 9, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
divisibilityCheck(arr, n);
return 0;
}
Java
// Java program to find special
// numbers in an array
import java.io.*;
import java.util.*;
class GFG {
// Function to find
// special numbers
static void divisibilityCheck(List arr,
int n)
{
// Storing all array elements
// in a hash and finding maximum
// element in array
List s = new ArrayList();
int max_ele = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
s.add(arr.get(i));
// finding maximum
// element of array
max_ele = Math.max(max_ele,
arr.get(i));
}
// traversing array element and
// storing the array multiples
// that are present in s in res.
LinkedHashSet res = new LinkedHashSet();
for (int i = 0; i < n; i++) {
// Check for non-zero values only
if (arr.get(i) != 0)
// checking the factor
// of current element
for (int j = arr.get(i) * 2;
j <= max_ele;
j += arr.get(i)) {
// if factor is already
// part of array element
// then store it
if (s.contains(j))
res.add(j);
}
}
// displaying elements that
// are divisible by at least
// one other in array
List list = new ArrayList(res);
Collections.reverse(list);
for (Integer temp : list)
System.out.print(temp + " ");
}
// Driver Code
public static void main(String args[])
{
List arr = Arrays.asList(2, 3, 8, 6, 9, 10);
int n = arr.size();
divisibilityCheck(arr, n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
Python3
# Python3 program to find special numbers
# in an array
import math as mt
# Function to find special numbers
def divisibilityCheck(arr, n):
# Storing all array elements in a hash
# and finding maximum element in array
s = dict()
max_ele = -10**9
for i in range(n):
s[arr[i]] = 1
# finding maximum element of array
max_ele = max(max_ele, arr[i])
# traversing array element and storing
# the array multiples that are present
# in s in res.
res = dict()
for i in range(n):
# Check for non-zero values only
if (arr[i] != 0):
# checking the factor of current element
for j in range(arr[i] * 2,
max_ele + 1, arr[i]):
# if factor is already part of
# array element then store it
if (j in s.keys()):
res[j] = 1
# displaying elements that are divisible
# by at least one other in array
for x in res:
print(x, end = " ")
# Driver code
arr = [ 2, 3, 8, 6, 9, 10]
n = len(arr)
divisibilityCheck(arr, n)
# This code is contributed by
# Mohit Kumar 29
C#
// C# program to find special
// numbers in an array
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
// Function to find
// special numbers
static void divisibilityCheck(List arr,
int n)
{
// Storing all array elements
// in a hash and finding maximum
// element in array
List s = new List();
int max_ele = Int32.MinValue;
for (int i = 0; i < n; i++) {
s.Add(arr[i]);
// finding maximum element of array
max_ele = Math.Max(max_ele,
arr[i]);
}
// traversing array element and
// storing the array multiples
// that are present in s in res.
HashSet res = new HashSet();
for (int i = 0; i < n; i++) {
// Check for non-zero values only
if (arr[i] != 0)
// checking the factor
// of current element
for (int j = arr[i] * 2; j <= max_ele;
j += arr[i]) {
// if factor is already part
// of array element then store it
if (s.Contains(j))
res.Add(j);
}
}
// displaying elements that
// are divisible by at least
// one other in array
foreach(int i in res.Reverse())
Console.Write(i + " ");
}
// Driver Code
static void Main()
{
List arr = new List() { 2, 3, 8,
6, 9, 10 };
int n = arr.Count;
divisibilityCheck(arr, n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
Javascript
6 8 9 10
注意:如果我们需要按排序顺序打印结果,我们可以使用 set 代替 unordered_set。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。