Asked this question in one of the leading MNC.
// Eg.
// Input: Day = Sunday
& number = 4
// Output: Wednesday
// Input: Day = Thursday & number = 9
// Output: Friday
import java.util.*;
{
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