C Code 33 _ find the sum of STABLE and UNSTABLE elements in the array and show their difference

C Code 33 _ find the sum of STABLE and UNSTABLE elements in the array and show their difference


Problem:


Given a array of integers and their size,
find the sum of STABLE and UNSTABLE elements in the array and
show their difference.

Sample Input:

5
11 50 112 22 111

Sample Output:  

stable=11 50 22 111
unstable=112
difference=82


Explanation:

STABLE-numbers with equal number of each digit-a number in which frequency of digits are equal.
example:-
112233 is a stable number since,
        1 occurs twice , 2 occurs twice so does 3
        i.e all digits in a given number must have equal frequencies
        Similarly,
123 (1,2,3 each occur once)
355533 (3,5 each occur thrice)
are STABLE NUMBERS.
**************************************************
8892 is not a stable number(UNSTABLE),(8's frequency 2, 9 & 2 frequency 1)
Similarly,
2122(2 occurs thrice,1 only once)
5667(6 occurs twice where as 5 & 7 occur only once)
are UNSTABLE NUMBERS.


Program:


#include<stdio.h>
bool isStable(int n)
{
int freq[10],Freqf[10];
//freq-to find the frequency of each digit
for(int i=0;i<10;i++)
freq[i]=Freqf[i]=0;
while(n!=0)
{
freq[n%10]++;
n/=10;
}
for(int i=0;i<10;i++)
{  
if(freq[i]==0)
continue;
Freqf[freq[i]]++;
//Freqf-to find the frequency of frequency
}
for(int i=0;i<10;i++)
{
if(Freqf[i]>0)
n++;
if(n>1)
return 0;    
}
return 1;
}
int main()
{
int size,inp[15],diff=0,stable[15],unstable[15],i,k,j;
scanf("%d",&size);
for(i=k=j=0;i<size;i++)
{
scanf("%d",&inp[i]);
if(isStable(inp[i]))
{
diff+=inp[i];
stable[k++]=inp[i];
}
else
{
diff-=inp[i];
    unstable[j++]=inp[i];
    }
}
printf("\nstable=");
for(i=0;i<k;i++)
printf("%d ",stable[i]);
printf("\nunstable=");
for(i=0;i<j;i++)
printf("%d ",unstable[i]);
printf("\ndifference=%d",diff);
}



Output:













Thank You ...

* * * * * * * The above Program is contributed by AjayRam KV * * * * * * * *
Previous Post
Next Post
Related Posts

0 comments:

Popular Posts