Skip to content

Commit 1e8e192

Browse files
committed
Add special array problem
1 parent b1edea3 commit 1e8e192

File tree

9 files changed

+148
-0
lines changed

9 files changed

+148
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ List of Programs related to data structures and algorithms
6666
| 25 | Destination City | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.md) | Easy | Array traversal & set |
6767
| 26 | Set mismatch | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.md) | Easy | Array traversal & marking numbers |
6868
| 27 | Intersection of arrays | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/27.intersectionOfArrays/intersectionOfArrays.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/27.intersectionOfArrays/intersectionOfArrays.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/27.intersectionOfArrays/intersectionOfArrays.md) | Easy | Set and array traversal |
69+
| 29 | Special array | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/29.specialArray/specialArray.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/29.specialArray/specialArray.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/29.specialArray/specialArray.md) | Easy | Frequency counter and array traversal |
6970

7071
### String
7172

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package java1.algorithms.array.specialArray;
2+
3+
public class SpecialArray {
4+
private static int specialArray(int[] nums) {
5+
int len = nums.length;
6+
int[] counts = new int[len+1];
7+
8+
for(int num: nums){
9+
if(num >=len) {
10+
counts[len]++;
11+
} else {
12+
counts[num]++;
13+
}
14+
}
15+
16+
int elementsGreaterThanX = 0;
17+
for (int x = len; x >= 1; x--) {
18+
elementsGreaterThanX += counts[x];
19+
if(elementsGreaterThanX == x) {
20+
return x;
21+
}
22+
}
23+
24+
return -1;
25+
}
26+
27+
public static void main(String[] args) {
28+
int[] nums1 = new int[]{2,3};
29+
int[] nums2 = new int[]{0,0,0};
30+
int[] nums3 = new int[]{0,4,3,0,4};
31+
System.out.println(specialArray(nums1));
32+
System.out.println(specialArray(nums2));
33+
System.out.println(specialArray(nums3));
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Description:**
2+
Given an array of non-negative integers `nums`, determine if there exists a number `x` such that exactly `x` numbers in the array are greater than or equal to `x`. If such an `x` exists, return it; otherwise, return -1. The value of `x` is unique if the array is special.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: nums = [2,3]
8+
Output: 2
9+
10+
Example 2:
11+
12+
Input: nums = [0,0,0]
13+
Output: -1
14+
15+
Example 3:
16+
17+
Input: nums = [0,4,3,0,4]
18+
Output: -1
19+
20+
**Algorithmic Steps**
21+
This problem is solved with the help of counting element frequencies and array traversal over the elements. The algorithmic approach can be summarized as follows:
22+
23+
1. Create a function named `specialArray` and accepts input array(`nums`), which is used to determine the element `x` such that `x` number of elements greater or equal to x.
24+
25+
2. Define a counts arrays with `len+1` elements, where each element is initialized to zero. Here, `len` is the number of elements in an input array.
26+
27+
3. Iterate over an input array(`nums`) and update each element's count frequency. If the element is greater (or equal) than length of an array, update the count frequency at length index.
28+
29+
4. Define a counter variable `elementsGreaterThanX` to caculate the total count of elements greater upto particular index.
30+
31+
5. Iterate over an input array(`nums`) in a backward direction. In each iteration, update the counter `elementsGreaterThanX` by adding counter frequency at each index(`x`). If the total count is equals to current index, return the element `x` as an output
32+
33+
6. Return -1 if there is no element exists.
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an input array. This is because we need to iterate over all the elements twice, one for finding the counter frequency and other one comparing the total count to each index.
37+
38+
It takes constant time complexity of `O(n)`. This is due to array counter used to calculate the frequency of elements.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package java1.algorithms.array.timeToBuyTickets;
2+
3+
public class TimeToBuyTickets {
4+
5+
}

src/java1/algorithms/array/timeToBuyTickets/TimeToBuyTickets.md

Whitespace-only changes.

src/javascript/algorithms/array/28.timeToBuyTickets/timeToBuyTickets.js

Whitespace-only changes.

src/javascript/algorithms/array/28.timeToBuyTickets/timeToBuyTickets.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function specialArray(nums){
2+
const len = nums.length;
3+
const counts = new Array(len+1).fill(0);
4+
5+
//Step1: Count frequencies
6+
for (const num of nums) {
7+
if(num >= len) {
8+
counts[len]++;
9+
} else {
10+
counts[num]++;
11+
}
12+
}
13+
14+
let elementsGreaterThanX = 0;
15+
//Step2: Return x number of elements greater or equal to x
16+
for (let x = len; x >=1 ; x--) {
17+
elementsGreaterThanX += counts[x];
18+
if(elementsGreaterThanX === x) {
19+
return x;
20+
}
21+
}
22+
23+
return -1;
24+
}
25+
26+
const nums1 = [2,3];
27+
const nums2 = [0,0,0];
28+
const nums3 = [0,4,3,0,4];
29+
console.log(specialArray(nums1));
30+
console.log(specialArray(nums2));
31+
console.log(specialArray(nums3));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Description:**
2+
Given an array of non-negative integers `nums`, determine if there exists a number `x` such that exactly `x` numbers in the array are greater than or equal to `x`. If such an `x` exists, return it; otherwise, return -1. The value of `x` is unique if the array is special.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: nums = [2,3]
8+
Output: 2
9+
10+
Example 2:
11+
12+
Input: nums = [0,0,0]
13+
Output: -1
14+
15+
Example 3:
16+
17+
Input: nums = [0,4,3,0,4]
18+
Output: -1
19+
20+
**Algorithmic Steps**
21+
This problem is solved with the help of counting element frequencies and array traversal over the elements. The algorithmic approach can be summarized as follows:
22+
23+
1. Create a function named `specialArray` and accepts input array(`nums`), which is used to determine the element `x` such that `x` number of elements greater or equal to x.
24+
25+
2. Define a counts arrays with `len+1` elements, where each element is initialized to zero. Here, `len` is the number of elements in an input array.
26+
27+
3. Iterate over an input array(`nums`) and update each element's count frequency. If the element is greater (or equal) than length of an array, update the count frequency at length index.
28+
29+
4. Define a counter variable `elementsGreaterThanX` to caculate the total count of elements greater upto particular index.
30+
31+
5. Iterate over an input array(`nums`) in a backward direction. In each iteration, update the counter `elementsGreaterThanX` by adding counter frequency at each index(`x`). If the total count is equals to current index, return the element `x` as an output
32+
33+
6. Return -1 if there is no element exists.
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an input array. This is because we need to iterate over all the elements twice, one for finding the counter frequency and other one comparing the total count to each index.
37+
38+
It takes constant time complexity of `O(n)`. This is due to array counter used to calculate the frequency of elements.

0 commit comments

Comments
 (0)