0% found this document useful (0 votes)
10 views3 pages

Arrays 3 Solutions.

The document contains solutions to various programming assignments related to arrays, including counting triplets with a specific sum, finding the factorial of a large number, identifying the first non-repeating element, and moving all zeros to the end of an array. Each solution is presented with C++ code snippets demonstrating the implementation. The document serves as a guide for solving these common array-related problems.

Uploaded by

prakashgyan1067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Arrays 3 Solutions.

The document contains solutions to various programming assignments related to arrays, including counting triplets with a specific sum, finding the factorial of a large number, identifying the first non-repeating element, and moving all zeros to the end of an array. Each solution is presented with C++ code snippets demonstrating the implementation. The document serves as a guide for solving these common array-related problems.

Uploaded by

prakashgyan1067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment Solutions | Arrays - 3 | Week 5

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;

for(int i = 0; i < 5; i++){


for(int j = i + 1; j < 5; j++){
for(int k = j + 1; k < 5; k++){
if(A[i] + A[j] == A[k]){
count++;
}
}
}
}
cout<<count<<endl;
return 0;
}

Find the factorial of a large number.

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;

Find the first non-repeating element in the array .

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;
}

Move all zeros to the end of the array.

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;
}

You might also like