-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance_partitioning.c
101 lines (89 loc) · 1.74 KB
/
balance_partitioning.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int sumleft(int arr[],int k){
int sum=0;
for(int i=k-1;i>=0;i--){
sum=sum+arr[i];
}
return sum;
}
int sumright(int arr[],int k,int size){
int sum=0;
for(int i=k+1;i<size;i++){
sum=sum+arr[i];
}
return sum;
}
/*
//recursive approach
int balance_partition(int arr[],int size,int N,int min,int diff){
if(min<diff){
return N+2;
}
int suml=sumleft(arr,N);
int sumr=sumright(arr,N,size);
int difference=suml-sumr;
diff=abs(difference);
if(diff<min){
min=diff;
}
return balance_partition(arr,size,N-1,min,diff);
}
*/
//balanced partition optimal
int balance_part(int N){
int a=4,d=2;
int lastterm=4+N*2;
int min=INT_MAX;
int index;
int totalsum=(N/2)*(4+lastterm);
for(int i=1;i<=N;i++){
int ithterm=4+(i*2);
int sumleft=((i/2)*(4+ithterm))-ithterm;
int sumright=(totalsum-sumleft)-ithterm;
int difference=abs(sumleft-sumright);
if(difference<min){
min=difference;
index=i;
}
// if(index<i);
// break;
}
return index;
}
int v_part(int N){
int a=4,d=2;
int sumofNterms=(N/2)*(4*2 + 2*N+4);
int min=sumofNterms;
int index;
for(int i=1;i<=N;i++){
//int ithterm=4+(i*2);
int sumleft=(i/2)*(2*4+ (i-1)*2);
int sumright=(N-i)/2 * (2 * (2*(i+1)+4) + 2*N+4);
int difference=abs(sumright-sumleft);
if(difference<min){
min=sumright-sumleft;
index=i;
}
// if(index<i);
// break;
}
return index;
}
void main(int argc, char *argv[]){
int N = atoi(argv[1]);
/*//creating the list
int arr[1000];
for(int i=0;i<N;i++){
arr[i]=(2*i)+4;
//printf(" %d",arr[i]);
}
*/
//int partition=balance_partition(arr,N,N-1,INT_MAX,0);
//printf("%d",partition);
int opt=balance_part(N);
printf("\n%d\n",opt);
int vi=v_part(N);
printf("%d",vi);
}