Beautiful Days at the Movies
Problem Statement :
Lily likes to play games with integers. She has created a new game where she determines the difference between a number and its reverse. For instance, given the number 12, its reverse is 21. Their difference is 9. The number 120 reversed is 21, and their difference is 99. She decides to apply her game to decision making. She will look at a numbered range of days and will only go to a movie on a beautiful day. Given a range of numbered days, [i.....j] and a number k, determine the number of days in the range that are beautiful. Beautiful numbers are defined as numbers where | i - reverse(i) | is evenly divisible by k. If a day's value is a beautiful number, it is a beautiful day. Return the number of beautiful days in the range. Function Description Complete the beautifulDays function in the editor below. beautifulDays has the following parameter(s): int i: the starting day number int j: the ending day number int k: the divisor Returns int: the number of beautiful days in the range Input Format A single line of three space-separated integers describing the respective values of i, j, and k. Constraints 1 <= i <= j <= 2*10^6 1 <= k <= 2*10^9
Solution :
Solution in C :
python 3 :
def beautifulDays(i, j, k):
daycount = int(0)
for x in range(i, j+1):
if (x - int(str(x)[::-1])) % k == 0:
daycount += 1
return daycount
Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int i = in.nextInt();
int j = in.nextInt();
int k = in.nextInt();
int count = 0;
for (int a=i;j>a; a++){
StringBuilder temp = new StringBuilder();
temp.append(a);
temp=temp.reverse();
String temp1 = temp.toString();
int aRev = Integer.parseInt(temp1);
if(Math.abs((a-aRev)%k)==0){
count++;
}
}
System.out.println(count);
}
}
C++ :
#include <iostream>
using namespace std;
bool isOk(int x, int mod) {
int n = x;
int m = 0;
while (x > 0) {
m = m * 10 + x % 10;
x /= 10;
}
int delta = abs(n - m);
delta %= mod;
return (delta == 0);
}
int main() {
int l, r, k;
cin >> l >> r >> k;
int ans = 0;
for (int i = l; i <= r; i++) {
if (isOk(i, k)) {
++ans;
}
}
cout << ans << endl;
return 0;
}
C :
#include<stdio.h>
int main()
{
int i=0,j=0,k=0,x=0,rem=0,sum=0,count=0;
scanf("%d%d%d",&i,&j,&k);
for(i;i<=j;i++)
{
x=i;
while(x!=0)
{
rem=x%10;
sum=(sum*10)+rem;
x=x/10;
}
if(abs(i-sum)%k==0)
count=count+1;
sum=0;
}
printf("%d",count);
return 0;
}
View More Similar Problems
Merging Communities
People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w
View Solution →Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →