0% found this document useful (0 votes)
5 views3 pages

Prime Balance Finder in C++

Uploaded by

nguyen04102006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Prime Balance Finder in C++

Uploaded by

nguyen04102006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream>

using namespace std;

bool checkPrime(int a){

if(a<2){

return 0;

for(int i = 2; i*i <= a; i++){ // chay i tu 2 den sqrt(a)

if(a%i == 0){

return 0;

// for(int i=2 ; i<sqrt(a); i++)

// {

// if

// }

return 1;

int findBalance(int arr[], int n){

// tra ve vi tri can bang (neu co)

// neu khong thi tra ve -1

int countPrimeL = 0 , countPrimeR = 0;

int pos = 1;

bool found = false;

while(found != true){

if(pos > n){

break;

for(int i = 0 ;i<pos; i++){

if(checkPrime(arr[i])){
countPrimeL ++;

for(int j = pos +1; j<n; j++){

if(checkPrime(arr[j])){

countPrimeR ++;

if ((countPrimeL == countPrimeR) and (countPrimeL != 0)){

found = true;

return pos;

else{

pos ++;

countPrimeL = 0;

countPrimeR = 0;

return -1;

int main() {

int n; cin>>n;

int arr[1000];

for(int i = 0 ;i<n ; i++){

cin>>arr[i];

int balancePos = findBalance(arr, n);

if(balancePos>0){
cout<<arr[balancePos]<<" "<<boolalpha<<bool(1);

else{

cout<<boolalpha<<bool(0);

return 0;

You might also like