Join Regular Classroom : Visit ClassroomTech

New Coding Program | Set-1 SOLVED | codewindow.in

1. Write a program that will convert the decimal number input to its corresponding binary value. In total 4 decimal inputs will be provided with one input per line. The program is required to print the corresponding binary value in one output per line. The program should read the next input from the console only after displaying the output for the previous input.

Example 1

Input:
4
6
65
1025

Output:
100
110
1000001
10000000001

Solution: In Java

import java.util.*;
import java.lang.*;
import java.io.*;

class Codewindow
{
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		while(str != null) {
		    int val = Integer.parseInt(str);
		    System.out.println(Integer.toBinaryString(val));
		    str = br.readLine();
		}
		
	}
}

2. People from a group are asked to form a circle for an activity. Then this group has to be split into two halves so that each group does a different activity as part of the original one. The task is to split it into two halves with exact same count or if there odd number of people, the first half can have more people that other. Consider the arrangement to be in the form a circular linked list and split accordingly.

Example 1

Input:
5
1 2 3 4 5

Output:
1 2 3
4 5

Explanation:
If list has odd number of values then first half can have more people, so first half 1, 2, 3 becomes group1 and 4, 5 becomes group2.

Solution: In Java

import java.util.*;
import java.lang.*;
import java.io.*;

class Codewindow
{
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int num = Integer.parseInt(br.readLine());
		int[] group = new int[num];
		String[] str = br.readLine().split(" ");
		for(int i=0; i<num; i++)
		    group[i] = Integer.parseInt(str[i]);
		if(num%2 == 0) {
		    for(int i=0; i<num/2; i++)
		        System.out.print(group[i] + " ");
		    System.out.println();
		    for(int i=num/2; i<num; i++)
		        System.out.print(group[i] + " ");
		} else {
		    for(int i=0; i<=num/2; i++)
		        System.out.print(group[i] + " ");
		    System.out.println();
		    for(int i=num/2 + 1; i<num; i++)
		        System.out.print(group[i] + " ");
		}
	}
}

3. You are given two numbers a and b

Task:
Determine the Nth number which is divisible by a and b.

Example:

Assumptions:
a = 3
b = 2
N = 3

Approach: You must find the Nth number which is divisible by a and b
Here the answer is 18.

Function Description:

Complete the solve function provided in the editor. This function takes the following 3 parameters and returns Nth number which is divisible by a and b.

Solution: In Java

import java.util.*;
import java.lang.*;
import java.io.*;

class Codewindow
{
    static int gcd(int a, int b)  
    {
        while(b != 0)
        {
            if(a > b)
                a = a - b;
            else
                b = b - a;
        }
        return a;
    }
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int a = Integer.parseInt(br.readLine());
		int b = Integer.parseInt(br.readLine());
		int n = Integer.parseInt(br.readLine());
		
		int lcm = (a * b) / gcd(a, b);
		System.out.println(n * lcm);
	}
}

4. Network of computers

You are given a computer network in form of a grph in which each computer represent the vertex and all wires connecting the computers act as edges. The computers are numbered from 1 to n and wires are numbered from 1 to m.

Each wire connect two computers a[i] and b[i].
Find the total number of ways to mark each computer with blue, green or red color such that two computers which are directly connected are marked with different colors.

Input Format:
First line contains n and m (representing the number of computers and the number of edges).
Next m lines contain computer numbers being connected (computer numbers a[i] and b[i] being connected by wire).

Constraints:
– 1<=n<=20
– 0<=m<=(n*(n-1))/2
– 1<= a[i] <= n
– 1<= b[i] <=n

Output Format:
Find the total number of ways to mark each computer.

Solution: In Java

// Solution will be available soon

// We are very sorry for the inconvenience caused

Also Checkout

Recent Posts
Categories
Pages