Three Equal Parts in C++

C++Server Side ProgrammingProgramming

Suppose we have one array A of 0s and 1s, we have to divide the array into 3 non-empty parts such that all of these parts represent the same binary value. If that is possible, return any [i, j] with i+1 < j, such that −

  • A[0], A[1], ..., A[i] is the first part;

  • A[i+1], A[i+2], ..., A[j-1] is the second part, and

  • A[j], A[j+1], ..., A[A.length - 1] is the third part.

All three parts have equal binary value. If that is not possible, return [-1, -1].

So, if the input is like [0,1,0,1,1], then the output will be [0,4]

To solve this, we will follow these steps −

  • Define a function getIdx(), this will take an array a, left, right,

  • while (left < right and a[left] is same as 0), do −

    • (increase left by 1)

  • while right < (int)a.size(), do −

    • if a[left] is not equal to a[right], then, return -1

    • (increase left by 1), (increase right by 1)

  • return left - 1

  • From the main method do the following −

  • Define an array ret of size 2 fill this with -1

  • num := 0, n := size of A

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • num := num + 1 when (A[i] is same as 1), otherwise 0

  • if num mod 3 is not equal to 0, then −

    • return ret

  • if num is same as 0, then −

    • return { 0, 2 }

  • req := num / 3

  • idx := n - 1

  • for initialize temp := 0, when idx >= 0 and temp < req, update (decrease idx by 1), do

    • temp := temp + 1 when (A[idx] is same as 1), otherwise 0

  • (increase idx by 1)

  • firstEnd := getIdx(A, 0, idx)

  • if firstEnd < 0, then −

    • return ret

  • secondEnd := getIdx(A, firstEnd + 1, idx)

  • if secondEnd < 0, then −

    • return ret

  • return { firstEnd, secondEnd + 1 }

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
   public:
   vector<int> threeEqualParts(vector<int>& A){
      vector<int> ret(2, -1);
      int num = 0;
      int n = A.size();
      for (int i = 0; i < n; i++) {
         num += (A[i] == 1);
      }
      if (num % 3 != 0)
         return ret;
      if (num == 0) {
         return { 0, 2 };
      }
      int req = num / 3;
      int idx = n - 1;
      for (int temp = 0; idx >= 0 && temp < req; idx--) {
         temp += A[idx] == 1;
      }
      idx++;
      int firstEnd = getIdx(A, 0, idx);
      if (firstEnd < 0)
         return ret;
      int secondEnd = getIdx(A, firstEnd + 1, idx);
      if (secondEnd < 0)
         return ret;
      return { firstEnd, secondEnd + 1 };
   }
   int getIdx(vector<int>& a, int left, int right){
      while (left < right && a[left] == 0)
      left++;
      while (right < (int)a.size()) {
         if (a[left] != a[right])
            return -1;
         left++;
         right++;
      }
      return left - 1;
   }
};
main(){
   Solution ob;
   vector<int> v = {0,1,0,1,1};
   print_vector(ob.threeEqualParts(v));
}

Input

{0,1,0,1,1}

Output

[1, 4, ]
raja
Published on 04-Jun-2020 09:09:28
Advertisements