1006C - Three Parts of the Array - CodeForces Solution


binary search data structures two pointers *1200

Please click on ads to support us..

Python Code:

input()
elementos = list(map(int, input().split()))

maior_soma = 0
soma_esquerda = 0
soma_direita = 0

esquerda = 0
direita = len(elementos) - 1

controle_incremento = 0

while esquerda < direita:

    if controle_incremento == 0:
        soma_esquerda += elementos[esquerda]
        soma_direita += elementos[direita]
    elif controle_incremento == 1:
        soma_esquerda += elementos[esquerda]
    else:
        soma_direita += elementos[direita]

    if soma_esquerda < soma_direita:
        esquerda += 1
        controle_incremento = 1
    elif soma_esquerda > soma_direita:
        direita -= 1
        controle_incremento = 2
    else:
        maior_soma = soma_esquerda
        esquerda += 1
        direita -= 1
        controle_incremento = 0

print(maior_soma)

 	 				    			  	 	 						 				

C++ Code:

#include<iostream>
using namespace std;
int main(){
    long long int n;
    // cout<<"check21";
    cin>>n;
    long long int arr[n];
    // vector<int>arr;
    // int arr[7]={1,2,3,4,5,6,7};
    for(long long int i=0;i<n;i++){
        cin>>arr[i];
    }
    
    long long int s1=arr[0];
    long long int s2=arr[n-1];
    long long int ans=0;
    long long int i=0;
    long long int j=n-1;
    // cout<<s1<<s2<<ans<<i<<j;

    while(i<j){
        if(s1==s2){
            ans=s1;
            // cout<<ans<<endl;
            i++;
            s1=s1+arr[i];
            j--;
            s2=s2+arr[j];
        }
        else if(s1<s2){
            i++;
            s1=s1+arr[i];
            // cout<<s1<<endl;
        }
        else if(s1>s2){
            j--;
            s2=s2+arr[j];
            // cout<<s2<<endl;
        }
    }
    cout<<ans;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST