Saturday, 4 Dec 2021
Category: partition

#include using namespace std;  int maxSumAfterPartition(int arr[], int n){        vector pos;          vector neg;          int zero = 0;          int pos_sum = 0;          int neg_sum = 0;          for (int i = 0; i < n; i++) {          if (arr[i] > 0) {              pos.push_back(arr[i]);            pos_sum += arr[i];        }        else if (arr[i] < 0) {              neg.push_back(arr[i]);            neg_sum += arr[i];        }        else {              zero++;        }    }          int ans = 0;              sort(pos.begin(), pos.end());              sort(neg.begin(), neg.end(), greater());              if (pos.size() > 0 && neg.size() > 0) […]

Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets.Examples:Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}Output: […]

#include using namespace std;  class Node {public:    int data;    Node* next;};  int sizeOfLL(Node* head){    int size = 0;          while (head != NULL) {        ++size;        head = head->next;    }    return size;}  vector Partition_of_list(Node* head){    int size = sizeOfLL(head);    Node* temp = head;    vector ans;          if (3 >= size) {                        while (temp != NULL) {            Node* next = temp->next;            temp->next = NULL;            ans.push_back(temp);            temp = next;        }                                  int y = 3 – size;        while (y != 0) {            ans.push_back(NULL);            y–;        }    }    else {                int minSize […]

Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR valuesGiven an array arr[] consisting of N integers, the task is to find the maximum Bitwise XOR of Bitwise OR of every subarray after splitting the array into subarrays(possible zero subarrays).Examples:Input: arr[] = {1, 5, 7}, N = 3Output: 7Explanation:The given […]

Count ways to split array into three non-empty subarrays having equal Bitwise XOR valuesGiven an array arr[] consisting of N non-negative integers, the task is to count the number of ways to split the array into three different non-empty subarrays such that Bitwise XOR of each subarray is equal. Examples:Input: arr[] = {7, 0, 5, 2, […]

Count ways to split array into two subsets having difference between their sum equal to KGiven an array A[] of size N and an integer diff, the task is to count the number of ways to split the array into two subsets (non-empty subset is possible) such that the difference between their sums is equal […]

#include using namespace std;  void PrintAllArrays(int arr[], int N){                int l = -1, r = -1;          for (int i = 0; i < N; i++) {                  if (l == -1) {                          if (arr[i] == -1) {                  l = i;                break;            }                          if (arr[i] == 0) {                  cout 0) {            r = i;            break;        }    }              if (l == -1 || r == -1 || l >= r        || count(arr + l, […]

#include using namespace std;  struct ListNode {    int val;    struct ListNode* next;};  void push(ListNode** head_ref,          int node_val){        ListNode* new_node = new ListNode();          new_node->val = node_val;          new_node->next = (*head_ref);          (*head_ref) = new_node;}  void splitListInParts(ListNode* head,                      int K){        vector ans;          if (!head) {                  while (K–)            ans.push_back(NULL);    }              int N = 0;              ListNode* p = head;          while (p) {                  p = p->next;                  N++;    }      int len = N / K;    int rem = N % K;      p = head;          while (K > 0 && […]

import java.io.*;import java.lang.*;import java.util.*;class GFG{static int maximumSum(int arr[], int S[],                      int N, int K){             int ans = 0;            Arrays.sort(arr);    for(int i = 0; i < N / 2; i++)    {        int temp = arr[i];        arr[i] = arr[N - 1 - i];        arr[N - 1 - i] = temp;    }        for(int i = 0; i < K; i++)        ans += arr[i];            Arrays.sort(S);        for(int i = 0; i < K; […]

#include using namespace std;  void checkPalindrome(string& s){            int a = { 0 };                  int o = 0, e = 0;          for (int i = 0; s[i] != ‘’; i++)        a[(int)s[i] – 97]++;          for (int i = 0; i < 26; i++) {                  if (a[i] == 1)            o++;                  else if (a[i] % 2 == 0                 and a[i] != 0)            e += (a[i] / 2);    }          if (e >= o)        cout […]