Given a 2D integer array of intervals whose length is n where intervals[i]=[start, end] i.e. all integers from start to end inclusive of start and end are also present and we are given an integer k. We have to return the powerful integer. A powerful Integer is an integer that occurs atleast k times. If multiple integers have at least k occurrences, we have to return the maximum integer out of all those elements, and If no integer occurs at least k times return -1.
Examples:
Input: n = 3, intervals = {{1, 3}, {4, 6}, {3, 4}}, k = 2
Output: 4
Explanation: As we can see that 3 and 4 are the 2 integers that have 2 occurrences (2>=k) so we have 4 in this case as the Powerful integer as it is the maximum element which satisfies the condition.Input: n = 4, intervals = {{1, 4}, {12, 45}, {3, 8}, {10, 12}}, k = 3
Output: -1
Explanation: As we can see that no integer occurs 3 times so in that case we have to return -1.
Approach: TO solve the problem follow the below idea:
The idea is to first store the count of numbers in each interval in a Map data structure and since Map stores the numbers in ascending order, and it keeps track of the count of numbers present in each interval. we iterates through the Map to find the most powerful integer.
Steps to solve the problem:
- Create a Map mp to store the count of numbers in each interval.
- Iterate through each interval in the given 2D integer array interval, and update the count of numbers in the corresponding range in mp. The count is incremented by 1 for the start of the interval and decremented by 1 for the end of the interval + 1. This is done to ensure that the count of numbers in each interval is properly maintained.
- Initialize two variables temp and ans to 0. temp stores the running count of numbers seen so far, and ans stores the most powerful integer seen so far.
- Iterate through the Map mp using a for loop. For each key-value pair in the Map, check if the value is greater than or equal to 0. If it is, the value is added to temp, and if temp is greater than or equal to k, ans is updated with the current key. If the value is less than 0, the value is subtracted from temp, and if temp is greater than or equal to k, ans is updated with the previous key.
- The code returns ans if it is not equal to 0, otherwise it returns -1.
Below is the implementation of the above approach:
// C++ code for the above approach:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
// Function to find the kth power of
// an integer within given intervals
int powerfullInteger(int n, vector<vector<int> >& intervals,
int k)
{
// Using a map to keep track
// of the count of intervals
map<int, int> mp;
// Iterating over all the intervals
// and updating the count in the map
for (auto x : intervals) {
mp[x[0]] += 1;
mp[x[1] + 1] -= 1;
}
int ans = -1;
int temp = 0;
// Iterating over the map to find
// the kth power of an integer
for (auto x : mp) {
// If the count in the map
// is positive
if (mp[x.first] >= 0) {
temp += mp[x.first];
// Checking if the count
// is greater than or
// equal to k
if (temp >= k)
ans = x.first;
}
// If the count in the map
// is negative
else {
// Checking if the previous
// count was greater
// than or equal to k
if (temp >= k)
ans = x.first - 1;
temp += mp[x.first];
}
}
// Returning the kth power
// of an integer
return ans;
}
// Drivers code
int main()
{
int n = 3;
vector<vector<int> > intervals
= { { 1, 3 }, { 4, 6 }, { 3, 4 } };
int k = 2;
// Call the function to find
// the kth power of an integer
int result = powerfullInteger(n, intervals, k);
// Print the result
cout << "The kth power of an integer within the given "
"intervals is: "
<< result << endl;
return 0;
}
/*code by flutterfly*/
import java.util.*;
public class Main {
// Function to find the kth power of
// an integer within given intervals
static int powerfullInteger(int n, List<int[]> intervals, int k) {
// Using a map to keep track
// of the count of intervals
Map<Integer, Integer> mp = new TreeMap<>();
// Iterating over all the intervals
// and updating the count in the map
for (int[] x : intervals) {
mp.put(x[0], mp.getOrDefault(x[0], 0) + 1);
mp.put(x[1] + 1, mp.getOrDefault(x[1] + 1, 0) - 1);
}
int ans = -1;
int temp = 0;
// Iterating over the map to find
// the kth power of an integer
for (Map.Entry<Integer, Integer> entry : mp.entrySet()) {
int key = entry.getKey();
int value = entry.getValue();
// If the count in the map
// is positive
if (value >= 0) {
temp += value;
// Checking if the count
// is greater than or
// equal to k
if (temp >= k) {
ans = key;
}
}
// If the count in the map
// is negative
else {
// Checking if the previous
// count was greater
// than or equal to k
if (temp >= k) {
ans = key - 1;
}
temp += value;
}
}
// Returning the kth power
// of an integer
return ans;
}
// Drivers code
public static void main(String[] args) {
int n = 3;
List<int[]> intervals = new ArrayList<>(Arrays.asList(new int[]{1, 3}, new int[]{4, 6}, new int[]{3, 4}));
int k = 2;
// Call the function to find
// the kth power of an integer
int result = powerfullInteger(n, intervals, k);
// Print the result
System.out.println("The kth power of an integer within the given " +
"intervals is: " + result);
}
}
# code by Flutterfly
def powerfull_integer(n, intervals, k):
# Using a dictionary to keep track
# of the count of intervals
mp = {}
# Iterating over all the intervals
# and updating the count in the dictionary
for x in intervals:
mp[x[0]] = mp.get(x[0], 0) + 1
mp[x[1] + 1] = mp.get(x[1] + 1, 0) - 1
ans = -1
temp = 0
# Iterating over the dictionary to find
# the kth power of an integer
for key in sorted(mp.keys()):
value = mp[key]
# If the count in the dictionary
# is positive
if value >= 0:
temp += value
# Checking if the count
# is greater than or
# equal to k
if temp >= k:
ans = key
# If the count in the dictionary
# is negative
else:
# Checking if the previous
# count was greater
# than or equal to k
if temp >= k:
ans = key - 1
temp += value
# Returning the kth power
# of an integer
return ans
# Drivers code
if __name__ == "__main__":
n = 3
intervals = [[1, 3], [4, 6], [3, 4]]
k = 2
# Call the function to find
# the kth power of an integer
result = powerfull_integer(n, intervals, k)
# Print the result
print("The kth power of an integer within the given "
"intervals is:", result)
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
// Function to find the kth power of
// an integer within given intervals
static int PowerfullInteger(int n, List<int[]> intervals, int k)
{
// Using a dictionary to keep track
// of the count of intervals
Dictionary<int, int> mp = new Dictionary<int, int>();
// Iterating over all the intervals
// and updating the count in the dictionary
foreach (var x in intervals)
{
if (mp.ContainsKey(x[0]))
mp[x[0]] += 1;
else
mp[x[0]] = 1;
if (mp.ContainsKey(x[1] + 1))
mp[x[1] + 1] -= 1;
else
mp[x[1] + 1] = -1;
}
int ans = -1;
int temp = 0;
// Iterating over the dictionary to find
// the kth power of an integer
foreach (var entry in mp.OrderBy(entry => entry.Key))
{
int key = entry.Key;
int value = entry.Value;
// If the count in the dictionary
// is positive
if (value >= 0)
{
temp += value;
// Checking if the count
// is greater than or
// equal to k
if (temp >= k)
ans = key;
}
// If the count in the dictionary
// is negative
else
{
// Checking if the previous
// count was greater
// than or equal to k
if (temp >= k)
ans = key - 1;
temp += value;
}
}
// Returning the kth power
// of an integer
return ans;
}
// Drivers code
static void Main()
{
int n = 3;
List<int[]> intervals = new List<int[]> { new int[] { 1, 3 }, new int[] { 4, 6 }, new int[] { 3, 4 } };
int k = 2;
// Call the function to find
// the kth power of an integer
int result = PowerfullInteger(n, intervals, k);
// Print the result
Console.WriteLine("The kth power of an integer within the given " +
"intervals is: " + result);
}
}
// Function to find the kth power of
// an integer within given intervals
function powerfullInteger(n, intervals, k) {
// Using an object to keep track
// of the count of intervals
const mp = {};
// Iterating over all the intervals
// and updating the count in the object
intervals.forEach(x => {
mp[x[0]] = (mp[x[0]] || 0) + 1;
mp[x[1] + 1] = (mp[x[1] + 1] || 0) - 1;
});
let ans = -1;
let temp = 0;
// Iterating over the object to find
// the kth power of an integer
Object.keys(mp).sort((a, b) => a - b).forEach(key => {
const value = mp[key];
// If the count in the object
// is positive
if (value >= 0) {
temp += value;
// Checking if the count
// is greater than or
// equal to k
if (temp >= k) {
ans = parseInt(key);
}
}
// If the count in the object
// is negative
else {
// Checking if the previous
// count was greater
// than or equal to k
if (temp >= k) {
ans = parseInt(key) - 1;
}
temp += value;
}
});
// Returning the kth power
// of an integer
return ans;
}
// Drivers code
const n = 3;
const intervals = [[1, 3], [4, 6], [3, 4]];
const k = 2;
// Call the function to find
// the kth power of an integer
const result = powerfullInteger(n, intervals, k);
// Print the result
console.log("The kth power of an integer within the given " +
"intervals is:", result);
Output
The kth power of an integer within the given intervals is: 4
Time Complexity: O(N*logN) Since the insertion complexity of Map is O(logN) and N elements so Complexity is O(NlogN).
Auxiliary Space:O(n), since we use a Map to store the intervals, which has at most n entries.