
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Three Equal Parts in C++
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
#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, ]
- Related Articles
- Partition Array Into Three Parts With Equal Sum in Python
- Split string into equal parts JavaScript
- Split a string in equal parts (grouper in Python)
- Divide a string in N equal parts in C++ Program
- Divide a string into n equal parts - JavaScript
- Java Program to divide a string in 'N' equal parts
- Golang Program to divide a string in 'N' equal parts
- Ravi divided pizza into 6 equal parts and ate 3 parts from it. What is the fraction ?
- Split the array into equal sum parts according to given conditions in C++
- Split 207 into three parts such that these are in A.P. and the product of the two smaller parts is 4623.
- Split 207 into three parts such that these are in AP and the product of the two smaller parts is 4623.
- Create Three Equal Columns with Bootstrap grid Layout
- Find maximum sum possible equal sum of three stacks in C++
- Find three parts of 207 such that they are in increasing order, they form an AP and the product of two smaller parts is 4623.
- Draw an angle of measure \( 153^{\circ} \) and divide it into four equal parts.
