C++ Program to get minimum difference between jigsaw puzzle pieces


Suppose we have an array A with m elements and another number n. Amal decided given a present for his n friend, so he will give each of them a jigsaw puzzle. The shop assistant told him that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the ith jigsaw puzzle consists of A[i] pieces. So Amal decided that the difference between the numbers of pieces in his presents must be as small as possible. Let x be the number of pieces in the largest puzzle that he buys and y be the number of pieces in the smallest such puzzle. He wants to choose such n puzzles that x - y is as minimum as possible. We have to find the least possible value of x - y.

Problem Category

The above-mentioned problem can be solved by applying Greedy problem-solving techniques. The greedy algorithm techniques are types of algorithms where the current best solution is chosen instead of going through all possible solutions. Greedy algorithm techniques are also used to solve optimization problems, like its bigger brother dynamic programming. In dynamic programming, it is necessary to go through all possible subproblems to find out the optimal solution, but there is a drawback of it; that it takes more time and space. So, in various scenarios greedy technique is used to find out an optimal solution to a problem. Though it does not gives an optimal solution in all cases, if designed carefully it can yield a solution faster than a dynamic programming problem. Greedy techniques provide a locally optimal solution to an optimization problem. Examples of this technique include Kruskal's and Prim's Minimal Spanning Tree (MST) algorithm, Huffman Tree coding, Dijkstra's Single Source Shortest Path problem, etc.

https://www.tutorialspoint.com/data_structures_algorithms/greedy_algorithms.htm

https://www.tutorialspoint.com/data_structures_algorithms/dynamic_programming.htm

So, if the input of our problem is like A = [10, 12, 10, 7, 5, 22]; n = 4., then the output will be 5, because there are 4 friends. The shop sells 6 puzzles. If Amal buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be 5. It is impossible to obtain a smaller difference.

Steps

To solve this, we will follow these steps −

ans := 1000
m := size of A
sort the array A
for initialize i := n - 1, when i < m, update (increase i by 1), do:
   ans := minimum of ans and (A[i] - A[i - (n - 1)])
return ans

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A, double n){
   int ans = 1000;
   int m = A.size();
   sort(A.begin(), A.end());
   for (int i = n - 1; i < m; i++)
      ans = min(ans, A[i] - A[i - (n - 1)]);
   return ans;
}
int main(){
   vector<int> A = { 10, 12, 10, 7, 5, 22 };
   double n = 4.;
   cout << solve(A, n) << endl;
}

Input

{ 10, 12, 10, 7, 5, 22 }, 4

Output

5

Updated on: 08-Apr-2022

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements