Maximum Number of Fruits in Two Baskets
Last Updated :
12 Apr, 2023
Given an array of positive numbers arr[] and integer K. You have to choose the maximum number possible from left to right such that each of them should fit in one of the K sections, where each section contains only one kind of number. You can start from any index of your choice. Once, you reach an index where you can’t fit your number in any of the K sections, STOP there.
Examples:
Input: N = 3, K = 2, arr[] = { 2, 1, 2 }
Output: 3
Explanation: We can pick all of them
Input: N = 6, K = 1, arr[] = { 0, 1, 2, 2, 2, 2 }
Output: 4
Explanation: It’s optimal to pick from index 2(0-indexed) [2, 3, 4, 5].
Approach: This can be solved by the following idea:
Maintaining a window of numbers in which only k distinct numbers are present and maximizing the length of the window.
Steps involved in the implementation of the code:
- Maintain a map to check the number of distinct elements present in the window.
- Start iterating the array.
- If, the count of distinct elements is more than K, start removing elements from starting until it becomes less than K.
- If at any point occurrence of the element becomes 0, remove it from the map.
- Keep on updating the maximum length until it is less than K.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int totalNumbers( int n, int arr[], int K)
{
int i = 0;
int start = 0;
int len = 0;
unordered_map< int , int > m;
int maxx = 0;
while (i < n) {
m[arr[i]]++;
len++;
while (m.size() > K) {
m[arr[start]]--;
len--;
if (m[arr[start]] == 0) {
m.erase(arr[start]);
}
start++;
}
if (m.size() <= K) {
maxx = max(maxx, len);
}
i++;
}
return maxx;
}
int main()
{
int n = 3;
int K = 2;
int arr[] = { 1, 2, 1 };
cout << totalNumbers(n, arr, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int totalNumbers( int n, int [] arr, int K)
{
int i = 0 ;
int start = 0 ;
int len = 0 ;
Map<Integer, Integer> m = new HashMap<>();
int maxx = 0 ;
while (i < n) {
m.put(arr[i], m.getOrDefault(arr[i], 0 ) + 1 );
len++;
while (m.size() > K) {
m.put(arr[start], m.get(arr[start]) - 1 );
len--;
if (m.get(arr[start]) == 0 ) {
m.remove(arr[start]);
}
start++;
}
if (m.size() <= K) {
maxx = Math.max(maxx, len);
}
i++;
}
return maxx;
}
public static void main(String[] args)
{
int n = 3 ;
int K = 2 ;
int [] arr = { 1 , 2 , 1 };
System.out.println(totalNumbers(n, arr, K));
}
}
|
Python3
def totalNumbers(n, arr, K):
i = 0
start = 0
length = 0
m = {}
maxx = 0
while i < n:
m[arr[i]] = m.get(arr[i], 0 ) + 1
length + = 1
while len (m) > K:
m[arr[start]] - = 1
length - = 1
if m[arr[start]] = = 0 :
del m[arr[start]]
start + = 1
if len (m) < = K:
maxx = max (maxx, length)
i + = 1
return maxx
n = 3
K = 2
arr = [ 1 , 2 , 1 ]
print (totalNumbers(n, arr, K))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int TotalNumbers( int n, int [] arr, int K)
{
int i = 0;
int start = 0;
int len = 0;
Dictionary< int , int > dict = new Dictionary< int , int >();
int maxx = 0;
while (i < n)
{
if (dict.ContainsKey(arr[i]))
dict[arr[i]]++;
else
dict.Add(arr[i], 1);
len++;
while (dict.Count > K)
{
dict[arr[start]]--;
len--;
if (dict[arr[start]] == 0)
dict.Remove(arr[start]);
start++;
}
if (dict.Count <= K)
maxx = Math.Max(maxx, len);
i++;
}
return maxx;
}
static void Main()
{
int n = 3;
int K = 2;
int [] arr = { 1, 2, 1 };
Console.WriteLine(TotalNumbers(n, arr, K));
}
}
|
Javascript
function totalNumbers(n, arr, K) {
let i = 0;
let start = 0;
let len = 0;
let m = new Map();
let maxx = 0;
while (i < n) {
m.set(arr[i], (m.get(arr[i]) || 0) + 1);
len++;
while (m.size > K) {
m.set(arr[start], m.get(arr[start]) - 1);
len--;
if (m.get(arr[start]) == 0) {
m. delete (arr[start]);
}
start++;
}
if (m.size <= K) {
maxx = Math.max(maxx, len);
}
i++;
}
return maxx;
}
let n = 3;
let K = 2;
let arr = [1, 2, 1];
console.log(totalNumbers(n, arr, K));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...