给定一个数组arr [],请为每个元素找到最近的元素,以使至少存在一个公共质因数。在输出中,我们需要打印最接近的元素的位置。
例子:
Input: arr[] = {2, 9, 4, 3, 13}
Output: 3 4 1 2 -1
Explanation :
Closest element for 1st element is 3rd.
=>Common prime factor of 1st and 3rd elements
is 2.
Closest element for 2nd element is 4th.
=>Common prime factor of 2nd and 4th elements
is 3.
天真的方法
仅当这两个数字的GCD大于1时,公共质因数才会存在。简单的蛮力是使两个循环在另一个循环内运行,并同时从每个索引到两侧逐个迭代并找到gcd,大于1。只要我们找到答案,就中断循环和打印。如果我们在遍历了两边之后到达了数组的末尾,则只需打印-1。
C++
// C++ program to print nearest element with at least
// one common prime factor.
#include
using namespace std;
void nearestGcd(int arr[], int n)
{
// Loop covers the every element of arr[]
for (int i=0; i0 || k<=n; --j, ++k)
{
if (j>=0 && __gcd(arr[i], arr[j]) > 1)
{
closest = j+1;
break;
}
if (k1)
{
closest = k+1;
break;
}
}
// print position of closest element
cout << closest << " ";
}
}
// Driver code
int main()
{
int arr[] = {2, 9, 4, 3, 13};
int n = sizeof(arr)/sizeof(arr[0]);
nearestGcd(arr, n);
return 0;
}
Java
// Java program to print nearest element
// with at least one common prime factor.
import java.util.*;
import java.lang.*;
class GFG
{
static void nearestGcd(int []arr, int n)
{
// Loop covers the every
// element of arr[]
for (int i = 0; i < n; ++i)
{
int closest = -1;
// Loop that covers from 0 to
// i-1 and i+1 to n-1 indexes
// simultaneously
for (int j = i - 1, k = i + 1;
j > 0 || k <= n; --j, ++k)
{
if (j >= 0 && __gcd(arr[i], arr[j]) > 1)
{
closest = j + 1;
break;
}
if (k < n && __gcd(arr[i], arr[k]) > 1)
{
closest = k + 1;
break;
}
}
// print position of closest element
System.out.print(closest + " ");
}
}
// Recursive function to return
// gcd of a and b
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver Code
public static void main(String args[])
{
int []arr = {2, 9, 4, 3, 13};
int n = arr.length;
nearestGcd(arr, n);
}
}
// This code is contributed
// by Akanksha Rai
Python 3
# Python 3 program to print nearest
# element with at least one common
# prime factor.
import math
def nearestGcd(arr, n):
# Loop covers the every element of arr[]
for i in range(n):
closest = -1
# Loop that covers from 0 to i-1 and
# i+1 to n-1 indexes simultaneously
j = i - 1
k = i + 1
while j > 0 or k <= n:
if (j >= 0 and
math.gcd(arr[i], arr[j]) > 1):
closest = j + 1
break
if (k < n and
math.gcd(arr[i], arr[k]) > 1):
closest = k + 1
break
k += 1
j -= 1
# print position of closest element
print(closest, end = " ")
# Driver code
if __name__=="__main__":
arr = [2, 9, 4, 3, 13]
n = len(arr)
nearestGcd(arr, n)
# This code is contributed by ita_c
C#
// C# program to print nearest element
// with at least one common prime factor.
using System;
class GFG
{
static void nearestGcd(int []arr, int n)
{
// Loop covers the every
// element of arr[]
for (int i = 0; i < n; ++i)
{
int closest = -1;
// Loop that covers from 0 to
// i-1 and i+1 to n-1 indexes
// simultaneously
for (int j = i - 1, k = i + 1;
j > 0 || k <= n; --j, ++k)
{
if (j >= 0 && __gcd(arr[i], arr[j]) > 1)
{
closest = j + 1;
break;
}
if (k < n && __gcd(arr[i], arr[k]) > 1)
{
closest = k + 1;
break;
}
}
// print position of closest element
Console.Write(closest + " ");
}
}
// Recursive function to return
// gcd of a and b
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
public static void Main()
{
int []arr = {2, 9, 4, 3, 13};
int n = arr.Length;
nearestGcd(arr, n);
}
}
// This code is contributed
// by 29AjayKumar
PHP
0 || $k <= $n; --$j, ++$k)
{
if ($j >= 0 && __gcd($arr[$i],
$arr[$j]) > 1)
{
$closest = $j + 1;
break;
}
if ($k < $n && __gcd($arr[$i],
$arr[$k]) > 1)
{
$closest = $k + 1;
break;
}
}
// print position of closest element
echo $closest . " ";
}
}
// Recursive function to return
// gcd of a and b
function __gcd($a, $b)
{
if ($b == 0)
return $a;
return __gcd($b, $a % $b);
}
// Driver Code
$arr = array(2, 9, 4, 3, 13);
$n = sizeof($arr);
nearestGcd($arr, $n);
// This code is contributed
// by Akanksha Rai
?>
C++
// C++ program to print nearest element with at least
// one common prime factor.
#include
using namespace std;
const int MAX = 100001;
const int INF = INT_MAX;
int primedivisor[MAX], dist[MAX], pos[MAX], divInd[MAX];
vector divisors[MAX];
// Pre-computation of smallest prime divisor
// of all numbers
void sieveOfEratosthenes()
{
for (int i=2; i*i < MAX; ++i)
{
if (!primedivisor[i])
for (int j = i*i; j < MAX; j += i)
primedivisor[j] = i;
}
// Prime number will have same divisor
for (int i = 1; i < MAX; ++i)
if (!primedivisor[i])
primedivisor[i] = i;
}
// Function to calculate all divisors of
// input array
void findDivisors(int arr[], int n)
{
for (int i=0; i 1)
{
int div = primedivisor[num];
divisors[i].push_back(div);
while (num % div == 0)
num /= div;
}
}
}
void nearestGCD(int arr[], int n)
{
// Pre-compute all the divisors of array
// element by using prime factors
findDivisors(arr, n);
// Traverse all elements,
for (int i=0; i abs(ind-i))
{
// Set the min distance of current
// index 'i' to nearest one
dist[i] = abs(ind-i);
// Add 1 as indexing starts from 0
pos[i] = ind + 1;
}
if (dist[ind] > abs(ind-i))
{
// Set the min distance of found index 'ind'
dist[ind] = abs(ind-i);
// Add 1 as indexing starts from 0
pos[ind] = i + 1;
}
}
}
}
}
// Driver code
int main()
{
// Simple sieve to find smallest prime
// divisor of number from 2 to MAX
sieveOfEratosthenes();
int arr[] = {2, 9, 4, 3, 13};
int n = sizeof(arr)/sizeof(arr[0]);
// function to calculate nearest distance
// of every array elements
nearestGCD(arr, n);
// Print the nearest distance having GDC>1
for (int i=0; i
Java
// Java program to print nearest element with at least
// one common prime factor.
import java.io.*;
import java.util.*;
class GFG
{
static int MAX = 100001;
static int INF = Integer.MAX_VALUE;
static int[] primedivisor = new int [MAX];
static int[] dist = new int [MAX];
static int[] pos = new int [MAX];
static int[] divInd = new int [MAX];
static ArrayList> divisors =
new ArrayList>();
// Pre-computation of smallest prime divisor
// of all numbers
static void sieveOfEratosthenes()
{
for (int i = 2; i * i < MAX; ++i)
{
if (primedivisor[i] == 0)
{
for (int j = i * i; j < MAX; j += i)
{
primedivisor[j] = i;
}
}
}
// Prime number will have same divisor
for (int i = 1; i < MAX; ++i)
{
if (primedivisor[i] == 0)
{
primedivisor[i] = i;
}
}
}
// Function to calculate all divisors of
// input array
static void findDivisors(int arr[], int n)
{
for (int i=0; i 1)
{
int div = primedivisor[num];
divisors.get(i).add(div);
while (num % div == 0)
{
num /= div;
}
}
}
}
static void nearestGCD(int arr[], int n)
{
// Pre-compute all the divisors of array
// element by using prime factors
findDivisors(arr, n);
// Traverse all elements,
for (int i = 0; i < n; ++i)
{
// For every divisor of current element,
// find closest element.
for(int div : divisors.get(i))
{
// Visit divisor if not visited
if (divInd[div] == -1)
{
divInd[div] = i;
}
else
{
// Fetch the index of visited divisor
int ind = divInd[div];
// Update the divisor index to current index
divInd[div] = i;
// Set the minimum distance
if (dist[i] > Math.abs(ind-i))
{
// Set the min distance of current
// index 'i' to nearest one
dist[i] = Math.abs(ind-i);
// Add 1 as indexing starts from 0
pos[i] = ind + 1;
}
if (dist[ind] > Math.abs(ind-i))
{
// Set the min distance of found index 'ind'
dist[ind] = Math.abs(ind-i);
// Add 1 as indexing starts from 0
pos[ind] = i + 1;
}
}
}
}
}
// Driver code
public static void main (String[] args)
{
for(int i = 0; i < MAX; i++)
{
divisors.add(new ArrayList());
}
// Simple sieve to find smallest prime
// divisor of number from 2 to MAX
sieveOfEratosthenes();
int arr[] = {2, 9, 4, 3, 13};
int n = arr.length;
// function to calculate nearest distance
// of every array elements
nearestGCD(arr, n);
// Print the nearest distance having GDC>1
for (int i = 0; i < n; ++i)
{
System.out.print(pos[i]+" ");
}
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 program to print nearest element with at least
# one common prime factor.
MAX = 100001
INF = 10**9
primedivisor = [0 for i in range(MAX)]
dist = [0 for i in range(MAX)]
pos = [0 for i in range(MAX)]
divInd = [0 for i in range(MAX)]
divisors = [[] for i in range(MAX)]
# Pre-computation of smallest prime divisor
# of all numbers
def sieveOfEratosthenes():
for i in range(2,MAX):
if i * i > MAX:
break
if (primedivisor[i] == 0):
for j in range(2 * i, MAX, i):
primedivisor[j] = i
# Prime number will have same divisor
for i in range(1, MAX):
if (primedivisor[i] == 0):
primedivisor[i] = i
# Function to calculate all divisors of
# input array
def findDivisors(arr, n):
for i in range(MAX):
pos[i] = divInd[i] = -1
dist[i] = 10**9
for i in range(n):
num = arr[i]
while (num > 1):
div = primedivisor[num]
divisors[i].append(div)
while (num % div == 0):
num //= div
def nearestGCD(arr, n):
# Pre-compute all the divisors of array
# element by using prime factors
findDivisors(arr, n)
# Traverse all elements,
for i in range(n):
# For every divisor of current element,
# find closest element.
for div in divisors[i]:
# Visit divisor if not visited
if (divInd[div] == -1):
divInd[div] = i
else:
# Fetch the index of visited divisor
ind = divInd[div]
# Update the divisor index to current index
divInd[div] = i
# Set the minimum distance
if (dist[i] > abs(ind-i)):
# Set the min distance of current
# index 'i' to nearest one
dist[i] = abs(ind-i)
# Add 1 as indexing starts from 0
pos[i] = ind + 1
if (dist[ind] > abs(ind-i)):
# Set the min distance of found index 'ind'
dist[ind] = abs(ind-i)
# Add 1 as indexing starts from 0
pos[ind] = i + 1
# Driver code
# Simple sieve to find smallest prime
# divisor of number from 2 to MAX
sieveOfEratosthenes()
arr =[2, 9, 4, 3, 13]
n = len(arr)
# function to calculate nearest distance
# of every array elements
nearestGCD(arr, n)
# Print the nearest distance having GDC>1
for i in range(n):
print(pos[i],end=" ")
# This code is contributed by mohit kumar 29
C#
// C# program to print nearest element with at least
// one common prime factor.
using System;
using System.Collections.Generic;
public class GFG{
static int MAX = 100001;
static int INF = Int32.MaxValue;
static int[] primedivisor = new int [MAX];
static int[] dist = new int [MAX];
static int[] pos = new int [MAX];
static int[] divInd = new int [MAX];
static List> divisors = new List>();
// Pre-computation of smallest prime divisor
// of all numbers
static void sieveOfEratosthenes()
{
for (int i = 2; i * i < MAX; ++i)
{
if (primedivisor[i] == 0)
{
for (int j = i * i; j < MAX; j += i)
{
primedivisor[j] = i;
}
}
}
// Prime number will have same divisor
for (int i = 1; i < MAX; ++i)
{
if (primedivisor[i] == 0)
{
primedivisor[i] = i;
}
}
}
// Function to calculate all divisors of
// input array
static void findDivisors(int[] arr, int n)
{
for (int i=0; i 1)
{
int div = primedivisor[num];
divisors[i].Add(div);
while (num % div == 0)
{
num /= div;
}
}
}
}
static void nearestGCD(int[] arr, int n)
{
// Pre-compute all the divisors of array
// element by using prime factors
findDivisors(arr, n);
// Traverse all elements,
for (int i = 0; i < n; ++i)
{
// For every divisor of current element,
// find closest element.
foreach(int div in divisors[i])
{
// Visit divisor if not visited
if (divInd[div] == -1)
{
divInd[div] = i;
}
else
{
// Fetch the index of visited divisor
int ind = divInd[div];
// Update the divisor index to current index
divInd[div] = i;
// Set the minimum distance
if (dist[i] > Math.Abs(ind-i))
{
// Set the min distance of current
// index 'i' to nearest one
dist[i] = Math.Abs(ind-i);
// Add 1 as indexing starts from 0
pos[i] = ind + 1;
}
if (dist[ind] > Math.Abs(ind-i))
{
// Set the min distance of found index 'ind'
dist[ind] = Math.Abs(ind-i);
// Add 1 as indexing starts from 0
pos[ind] = i + 1;
}
}
}
}
}
// Driver code
static public void Main ()
{
for(int i = 0; i < MAX; i++)
{
divisors.Add(new List());
}
// Simple sieve to find smallest prime
// divisor of number from 2 to MAX
sieveOfEratosthenes();
int[] arr = {2, 9, 4, 3, 13};
int n = arr.Length;
// function to calculate nearest distance
// of every array elements
nearestGCD(arr, n);
// Print the nearest distance having GDC>1
for (int i = 0; i < n; ++i)
{
Console.Write(pos[i]+" ");
}
}
}
// This code is contributed by rag2127
Output: 3 4 1 2 -1
时间复杂度: O(n 2 )
辅助空间: O(1)
高效方法
我们找到所有数组元素的素因。为了快速找到主要因素,我们使用Eratosthenes筛。对于每个元素,我们都考虑所有主要因素,并跟踪与公共因素最接近的元素。
C++
// C++ program to print nearest element with at least
// one common prime factor.
#include
using namespace std;
const int MAX = 100001;
const int INF = INT_MAX;
int primedivisor[MAX], dist[MAX], pos[MAX], divInd[MAX];
vector divisors[MAX];
// Pre-computation of smallest prime divisor
// of all numbers
void sieveOfEratosthenes()
{
for (int i=2; i*i < MAX; ++i)
{
if (!primedivisor[i])
for (int j = i*i; j < MAX; j += i)
primedivisor[j] = i;
}
// Prime number will have same divisor
for (int i = 1; i < MAX; ++i)
if (!primedivisor[i])
primedivisor[i] = i;
}
// Function to calculate all divisors of
// input array
void findDivisors(int arr[], int n)
{
for (int i=0; i 1)
{
int div = primedivisor[num];
divisors[i].push_back(div);
while (num % div == 0)
num /= div;
}
}
}
void nearestGCD(int arr[], int n)
{
// Pre-compute all the divisors of array
// element by using prime factors
findDivisors(arr, n);
// Traverse all elements,
for (int i=0; i abs(ind-i))
{
// Set the min distance of current
// index 'i' to nearest one
dist[i] = abs(ind-i);
// Add 1 as indexing starts from 0
pos[i] = ind + 1;
}
if (dist[ind] > abs(ind-i))
{
// Set the min distance of found index 'ind'
dist[ind] = abs(ind-i);
// Add 1 as indexing starts from 0
pos[ind] = i + 1;
}
}
}
}
}
// Driver code
int main()
{
// Simple sieve to find smallest prime
// divisor of number from 2 to MAX
sieveOfEratosthenes();
int arr[] = {2, 9, 4, 3, 13};
int n = sizeof(arr)/sizeof(arr[0]);
// function to calculate nearest distance
// of every array elements
nearestGCD(arr, n);
// Print the nearest distance having GDC>1
for (int i=0; i
Java
// Java program to print nearest element with at least
// one common prime factor.
import java.io.*;
import java.util.*;
class GFG
{
static int MAX = 100001;
static int INF = Integer.MAX_VALUE;
static int[] primedivisor = new int [MAX];
static int[] dist = new int [MAX];
static int[] pos = new int [MAX];
static int[] divInd = new int [MAX];
static ArrayList> divisors =
new ArrayList>();
// Pre-computation of smallest prime divisor
// of all numbers
static void sieveOfEratosthenes()
{
for (int i = 2; i * i < MAX; ++i)
{
if (primedivisor[i] == 0)
{
for (int j = i * i; j < MAX; j += i)
{
primedivisor[j] = i;
}
}
}
// Prime number will have same divisor
for (int i = 1; i < MAX; ++i)
{
if (primedivisor[i] == 0)
{
primedivisor[i] = i;
}
}
}
// Function to calculate all divisors of
// input array
static void findDivisors(int arr[], int n)
{
for (int i=0; i 1)
{
int div = primedivisor[num];
divisors.get(i).add(div);
while (num % div == 0)
{
num /= div;
}
}
}
}
static void nearestGCD(int arr[], int n)
{
// Pre-compute all the divisors of array
// element by using prime factors
findDivisors(arr, n);
// Traverse all elements,
for (int i = 0; i < n; ++i)
{
// For every divisor of current element,
// find closest element.
for(int div : divisors.get(i))
{
// Visit divisor if not visited
if (divInd[div] == -1)
{
divInd[div] = i;
}
else
{
// Fetch the index of visited divisor
int ind = divInd[div];
// Update the divisor index to current index
divInd[div] = i;
// Set the minimum distance
if (dist[i] > Math.abs(ind-i))
{
// Set the min distance of current
// index 'i' to nearest one
dist[i] = Math.abs(ind-i);
// Add 1 as indexing starts from 0
pos[i] = ind + 1;
}
if (dist[ind] > Math.abs(ind-i))
{
// Set the min distance of found index 'ind'
dist[ind] = Math.abs(ind-i);
// Add 1 as indexing starts from 0
pos[ind] = i + 1;
}
}
}
}
}
// Driver code
public static void main (String[] args)
{
for(int i = 0; i < MAX; i++)
{
divisors.add(new ArrayList());
}
// Simple sieve to find smallest prime
// divisor of number from 2 to MAX
sieveOfEratosthenes();
int arr[] = {2, 9, 4, 3, 13};
int n = arr.length;
// function to calculate nearest distance
// of every array elements
nearestGCD(arr, n);
// Print the nearest distance having GDC>1
for (int i = 0; i < n; ++i)
{
System.out.print(pos[i]+" ");
}
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 program to print nearest element with at least
# one common prime factor.
MAX = 100001
INF = 10**9
primedivisor = [0 for i in range(MAX)]
dist = [0 for i in range(MAX)]
pos = [0 for i in range(MAX)]
divInd = [0 for i in range(MAX)]
divisors = [[] for i in range(MAX)]
# Pre-computation of smallest prime divisor
# of all numbers
def sieveOfEratosthenes():
for i in range(2,MAX):
if i * i > MAX:
break
if (primedivisor[i] == 0):
for j in range(2 * i, MAX, i):
primedivisor[j] = i
# Prime number will have same divisor
for i in range(1, MAX):
if (primedivisor[i] == 0):
primedivisor[i] = i
# Function to calculate all divisors of
# input array
def findDivisors(arr, n):
for i in range(MAX):
pos[i] = divInd[i] = -1
dist[i] = 10**9
for i in range(n):
num = arr[i]
while (num > 1):
div = primedivisor[num]
divisors[i].append(div)
while (num % div == 0):
num //= div
def nearestGCD(arr, n):
# Pre-compute all the divisors of array
# element by using prime factors
findDivisors(arr, n)
# Traverse all elements,
for i in range(n):
# For every divisor of current element,
# find closest element.
for div in divisors[i]:
# Visit divisor if not visited
if (divInd[div] == -1):
divInd[div] = i
else:
# Fetch the index of visited divisor
ind = divInd[div]
# Update the divisor index to current index
divInd[div] = i
# Set the minimum distance
if (dist[i] > abs(ind-i)):
# Set the min distance of current
# index 'i' to nearest one
dist[i] = abs(ind-i)
# Add 1 as indexing starts from 0
pos[i] = ind + 1
if (dist[ind] > abs(ind-i)):
# Set the min distance of found index 'ind'
dist[ind] = abs(ind-i)
# Add 1 as indexing starts from 0
pos[ind] = i + 1
# Driver code
# Simple sieve to find smallest prime
# divisor of number from 2 to MAX
sieveOfEratosthenes()
arr =[2, 9, 4, 3, 13]
n = len(arr)
# function to calculate nearest distance
# of every array elements
nearestGCD(arr, n)
# Print the nearest distance having GDC>1
for i in range(n):
print(pos[i],end=" ")
# This code is contributed by mohit kumar 29
C#
// C# program to print nearest element with at least
// one common prime factor.
using System;
using System.Collections.Generic;
public class GFG{
static int MAX = 100001;
static int INF = Int32.MaxValue;
static int[] primedivisor = new int [MAX];
static int[] dist = new int [MAX];
static int[] pos = new int [MAX];
static int[] divInd = new int [MAX];
static List> divisors = new List>();
// Pre-computation of smallest prime divisor
// of all numbers
static void sieveOfEratosthenes()
{
for (int i = 2; i * i < MAX; ++i)
{
if (primedivisor[i] == 0)
{
for (int j = i * i; j < MAX; j += i)
{
primedivisor[j] = i;
}
}
}
// Prime number will have same divisor
for (int i = 1; i < MAX; ++i)
{
if (primedivisor[i] == 0)
{
primedivisor[i] = i;
}
}
}
// Function to calculate all divisors of
// input array
static void findDivisors(int[] arr, int n)
{
for (int i=0; i 1)
{
int div = primedivisor[num];
divisors[i].Add(div);
while (num % div == 0)
{
num /= div;
}
}
}
}
static void nearestGCD(int[] arr, int n)
{
// Pre-compute all the divisors of array
// element by using prime factors
findDivisors(arr, n);
// Traverse all elements,
for (int i = 0; i < n; ++i)
{
// For every divisor of current element,
// find closest element.
foreach(int div in divisors[i])
{
// Visit divisor if not visited
if (divInd[div] == -1)
{
divInd[div] = i;
}
else
{
// Fetch the index of visited divisor
int ind = divInd[div];
// Update the divisor index to current index
divInd[div] = i;
// Set the minimum distance
if (dist[i] > Math.Abs(ind-i))
{
// Set the min distance of current
// index 'i' to nearest one
dist[i] = Math.Abs(ind-i);
// Add 1 as indexing starts from 0
pos[i] = ind + 1;
}
if (dist[ind] > Math.Abs(ind-i))
{
// Set the min distance of found index 'ind'
dist[ind] = Math.Abs(ind-i);
// Add 1 as indexing starts from 0
pos[ind] = i + 1;
}
}
}
}
}
// Driver code
static public void Main ()
{
for(int i = 0; i < MAX; i++)
{
divisors.Add(new List());
}
// Simple sieve to find smallest prime
// divisor of number from 2 to MAX
sieveOfEratosthenes();
int[] arr = {2, 9, 4, 3, 13};
int n = arr.Length;
// function to calculate nearest distance
// of every array elements
nearestGCD(arr, n);
// Print the nearest distance having GDC>1
for (int i = 0; i < n; ++i)
{
Console.Write(pos[i]+" ");
}
}
}
// This code is contributed by rag2127
Output:
3 4 1 2 -1