Programming Questions For FCA
Programming Questions For FCA
Programming Questions For FCA
struct Result{
int output1[100];
};
// Code here
}
Ex 1
input1 = 5
input2 = {1,2,3,4,5}
input3 = 1
output1 = { -1}
Explanation: x=1 is root of the tree and has no sibling.
Ex2
input1 = 6
input2 = {1,2,3,4,5,6}
input3 = 5
output1 = {4,6}
Explanation : {2,3} are the children of {1} and {4,5,6} are children of {2,3}. thus
the sibling of x=5 are {4,6}.
----------------------------------------------------------------------------
LARGEST PRIME DIVISOR
Client loves numbers so much that he tries to find some quirky math bricks whenever
he can. One day, he decides to take up a number and tries to find out its largest
prime divisor.
Note: Largest prime divisor has to be greater that 1 and less than the number
itself. If there is no such divisor then return -1.
Input specification
input: the number N
Output specification
output: Return the largest prime divisor.
Example1
input: 9
output: 3
Here 9 divisor is only 3 which also prime. Hence output is 3.
Example2
input: 20
output: 5
Here,20 divisor are 1,2,4,5,10. The largest prime divisor is 5. Hence the output is
5.
----------------------------------------------------------------------------
CODING MARATHON
N number of the people participated in a coding marathon where they were asked to
solve some problems. Each problem carried 1 mark and at the end of marathon, the
total marks that each person achieved was calculated.
As an organizer, you have the list of total marks that each person achieved. you
have to calculate the sum of the marks ot top k scorers from the list.
input Specifications
input1: N, Total number of participants
input2: K top scorers
input3: An array of length N with scores of all N participants.
Output Specifications
Return S, sum of the marks of top K scorers from the list.
MORSE CODE
#include <stdio.h>
#include <string.h>
int main ()
{
int dot = 0, dash = 0, count = 0;
char temp[] = " ";
char *input[50] = { ".-.-.-.-", "...---.-"};
int input1 = 2;
int len;
for (int i = 0; i < input1; i++)
{
printf("%s\n", input[i]);
printf("temp:%c\n, templen-1 %c\n",input[i][0],input[i][strlen(input[i])
-1]);
if ((input[i][0] == '.') && (input[i][strlen(input[i]) -1] == '-'))
{
for (int j = 0; j < strlen(input[i]); j++)
{
if (input[i][j] == '.')
dot++;
else
dash++;
}
printf("Dash:%d\n DOt:%d\n", dash, dot);
}
if (dash == dot && dot > 0)
{
count++;
}
}
//return count;
printf("%d\n", count);
}
----------------------------------------------------------------------------
ZOMBIE APOCALYPSE
A patient with first stage zombie virus has escaped from the facility in a city of
population N. The zombie virus is very dangerous as it passes on to othr people as
they come in contact with the patient makin them a petient as well.
Zombie virus has K life stages to develop, each of which takes 1 unit of time. Only
the last stage is contagious. A person with zombie attack only healthy people and
can oly affect one person in 1 unit of time.
If the patient escaped on day 1, find out the number of days it will take to wipe
out the entire city's healthy population and turn them into a last stage zombie.
Note: 1 unit of time is equal to 1 day.
Input Specifications:
input1: Total healthy population of the city.
input2: No. of stages of zombie virus.
Output Specifications:
Your function must return the number of days.
Example1
input1:10
input2:1
output:5
The virus will evolve as soon as it is transmitted. Hence, on day2, the affected
population will be 2 and so on. On day 5, the affected population will be 16 which
is bigger than 10.
Example2
input1:10
input2:2
output:6
It takes 2 days for virus to evolve. Hence affected population on day 5 will be 8
and on day 6 it will be 13 which is bigger than 10.
#include <stdio.h>
int main()
{
int i,j,k,n,z;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]>a[i+1])
{
for(j=2;j<a[i];j++)
{
if(a[i]%j==0 && a[i+1]%j==0)
break;
}
if(j==a[i])
printf("%d",i);
else
continue;
}
}
return 0;
}
----------------------------------------------------------------------------
ZOMBIE 2
#include <stdio.h>
int main()
{
int n;
printf("no of elements n ");
scanf("%d", &n);
int a[n];
int i;
printf("Array elements ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
int high = a[0],count=0;
for(i=1;i<n;i++)
{
if(a[i]>high)
{
high=a[i];
count = i;
}
}
printf("%d ", count);
return 0;
}
----------------------------------------------------------------------------
Children’s day:
On Children's Day, a teacher wants to distribute candies to all her students, She
has N candies, and each candy has a special value. She wants all candies to have
the same special value so she decides to reduce the special values of some candies.
However, the special values of all the candies cannot be reduced.
Hence, she decides to reduce the special values for as many candies as possible so
that at least K candies have the same special value and this value must be as large
as possible. Find the maximum value that the teacher can achieve.
Input specification:
Input1: N, the total number of candies.
Input2: K, number of special value.
Input3: The array representing the special values of the candies with in
Output specification:
Return the maximum value that the teacher can achieve.
Example1:
Input1: 3
Input2: 2
Input3: {26,20,23}
Output: 23
Example 2:
Input1: 6
Input2: 4
Input3: {100,20,40,20,50,50}
Output:40
CODE:
#include<stdio.h>
int childern_candies(int input1,int input2,int*input3)
{
int i,j,temp;
for(i=0;i<input1;i++)//better to use quick sort.
{
for(j=i+1;j<input1;j++)
{
if(input3[i]<input3[j])
{
temp=input3[i];
input3[i]=input3[j];
input3[j]=temp;
}
}
if(i+1==input2)
break;
}
return input3[i];
}
----------------------------------------------------------------------------
Circle of Death
A is encircled by N soldiers in. A kills every alternate soldier from position 1 in
clockwise direction. find the position of last soldier.
#include<stdio.h>
int abraham(int n) {
int k = 2, pos = 0;
int NumOfSoldierbeforeLast;
if(pos -1 == 0) {
pos = n;
} else {
pos = pos -1;
}
int main()
{
int n, pos ;
int k = 2;
printf("Enter the number of soldier");
scanf("%d", &n);
abraham(n);
return 0;
}
----------------------------------------------------------------------------
ALT+TAB
int main()
{
int n,k,i,t;
scanf("%d %d",&n,&k);
int a[n];
for(i=0;i<n;i++)
{
a[i]=i+1;
}
t=a[k];
for(i=k;i>0;i--)
{
a[i]=a[i-1];
}
a[0]=t;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
----------------------------------------------------------------------------
Mr. Myers and the Exam
A mathematics question paper has certain number of question and each question is
assigned some random maximum mark. Mr. Myers want to edit the marks assigned to the
question such that
1. all quest in the paper should have distinct maximum marks.
2. the total marks of all the question should be as low as possible.
Mr. Myer wants to achieve this by making minimal changes in the original format,
assigning the question at least as much marks as it originally had. Find the
minimum total marks that he can set the paper for.
Input Specification
Input1: The number of questions in the paper
Input2: the array representing original marks that ssigned to every question.
Output Specification
Output : The minum total marks Mr. Myers can set the paper for.
Example1
Input1: 5
Input2: {1,2,3,4,5}
Output: 15
Explanation: As all the question already have distinct marks he can set the paper
as it is with minimum mark as 1+2+3+4+5=15
Example2
Input1: 5
Input2: {1,4,5,4,5}
Output: 23
Explanation: Here, the question1 would have atleast 1 mark, question 2 would have
at least 4 marks, question 3 would have at least 5 marks. So, the new set of marks
will have to be {1,4,5,6,7} such that all condition are satisfied.
cnt =cnt+1;
input2[i] = input2[i-1] + 1;
}
}
}
for(i=0;i<input1; i++){
printf("%d", input2[i]);
sum = sum+input2[i];
}
printf("\ncnt= %d\n", cnt);
printf("\nsum= %d\n", sum);
return sum;
}
----------------------------------------------------------------------------
WEDDING GAME
#include<stdio.h>
#include<string.h>
int main()
{
char s[1000];
int cnt=0,i,j,k,len=0,m,in,n;
scanf("%s",s);
scanf("%d",&k);
n=strlen(s);
m=k;
while(m)
{
len++;
m/=10;
}
if(len==1)
{
for(i=0;i<n;i++)
if(s[i]-48<=k)
cnt++;
}
else
{
for(i=0;i<n;)
{
if(i+len-1<n)
{
in=0;
for(j=0;j<(i+len);j++)
{
in=in*10+(s[i+j]-48);
}
if(k>in)
{
cnt++;
i=i+len-1;
}
else
{
cnt++;
i=i+len;
}}
else
{
cnt++;
break;
}
}}
printf("%d\n",cnt);
}
----------------------------------------------------------------------------
CAR RACE
There is F1 race going on and there is a just one pit crew for a team. There is a
position matrix, which contains the current position of each of these racers and
the end point, till which the point, the fuel will get over. Pit crew can put their
location to just one place. What is the maximum number of racers the pit crew can
attend to.
Input1 : X number of races.
Input2 : Position matrix of X racers.
Output Specifications : Return the maximum number of racers that can be serviced,
setting up just one pit crew.
Input1 : 4
Input2 : { {1,3}, {2,5}, {2,4} , {3,5} }
Output : 3
----------------------------------------------------------------------------
ELECTROSTATICS
Doug caught up with a rod comprising of negative(N) and positive(P) charges. He is
asked to calculate the maximum net absolute value of electrostatic field possible
in the region due to the rod.
input1 : {4,3,5}
input2 : PNP
output : (4-3+5) * 100 = 600
#include<iostream.h>
#include<string.h>
using namespace std;
int main()
{
int n,sum=0;
cin>>n;
int a[n],i,j,k;
char b[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
cin>>b;
for(i=0;i<n;i++)
{
if(b[i]=='P')
sum+=a[i];
else
sum-=a[i];
}
if(sum<0)
{
sum*=-1;
}
cout<<sum*100<<endl;
}
----------------------------------------------------------------------------
Form a necklace with X pearls with increasing order of its magnificence power.
ex :
input1 : 2
input2 : 8
input3 : 9
output : 5
(8), (9), (8,8), (8,9), (9,9)
#include<stdio.h>
int necklas_possible(int input1,int input2,int input3)
{
int n,i,j,fact1=1,fact2,fact3=1,sum=0,x;
n=input3-input2+1;
for(i=1;i<=n-1;i++)
fact1*=i;
for(i=1;i<=input1;i++)
{
fact2=1;
x=n+i-1;
for(j=1;j<=x;j++)
fact2*=j;
fact3*=i;
sum+=fact2/(fact1*fact3);
}
return sum;
}
main()
{
int pearls,starting_coef,ending_coef;
scanf("%d%d%d",&pearls,&starting_coef,&ending_coef);
printf("%d", necklas_possible(pearls,starting_coef,ending_coef));
}
----------------------------------------------------------------------------
HALINDROME
Given string S. Let us divide S to two equal parts S1 and S2. S is called
halindrome if atleast any one of the following condition satisfy.
In case of odd length string, the middle element is not present in both S1 and S2.
If index of middle element is M, then S1 is equal to S[0,m-1] and S2 = S[m+1, |S|-
1].
Input Specification
Input1: No. of strings. 1<= Input1 <=100
Output Specification
find whether string is halindrome. A string is Halindrom if any one of the criteria
is correct .
a) String S is palindrom and its length is >=2
b) String S1 is halindrom.
c) String s2 is halindrom.
if string length of S is odd then center letter is ignored and S1 is [0,
len/2 -1] and S2 is [len/2+1, len-1]
----------------------------------------------------------------------------
DOCUMENTS(DISTINCT YEARS)
The UN released an official document regarding the most important events from
begining of the time (dated 00-00-0000) with a brief description of the events. The
dateof all event is mentioned in DD-MM-YYYY format. Find the total number of
distinct years referenced in the document.
Example1
input: UN was established on 24-10-1945. India got freedom on 15-08-1947.
Output: 2
Explanation: distinct years, 1945 and 1947 have been referenced.
Example2
Input: Soon after the WW2 ended on 2-9-1945, the UN was established on 24-10-1945.
Output: 16
Explanation: Only 1 distict year i.e. 1945 have been referenced.
----------------------------------------------------------------------------
PLACEMENT
Input1: 6
Input2 : [3,5,1,4,6,9]
Output : [0,0,2,1,0,0]
#include <stdio.h>
int main()
{
int n,a[100],b[100],i,j,count=0;
printf("n ");
scanf("%d",&n);
printf("elements\n ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++)
{
count=0;
for(j=0;j<i;j++)
{
if(a[i]<a[j])
{
count++;
}
}
b[i]=count;
}
for(i=0;i<n;i++)
{
printf("%d ",b[i]);
}
return 0;
}
----------------------------------------------------------------------------
BEST FRIEND
----------------------------------------------------------------------------
Planting Trees
----------------------------------------------------------------------------
Coin Collection
----------------------------------------------------------------------------
STOCK PROBLEM
----------------------------------------------------------------------------
Coefficient
----------------------------------------------------------------------------
Social network
----------------------------------------------------------------------------
Long Number possibility
----------------------------------------------------------------------------
LCS with vowels
----------------------------------------------------------------------------
Parents - John
----------------------------------------------------------------------------
SHUNTING YARD PROBLEM
----------------------------------------------------------------------------
MISSING BOWL
----------------------------------------------------------------------------