-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq13)count number of smaller elements .c
143 lines (141 loc) · 3.5 KB
/
q13)count number of smaller elements .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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// q13)count no. of smaller elements on the right of each element in an array
//Approach1:using bruteforce O(n^2)
#include<stdio.h>
#include<stdlib.h>
int main(int argc, const char * argv[]) {
int arr[]={10,3,4,5,7,1,3,2};
int n=sizeof(arr)/sizeof(arr[0]);
int res[8]={0};
int d,count=0;
for(int i=0;i<n;i++)
{
d=arr[i];
for(int j=i+1;j<n;j++)
{
if(d>arr[j])
{
count++;
}}
res[i]=count;
count=0;
}
for(int k=0;k<n;k++){
printf("%d\t",res[k]);
}
//Approach2:Using AVL leads to T.C=O(n)
/*
struct node
{
int data, height, size;
struct node *left, *right;
};
int height(struct node *root)
{
return !root ? 0: root->height;
}
int size(struct node *root)
{
return !root ? 0: root->size;
}
int max(int a, int b)
{
return (a > b)? a : b;
}
struct node* newNode(int data)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right = NULL;
temp->height = temp->size = 1;
return temp;
}
struct node *rightRotate(struct node *root)
{
struct node *temp1 = root->left;
struct node *temp2 = temp1->right;
temp2 = root;
root->left = temp2;
root->height = max(height(root->left), height(root->right)) + 1;
temp1->height = max(height(temp1->left), height(temp1->right))+1;
root->size = size(root->left) + size(root->right) + 1;
temp1->size = size(temp1->left) + size(temp1->right) + 1;
return temp1;
}
struct node *leftRotate(struct node *root)
{
struct node *temp1 = root->right;
struct node *temp2 = temp1->left;
temp1->left = root;
root->right = temp2;
root->height = max(height(root->left), height(root->right)) + 1;
temp1->height = max(height(temp1->left), height(temp1->right)) + 1;
root->size = size(root->left) + size(root->right) + 1;
temp1->size = size(temp1->left) + size(temp1->right) + 1;
return temp1;
}
int getBalance(struct node *root)
{
return !root ? 0: height(root->left) - height(root->right);
}
struct node* insert(struct node *root, int data, int *count)
{
if (!root)
return newNode(data);
if (data < root->data)
root->left = insert(root->left, data, count);
else
{
root->right = insert(root->right, data, count);
*count += size(root->left) + 1;
}
root->height = max(height(root->left), height(root->right)) + 1;
root->size = size(root->left) + size(root->right) + 1;
int balance = getBalance(root);
if(balance > 1 && data < root->left->data)
return rightRotate(root);
if(balance < -1 && data > root->right->data)
return leftRotate(root);
if(balance > 1 && data > root->left->data)
{
root->left = leftRotate(root->left);
return rightRotate(root);
}
if(balance < -1 && data < root->right->data)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
void countSmallerArray (int *arr, int *smaller, int size)
{
int index;
struct node *root = NULL;
for(index = 0; index < size; index++)
smaller[index] = 0;
for(index = size - 1; index >= 0; index--)
root = insert(root, arr[index], &smaller[index]);
}
void printArray(int *arr, int size)
{
for(int index = 0; index < size; index++)
printf("%d\t", arr[index]);
}
int main()
{
int *arr, size, *lower;
printf("Enter size of the array\n");
scanf("%d", &size);
//allocate memory
arr = (int *)malloc(sizeof(int) * size);
lower = (int *)malloc(sizeof(int) * size);
printf("Enter elements in array\n");
for(int index = 0; index < size; index++)
scanf("%d", &arr[index]);
countSmallerArray(arr, lower, size);
printArray(lower, size);
return 0;
}
*/
return 0;
}