Skip to main content

Microsoft CoCubes Java Programming Questions – 4 Find the number closest to n and divisible by m

 

Microsoft  CoCubes Java Programming Questions – 4

Find the number closest to n and divisible by m

Given two integers n and m. The problem is to find the number closest to n and divisible by m. If there are more than one such number, then output the one having maximum absolute value. If n is completely divisible by m, then output n only. Time complexity of O(1) is required.

Constraints: m != 0

We find value of n/m. Let this value be q. Then we find closest of two possibilities. One is q * m other is (m * (q + 1)) or (m * (q – 1)) depending on whether one of the given two numbers is negative or not.

 

Algorithm:

closestNumber(n, m)

    Declare q, n1, n2

    q = n / m

    n1 = m * q

 

    if (n * m) > 0

        n2 = m * (q + 1)

    else

        n2 = m * (q - 1)

 

    if abs(n-n1) < abs(n-n2)

        return n1

    return n2 

 

Program :

 

// Microsoft CoCubes Java Programming Questions – 4

 

import java.util.*;

class Main

{

// function to find the number closest to n

// and divisible by m

  static int closestNumber (int n, int m)

  {

// find the quotient

    int q = n / m;

 

// 1st possible closest number

    int n1 = m * q;

 

// 2nd possible closest number

    int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));

 

// if true, then n1 is the required closest number

    if (Math.abs (n - n1) < Math.abs (n - n2))

        return n1;

 

// else n2 is the required closest number

      return n2;

  }

 

// Driver program to test above

  public static void main (String args[])

  {

      Scanner sc=new Scanner (System.in);

      int n=sc.nextInt();

      int m=sc.nextInt();

    System.out.println (closestNumber (n, m));

  }

}

 

For Output check the Video :




Comments

Popular posts from this blog

Hackerrank Java Coding Questions and Answers - Maximum Passengers

  Hackerrank Java Coding Questions and Answers Maximum Passengers Problem Statement -:  A taxi can take multiple passengers to the railway station at the same time.On the way back to the starting point,the taxi driver may pick up additional passengers for his next trip to the airport.A map of passenger location has been created,represented as a square matrix. The Matrix is filled with cells,and each cell will have an initial value as follows: A value greater than or equal to zero represents a path. A value equal to 1 represents a passenger. A value equal to -1 represents an obstruction. The rules of motion of taxi are as follows: The Taxi driver starts at (0,0) and the railway station is at (n-1,n-1).Movement towards the railway station is right or down,through valid path cells. After reaching (n-1,n-1) the taxi driver travels back to (0,0) by travelling left or up through valid path cells. When passing through a path cell containing a passenger,the passe...

HackerRank Java Coding Questions and Answers - Disk Space Analysis

  HackerRank   Java   Coding   Questions   and   Answers Disk Space Analysis Problem Statement -:   You are given an array, You have to choose a contiguous subarray of length ‘k’, and find the minimum of that segment, return the maximum of those minimums. Sample input 1 →  Length of segment x =1 5 →  size of space n = 5 1 → space = [ 1,2,3,1,2] 2  3  1  2  Sample output 3 Explanation The subarrays of size x = 1 are [1],[2],[3],[1], and [2],Because each subarray only contains 1 element, each value is minimal with respect to the subarray it is in. The maximum of these values is 3. Therefore, the answer is 3   Program :   // Hackerrank Java Coding Questions and Answers //   Disk Space Analysis   import java.util.*; class DiskSpace {   public static void main(String[] args) {   Scanner sc=new Scanner(System.in);   System.out.println...

TCS CodeVita Coding Questions with Answers - Counting Rock Sample

  TCS CodeVita Coding Questions with Answers Counting Rock Sample Problem Description Question – :  Juan Marquinho is a geologist and he needs to count rock samples in order to send it to a chemical laboratory. He has a problem: The laboratory only accepts rock samples by a range of its size in ppm (parts per million). Juan Marquinho receives the rock samples one by one and he classifies the rock samples according to the range of the laboratory. This process is very hard because the number of rock samples may be in millions. Juan Marquinho needs your help, your task is to develop a program to get the number of rocks in each of the ranges accepted by the laboratory. Input Format :  An positive integer S (the number of rock samples) separated by a blank space, and a positive integer R (the number of ranges of the laboratory); A list of the sizes of S samples (in ppm), as positive integers separated by space R lines where the ith line containing two positive...