Tuesday, July 9, 2019

Converting the candies shape

Question from TechGig:

Converting the candies shape
An Annual learning competition was organised by a college in its various branches. Various students enrolled their name in the competition for their participation. Children are assigned a task on the spot about a puzzle which they have to solve in a very limited duration of time. They have been given a right isosceles triangle made of n > 0 lines containing 1, 3, . . . , 2n − 1 identical candies, respectively. They have to find out the minimum number of candies that will be needed to move these candies to create a square made up of all the candies given in the triangle. The student who solves this puzzle first will be awarded first and so on..

You have to return the minimum number of candies that is needed to move to convert the isosceles triangle into square made up of all the candies.


For example, assuming the number of lines of candies in the triangle is 3.




                                                                                         

Hence, the minimum number of candies that is needed to move to convert the isosceles triangle into square will be 2.

Input Format
Total number of lines of candies in the triangle.

Constraints
0 < N <=10000

Output Format
The minimum number of candies that are needed to move to convert the isosceles triangle into a square.

Sample TestCase 1
Input
3
Output
2


PROGRAM IN C:

#include<stdio.h>
int main(int argc, char *a[])
{
int i,number,result1,result2;
printf("Give the value of heigh");
scanf("%d",&number);
number=(number-1)/2;
for(i=1;i<=number;i++)
result1=result1+i;
result2=result1*2;
printf("%d",result2);
return 0;  
}

PROGRAM IN JAVA:


import java.util.Scanner; public class Main
{
  public static void main(String [] args)    {     int number,result1=0,result2=0;     System.out.println("Give the value of height");     Scanner input = new Scanner(System.in); number = input.nextInt(); number=(number-1)/2; for(int i=1;i<=number;i++) result1=result1+i; result2=result1*2; System.out.println(result2); } }




No comments:

Post a Comment