Category: partition

Maximum possible difference of two subsets of an array | Set 2

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

Continue Reading

Partition a Linked List into 3 parts such that the maximum difference between their sizes is minimum

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

Continue Reading

Split array into three continuous subarrays with negative, 0 and positive product respectively

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

Continue Reading

Partition a Linked List into K continuous groups with difference in their sizes at most 1

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

Continue Reading

Split array into K non-empty subsets such that sum of their maximums and minimums is maximized

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

Continue Reading

Partition a string into palindromic strings of at least length 2 with every character present in a single string

  #include using namespace std;  void checkPalindrome(string& s){            int a[26] = { 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 […]

Continue Reading