Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
2.1.
Example
2.2.
Approach
3.
Implementation in c++
3.1.
Output
4.
Implementation in Java 
4.1.
Output
5.
Complexity Analysis
5.1.
Time Complexity
5.2.
Space Complexity
6.
Frequently Asked Questions
6.1.
Explain hashing?
6.2.
What do you mean by a hash key?
6.3.
What is the DP?
6.4.
What is the purpose of hashing?
6.5.
Why is hashing quicker?
7.
Conclusion
Last Updated: Mar 27, 2024

Distribute items such that a person cannot take more than two items of the same type

Introduction

If you want to have a successful career in the tech field, you need to have a strong knowledge of Data Structure and Algorithms. In this article, we’ll solve a question frequently asked in the technical interview based on the concept of the Hashing.

Problem Statement

The job is to determine if it is possible to distribute all of the N sweets, which may be of various varieties, and k consumers, where one customer won't make the same sweet in more than two pieces. If it is not possible, print "No"; otherwise, "Yes".

Arr [] represents a list of candies given an array. Sweets are what arr[i] is.

Example

Consider array arr[] as {1, 1, 2, 3, 1} and  k = 2, and the output is Yes.

Three pieces of sweet type 1 are available, along with one part of type 2 and one piece of type 3. Under the restrictions set above, two consumers may share sweets.

arr[] = {2, 3, 5, 3, 3} and k = 2; Output: Yes

With arr[] = {2, 3, 5, 3, 3, 3} and k = 2, the output is No.

Approach

In this method, we are using some hashing techniques,

1. Keep track of 32 distinct kinds of sweets in a hash.

2. Go over an array and look for each arr[I]

 =2*k for hash[arr[i]].

Implementation in c++

#include <bits/stdc++.h>
using namespace std;


/* Function to check hash array
 corresponding to the given array */
bool check(int arr[], int n, int k) {
unordered_map<int, int> t;
/* Maintaining a hash for the array */
for (int i = 0; i < n; i++)
t[arr[i]]++;
/* Checking for each value in hash */
for (auto x : t)
if (x.second > 2 * k)
return false;
return true;
}


int main() {
int arr[] = { 1, 1, 2, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
check(arr, n, k)?cout << "Yes":cout << "No";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Yes
You can also try this code with Online Java Compiler
Run Code

Implementation in Java 

// Java program for above implementation
import java.util.HashMap;
import java.util.Map;


class HelloWorld {
/* Function to check hash array
  corresponding to the given array */
static boolean check(int arr[], int n, int k)
{
HashMap <Integer, Integer> hash = new HashMap<>();

/* Maintaining a hash */
for (int i = 0; i < n; i++)
{
if (!hash.containsKey(arr[i]))
hash.put(arr[i], 0);
hash.put(arr[i], hash.get(arr[i]) + 1);
}

/* Checking for each value in hash */
for (Map.Entry x : hash.entrySet())
if ((int)x.getValue() > 2 * k)
return false;

return true;
}


public static void main(String []args)
{

int arr[] = { 1, 1, 2, 3, 1 };
int n = arr.length;
int k = 2;
if (check(arr, n, k))
System.out.println("Yes");
else
System.out.println("No");
}
}

Output

Yes


You can also read about the Longest Consecutive Sequence.

Complexity Analysis

Time Complexity

O(N), Where ‘N’ stands for the array's length.

Reason: We are using only two loops, one while hashing and the other while checking the value in the hash.

Space Complexity

O(N), Where ‘N’ stands for the array's length.

Reason: Even in the worst-case scenario, the maximum length of the hash can be up to the array's size.

Frequently Asked Questions

Explain hashing?

Hashing can transform any key or string of characters into another value. The original string is typically represented with a shorter, fixed-length value or key, making it simpler to locate or use.

What do you mean by a hash key?

A form of data structure that contains key-value pairs is a hash table. A hash function receives the key and runs mathematical operations on it.

What is the DP?

Dynamic programming, often known as DP, is an algorithmic technique for solving problems by recursively dividing them into easier subproblems and taking advantage of the fact that the best solution to the whole problem depends on the best solution to each of its subproblems.

What is the purpose of hashing?

Data is mapped to a fixed-length value using the one-way function of hashing. Authentication is the main purpose of hashing. Salting is an extra step in the hashing process that adds an extra value to the end of the password to alter the hash value that is generated. It is commonly used in conjunction with hashed passwords.

Why is hashing quicker?

The linear time complexity of O(n) is presented when searching through an array-like data structure. In other words, the search time grows linearly in proportion to the size of the data structure.

Conclusion

In this article, we have discussed the question related to array and hashing. Distribute items such that a person cannot take more than two items of the same type in multiple languages.

Want to learn data structure in java language click here.
Check out this problem - Two Sum Problem

Refer to our guided paths on Coding Ninjas Studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.
Do upvote our blog to help other ninjas grow.
Happy Learning!

Live masterclass