-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargetSumArray.cpp
More file actions
137 lines (127 loc) · 3.67 KB
/
targetSumArray.cpp
File metadata and controls
137 lines (127 loc) · 3.67 KB
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
#include <iostream>
#include <vector>
#include <algorithm> //for reverse() method
using namespace std;
// Q:4
// int secondLargestEle(int arr[], int size)
// {
// int max = INT16_MIN;
// int secondMax = INT16_MIN;
// for (int i = 0; i < size; i++)
// {
// if (arr[i] > max)
// {
// max = arr[i];
// }
// }
// for (int i = 0; i < size; i++)
// {
// if (arr[i] > secondMax && arr[i] != max)
// {
// secondMax = arr[i];
// }
// }
// return secondMax;
// }
int main()
{
// int arr1[] = {1, 2, 4, 5, 3, 6};
// int ln = sizeof(arr1) / sizeof(arr1[0]);
// int x;
// int count = 0;
// cout << "Enter x: ";
// cin >> x;
// Q1: Find the total number of pairs in the array whose sum is equal to the given value of x.
// for (int i = 0; i < ln; i++)
// {
// for (int j = i + 1; j < ln; j++)
// {
// if (arr1[i] + arr1[j] == x)
// {
// count++;
// }
// }
// }
// cout << "Total number of pairs is: " << count << endl;
// Q2: Find the total number of triplets in the array whose sum is equal to the given value of x.
// for (int i = 0; i < ln; i++)
// {
// for (int j = i + 1; j < ln; j++)
// {
// for (int k = j + 1; k < ln; k++)
// {
// if (arr1[i] + arr1[j] + arr1[k] == x)
// {
// count++;
// }
// }
// }
// }
// cout << "Total triplet is: " << count << endl;
// Q3: Find the unique number in given array where all the elements are being repeated twice with one value being unique.
// int arr2[] = {1, 2, 3, 2, 3, 4, 1, 5};
// int ln2 = sizeof(arr2) / sizeof(arr2[0]);
// for (int i = 0; i < ln2; i++)
// {
// for (int j = i + 1; j < ln2; j++)
// {
// if (arr2[i] == arr2[j])
// {
// arr2[i] = arr2[j] = -1;
// }
// }
// }
// for (int i = 0; i < ln2; i++)
// {
// if (arr2[i] > 0)
// {
// cout << arr2[i] << " ";
// }
// }
// Q4: Find the second largest number in the given array.
// int arr[] = {2, 3, 5, 7, 6, 1, 7};
// int ln = sizeof(arr) / sizeof(arr[0]);
// cout << secondLargestEle(arr, ln);
// Q5: Rotate the given array 'a' by k steps, where k is non-negative. Note: k can be greater than n as well where n is the size of array 'a'.
// vector<int> v2 = {1, 2, 3, 4, 5, 6};
// int k = 2;
// k %= v2.size();
// reverse(v2.begin(), v2.end());
// reverse(v2.begin(), v2.begin() + k);
// reverse(v2.begin() + k, v2.end());
// for (int ele : v2)
// {
// cout << ele << " ";
// }
// int n = 1e5 + 10;
// cout << n;
// Q6: Given Q queries, check if the given number is present in the array or not.
// note:- value of all the elements in the array is less than 10 to the power 5.
int n;
cout << "Enter number of element do you want to insert in the array :" << endl;
cin >> n;
vector<int> v3(n);
for (int i = 0; i < n; i++)
{
cin >> v3[i];
}
const int N = 1e5 + 10; // 10^5+10
// create a vector(array) of size N and every element of the array is 0.
vector<int> freq(N, 0);
for (int i = 0; i < n; i++)
{
freq[v3[i]]++;
}
cout << "Enter a queries? : ";
int q;
cin >> q;
while (q--)
{
cout << "Enter query element? : ";
int queryElement;
cin >> queryElement;
cout << freq[queryElement] << endl;
}
cout << endl;
return 0;
}