Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Friday, 17 May 2019

Triplets with a given sum

Question:

Write Program to find Triplets with a given sum

Solution:


Repeating elements in an array

Question:


Write Program to find repeating elements in an array 

Solution:



Count distinct elements of an array

Question:

Write a Program to count distinct elements of an array
Solution:



The numbers of an array be made equal

Question:

Write Program to find whether the numbers of an array be made equal?


Solution:


The maximum scalar product of two vectors

Question:

Write Program to find the maximum scalar product of two vectors


Solution:


Array is a subset of another array or not

Question:

Write Program to find whether an array is a subset of another array or not


Solution:


Arrays are disjoint or not

Question:

Write a Program to find whether Arrays are disjoint or not


Solution:


Maximum product subarray in a given array

Question:

Write Program to find maximum product subarray in a given array

Solution:


Reversing an array

Question:

Write Program for Reversing an array
Solution:



Sorting the elements of an array

Question:

Write Program for  Sorting the elements of an array


Solution:


The second smallest element in an array

Question:

Write a Program to find the second smallest element in an array

Solution:


The sum of positive square elements in an array

Question:

Write a Program to find the sum of positive square elements in an array

Solution:


The minimum scalar product of two vectors

Question:

Write Program to find the minimum scalar product of two vectors
Solution:








































}

Remove duplicate elements in an array

Question:

Write Program to remove duplicate elements in an array


Solution:























  }

The longest palindrome in an array

Question:

Write Program to find the longest palindrome in an array

Solution:



The sum of elements in an array

Question:

Write Program to find the sum of elements in an array

Solution:


The smallest and largest element in an array

Question:

Write Program to find the smallest and largest element in an array

Solution:


Basic array operations

Question:

Write Program for basic array operations (Insert, delete and search an element)

Solution:

#include<stdio.h>

#include<stdlib.h>


int a[20],b[20],c[40];

int m,n,p,val,i,j,key,pos,temp;

/*Function Prototype*/void create();

void display();

void insert();

void del();

void search();

void merge();

void sort();

int main()
{
int choice;

do{
printf(
"\n\n--------Menu-----------\n");

printf("1.Create\n");

printf("2.Display\n");

printf("3.Insert\n");

printf("4.Delete\n");

printf("5.Search\n");

printf("6.Sort\n");

printf("7.Merge\n");

printf("8.Exit\n");

printf("-----------------------");

printf("\nEnter your choice:\t");

scanf("%d",&choice);

switch(choice)
{
case 1:

 create();

break;

case 2:
display();


break;

case 3:
insert();


break;

case 4:
del();


break;

case 5:
search();



break;

case 6:
sort();


break;

case 7:
merge();


break;

case 8:
exit(
0);

break;

default:printf("\nInvalid choice:\n");

break;
}

 }while(choice!=8);

return 0;
}

void create() //creating an array
{
printf(
"\nEnter the size of the array elements:\t");
scanf(
"%d",&n);

printf("\nEnter the elements for the array:\n");

for(i=0;i<n;i++)
{
scanf(
"%d",&a[i]);
}


 }//end of create()

void display() //displaying an array elements{int i;
printf(
"\nThe array elements are:\n");

for(i=0;i<n;i++)
{
printf(
"%d\t",a[i]);

}
 }//end of display()

void insert() //inserting an element in to an array{
printf(
"\nEnter the position for the new element:\t");
scanf(
"%d",&pos);

printf("\nEnter the element to be inserted :\t");
scanf(
"%d",&val);

for(i=n-1;i>=pos;i--)
{
a[i
+1]=a[i];
}

a[pos]=val;
n=n+1;
}
//end of insert()

void del() //deleting an array element{
printf(
"\nEnter the position of the element to be deleted:\t");
scanf(
"%d",&pos);

val=a[pos];

for(i=pos;i<n-1;i++)
{
a[i]
=a[i+1];
}

 n=n-1;
printf(
"\nThe deleted element is =%d",val);

}//end of delete()

void search() //searching an array element{
printf(
"\nEnter the element to be searched:\t");
scanf(
"%d",&key);

for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf(
"\nThe element is present at position %d",i);

break;
}

 }

if(i==n)
{
printf(
"\nThe search is unsuccessful");
}

 }
 }//end of search()

void sort() //sorting the array elements{for(i=0;i<n-1;i++)
{


for(j=0;j<n-1-i;j++) { 

if(a[j]>a[j+1])
{
temp
=a[j];
a[j]
=a[j+1];
a[j
+1]=temp;
}

 }
 }
printf(
"\nAfter sorting the array elements are:\n");

display();


}//end of sort

void merge() //merging two arrays{
printf(
"\nEnter the size of the second array:\t");
scanf(
"%d",&m);

printf("\nEnter the elements for the second array:\n");

for(i=0;i<m;i++)
{
scanf(
"%d",&b[i]);
}


for(i=0,j=0;i<n;i++,j++)
{
c[j]
=a[i];
}


for(i=0;i<m;i++,j++)
{
c[j]
=b[i];
}


 p=n+m;
printf(
"\nArray elements after merging:\n");


for(i=0;i<p;i++)
{
printf(
"%d\t",c[i]);
}


 }

Thursday, 16 May 2019

Frequency of each element of an array

Question:

Write Program to find Frequency of each element of an array


Solution:


Number of even and odd elements in an array

Question:

Write Program to find the number of even and odd elements in an array

Solution: