Given a binary array arr1[] and an integer array arr2[], each of length N, the task is to rearrange the elements in the array arr2 such that the total cost generated is maximized. The total cost generated is calculated by summation of modified values in the arr2 array. The values are modified in such a […]

Continue Reading#include using namespace std;bool binarySearch(float arr[], int size, float key){ int low = 0, high = size – 1, mid; while (low arr[mid]) low = mid + 1; else if (key < arr[mid]) high = mid - 1; else return 1; } return 0;}bool twoPointers(float arr[], int N, float mean){ int i = 0, j = N - 1; while (i < j) { float temp = […]

Continue ReadingGiven an array arr[] of length N and an integer K, the task is to find the maximize the length of the array by appending at most K elements such that the array becomes a permutation of consecutive numbers starting from 1. Once K elements are added, sum of two or more array elements can […]

Continue ReadingGiven an array, the task is to shift the middle element to the start and end of the array alternatively, till the middle element becomes equal to the first element of the original array.Input: arr[]=[2, 8, 5, 9, 10]Output: [9, 5, 2, 10, 8]Explanation: We can get this output by shifting middle elementstep1: middle element […]

Continue Reading#include using namespace std; void Rearrange(int arr[], int K, int N){ int ans[N + 1]; int f = -1; for (int i = 0; i < N; i++) { ans[i] = -1; } K = find(arr, arr + N, K) – arr; vector smaller, greater; for (int i = 0; i < N; i++) { if (arr[i] < arr[K]) smaller.push_back(arr[i]); else if (arr[i] > arr[K]) greater.push_back(arr[i]); } int low = […]

Continue ReadingGiven an array arr[] of size N, the task is to print the minimum number of moves required to make all array elements equal by selecting any pair of distinct indices and then increment the element at the first index and decrement the element at the other index by 1 each time. If it is […]

Continue Reading#include using namespace std; double change(int pass, int total){ double currentPassRatio, newPassRatio; double increase; currentPassRatio = ((double)pass) / total; newPassRatio = ((double)(pass + 1)) / (total + 1); increase = newPassRatio – currentPassRatio; return increase;} double maximumAverage( vector v, int M, int N){ double sum = 0; double increase, average; priority_queue pq; for (int i = 0; i < N; i++) { increase = change(v[i][0], v[i][1]); pq.push({ increase, i }); average = v[i][0] * 1.0 […]

Continue Readingdef Swap(x, y, p, posOfCurrNum): posOfCurrNum[p[x]], posOfCurrNum[p[y]] = posOfCurrNum[p[y]], posOfCurrNum[p[x]] p[x], p[y] = p[y], p[x] return p, posOfCurrNumdef sortArray(p, n): posOfCurrNum = [0] * (n + 1) for i in range(1, n + 1): posOfCurrNum[p[i]] = i ans = [] for i in range(1, n + 1): if (p[i] == i): continue j = posOfCurrNum[i] if (abs(i – j) * 2 >= n): p, posOfCurrNum = Swap(i, j, […]

Continue Reading