0% found this document useful (0 votes)
14 views7 pages

Array

The document contains multiple C++ programs demonstrating array manipulation. It includes initializing an array, summing two arrays into a third, finding indices of two numbers that add up to a target, and identifying a single unique element in an array where every other element appears twice. Each program is accompanied by example inputs and expected outputs.

Uploaded by

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

Array

The document contains multiple C++ programs demonstrating array manipulation. It includes initializing an array, summing two arrays into a third, finding indices of two numbers that add up to a target, and identifying a single unique element in an array where every other element appears twice. Each program is accompanied by example inputs and expected outputs.

Uploaded by

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

1.

declare an array of 15 integers and


initialize the array so that all array
elements are zero, then assign 10 to
the 1st element and 150 the last
element.

#include <iostream>
using namespace std;
int main() {
int arr[15]={0};
arr[0]=10;
arr[14]=150;
for(int i=0;i<15;i++)
{
cout<<"index of of element: "<<i<< "
value at index "<<arr[i]<<endl;
}
return 0;
}

2. WAP to read 2 array elements each


size 5 and store the sum of these 2
arrays into the 3rd array.

#include <iostream>
using namespace std;
int main() {
int i=0;
int arr1[5],arr2[5],arr_sum[5];
cout<<"enter 1st array element";
for(int i=0;i<5;i++)
{
cin>>arr1[i];
}
cout<<"enter 2nd array element";
for(int i=0;i<5;i++)
{
cin>>arr2[i];
}

for(int i=0;i<5;i++)
{
arr_sum[i]=arr1[i]+arr2[i];
cout<<"sum at index "<<i<<"
is”<<arr_sum[i]<<endl;
}
return 0;
}
3. Given an array of integers nums & an
integer target, return indices of the two
numbers such that they add up to target.
You may assume that each input would
have exactly one solution and you may
not use the same element twice. You can
return the answer in any order.

Eg input: nums [2,7,11,15] target=9 output:


[0,1]
Input :num[2,6,3,9] target=5 output :[0,2]

Program: nums[2,7,11,15] target is 26 and


output is [2,3]
#include <iostream>
using namespace std;
int main() {
int i=0;
int nums[4]={2,7,11,15};
int target=26;
for(int i=0;i<4;i++)
{
for(int j=i+1; j<4;j++)
{
if(target==(nums[i]+nums[j]))
{
cout<<i<<" "<<j<<endl;
}}}
cout<<"no such element is existing in this
array";
return 0;
}
4. Given a non-empty array of integers
nums, every element appears twice
except for one. Find that single one.
You must implement a solution with a
linear runtime complexity and use only
constant extra space
input:nums=[2,2,1]
Output:1
input:nums=[4,1,2,1,2]
Output:4

Program:
#include <iostream>
using namespace std;
int main() {
int nums[5]={4,1,2,1,2};
int ans=0;
for(int i=0;i<5;i++)
{
ans=ans^ nums[i];

cout<<ans;
return 0;
}

You might also like