Find Maximum Powerful Integer with Occurrences ≥ k in 2D Intervals
Last Updated :
30 Nov, 2023
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++
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int powerfullInteger( int n, vector<vector< int > >& intervals,
int k)
{
map< int , int > mp;
for ( auto x : intervals) {
mp[x[0]] += 1;
mp[x[1] + 1] -= 1;
}
int ans = -1;
int temp = 0;
for ( auto x : mp) {
if (mp[x.first] >= 0) {
temp += mp[x.first];
if (temp >= k)
ans = x.first;
}
else {
if (temp >= k)
ans = x.first - 1;
temp += mp[x.first];
}
}
return ans;
}
int main()
{
int n = 3;
vector<vector< int > > intervals
= { { 1, 3 }, { 4, 6 }, { 3, 4 } };
int k = 2;
int result = powerfullInteger(n, intervals, k);
cout << "The kth power of an integer within the given "
"intervals is: "
<< result << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static int powerfullInteger( int n, List< int []> intervals, int k) {
Map<Integer, Integer> mp = new TreeMap<>();
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 ;
for (Map.Entry<Integer, Integer> entry : mp.entrySet()) {
int key = entry.getKey();
int value = entry.getValue();
if (value >= 0 ) {
temp += value;
if (temp >= k) {
ans = key;
}
}
else {
if (temp >= k) {
ans = key - 1 ;
}
temp += value;
}
}
return ans;
}
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 ;
int result = powerfullInteger(n, intervals, k);
System.out.println( "The kth power of an integer within the given " +
"intervals is: " + result);
}
}
|
Python
def powerfull_integer(n, intervals, k):
mp = {}
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
for key in sorted (mp.keys()):
value = mp[key]
if value > = 0 :
temp + = value
if temp > = k:
ans = key
else :
if temp > = k:
ans = key - 1
temp + = value
return ans
if __name__ = = "__main__" :
n = 3
intervals = [[ 1 , 3 ], [ 4 , 6 ], [ 3 , 4 ]]
k = 2
result = powerfull_integer(n, intervals, k)
print ( "The kth power of an integer within the given "
"intervals is:" , result)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static int PowerfullInteger( int n, List< int []> intervals, int k)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
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;
foreach ( var entry in mp.OrderBy(entry => entry.Key))
{
int key = entry.Key;
int value = entry.Value;
if (value >= 0)
{
temp += value;
if (temp >= k)
ans = key;
}
else
{
if (temp >= k)
ans = key - 1;
temp += value;
}
}
return ans;
}
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;
int result = PowerfullInteger(n, intervals, k);
Console.WriteLine( "The kth power of an integer within the given " +
"intervals is: " + result);
}
}
|
Javascript
function powerfullInteger(n, intervals, k) {
const mp = {};
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;
Object.keys(mp).sort((a, b) => a - b).forEach(key => {
const value = mp[key];
if (value >= 0) {
temp += value;
if (temp >= k) {
ans = parseInt(key);
}
}
else {
if (temp >= k) {
ans = parseInt(key) - 1;
}
temp += value;
}
});
return ans;
}
const n = 3;
const intervals = [[1, 3], [4, 6], [3, 4]];
const k = 2;
const result = powerfullInteger(n, intervals, k);
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.
Share your thoughts in the comments
Please Login to comment...