Posts

Showing posts with the label ARRAYS & LOOPS

Akash the die heart fan of AR Rahman went to the live concert happened in Bangalore with his family members.The event management firm responsible for the event arranged the seats for the audience in descending order of maximum number of tickets booked for single family.

 #include <stdio.h> int main() {int nooffamilymembers; scanf("%d",&nooffamilymembers); int r=nooffamilymembers; for(int i=1;i<=nooffamilymembers;i++){     for(int j=1;j<=r;j++){         printf("%d ",r);     }r--;     printf("\n");} return 0; }

Laaysa with his friends going to the theatre for a movie. The seating arrangement is triangular in size. Theatre staffs insisted the audience to sit in odd row if the seat number is odd and in even row if the seat number is even.But the instruction is very confusing for Laaysa and his friends.So help them with the seating layout so that they can sit in correct seats.

 #include<stdio.h> int main() {int t,i,j,c; scanf("%d",&t); for(i=1;i<=t;i++){     if(i%2==0)     c=2;     else     c=1;     for(j=1;j<=i;j++){         printf("%d ",c);         c+=2;     }printf("\n"); }              return 0;}

Ambikapathy wants to decorate his girlfriend Amaravathi's house with a series of lights. The number of lights is M. Initially, all the lights are switched off. Ambikapathy finds that there are K numbers of buttons, each of them is connected to a set of lights. Ambikapathy can press any of these buttons. When the button is pressed, it turns on all the lights it’s connected to. Can Ambikapathy accomplish his task to switch on all the lights? If Ambikapathy presses the button such that some lights connected to it are already turned on, they do not change their state, i.e. remain turned on. Help him in his task to impress his valentine.

 #include <stdio.h> int main() {  int  k,m,count=0; scanf("%d %d",&k,&m); while(k--) {  int lights[m];  int i; for(i=0;i<m;i++) { scanf("%d",&lights[i]); } for(i=0;i<m;i++){ if(lights[i]<=m); else{ count+=1; } } } if(count==0){ printf("YES"); } else{ printf("NO"); } return 0; }

The Matriculation school have arranged an Annual Day Function.Volunteers have decorated a floor on various places of the school using Rose and Tulip flowers. But one of the coordinators requested the volunteers to rearrange the decoration like a triangular size.Coordinator also told them that tulips flowers need to be positioned at the middle of the roses

 #include <stdio.h> int main() {int rows; scanf("%d",&rows); for(int i=1;i<=rows;i++){     for(int j=1;j<=i;j++){         if(i==rows||i==j||j==1) printf("1 ");         else printf("0 ");     }printf("\n"); } return 0; }

Pari is an Architect who is currently doing his design work for his new project in one of the congested location of Paris.For making his work simpler he is looking for the automated tool which check whether the area is greater or perimeter is greater or both are equal if the Length (L) and Breadth (B) of a rectangle is provided.

 #include <stdio.h> int main() {int l,b,area,peri; scanf("%d\n%d",&l,&b); area=l*b; peri=2*(l+b); if(area>peri) printf("Area\n%d",area); else if(area<peri) printf("Peri\n%d",peri); else printf("Eq\n%d",area);                return 0;}

You like tracking airplane flights a lot. Specifically, you maintain history of an airplane’s flight at several instants and record them in your notebook. Today, you have recorded N such records h1, h2, ..., hN, denoting the heights of some airplane at several instants. These records mean that airplane was first flying on height h1, then started changing its height to h2, then from h2 to h3 and so on. The airplanes are usually on cruise control while descending or ascending, so you can assume that plane will smoothly increase/decrease its height from hi to hi + 1 with a constant speed. You can see that during this period, the airplane will cover all possible heights in the range [min(hi, hi+1), max(hi, hi+1)] (both inclusive). It is easy to see that the plane will be at all possible heights in the range exactly a single instant of time during this ascend/descend.You are interested in finding the maximum integer K such that the plane was at some height exactly K times during the flight.

 #include<stdio.h> int main() {   int h[100001],i,j,max=0,n;     scanf("%d",&n);     for(i=0;i<n;i++)     { scanf("%d",&h[i]);       if(h[i]>max)        max=h[i];     }     int s[100]={0};     for(i=0;i<n-1;i++)      { if(h[i]>h[i+1])        {         for(j=h[i+1];j<h[i];j++)            s[j]++;        }         if(h[i]<h[i+1])          {            for(j=h[i];j<h[i+1];j++)            s[j]++;          }      }      int k=0;      for(j=0;j<max;j++)      { if(s[j]>k)          k=s[j];      }      pr...

The software tool is a repository of files, often the files for the source code of computer programs, with monitored access. Every change made to the source is tracked, along with who made the change, why they made it, and references to problems fixed, or enhancements introduced, by the change.Software tools are essential for any form of distributed, collaborative development. Whether it is the history of a wiki page or large software development project, the ability to track each change as it was made, and to reverse changes when necessary can make all the difference between a well managed and controlled process and an uncontrolled ‘first come, first served’ system. It can also serve as a mechanism for due diligence for software projects.In this problem we'll consider a simplified model of a development project. Let's suppose, that there are N source files in the project. All the source files are distinct and numbered from 1 to N.The tool, that is used for maintaining the project, contains two sequences of source files. The first sequence contains the source files, that are ignored by the tool. If a source file is not in the first sequence, then it's considered to be unignored. The second sequence contains the source files, that are tracked by the tool. If a source file is not in the second sequence, then it's considered to be untracked. A source file can either be or not be in any of these two sequences.

 #include <stdio.h> int main() { int t,i; scanf("%d",&t); while(t--){     int num[3];     int f[100]={0};     for(i=0;i<3;i++){         scanf("%d ",&num[i]);   }     int m=num[1], k=num[1];     int a[m],b[k];     for(i=0;i<num[1];i++){         scanf("%d ",&a[i]);         f[a[i]-1]++; }     for(i=0;i<num[2];i++){         scanf("%d ",&b[i]);         f[b[i]-1]++;  }     int tracigno=0;     int untracinigno=0;     for(i=0;i<num[0];i++){         if(f[i]==0){             untracinigno++;   }         else if(f[i]==2){             tracigno++;         }  }   ...

Ganapathy the Mathematics professor distributed the answer sheets for his students after the examination.His class had students who have passed the exam as well as the students who have failed in the exam.In order to have the proper analysis of individual student performances in his class he have ordered them to sit in the order were the students passed the exam should sit in ODD numbered row and the students who have failed in exam should sit in EVEN numbered row. The total number of rows in which students have to sit will be given by the professor. But since the dimension of the class is triangular in shape the students got confused. Can you help them with the order in which they have to sit if the number of rows is given by the professor?

#include <stdio.h> int main() {  int noofrowsinclass; int i,j; scanf("%d",&noofrowsinclass); for(i=1;i<=noofrowsinclass;i++) { for(j=1;j<=i;j++) {     if(i%2==0)     {         printf("Fail ");     }     else     {         printf("Pass ");     }     }     printf("\n");     }     return 0; } 

Asraf has N lights, arranged in a line, with him. A[i] denotes the initial state of 'i'th light. He wants to toggle some lights, but he can only toggle the lights in ranges. Toggling a light means changing the state of the light. That is, if the light was ON then after toggling it becomes OFF. He does this 'range toggling' Q times. In the 'i'th range toggling, he toggles the lights all the lights between Li and Ri lights (Li and Ri inclusive). You need to find the final states of all the N lights after these Q toggles.A[i] = 1 means the light is ON and A[i] = 0 means the light is OFF

 #include <stdio.h> int main() {   int lights[100001],n,q,i,a,b;   scanf("%d %d",&n,&q);   for(i=0;i<n;i++)   {scanf("%d",&lights[i]);}   while(q-->0)   {scanf("%d %d",&a,&b);   for(i=a-1;i<b;i++)   lights[i]=!lights[i];   }   for(i=0;i<n;i++){    printf("%d ",lights[i]);} return 0; }

Rakesh has given an array of integers and you need to find out if the absolute difference of values of any two consecutive array integers is at-most D.

 #include <stdio.h> #include <stdlib.h> int main() {     int t;     scanf("%d",&t);     for(int i=1;i<=t;i++){         int n,k;int tag=0;         scanf("%d %d",&n,&k);         int integers[n];         for(int j=0;j<n;j++){             scanf("%d",&integers[j]);         }         for(int l=0;l<=n-1;l++){             if(abs(integers[l+1]-integers[l])<=k)tag++;         }         if(tag==n-1) printf("YES\n");         else printf("NO\n");     } return 0; }

Caleb likes to challenge Selvan's math ability. He will provide a starting and ending value that describes a range of integers, inclusive of the endpoints. Selvan must determine the number of square integers within that range.

 #include<stdio.h> #include<math.h> void Clac_square(long int start,long int end) { int i,count=0; for(i=start;i<=end;i++) { int n=sqrt(i); if(n==sqrt(i))     count++;} printf("%d\n",count); } int main() { long int q,start,end; scanf("%ld",&q); while(q--){     scanf("%ld %ld",&start,&end);     Clac_square(start,end);}     return 0; }

Mahesh has given a two-dimensional 3*3 array starting from A [0][0]. You should add the alternate elements of the array and print its sum. It should print two different numbers the first being sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 and A 0 1, A 1 0, A 1 2, A 2 1.

 #include <stdio.h> int main() {   int A[3][3],i,j;     for(i=0;i<3;i++)     {for(j=0;j<3;j++)     {scanf("%d ",&A[i][j]);}}     int m=0,n=0;     for(i=0;i<3;i++){     for(j=0;j<3;j++){     if((i+j)%2==0)     m=m+A[i][j];     else     n=n+A[i][j];}}     printf("%d\n%d",m,n); return 0; }

Mukesh and Salima was looking to buy Organic Apples in the nearest fruits shop.But Shop Owner mixed the Hybrid apples in that Apple box. After sometimes Mukesh checking the box, he was confused among organic and hybrid apple. He started to count the hybrid apples. Please help to Mukesh Count the total number of hybrid apples in the box.

 #include <stdio.h> #include<string.h> int main() {int numofapples ,x,i,c=0; scanf("%d",&numofapples); int arr[100]={0}; for(i=0;i<numofapples;++i){     scanf("%d",&x);     arr[x]++; } for(i=0;i<100;++i){     if(arr[i]>1)c+=(arr[i]-1);     } printf("%d",c); return 0; }

Once N Men and M Women attended a matrimonial event. The event is represented by a matrix named "a" of N rows and M columns where Aij is 1 if the i-th Men likes the j-th Women.Otherwise it will be 0. Note that it is not necessary that if a Men x likes Women y, then Women y should like Men x.If there are two different Men x and y, who both like Women z, then there will be a collision. Can you calculate the number of different collisions in the matrimonial event? Note that order of Men in the collision doesn't matter.

 #include <stdio.h> int main() {     int t,men,women,collisions=0;     scanf("%d",&t);     while(t--)     {scanf("%d%d",&men,&women);          char a[men][women];int i;          for(i=0;i<men;i++)          scanf("%s",a[i]);          int pro,flag,j; collisions=0;          for(j=0;j<women;j++){             pro=0;             for(i=0;i<men;i++)          if(a[i][j]=='1')          pro++;          flag=pro*(pro-1)*0.5;          collisions+=flag;     }     printf("%d\n",collisions); } return 0; }

Let's consider a triangle of numbers in which one number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a logic which will compute the largest of the sums of numbers that appear on the paths starting from the top towards the base, so that:1. On each path the next number is located on the row below, more precisely either directly below or below and one place to the right;2. The number of rows is strictly positive, but less than 1003. All numbers are positive integers between 0 and 99.

 #include <stdio.h> int main() {     int i,n,j,t;     scanf("%d",&t);     while(t--)     {     scanf("%d",&n);     int ar[n][n];     for(i=0;i<n;i++){      for(j=0;j<=i;j++){     scanf("%d",&ar[i][j]);     scanf("\n");}     }     for(i=n-1;i>=1;i--)     {     for(j=0;j<i;j++)     {if(ar[i][j]>ar[i][j+1])     ar[i-1][j]+=ar[i][j];     else ar[i-1][j]+=ar[i][j+1];     }     }     printf("%d\n",ar[0][0]);     } return 0; }

You like tracking airplane flights a lot. Specifically, you maintain history of an airplane’s flight at several instants and record them in your notebook. Today, you have recorded N such records h1, h2, ..., hN, denoting the heights of some airplane at several instants. These records mean that airplane was first flying on height h1, then started changing its height to h2, then from h2 to h3 and so on. The airplanes are usually on cruise control while descending or ascending, so you can assume that plane will smoothly increase/decrease its height from hi to hi + 1 with a constant speed. You can see that during this period, the airplane will cover all possible heights in the range [min(hi, hi+1), max(hi, hi+1)] (both inclusive). It is easy to see that the plane will be at all possible heights in the range exactly a single instant of time during this ascend/descend.You are interested in finding the maximum integer K such that the plane was at some height exactly K times during the flight.

 #include <stdio.h> int main() {int h[100001]; int i,j,max=0,n; scanf("%d",&n); for(i=0;i<n;i++) {scanf("%d",&h[i]); if(h[i]>max) max=h[i];} int s[100]={0}; for(i=0;i<n-1;i++) {if(h[i]>h[i+1]){ for(j=h[i+1];j<h[i];j++)     s[j]++;} if(h[i]<h[i+1]) {for(j=h[i];j<h[i+1];j++)s[j]++;}} int k=0; for(j=0;j<max;j++) {if(s[j]>k) k=s[j];} printf("%d",k); return 0;}

Today is Caleb's birthday. His dad has surprised him with truly fruity gifts: 2 fruit baskets. The first basket contains N Avacados, and the second one contains M dragon fruits. Caleb likes Avacados and Dragon fruits very much but he likes them equally, and therefore, wants to have the minimum possible difference between the number of Avacados and Dragon fruits he has. To do so, he can purchase 1 Avacado or 1 Dragon fruit by paying exactly 1 gold coin (that's some expensive fruit, eh?). Caleb can purchase fruits at most K times (as he has only K gold coins in his pocket) to make the difference the minimum possible.

 #include <stdio.h> int main() {int t,n,m,k; scanf("%d",&t); while(t>0) {scanf("%d %d %d",&n,&m,&k); while(k>0) {if(n>m) {m++;} else if(m>n) {n++;} k--;} printf("%d\n",(n-m)); t--;} return 0; }

Mr. Arulmozhivalman loves programming and he likes to face new programming challenges. After completing many challenges he has now given you one challenge which is one of his favourites. He has given you a list of N random integers and he wants you to find the integer which has the maximum frequency in the given list.Being a programmer himself, he had made this task a challenge for you and he will calculate your score for this task using a formula.Formula for calculating the score : (max_score) - (number of characters in your code/15.0)Mr. Arulmozhivalman already has a solution for this but he is not satisfied with his solution. He wants you to write a shortest possible code for this task.

 #include <stdio.h> int main() {     int FreqArr[100000];     int Size,m=0,c=0,e=0;     int i,j;     scanf("%d",&Size);     for(i=0;i<Size;i++)     {scanf("%d",&FreqArr[i]);}     for(i=0;i<Size;i++){c=1;     for(j=1+i;j<Size;j++){         if (FreqArr[i]==FreqArr[j])         c++;}         if(m<c)         {m=c;         e=FreqArr[i];}}     printf("%d",e); return 0; }

Nathan is a Researcher in English Literature. According to him string which when split in the middle, gives two halves having the same characters and the same frequency of each character is termed as Lapindrome.

 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() {int arr1[26],arr2[26]; int t; scanf("%d",&t); char S[1000];int i=0; while(i<t){     scanf("%s",S);     int l=strlen(S);     arr1[0]=0;arr2[0]=0;     for(int j=0;j<l;j++){         arr1[0]+=S[j]*(j<l/2);         if(l%2==0) arr2[0]+=S[j]*(j>=l/2);         else arr2[0]+=S[j]*(j>l/2);     }     if(arr1[0]==arr2[0])printf("YES\n");     else printf("NO\n");     i++; } return 0; }

Fazil loves to preform different operations on arrays, and so being the Head of the higher education institution, he assigned a task to his new student Rohan. Rohan will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array.

#include<stdio.h> int main() {int rot[100000]; int n,k,i,j,temp,T; scanf("%d",&T); while(T--){     scanf("%d %d",&n,&k);     for(j=0;j<n;j++)     {scanf("%d ",&rot[j]);}     for(j=0;j<k;j++)     {temp=rot[n-1];         for(i=n-1;i>0;i--)         {rot[i]=rot[i-1];} rot[i]=temp;     }     for(j=0;j<n;j++)     {printf("%d ",rot[j]);}     printf("\n"); } return 0; }