Given an array of length N that contains only integers, the task is to print the special numbers of array. A number in this array is called Special number if it is divisible by at least one other number in the array.
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
Explanation : all elements are relatively prime so no special number.
A simple solution is to traverse through all elements, then check for every element if it is divisible by any other. Time complexity of this solution is O(n2)
Another solution that works better when there are many elements with not very big values. Store all array elements into hash and find out the max element in array then up-to max element find out the multiples of a given number then if multiple of array element is in hash then that number is divisible by at-least one element of array .To remove duplicate values we store the value into set because if array has 2, 3 and 6 then only 6 is divisible by at-least one element of array, both 2 and 3 divide 6 so 6 will be stored only one time.
6 8 9 10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of elements not divisible by any other elements of Array
- Length of array pair formed where one contains all distinct elements and other all same elements
- Find the smallest subarray having atleast one duplicate
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Minimum value that divides one number and divisible by other
- Divide every element of one array by other array elements
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Count elements that are divisible by at-least one element in another array
- Find original array from encrypted array (An array of sums of other elements)
- Count pairs in an array such that frequency of one is at least value of other
- Closest pair in an Array such that one number is multiple of the other
- Count array elements that divide the sum of all other elements
- Find the number of points that have atleast 1 point above, below, left or right of it
- Maximum average of a subarray of size of atleast X and atmost Y
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Count subarrays of atleast size 3 forming a Geometric Progression (GP)
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.