Wednesday 4 April 2018

Getting super digit of a number



Given an integer 'z' we can find it's super digit as follow:

If it is a single digit number then it itself is a super digit else the super digit is equal to the super digit of the digit-sum of 'z'.

Example:

1) Input: 5
SuperDigit = 5

2) Input: 1234

SuperDigit(1234) = SuperDigit(1+2+3+4)
                             = SuperDigit(10)
                             = SuperDigit(1+0)
                             = SuperDigit(1)
                             = 1

Input Format:
You are given two numbers n and k. You have to calculate the super digit of p. p is created when the number is concatenated k times. That is, if n= 756  and k=2, then p=756756.
That means you have to calculate super digit for 756756.

The first line contains two space-separated integers n and k.

Java Code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

   
    static int digitSum(String n, int k) {
       if(n.length()==1)
           return (Integer.valueOf(n));
        else{
            long sum=0;
            for(int i=0;i<n.length();i++)
            {
                sum=sum+(Long.parseLong(String.valueOf(n.charAt(i))));
               
            }
         
           
            String num=  String.format ("%d", sum);
            return digitSum(num,k);
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String n = in.next();
        int k = in.nextInt();
        long sum=0;
        for(int i=0;i<n.length();i++)
            {
                sum=sum+(Long.parseLong(String.valueOf(n.charAt(i))));
               
            }
        sum=sum*k;
   
        int result = digitSum(Long.toString(sum), k);
        System.out.println(result);
        in.close();
    }
}

Constraints:

1<= n < 10^100000
1<= k< 10^5


Tuesday 23 January 2018

 Printing a Series:

 Consider a series of number generated from the following formula

fn+1=fn-1+(fn-2)^2;

and the first two numbers are a0 = 0 and a1 = 1

Find the nth number of the series?

The logic to solve:

1) Generate the series using the above formula until nth term is obtained
2) Print the nth number in the series

Tips: As n can be a larger number so you need to choose a data type capable of handling the larger number.

Solution (Java Code):

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Demo {

    static BigInteger generateSeries(BigInteger t1, BigInteger t2, BigInteger n) {
        int condition=n.compareTo(new BigInteger("0"));
                if(condition==0)
                    return t2;
                else
                {
                        BigInteger one= new BigInteger("1");
                       
                        return generateSeries(t2,(t1.add(t2.multiply(t2))),n.subtract(one));
                   
                }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
       String a = in.next();
        String b = in.next();
        String c = in.next();
        BigInteger t1= new BigInteger(a);
                BigInteger t2= new BigInteger(b);
                BigInteger n= new BigInteger(c);

        BigInteger term= generateSeries(t1, t2, n.subtract(new BigInteger("2")));
        System.out.println(term);
        in.close();
    }

}

Friday 2 June 2017

Printing patterns


To print a pattern using '*' and dots ( . ):

code:
#include<stdio.h>
void main()
{
    int i,j,k,m,n;
    printf("enter the value for m and n");
    scanf("%d%d",&n,&m);
        for(i=0;i<((3*n)+1);i++)
        {
            for(j=0;j<3*m+1;j++)
            {
                if(i==0||j==0||i%3==0||j%3==0)
                    printf("*");
                else
                    printf(".");
            }
            printf("\n");
        }

}


input: 3 1
output:

****
*. . *
*. . *
****
*. . *
*. . *
****
*. . *
*. . *
****

input: 3 4

output:
*************
*. . *. . *. . *. . *
*. . *. . *. . *. . *
*************
*. . *. . *. . *. . *
*. . *. . *. . *. . *
*************
*. . *. . *. . *. . *
*. . *. . *. . *. . *
*************


Wednesday 3 May 2017

Privacy Policy for Aqua Shooter: AR Game

This app makes use of the rear camera of your device for immersive gaming experience.

Saturday 22 April 2017

use of concat() and lower() in sql

Generate the following two result sets:

1)Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).

2)Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:

There are total [occupation_count] [occupation]s.

where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.

the table is

occupations( name varchar(45), occupation varchar(25))
QUERY:

select concat(name,'(',left(occupation,1),')')
from occupations
order by name;
select concat('There are total ',count(occupation),' ',lower(occupation),'s','.')
from occupations
group by occupation

order by count(occupation),occupation;

output:
Aamina(D) 
Ashley(P) 
Belvet(P) 
Britney(P) 
Christeen(S) 
Eve(A) 
Jane(S) 
Jennifer(A) 
Jenny(S) 
Julia(D) 
Ketty(A) 
Kristeen(S) 
Maria(P) 
Meera(P) 
Naomi(P) 
Priya(D) 
Priyanka(P) 
Samantha(A) 
There are total 3 doctors. 
There are total 4 actors. 
There are total 4 singers. 

There are total 7 professors. 



Graph colouring using backtracking

GRAPH COLOURING:

Given a graph G (V,E) where V represents the vertices in the graph and E represents the edges in the graph . Given  m different colours to colour the nodes in such a way that no two adjacent nodes are of same colour.

eg:
for the given graph

and given two colours yellow and red

there are two solutions possible:






code:
#include<stdio.h>
int GAM[5][5];
int X[5];
int n,m;
int print()
{
    int i,j;
    printf("THE COLOURS OF EACH NODE IS:");
    printf("for node 1\t2\t3\t4\n");
    for(i=1;i<=n;i++)
    {

        printf("%d",X[i]);
        printf("\n");
    }

}
int issafe( int k,int p)
{
    int c;
    for(c=1;c<k;c++)
    {
        if(X[c]==p && GAM[c][k]==1)
            return 0;
    }
   return 1;
}
void graphcolor(int k,int m)
{
    int p;
    for(p=1;p<=m;p++)// p represents colour no
    {
        if(issafe(k,p))//checking if could be coloured or not
         {
            X[k]=p;
            if(k==n)
            print();
            else
                graphcolor(k+1,m);
         }
    }

}

int main()
{

    printf("enter the number of nodes");
    scanf("%d",&n);
    printf("enter 1 if edge exists else 0");
    int i,j;
    for( i=1;i<=n;i++)
        for(j=1;j<=n;j++)
    {
        printf("edge %d->%d",i,j);
        scanf("%d",&GAM[i][j]);

    }
    printf("enter number of colours available");
    scanf("%d",&m);
    graphcolor(1,m);
    return 0;
}

steps involved ;

for solution 1:(solution two similar to this)


Saturday 1 April 2017

Type of Triangle


Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
  • Not A Triangle: The given values of AB, and C don't form a triangle.
  • Equilateral: It's a triangle with  sides of equal length.
  • Isosceles: It's a triangle with  sides of equal length.
  • Scalene: It's a triangle with  sides of differing lengths.
Input Format
The TRIANGLES table is described as follows:
Each row in the table denotes the lengths of each of a triangle's three sides.
Sample Input
Sample Output
Isosceles
Equilateral
Scalene
Not A Triangle
Explanation
Values in the tuple  form an Isosceles triangle, because 
Values in the tuple  form an Equilateral triangle, because . Values in the tuple  form a Scalene triangle, because 
Values in the tuple  cannot form a triangle because the combined value of sides  and  is not larger than that of side .

Query:
select
case
when (a+b)<=c or (a+c)<=b or (b+c)<=a  then 'Not A Triangle'
when a=b and b=c then 'Equilateral'
when (a=b and b<>c) or (b=c and a<>c) or (a=c and c<>b)  then 'Isosceles'
else    'Scalene'
end
from Triangles;