📌  相关文章
📜  找到’k’,使其与每个数组元素的模数相同

📅  最后修改于: 2021-04-27 22:53:41             🧑  作者: Mango

给定一个由n个整数组成的数组,我们需要找到所有的’k’使得

arr[0] % k = arr[1] % k = ....... = arr[n-1] % k 

例子:

Input  : arr[] = {6, 38, 34}
Output : 1 2 4
        6%1 = 38%1 = 34%1 = 0
        6%2 = 38%2 = 34%2 = 0
        6%4 = 38%4 = 34%2 = 2

Input  : arr[] = {3, 2}
Output : 1

假设该数组仅包含两个元素a和b(b> a)。因此我们可以写成b = a + d ,其中d是一个正整数,而’k’是一个数字,使得b%k = a%k。

(a + d)%k = a%k
a%k + d%k = a%k 
d%k = 0

现在,我们从上述计算中得出的结论是,“ k”应该是两个数字之间的差的除数。
现在,当我们有一个整数数组时我们要做的是

  1. 找出数组的最大和最小元素之间的差异“ d”
  2. 找出所有’d’的除数
  3. 步骤3:对于每个除数,检查arr [i]%divisor(d)是否相同。如果相同,则将其打印出来。
C++
// C++ implementation of finding all k
// such that arr[i]%k is same for each i
#include
using namespace std;
  
// Prints all k such that arr[i]%k is same for all i
void printEqualModNumbers (int arr[], int n)
{
    // sort the numbers
    sort(arr, arr + n);
  
    // max difference will be the difference between
    // first and last element of sorted array
    int d = arr[n-1] - arr[0];
      
    // Case when all the array elements are same
    if(d==0){
        cout<<"Infinite solution";
        return;
    }
  
    // Find all divisors of d and store in
    // a vector v[]
    vector  v;
    for (int i=1; i*i<=d; i++)
    {
        if (d%i == 0)
        {
            v.push_back(i);
            if (i != d/i)
                v.push_back(d/i);
        }
    }
  
    // check for each v[i] if its modulus with
    // each array element is same or not
    for (int i=0; i


Java
//  Java implementation of finding all k
// such that arr[i]%k is same for each i
  
import java.util.Arrays;
import java.util.Vector;
  
class Test
{
    // Prints all k such that arr[i]%k is same for all i
    static void printEqualModNumbers (int arr[], int n)
    {
        // sort the numbers
        Arrays.sort(arr);
       
        // max difference will be the difference between
        // first and last element of sorted array
        int d = arr[n-1] - arr[0];
        // Case when all the array elements are same
        if(d==0){
            System.out.println("Infinite solution");
            return;
        }
        // Find all divisors of d and store in
        // a vector v[]
        Vector v = new Vector<>();
        for (int i=1; i*i<=d; i++)
        {
            if (d%i == 0)
            {
                v.add(i);
                if (i != d/i)
                    v.add(d/i);
            }
        }
       
        // check for each v[i] if its modulus with
        // each array element is same or not
        for (int i=0; i


Python3
# Python3 implementation of finding all k 
# such that arr[i]%k is same for each i 
  
# Prints all k such that arr[i]%k is 
# same for all i 
def printEqualModNumbers(arr, n):
      
    # sort the numbers 
    arr.sort(); 
      
    # max difference will be the difference 
    # between first and last element of 
    # sorted array 
    d = arr[n - 1] - arr[0]; 
    // Case when all the array elements are same
    if(d==0):
        print("Infinite solution")
        return
      
    # Find all divisors of d and store 
    # in a vector v[] 
    v = [];
    i = 1;
    while (i * i <= d): 
        if (d % i == 0): 
                v.append(i);
                if (i != d / i):
                    v.append(d / i);
        i += 1;
      
    # check for each v[i] if its modulus with 
    # each array element is same or not 
    for i in range(len(v)): 
        temp = arr[0] % v[i]; 
      
        # checking for each array element if 
        # its modulus with k is equal to k or not 
        j = 1; 
        while (j < n): 
            if (arr[j] % v[i] != temp): 
                break;
            j += 1;
  
        # if check is true print v[i] 
        if (j == n): 
            print(v[i], end = " "); 
  
# Driver Code
arr = [38, 6, 34]; 
printEqualModNumbers(arr, len(arr)); 
          
# This code is contributed by mits


C#
// C# implementation of finding all k 
// such that arr[i]%k is same for each i 
using System;
using System.Collections;
class Test 
{ 
    // Prints all k such that arr[i]%k is same for all i 
    static void printEqualModNumbers (int []arr, int n) 
    { 
        // sort the numbers 
        Array.Sort(arr); 
      
        // max difference will be the difference between 
        // first and last element of sorted array 
        int d = arr[n-1] - arr[0]; 
        // Case when all the array elements are same
        if(d==0){
            Console.write("Infinite solution");
            return;
        }
        // Find all divisors of d and store in 
        // a vector v[] 
        ArrayList v = new ArrayList(); 
        for (int i=1; i*i<=d; i++) 
        { 
            if (d%i == 0) 
            { 
                v.Add(i); 
                if (i != d/i) 
                    v.Add(d/i); 
            } 
        } 
      
        // check for each v[i] if its modulus with 
        // each array element is same or not 
        for (int i=0; i


PHP


输出:

1 2 4