Check if Array is Consecutive Integers | Algorithms
Given a array of unsorted numbers, check if all the numbers in the array are consecutive numbers.
Examples:
Related:
http://algorithms.tutorialhorizon.com/find-the-element-which-appears-maximum-number-of-times-in-the-array/
public void maxRepeatingElementUsingSorting(int [] arrA){
if(arrA.length<1){
System.out.println("Inavlid Array");
return;
}
Arrays.sort(arrA);
int count=1;
int maxCount=1;
int currentElement = arrA[0];
int maxCountElement =arrA[0];
for (int i = 1; i <arrA.length ; i++) {
if(currentElement==arrA[i]){
count++;
}else{
if(count>maxCount){
maxCount = count;
maxCountElement = currentElement;
}
currentElement = arrA[i];
count = 1;
}
}
System.out.println("Element repeating maximum no of times: " + maxCountElement + ", maximum count: " + maxCount);
}
int size = arrA.length;
int maxCount=0;
int maxIndex=0;
for (int i = 0; i <size ; i++) {
//get the index to be updated
int index = arrA[i]% size;
arrA[index] = arrA[index] + size;
}
for (int i = 0; i <size ; i++) {
if(arrA[i]/size>maxCount){
maxCount=arrA[i]/size;
maxIndex=i;
}
}
System.out.println("Element repeating maximum no of times: " + arrA[maxIndex]%size + ", maximum count: " + maxCount);
}
http://algorithms.tutorialhorizon.com/find-duplicates-in-an-given-array-in-on-time-and-o1-extra-space/
for (int i = 0; i < arrA.length; i++) {
//check if element is negative, if yes the we have found the duplicate
if (arrA[Math.abs(arrA[i])] < 0) {
System.out.println("Array has duplicates : " + Math.abs(arrA[i]));
} else {
arrA[Math.abs(arrA[i])] = arrA[Math.abs(arrA[i])] * -1;
}
}
}
Read full article from Check if Array is Consecutive Integers | Algorithms
Given a array of unsorted numbers, check if all the numbers in the array are consecutive numbers.
Examples:
int [] arrA = {21,24,22,26,23,25}; - True
(All the integers are consecutive from 21 to 26)
int [] arrB = {11,10,12,14,13}; - True (All the integers are consecutive from 10 to 14) int [] arrC = {11,10,14,13}; - False (Integers are not consecutive, 12 is missing)
Better Approach: Time Complexity — O(n).
- Find the Maximum and minimum elements in array (Say the array is arrA)
- Check if array length = max-min+1
- Subtract the min from every element of the array.
- Check if array doesn’t have duplicates
for Step 4
a) If array contains negative elements
- Create an aux array and put 0 at every position
- Navigate the main array and update the aux array as aux[arrA[i]]=1
- During step 2 if u find any index position already filled with 1, we have duplicates, return false
- This operation performs in O(n) time and O(n) space
b) If array does not contains negative elements — Time Complexity : O(n), Space Complexity : O(1)
- Navigate the array.
- Update the array as for ith index :- arrA[arrA[i]] = arrA[arrA[i]]*-1 (if it already not negative).
- If is already negative, we have duplicates, return false.
public
Boolean WihtOutAuxArray(
int
[] arrA){
//this method with work if numbers are non negative
int
max = findMax(arrA);
int
min = findMin(arrA);
if
(arrA.length!=max-min+
1
)
return
false
;
for
(
int
i =
0
;i<arrA.length;i++){
arrA[i] = arrA[i]-min+
1
;
}
for
(
int
i =
0
;i<arrA.length;i++){
int
x = Math.abs(arrA[i]);
if
(arrA[x-
1
]>
0
){
arrA[x-
1
] = arrA[x-
1
]*-
1
;
}
else
{
return
false
;
}
}
return
true
;
}
public
Boolean withAuxArray(
int
[] arrA){
// this method with work even if numbers are negative
int
[] aux =
new
int
[arrA.length];
// why not just use hashmap?
int
max = findMax(arrA);
int
min = findMin(arrA);
if
(arrA.length!=max-min+
1
)
return
false
;
for
(
int
i =
0
;i<arrA.length;i++){
arrA[i] = arrA[i]-min;
aux[i] =
0
;
}
for
(
int
i =
0
;i<arrA.length;i++){
if
(aux[arrA[i]]==
0
){
aux[arrA[i]]=
1
;
}
else
{
return
false
;
}
}
//If we have reached till here means , we satisfied all the requirements
return
true
;
}
Related:
http://algorithms.tutorialhorizon.com/find-the-element-which-appears-maximum-number-of-times-in-the-array/
Objective: Given an array of integers, write a algorithm to find the element which appears maximum number of times in the array.
Better Solution : Use Hashmap. Store the count of each element of array in a hash table and later check in Hash map which element has the maximum count
public void maxRepeatingElementUsingSorting(int [] arrA){
if(arrA.length<1){
System.out.println("Inavlid Array");
return;
}
Arrays.sort(arrA);
int count=1;
int maxCount=1;
int currentElement = arrA[0];
int maxCountElement =arrA[0];
for (int i = 1; i <arrA.length ; i++) {
if(currentElement==arrA[i]){
count++;
}else{
if(count>maxCount){
maxCount = count;
maxCountElement = currentElement;
}
currentElement = arrA[i];
count = 1;
}
}
System.out.println("Element repeating maximum no of times: " + maxCountElement + ", maximum count: " + maxCount);
}
Better Solution (Conditional) : O(n) time and O(1) extra space.
- This solution works only if array has positive integers and all the elements in the array are in range from 0 to n-1 where n is the size of the array.
- Navigate the array.
- Update the array as for ith index :- arrA[arrA[i]% n] = arrA[arrA[i]% n] + n;
- Now navigate the updated array and check which index has the maximum value, that index number is the element which has the maximum occurrence in the array.
int size = arrA.length;
int maxCount=0;
int maxIndex=0;
for (int i = 0; i <size ; i++) {
//get the index to be updated
int index = arrA[i]% size;
arrA[index] = arrA[index] + size;
}
for (int i = 0; i <size ; i++) {
if(arrA[i]/size>maxCount){
maxCount=arrA[i]/size;
maxIndex=i;
}
}
System.out.println("Element repeating maximum no of times: " + arrA[maxIndex]%size + ", maximum count: " + maxCount);
}
http://algorithms.tutorialhorizon.com/find-duplicates-in-an-given-array-in-on-time-and-o1-extra-space/
Given an array of integers, find out duplicates in it.
Better Solution : Use Hash map. Store the count of each element of array in a hash table and later check in Hash table if any element has count more than 1
Better Solution (Conditional) : O(n) time and O(1) extra space.
- This solution works only if array has positive integers and all the elements in the array are in range from 0 to n-1 where n is the size of the array.
- Navigate the array.
- Update the array as for ith index :- arrA[arrA[i]] = arrA[arrA[i]]*-1 (if it already not negative).
- If is already negative, we have duplicates, return false.
Note:
- The code given below does not handle the case when 0 is present in the array.
- To handle 0 in array, while navigating the array, when 0 is encountered, replace it with INT_MIN and if INT_MIN is encountered while traversing, this means 0 is repeated in the array.
for (int i = 0; i < arrA.length; i++) {
//check if element is negative, if yes the we have found the duplicate
if (arrA[Math.abs(arrA[i])] < 0) {
System.out.println("Array has duplicates : " + Math.abs(arrA[i]));
} else {
arrA[Math.abs(arrA[i])] = arrA[Math.abs(arrA[i])] * -1;
}
}
}