Tuesday, February 15, 2022

Suppose you are given with a day name, and a number (int value), you need to write a code to determine the number of days after the given day is which day ?

Asked this question in one of the leading MNC.

// Eg.  

// Input: Day = Sunday  & number = 4

// Output: Wednesday

 // Eg.

// Input: Day = Thursday & number = 9

// Output: Friday


import java.util.*;

 class Solution

{   

    public String solution(String day, int number)

    {

        String days[]={"Sunday", "Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday"};

        String output=null;

        for(int i=0;i<days.length;i++)

        {

            if(days[i]==day)

            {

                output = days[(i+number-1)%7];

                break;

            }

        }

         return output;

    }

}

class Main 

{

    public static void main(String[] args)

   

        String ans = new Solution().solution("Sunday",21);

        System.out.println(ans);

    }

}

==============

Find occurrences of a character without using loops and conditions -

String str = "Balaji"; //Find how many times 'a' is repeating

System.out.println(str.split("a").length-1);

No comments:

Post a Comment