Created
November 29, 2016 21:11
-
-
Save parhamzm/a7b8a8512ab79c301a2bf294f988e7d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IT_Work_2.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
#include <cstdlib> | |
#include <climits> | |
using namespace std; | |
class Solution { | |
public: | |
int plenty; | |
//int P; | |
int *A; | |
Solution() { | |
//this->plenty = pe; | |
//this->P = pz; | |
} | |
void input_data() { | |
do | |
{ | |
cout << "please Enter the plenty of the Array that you want to create : " << endl; | |
cin >> this->plenty; | |
} while (this->plenty <= 0); | |
this->A = new int[this->plenty]; | |
for (int i = 0;i < this->plenty;i++) { | |
cout << "please Enter the A[" << i << "] Array : " << endl; | |
cin >> A[i]; | |
} | |
} | |
double diffrence_count() { | |
double sum1 = 0; | |
double sum2 = 0; | |
double dif = 0; | |
double ans = 0; | |
for (int k = 0;k < this->plenty;k++) { | |
ans += this->A[k]; | |
} | |
sum2 = ans; | |
//we want the program with O(1) space copsulity so : | |
for (int p = 0;p < (this->plenty);p++) { | |
dif = abs(sum2 - sum1); | |
if (dif <= ans) { | |
ans = dif; | |
} | |
sum1 += this->A[p]; | |
sum2 -= this->A[p]; | |
} | |
return ans; | |
} | |
}; | |
int main() | |
{ | |
Solution A; | |
A.input_data(); | |
double answ; | |
answ = A.diffrence_count(); | |
cout << "the result is equal with : " << answ << endl; | |
int x; | |
cin >> x; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment