Coding challenge- Find minimum possible size of array with given rules for removing elements - TechSho

Post Top Ad

Responsive Ads Here

Coding challenge- Find minimum possible size of array with given rules for removing elements

Share This
Problem:-
Given an array of numbers and a constant k, Write a program to minimize the size of the array with following rules for removing elements.

You can remove exactly three elements at one go.
The removed three elements must be adjacent in array, i.e., arr[i], arr[i+1], arr[i+2].
The second element must be k greater than first and third element must be k greater than second.

Examples:

Input:  arr[] = {2, 3, 4, 7, 6, 4}, k = 1
Output: 3

Business Rules
Input array size should be a minimum of three. Else display "Invalid Input".
If the input array does not contain any triplets with the difference display -1.

Input Format
Input contains the first is array size 'n', the next 'n' elements correspond to the elements of the array and the last input is constant k that indicates the difference between the members of a triplet. Refer sample input for the format.

Output Format
Output one integer which is the minimum size of the array after minimizing the array. Refer sample output for the format.

Sample Input 1:
6
2
3
4
7
6
4
1

Sample Output 1 :
3

Sample Input 2 :
2
Sample Output 2 :
Invalid Input

Sample Input 3:
6
2
12
5
9
4
6
1
Sample Output 3 :
-1

Function specification is the following for C#, Java and C++.
Name:- GetMinSize(C#)/getMinSize(Java,C++)
Return Type:- int
Parameter(s):- int []arr,int k

Function specification for C is the following.
Name:- getMinSize
Return Type:- int
Parameter(s):- int* arr,int k;


Program:-

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

// dp[i][j] denotes the minimum number of elements left in 
// the subarray arr[i..j]. 
int dp[MAX][MAX]; 

int minSizeRec(int arr[], int low, int high, int k) 

// If already evaluated 
if (dp[low][high] != -1) 
return dp[low][high]; 

// If size of array is less than 3 
if ( (high-low + 1) < 3) 
return high-low +1; 

// Initialize result as the case when first element is 
// separated (not removed using given rules) 
int res = 1 + minSizeRec(arr, low+1, high, k); 

// Now consider all cases when first element forms a triplet 
// and removed. Check for all possible triplets (low, i, j) 
for (int i = low+1; i<=high-1; i++) 

for (int j = i+1; j <= high; j++ ) 

// Check if this triplet follows the given rules of 
// removal. And elements between 'low' and 'i' , and 
// between 'i' and 'j' can be recursively removed. 
if (arr[i] == (arr[low] + k) && 
arr[j] == (arr[low] + 2*k) && 
minSizeRec(arr, low+1, i-1, k) == 0 && 
minSizeRec(arr, i+1, j-1, k) == 0) 

res = min(res, minSizeRec(arr, j+1, high, k)); 




// Insert value in table and return result 
return (dp[low][high] = res); 


// This function mainlu initializes dp table and calls 
// recursive function minSizeRec 
int minSize(int arr[], int n, int k) 

memset(dp, -1, sizeof(dp)); 
return minSizeRec(arr, 0, n-1, k); 


// Driver prrogram to test above function 
int main() 

         int n,i;
         cin>>n;
 int arr[n];

   for (int i = 0; i < n; ++i) 
    {
        cin >> numbers[i];
     }
int n;
int k = 1; 
cout << minSize(arr, n, k) << endl; 
return 0; 



Thank you guys for reading. If you have any doubt ask in the comment section.

No comments:

Post a Comment

Post Bottom Ad

Responsive Ads Here

Pages