Given an array A of length n such that it contains only ā0sā and ā1sā. The task is to divide the array into THREE different non-empty parts such that all of these parts represent the same binary value(in decimals).
If it 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.Ā
- A[j], A[j+1], ā¦, A[n- 1] is the third part. Note: All three parts should have equal binary values. However, If it is not possible, return [-1, -1].Ā
Examples:
Input : A = [1, 1, 1, 1, 1, 1] Output : [1, 4] All three parts are, A[0] to A[1] first part, A[2] to A[3] second part, A[4] to A[5] third part. Input : A = [1, 0, 0, 1, 0, 1] Output : [0, 4]
Naive Approach
The idea is to find every possible index pair i and j with i+1<j to divide the array into three parts. Then find the decimal value of each part and if the decimal value of all three parts are equal then return/print that i and j.
Steps to implement-
- Find the size of the input array/vector
- Declare a vector to store the final answer
- Now run two nested for loops to choose index i and j with i+1<j such that we will get three parts of array
- After that initialize three variables with a value of 0 to find the decimal value of those three parts
- Run a for loop from i to 0 to find the decimal value of the first part
- Run a for loop from j-1 to i+1 to find the decimal value of the second part
- Run a for loop from N-1 to j to find the decimal value of the third part
- When decimal value of all three part becomes equal then print or return i,j value
- In last if nothing got printed or returned then print or return -1,-1
Code
C++
// C++ implementation of the // above approach #include <bits/stdc++.h> using namespace std; Ā
// Function to return required // interval answer. vector< int > ThreeEqualParts(vector< int > arr) { Ā Ā Ā Ā //Size of input vector Ā Ā Ā Ā int N=arr.size(); Ā Ā Ā Ā Ā Ā Ā Ā Ā //To store answer Ā Ā Ā vector< int > ans;Ā Ā Ā Ā Ā Ā Ā Ā //Check for different possible i,j Ā Ā Ā for ( int i=0;i<N-2;i++){ Ā Ā Ā Ā Ā Ā Ā for ( int j=i+2;j<N;j++){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā //Initialize decimal value of all three part to zero Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int val1=0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int val2=0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int val3=0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā //Finding decimal value of first part Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp=1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k=i;k>=0;k--){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val1+=temp*arr[k]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp=temp*2; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā //Finding decimal value of second part Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp=1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k=j-1;k>=i+1;k--){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val2+=temp*arr[k]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp=temp*2; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā //Finding decimal value of third part Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp=1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k=N-1;k>=j;k--){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val3+=temp*arr[k]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp=temp*2; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā //When all three part has equal value Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (val1==val2 && val2==val3 && val1==val3){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.push_back(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.push_back(j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ans; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā //If no such value is possible then Ā Ā Ā //we need to return [-1,-1] Ā Ā Ā ans.push_back(-1); Ā Ā Ā ans.push_back(-1); Ā Ā Ā return ans; } Ā
// Driver Code int main() { Ā Ā Ā Ā vector< int > arr = {1, 1, 1, 1, 1, 1}; Ā
Ā Ā Ā Ā // Output required result Ā Ā Ā Ā vector< int > res = ThreeEqualParts(arr); Ā
Ā Ā Ā Ā for ( auto it :res) Ā Ā Ā Ā Ā Ā Ā Ā cout << it << " " ; Ā
Ā Ā Ā Ā return 0; } |
Java
import java.util.ArrayList; import java.util.List; Ā
public class Main { Ā
Ā Ā Ā Ā public static List<Integer> threeEqualParts(List<Integer> arr) { Ā Ā Ā Ā Ā Ā Ā Ā int N = arr.size(); Ā Ā Ā Ā Ā Ā Ā Ā List<Integer> ans = new ArrayList<>(); Ā
Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i < N - 2 ; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = i + 2 ; j < N; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int val1 = 0 , val2 = 0 , val3 = 0 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int temp; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k = i; k >= 0 ; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = ( int ) Math.pow( 2 , i - k); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val1 += arr.get(k) * temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k = j - 1 ; k >= i + 1 ; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = ( int ) Math.pow( 2 , j - k - 1 ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val2 += arr.get(k) * temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k = N - 1 ; k >= j; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = ( int ) Math.pow( 2 , N - 1 - k); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val3 += arr.get(k) * temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (val1 == val2 && val2 == val3 && val1 == val3) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.add(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.add(j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ans; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā ans.add(- 1 ); Ā Ā Ā Ā Ā Ā Ā Ā ans.add(- 1 ); Ā Ā Ā Ā Ā Ā Ā Ā return ans; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā public static void main(String[] args) { Ā Ā Ā Ā Ā Ā Ā Ā List<Integer> arr = List.of( 1 , 1 , 1 , 1 , 1 , 1 ); Ā Ā Ā Ā Ā Ā Ā Ā List<Integer> res = threeEqualParts(arr); Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(res); Ā Ā Ā Ā } } |
Python3
from typing import List Ā
def threeEqualParts(arr: List [ int ]) - > List [ int ]: Ā Ā Ā Ā N = len (arr) Ā Ā Ā Ā ans = [] Ā
Ā Ā Ā Ā for i in range (N - 2 ): Ā Ā Ā Ā Ā Ā Ā Ā for j in range (i + 2 , N): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val1, val2, val3 = 0 , 0 , 0 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = 1 Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for k in range (i, - 1 , - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val1 + = arr[k] * temp Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp * = 2 Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = 1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for k in range (j - 1 , i, - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val2 + = arr[k] * temp Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp * = 2 Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = 1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for k in range (N - 1 , j - 1 , - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val3 + = arr[k] * temp Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp * = 2 Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if val1 = = val2 = = val3: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.append(i) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.append(j) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ans Ā
Ā Ā Ā Ā ans.append( - 1 ) Ā Ā Ā Ā ans.append( - 1 ) Ā Ā Ā Ā return ans Ā
arr = [ 1 , 1 , 1 , 1 , 1 , 1 ] res = threeEqualParts(arr) print (res) |
C#
using System; using System.Collections.Generic; Ā
public class ThreeEqualParts { Ā Ā Ā Ā public static List< int > FindEqualParts(List< int > arr) { Ā Ā Ā Ā Ā Ā Ā Ā int N = arr.Count; Ā Ā Ā Ā Ā Ā Ā Ā List< int > ans = new List< int >(); Ā Ā Ā Ā Ā Ā Ā Ā for ( int i=0; i<N-2; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j=i+2; j<N; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int val1=0, val2=0, val3=0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k=i; k>=0; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val1 += arr[k] * ( int )Math.Pow(2, i-k); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k=j-1; k>=i+1; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val2 += arr[k] * ( int )Math.Pow(2, j-1-k); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int k=N-1; k>=j; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val3 += arr[k] * ( int )Math.Pow(2, N-1-k); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (val1 == val2 && val2 == val3) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.Add(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.Add(j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ans; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā ans.Add(-1); Ā Ā Ā Ā Ā Ā Ā Ā ans.Add(-1); Ā Ā Ā Ā Ā Ā Ā Ā return ans; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā public static void Main() { Ā Ā Ā Ā Ā Ā Ā Ā List< int > arr = new List< int > {1, 1, 1, 1, 1, 1}; Ā Ā Ā Ā Ā Ā Ā Ā List< int > res = FindEqualParts(arr); Ā Ā Ā Ā Ā Ā Ā Ā foreach ( int it in res) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(it + " " ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } |
PHP
<?php Ā Ā Ā Ā function threeEqualParts( $arr ) { Ā Ā Ā Ā // Size of input array Ā Ā Ā Ā $N = count ( $arr ); Ā Ā Ā Ā Ā Ā Ā Ā Ā // To store answer Ā Ā Ā Ā $ans = array ();Ā Ā Ā Ā Ā Ā Ā Ā Ā // Check for different possible i, j Ā Ā Ā Ā for ( $i = 0; $i < $N - 2; $i ++) { Ā Ā Ā Ā Ā Ā Ā Ā for ( $j = $i + 2; $j < $N ; $j ++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize decimal value of all three part to zero Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $val1 = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $val2 = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $val3 = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $temp ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding decimal value of first part Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $temp = 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( $k = $i ; $k >= 0; $k --) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $val1 += $temp * $arr [ $k ]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $temp *= 2; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding decimal value of second part Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $temp = 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( $k = $j - 1; $k >= $i + 1; $k --) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $val2 += $temp * $arr [ $k ]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $temp *= 2; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding decimal value of third part Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $temp = 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( $k = $N - 1; $k >= $j ; $k --) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $val3 += $temp * $arr [ $k ]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $temp *= 2; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // When all three part has equal value Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ( $val1 == $val2 && $val2 == $val3 && $val1 == $val3 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array_push ( $ans , $i ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array_push ( $ans , $j ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return $ans ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā // If no such value is possible then Ā Ā Ā Ā // we need to return [-1,-1] Ā Ā Ā Ā array_push ( $ans , -1); Ā Ā Ā Ā array_push ( $ans , -1); Ā Ā Ā Ā return $ans ; } Ā
// Driver code $arr = array (1, 1, 1, 1, 1, 1); Ā Ā // Output required result $res = threeEqualParts( $arr ); Ā Ā foreach ( $res as $it ) Ā Ā Ā Ā echo $it . " " ; Ā
?> |
Javascript
function findEqualParts(arr) { Ā Ā Ā Ā const N = arr.length; Ā Ā Ā Ā const ans = []; Ā Ā Ā Ā for (let i=0; i<N-2; i++) { Ā Ā Ā Ā Ā Ā Ā Ā for (let j=i+2; j<N; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let val1=0, val2=0, val3=0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let k=i; k>=0; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val1 += arr[k] * Math.pow(2, i-k); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let k=j-1; k>=i+1; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val2 += arr[k] * Math.pow(2, j-1-k); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let k=N-1; k>=j; k--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā val3 += arr[k] * Math.pow(2, N-1-k); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (val1 === val2 && val2 === val3) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.push(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans.push(j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ans; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā Ā Ā ans.push(-1); Ā Ā Ā Ā ans.push(-1); Ā Ā Ā Ā return ans; } Ā
const arr = [1, 1, 1, 1, 1, 1]; const res = findEqualParts(arr); console.log(res); |
1 4
Time Complexity: O(N3), because of two nested loops to find i and j, and then loops to find the decimal value of all three parts
Auxiliary Space: O(1), because no extra space has been used
Another Approach:Ā
Lets say total number of ones in A be S. Since every part has the same number of ones, thus all parts should have K = S / 3 ones. If S isnāt divisible by 3 i:e S % 3 != 0, then the task is impossible. Now we will find the position of the 1st, K-th, K+1-th, 2K-th, 2K+1-th, and 3K-th one. The positions of these ones will form 3 intervals: [i1, j1], [i2, j2], [i3, j3]. (If there are only 3 ones, then the intervals are each length 1.) Between the intervals, there may be some number of zeros.Ā
The zeros after third interval j3 must be included in each part: say there are z of them (z = length of (S) ā j3). So the first part, [i1, j1], is now [i1, j1+z]. Similarly, the second part, [i2, j2], is now [i2, j2+z]. If all this is actually possible, then the final answer is [j1+z, j2+z+1].Ā
Below is the implementation of above approach.Ā
C++
// C++ implementation of the // above approach #include <bits/stdc++.h> using namespace std; Ā
// Function to return required // interval answer. vector< int > ThreeEqualParts(vector< int > A) { Ā Ā Ā Ā int imp[] = {-1, -1}; Ā Ā Ā Ā vector< int > IMP(imp, imp + 2); Ā
Ā Ā Ā Ā // Finding total number of ones Ā Ā Ā Ā int Sum = accumulate(A.begin(), Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A.end(), 0); Ā
Ā Ā Ā Ā if (Sum % 3) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return IMP; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā int K = Sum / 3; Ā
Ā Ā Ā Ā // Array contains all zeros. Ā Ā Ā Ā if (K == 0) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return {0, ( int )A.size() - 1}; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā vector< int > interval; Ā Ā Ā Ā int S = 0; Ā
Ā Ā Ā Ā for ( int i = 0 ;i < A.size(); i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int x = A[i]; Ā Ā Ā Ā Ā Ā Ā Ā if (x) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā S += x; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (S == 1 or S == K + 1 or S == 2 * K + 1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.push_back(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (S == K or S == 2 * K or S == 3 * K) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.push_back(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā int i1 = interval[0], j1 = interval[1], Ā Ā Ā Ā Ā Ā Ā Ā i2 = interval[2], j2 = interval[3], Ā Ā Ā Ā Ā Ā Ā Ā i3 = interval[4], j3 = interval[5]; Ā
Ā Ā Ā Ā vector< int > a(A.begin() + i1, A.begin() + j1 + 1); Ā Ā Ā Ā vector< int > b(A.begin() + i2, A.begin() + j2 + 1); Ā Ā Ā Ā vector< int > c(A.begin() + i3, A.begin() + j3 + 1); Ā
Ā Ā Ā Ā // The array is in the form Ā Ā Ā Ā // W [i1, j1] X [i2, j2] Y [i3, j3] Z Ā Ā Ā Ā // where [i1, j1] is a block of 1s, and so on. Ā Ā Ā Ā if (!((a == b) and (b == c))) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return {-1, -1}; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // x, y, z: the number of zeros Ā Ā Ā Ā // after part 1, 2, 3 Ā Ā Ā Ā int x = i2 - j1 - 1; Ā Ā Ā Ā int y = i3 - j2 - 1; Ā Ā Ā Ā int z = A.size() - j3 - 1; Ā
Ā Ā Ā Ā if (x < z or y < z) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return IMP; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // appending extra zeros at end of Ā Ā Ā Ā // first and second interval Ā Ā Ā Ā j1 += z; Ā Ā Ā Ā j2 += z; Ā Ā Ā Ā return {j1, j2 + 1}; } Ā
// Driver Code int main() { Ā Ā Ā Ā vector< int > A = {1, 1, 1, 1, 1, 1}; Ā
Ā Ā Ā Ā // Output required result Ā Ā Ā Ā vector< int > res = ThreeEqualParts(A); Ā
Ā Ā Ā Ā for ( auto it :res) Ā Ā Ā Ā Ā Ā Ā Ā cout << it << " " ; Ā
Ā Ā Ā Ā return 0; } Ā
// This code is contributed // by Harshit Saini |
Java
// Java implementation of the // above approach import java.util.*; Ā
class GFG { Ā
// Function to return required // interval answer. public static int [] ThreeEqualParts( int [] A) { Ā Ā Ā Ā int IMP[] = new int []{- 1 , - 1 }; Ā
Ā Ā Ā Ā // Finding total number of ones Ā Ā Ā Ā int Sum = Arrays.stream(A).sum(); Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((Sum % 3 ) != 0 ) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return IMP; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā int K = Sum / 3 ; Ā
Ā Ā Ā Ā // Array contains all zeros. Ā Ā Ā Ā if (K == 0 ) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return new int []{ 0 , A.length - 1 }; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā ArrayList<Integer> interval = Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā new ArrayList<Integer>(); Ā Ā Ā Ā int S = 0 ; Ā
Ā Ā Ā Ā for ( int i = 0 ;i < A.length; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int x = A[i]; Ā Ā Ā Ā Ā Ā Ā Ā if (x != 0 ) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā S += x; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((S == 1 ) || (S == K + 1 ) || Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (S == 2 * K + 1 )) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.add(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((S == K) || (S == 2 * K) || Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (S == 3 * K)) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.add(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā int i1 = interval.get( 0 ), j1 = interval.get( 1 ), Ā Ā Ā Ā Ā Ā Ā Ā i2 = interval.get( 2 ), j2 = interval.get( 3 ), Ā Ā Ā Ā Ā Ā Ā Ā i3 = interval.get( 4 ), j3 = interval.get( 5 ); Ā
Ā Ā Ā Ā int [] a = Arrays.copyOfRange(A, i1, j1 + 1 ); Ā Ā Ā Ā int [] b = Arrays.copyOfRange(A, i2, j2 + 1 ); Ā Ā Ā Ā int [] c = Arrays.copyOfRange(A, i3, j3 + 1 ); Ā
Ā Ā Ā Ā // The array is in the form Ā Ā Ā Ā // W [i1, j1] X [i2, j2] Y [i3, j3] Z Ā Ā Ā Ā // where [i1, j1] is a block of 1s, and so on. Ā Ā Ā Ā if (!(Arrays.equals(a, b) && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Arrays.equals(b, c))) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return new int []{- 1 , - 1 }; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // x, y, z: Ā Ā Ā Ā // the number of zeros after part 1, 2, 3 Ā Ā Ā Ā int x = i2 - j1 - 1 ; Ā Ā Ā Ā int y = i3 - j2 - 1 ; Ā Ā Ā Ā int z = A.length - j3 - 1 ; Ā
Ā Ā Ā Ā if (x < z || y < z) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return IMP; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // appending extra zeros at end Ā Ā Ā Ā // of first and second interval Ā Ā Ā Ā j1 += z; Ā Ā Ā Ā j2 += z; Ā Ā Ā Ā return new int []{j1, j2 + 1 }; } Ā
// Driver Code public static void main(String []args) { Ā Ā Ā Ā int [] A = new int []{ 1 , 1 , 1 , 1 , 1 , 1 }; Ā
Ā Ā Ā Ā // Output required result Ā Ā Ā Ā int [] res = ThreeEqualParts(A); Ā
Ā Ā Ā Ā System.out.println(Arrays.toString(res)); } } Ā
// This code is contributed // by Harshit Saini |
Python3
# Python implementation of the above approach Ā
# Function to return required interval answer. def ThreeEqualParts(A): Ā Ā Ā Ā IMP = [ - 1 , - 1 ] Ā
Ā Ā Ā Ā # Finding total number of ones Ā Ā Ā Ā Sum = sum (A) Ā
Ā Ā Ā Ā if Sum % 3 : Ā Ā Ā Ā Ā Ā Ā Ā return IMP Ā
Ā Ā Ā Ā K = Sum / 3 Ā
Ā Ā Ā Ā # Array contains all zeros. Ā Ā Ā Ā if K = = 0 : Ā Ā Ā Ā Ā Ā Ā Ā return [ 0 , len (A) - 1 ] Ā
Ā Ā Ā Ā interval = [] Ā Ā Ā Ā S = 0 Ā
Ā Ā Ā Ā for i, x in enumerate (A): Ā Ā Ā Ā Ā Ā Ā Ā if x: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā S + = x Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if S in { 1 , K + 1 , 2 * K + 1 }: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.append(i) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if S in {K, 2 * K, 3 * K}: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.append(i) Ā
Ā Ā Ā Ā i1, j1, i2, j2, i3, j3 = interval Ā
Ā Ā Ā Ā # The array is in the form W [i1, j1] X [i2, j2] Y [i3, j3] Z Ā Ā Ā Ā # where [i1, j1] is a block of 1s, and so on. Ā Ā Ā Ā if not (A[i1:j1 + 1 ] = = A[i2:j2 + 1 ] = = A[i3:j3 + 1 ]): Ā Ā Ā Ā Ā Ā Ā Ā return [ - 1 , - 1 ] Ā
Ā Ā Ā Ā # x, y, z: the number of zeros after part 1, 2, 3 Ā Ā Ā Ā x = i2 - j1 - 1 Ā Ā Ā Ā y = i3 - j2 - 1 Ā Ā Ā Ā z = len (A) - j3 - 1 Ā
Ā Ā Ā Ā if x < z or y < z: Ā Ā Ā Ā Ā Ā Ā Ā return IMP Ā
Ā Ā Ā Ā # appending extra zeros at end of first and second interval Ā Ā Ā Ā j1 + = z Ā Ā Ā Ā j2 + = z Ā Ā Ā Ā return [j1, j2 + 1 ] Ā
Ā
# Driver Program A = [ 1 , 1 , 1 , 1 , 1 , 1 ] Ā
# Output required result print (ThreeEqualParts(A)) Ā
# This code is written by # Sanjit_Prasad |
C#
// C# implementation of the // above approach using System; using System.Linq; using System.Collections.Generic; Ā
class GFG { Ā
// Function to return required // interval answer. static int [] ThreeEqualParts( int [] A) { Ā
Ā Ā Ā Ā int []IMP = new int []{-1, -1}; Ā
Ā Ā Ā Ā // Finding total number of ones Ā Ā Ā Ā int Sum = A.Sum(); Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((Sum % 3) != 0) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return IMP; Ā Ā Ā Ā } Ā
Ā
Ā Ā Ā Ā int K = Sum / 3; Ā
Ā Ā Ā Ā // Array contains all zeros. Ā Ā Ā Ā if (K == 0) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return new int []{0, A.Length - 1}; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā List< int > interval = new List< int >(); Ā
Ā Ā Ā Ā int S = 0; Ā
Ā Ā Ā Ā for ( int i = 0 ;i < A.Length; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int x = A[i]; Ā Ā Ā Ā Ā Ā Ā Ā if (x != 0) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā S += x; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((S == 1) || (S == K + 1) || Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (S == 2 * K + 1)) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.Add(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((S == K) || (S == 2 * K) || Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (S == 3 * K)) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.Add(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā int i1 = interval[0], j1 = interval[1], Ā Ā Ā Ā Ā Ā Ā Ā i2 = interval[2], j2 = interval[3], Ā Ā Ā Ā Ā Ā Ā Ā i3 = interval[4], j3 = interval[5]; Ā
Ā Ā Ā Ā var a = A.Skip(i1).Take(j1 - i1 + 1).ToArray(); Ā Ā Ā Ā var b = A.Skip(i2).Take(j2 - i2 + 1).ToArray(); Ā Ā Ā Ā var c = A.Skip(i3).Take(j3 - i3 + 1).ToArray(); Ā
Ā Ā Ā Ā // The array is in the form Ā Ā Ā Ā // W [i1, j1] X [i2, j2] Y [i3, j3] Z Ā Ā Ā Ā // where [i1, j1] is a block of 1s, Ā Ā Ā Ā // and so on. Ā Ā Ā Ā if (!(Enumerable.SequenceEqual(a,b) && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Enumerable.SequenceEqual(b,c))) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return new int []{-1, -1}; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // x, y, z: the number of zeros Ā Ā Ā Ā // after part 1, 2, 3 Ā Ā Ā Ā int X = i2 - j1 - 1; Ā Ā Ā Ā int y = i3 - j2 - 1; Ā Ā Ā Ā int z = A.Length - j3 - 1; Ā
Ā Ā Ā Ā if (X < z || y < z) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return IMP; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // appending extra zeros at end Ā Ā Ā Ā // of first and second interval Ā Ā Ā Ā j1 += z; Ā Ā Ā Ā j2 += z; Ā Ā Ā Ā return new int []{j1, j2 + 1}; } Ā
// Driver Code public static void Main() { Ā Ā Ā Ā int [] A = new int []{1, 1, 1, 1, 1, 1}; Ā
Ā Ā Ā Ā // Output required result Ā Ā Ā Ā int [] res = ThreeEqualParts(A); Ā
Ā Ā Ā Ā Console.WriteLine( string .Join( " " , res)); } } Ā
// This code is contributed // by Harshit Saini |
PHP
<?php // PHP implementation of the // above approach Ā
// Function to return required // interval answer. function ThreeEqualParts( $A ) { Ā
Ā Ā Ā Ā $IMP = array (-1, -1); Ā
Ā Ā Ā Ā // Finding total number of ones Ā Ā Ā Ā $Sum = array_sum ( $A ); Ā
Ā Ā Ā Ā if ( $Sum % 3) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return $IMP ; Ā Ā Ā Ā } Ā Ā Ā Ā $K = $Sum / 3; Ā
Ā Ā Ā Ā // Array contains all zeros. Ā Ā Ā Ā if ( $K == 0) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return array (0, count (A, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā COUNT_NORMAL) - 1); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā $interval = array (); Ā Ā Ā Ā $S = 0; Ā
Ā Ā Ā Ā for ( $i = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā $i < count ( $A , COUNT_NORMAL); $i ++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā $x = $A [ $i ]; Ā Ā Ā Ā Ā Ā Ā Ā if ( $x ) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $S += $x ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ( $S == 1 or $S == $K + 1 or Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $S == 2 * $K + 1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array_push ( $interval , $i ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ( $S == $K or $S == 2 * $K or Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $S == 3 * $K ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array_push ( $interval , $i ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā $i1 = $interval [0]; $j1 = $interval [1]; Ā Ā Ā Ā $i2 = $interval [2]; $j2 = $interval [3]; Ā Ā Ā Ā $i3 = $interval [4]; $j3 = $interval [5]; Ā
Ā Ā Ā Ā $a = array_slice ( $A , $i1 , $j1 - $i1 + 1); Ā Ā Ā Ā $b = array_slice ( $A , $i1 , $j2 - $i2 + 1); Ā Ā Ā Ā $c = array_slice ( $A , $i1 , $j3 - $i3 + 1); Ā
Ā Ā Ā Ā // The array is in the formĀ Ā Ā Ā Ā // W [i1, j1] X [i2, j2] Y [i3, j3] Z Ā Ā Ā Ā // where [i1, j1] is a block of 1s, Ā Ā Ā Ā // and so on. Ā Ā Ā Ā if (!( $a == $b and $b == $c )) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return array (-1, -1); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // x, y, z: the number of zeros Ā Ā Ā Ā // after part 1, 2, 3 Ā Ā Ā Ā $x = $i2 - $j1 - 1; Ā Ā Ā Ā $y = $i3 - $j2 - 1; Ā Ā Ā Ā $z = count ( $A ) - $j3 - 1; Ā
Ā Ā Ā Ā if ( $x < $z or $y < $z ) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return $IMP ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // appending extra zeros at end Ā Ā Ā Ā // of first and second interval Ā Ā Ā Ā $j1 += $z ; Ā Ā Ā Ā $j2 += $z ; Ā Ā Ā Ā return array ( $j1 , $j2 + 1); } Ā
// Driver Code $A = array (1, 1, 1, 1, 1, 1); Ā
// Output required result $res = ThreeEqualParts( $A ); Ā
foreach ( $res as $key => $value ) { Ā Ā Ā Ā echo $value . " " ; } Ā
// This code is contributed // by Harshit Saini ?> |
Javascript
//JavaScript implementation of the above approach Ā
//function to compare two arrays Ā Ā function isEqual(a, b) Ā Ā { Ā Ā Ā Ā // If length is not equal Ā Ā Ā Ā if (a.length!=b.length) Ā Ā Ā Ā Ā return false ; Ā Ā Ā Ā else Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Comparing each element of array Ā Ā Ā Ā Ā for ( var i=0;i<a.length;i++) Ā Ā Ā Ā Ā if (a[i]!=b[i]) Ā Ā Ā Ā Ā Ā return false ; Ā Ā Ā Ā Ā Ā return true ; Ā Ā Ā Ā } Ā Ā } Ā
// Function to return required interval answer. function ThreeEqualParts(A) { Ā Ā Ā Ā var IMP = [-1, -1]; Ā
Ā Ā Ā Ā // Finding total number of ones Ā Ā Ā Ā var Sum = A.reduce( function (x, y) { Ā Ā Ā Ā Ā Ā Ā Ā return x + y; Ā Ā Ā Ā }, 0); Ā
Ā Ā Ā Ā if (Sum % 3 > 0) Ā Ā Ā Ā Ā Ā Ā Ā return IMP; Ā
Ā Ā Ā Ā var K = Sum / 3; Ā
Ā Ā Ā Ā // Array contains all zeros. Ā Ā Ā Ā if (K == 0) Ā Ā Ā Ā Ā Ā Ā Ā return [0, A.length - 1]; Ā
Ā Ā Ā Ā var interval = []; Ā Ā Ā Ā var S = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( var i = 0; i < A.length; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā x = A[i]; Ā Ā Ā Ā Ā Ā Ā Ā if (x) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā S += x; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (S == 1 || S == K + 1 || S == 2 * K + 1) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.push(i); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (S == K || S == 2 * K || S == 3 * K) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā interval.push(i); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā Ā Ā var i1 = interval[0]; Ā Ā Ā Ā var j1 = interval[1]; Ā Ā Ā Ā var i2 = interval[2]; Ā Ā Ā Ā var j2 = interval[3]; Ā Ā Ā Ā var i3 = interval[4]; Ā Ā Ā Ā var j3 = interval[5]; Ā
Ā
Ā Ā Ā Ā // The array is in the form W [i1, j1] X [i2, j2] Y [i3, j3] Z Ā Ā Ā Ā // where [i1, j1] is a block of 1s, and so on. Ā Ā Ā Ā if (!(isEqual(A.slice(i1, j1 + 1), A.slice(i2, j2 + 1)) || isEqual(A.slice(i2, j2 + 1), A.slice(i3,j3 + 1)))) Ā Ā Ā Ā Ā Ā Ā Ā return [-1, -1]; Ā
Ā Ā Ā Ā // x, y, z: the number of zeros after part 1, 2, 3 Ā Ā Ā Ā var x = i2 - j1 - 1; Ā Ā Ā Ā var y = i3 - j2 - 1; Ā Ā Ā Ā var z = A.length - j3 - 1; Ā
Ā Ā Ā Ā if (x < z || y < z) Ā Ā Ā Ā Ā Ā Ā Ā return IMP; Ā
Ā Ā Ā Ā // appending extra zeros at end of first and second interval Ā Ā Ā Ā j1 += z; Ā Ā Ā Ā j2 += z; Ā Ā Ā Ā return [j1, j2 + 1]; } Ā
Ā
// Driver Program var A = [1, 1, 1, 1, 1, 1]; Ā
// Output required result console.log(ThreeEqualParts(A)); Ā
// This code is written by phasing17 |
1 4
Complexity Analysis:
- Time Complexity: O(N), where N is the length of S.Ā
- Space Complexity: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!