Unable to understand the problem.
by ajiten from LinuxQuestions.org on (#6F0JE)
Given an array A consisting of N positive integers.
Find the smallest positive integer, d, s.t. there exists no pair of integers (i,j)(1<=i<j<= N) where abs(A_i - A_j) is divisible by d.
----------------------------------------------------------
Doubt #1: Is the above problem linked to finding the gcd of all possible differences among the array elements, assuming all are unique? But, not clear how the problem is linked to finding gcd.
If yes, then given that there two indices, i, j with i<j; need to run in outer loop with j = 1 to N-1(last array element's index); while i runs from 0 to N-2.
And, initially choose the smallest element in the array as the value of d; but not clear how to get the correct value of d?
Also, given N elements, and assuming all unique, the number of differences can be:
Nth - 1st, Nth - (N-1)th, ..., Nth - 1st;
(N-1)th - 1st, (N-1)th - 2nd, ..., (N-1)th - (N-2)th;
(N-2)th - 1st, (N-2)th - 2nd, (N-2)th - 3rd, (N-2)th - (N-3)th;
...
2nd - 1st.
i.e., a total of N(N-1)/2 differences.
-------------------
Doubt #2: Can we directly pass the array of integers to the main()? If yes, how; as could see only the second input the main() as an array of chars as char ** argv.
---------------------
Doubt #3: My program finds gcd, hence does not use those N(N-1)/2 differences.
The program for the same is
Code:#include <stdio.h>
int gcd(int a, int b){
if (b==0)
return a;
}
int find_smallest_d(int *arr, int N){
int smallest_d = arr[0];
for (int i=1; i<N; i++){
smallest_d = gcd(smallest_d, arr[i]);
}
}
int main(){
int N, *arr;
printf("Enter the size of array");
scanf("%d", &N);
printf("Enter the %d elements of the array of integers", N);
for (int i=0; i<N; i++)
scanf("%d", arr[i]);
int smallest_d = find_smallest_d(arr, N);
printf("%d", smallest_d);
}The output gives SIGSEGV, segmentation fault; which is unclear as how to remove it.
Request approach using gdb/ddd tool.
Attached Thumbnails
Find the smallest positive integer, d, s.t. there exists no pair of integers (i,j)(1<=i<j<= N) where abs(A_i - A_j) is divisible by d.
----------------------------------------------------------
Doubt #1: Is the above problem linked to finding the gcd of all possible differences among the array elements, assuming all are unique? But, not clear how the problem is linked to finding gcd.
If yes, then given that there two indices, i, j with i<j; need to run in outer loop with j = 1 to N-1(last array element's index); while i runs from 0 to N-2.
And, initially choose the smallest element in the array as the value of d; but not clear how to get the correct value of d?
Also, given N elements, and assuming all unique, the number of differences can be:
Nth - 1st, Nth - (N-1)th, ..., Nth - 1st;
(N-1)th - 1st, (N-1)th - 2nd, ..., (N-1)th - (N-2)th;
(N-2)th - 1st, (N-2)th - 2nd, (N-2)th - 3rd, (N-2)th - (N-3)th;
...
2nd - 1st.
i.e., a total of N(N-1)/2 differences.
-------------------
Doubt #2: Can we directly pass the array of integers to the main()? If yes, how; as could see only the second input the main() as an array of chars as char ** argv.
---------------------
Doubt #3: My program finds gcd, hence does not use those N(N-1)/2 differences.
The program for the same is
Code:#include <stdio.h>
int gcd(int a, int b){
if (b==0)
return a;
}
int find_smallest_d(int *arr, int N){
int smallest_d = arr[0];
for (int i=1; i<N; i++){
smallest_d = gcd(smallest_d, arr[i]);
}
}
int main(){
int N, *arr;
printf("Enter the size of array");
scanf("%d", &N);
printf("Enter the %d elements of the array of integers", N);
for (int i=0; i<N; i++)
scanf("%d", arr[i]);
int smallest_d = find_smallest_d(arr, N);
printf("%d", smallest_d);
}The output gives SIGSEGV, segmentation fault; which is unclear as how to remove it.
Request approach using gdb/ddd tool.
Attached Thumbnails