C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
43 comments:
this is more easy.....
#include
#include
void main()
{
clrscr();
int a[5];
int n=5,i,j,k;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n=n-1;
}
}
}
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch();
}
this code doesn't work as a[k+1] is not defined for k=n-1.
who told this doesn't works?it works perfecctly
It works fine no issues.
please anyone can explain this
i cant understand why he took k variable here
#include
void main()
{
int a[20],i,j,m,n;
printf("enter no. of elements: ");
scanf("%d",&n);
printf("\nNow, enter the elements: ");
for(i=0;i1)
for(j=i;j<n-1;j++)
a[j]=a[j+1];
n--;
}
printf("\nThe array after removing duplicates is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
I dont think the first program can remove a no repeating 3 times
can someone give me solution in o(n) or less than o(n2) time.
plz replyyyy
very nice program sweatab malvia.............
the program below is very good to prevent duplicate entry in an array:
#include
#include
void main()
{
int a[50],i,j=0,n,t;
printf("enter the size=");
scanf("%d",&n);
printf("enter values=");
for(i=0;i=0)
{
if(a[i]==a[j])
{
printf("u have already enter the number=%d\n",a[i]);
printf("enter new value=");
scanf("%d",&a[i]);
}
j=j-1;
}
}
for(i=0;i<n;i++)
{
printf("numbers=%d\n",a[i]);
}
getch();
}
swetabh malviya's program worked correctly...thank u
really its a good one awetabh!
swetabh malviya's Program doesnt work for these test case:
Length : 5 array : 11211
Length : 5 array : 11123
And Many More....
But guys,the solutions are in n^2 complexity...
dnxxx dudes.
#include
#include
void main()
{
int a[10],i,j,n;
printf("enter the no of elements");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("original array");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
j=0;
for(i=1;i<n;i++)
{
if(a[i]!=a[j])
{
j++;
a[j]=a[i];
}
}
n=j+1;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}
one way is to sort the array using best sorting algo.
take a variable that compares each value to its previous value.
Scan array and copy each different element onto a new array
new array would have all duplicate elements removed
time cmplexity:O(nlogn) if used merge/quick/heap sort
int arrayUnique(int array[], int size) {
for ( int i = 0; i < size; i++ ) {
for ( int j = i + 1; j < size; j++ ) {
if ( array[i] == array[j] ) {
size -= 1;
for ( int k = j; k < size; k++ ) {
array[k] = array[k+1];
}
j -= 1;
}
}
}
return size;
}
To give an answer in less than O(n^2), first sort the array using any known sorting algo(for eg. merge-sort) which requires a time complexity of O(nlogn) and then check every element with the element on its right for duplicity.
So, overall it requires a time complexity of O(nlogn)
another simple code for deletion of duplicate elements in an array....
#include
#include
void swap(int a[],int,int);
void main()
{
int n,a[20],i,j;
printf("Input the number of elements...");
scanf("%d",&n);
printf("\nInput the elements...");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Elements before deletion...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=(i+1);j<n;j++)
{
if(a[i]==a[j])
{
swap(a,j,n);
j=j-1;
n=n-1;
}
}
}
printf("\nElements after deletion...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void swap(int a[],int j,int n)
{
while(j<(n-1))
{
a[j]=a[j+1];
j++;
}
}
this program does not work for more then one duplicate element..........
its easy thnks...
i didnt revome more than one duplication
/*this is for to remove duplicate elements from array*/
#include
int main()
{
int a[8]={11,3,4,3,1,6,3,4};
int n=8,i,j,k;
printf("array element are:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
a[j]=0;
}
}
}
printf("\nafter deleting duplicate element are:");
for(i=0;i<n;i++)
{
if(a[i]==0)
{
continue;
}
else
{
printf("%d\t",a[i]);
}
}
return 0;
}
This one works
#include
void main(){
int a[30],n,h,i,j,k,l,x;
printf("Enter number of elements in the array:");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(h=0;h<2;h++){
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
for(k=j;k<n-1;k++){
a[k]=a[k+1];
}
n=n-1;
}
}
}
}
printf("Now the array is:\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
}
It works!
#include
int main()
{
int x[10]={8,3,5,4,4,3,1,8,5,9},n=10,i,k,t,count=0,j;
for(i=0;i0)
{
i--;
n--;
}
}
}
n=n-count;
printf("the array is:");
for(i=0;i<n;i++)
{
printf("%d",x[i]);
}
}
can u correct my logic
#include
int main()
{
int x[10]={8,3,5,4,4,3,1,8,5,9},n=10,i,k,t,r,count=0,j;
for(i=0;i<n;i++)
{
k=i;
for(j=k+1;k<n;j++)
{
if(x[k]==x[j])
{
x[k]=x[j];
for(t=j+1;t<n;t++)
{
x[j]=x[t];
}
n--;
count++;
}
}
}
r=n-count;
printf("the array is:");
for(i=0;i<n;i++)
{
printf("%d",x[i]);
}
}
can u tel me whr is wrng
Even swetha's code doesn't work....take input as "1,1,1,1,2,2,3,4,5,5". output is "1,1,2,3,4,5".
It is easy if u take a flag and take an array. Initialize the first element of the new array to the input array and check if elements. If the elements are equal, then make flag as '0', initially flag is '1'. if the element is not equal then insert the element to the new array in the next position and continue. This even requires two for loops. and the condition for second for loop is till the size of the new array.
I tried this code did not remove more than one duplicate .please anyone explain
#include
#include
int main ()
{
int n,a[100], b[100],i,j,count=0;
printf("How many no. do you want to enter ?");
scanf("%d",&n);
printf("Enter %d no.s of the array ",n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for (j=0;j<count;j++)
if (a[i]==b[j]) break;
if (j==count)
{
b[count]=a[i];
count++;
}
}
printf("\nthe array elements after deleting duplicates is :");
for (i=0;i<count;i++)
printf("\n%d",b[i]);
getch();
}
helpfull
i want one to help me please :/
Really Good Program easy to understood,thanks.
How to replace the duplicate element with 0?
i can't understand plz explain
dont need third inner loop
just 2 loop will remove duplicate
and to remove a duplicate value we need not replace all the values after duplicate
just swap the last value with duplicate and decrease size
void del_duplicate(int *array,int *size){
int i,j,newsize=*size;
for(i=0;i<newsize;i++){
for(j=i+1;j<newsize;j++){
if(array[i]==array[j] && array[newsize-1]!=array[i]){
newsize--;
array[j]=array[newsize];
}else if(array[i]==array[j]){
newsize--;
j=i;
}
}
}
*size = newsize;
}
dont need third inner loop
just 2 loop will remove duplicate
and to remove a duplicate value we need not replace all the values after duplicate
just swap the last value with duplicate and decrease size
void del_duplicate(int *array,int *size){
int i,j,newsize=*size;
for(i=0;i<newsize;i++){
for(j=i+1;j<newsize;j++){
if(array[i]==array[j] && array[newsize-1]!=array[i]){
newsize--;
array[j]=array[newsize];
}else if(array[i]==array[j]){
newsize--;
j=i;
}
}
}
*size = newsize;
}
In c program I need remove duplicate array and reverse the array ,size of the removed duplicate array
Post a Comment