Saturday, 4 Dec 2021
Category: array-rearrange

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 […]

#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 = […]

Given 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 […]

Given 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 […]

#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 = […]

Given an array arr[] of size N, and integers M and K, the task is to find the maximum possible value of the smallest array element by performing M operations. In each operation, increase the value of all elements in a contiguous subarray of length K by 1.Examples:Input: arr[ ] = {2, 2, 2, 2, […]

Given 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 […]

#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], v[i]);                          pq.push({ increase, i });                  average = v[i] * 1.0 […]

def 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 =  * (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, […]

Given an array arr[] consisting of N positive integers and an integer K, the task is to make the sum of all K-length subarrays equal by replacing minimum number of array elements with any integer.Examples:Input: arr[] = {3, 4, 3, 5, 6}, K = 2Output: 2Explanation:Operation 1: Replacing arr by 4 modiifes arr[] to {3, […]