Arrays 3 Solutions.
Arrays 3 Solutions.
Count the number of triplets whose sum is equal to the given value x.
Solution:
#include <iostream>
using namespace std;
int main() {
int x;
cin>>x;
int A[5];
cout<<”Enter 5 elements for the array”<<endl;
for(int i=0;i<5;i++)cin>>A[i];
int count = 0;
Solution:
#include <iostream>
using namespace std;
int mul(int x, int res[], int res_size){
int carry = 0;
for (int i = 0; i < res_size; i++) {
int prod = res[i] * x + carry;
res[i] = prod % 10;
carry = prod / 10;
}
while (carry) {
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
int main() {
int n;
cin>>n;
int res[500];
res[0] = 1;
int res_size = 1;
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[5]={1,2,2,4,7};
int n=5;
for (int i = 0; i < n; i++) {
int j;
// Checking if ith element is present in array
for (j = 0; j < n; j++)
if (i != j && arr[i] == arr[j])break;
if (j == n){
cout<<arr[i];
return 0;
}
}
return 0;
}
Solution:
#include <iostream>
using namespace std;
int main(){
int A[] = { 0, 6, 0, 7, 6, 0, 9, 1 };
int n = 8;
int j = 0;
for (int i = 0; i < n; i++) {
if (A[i] != 0) {
swap(A[j], A[i]);
j++;
}
}
for (int i = 0; i < n; i++) {
cout << A[i] << " ";
}
return 0;
}