[Solved] Winter Challenge with Java, C++, Python

Swapna is a regular reader of Youth Digest magazine. The magazine has a whole host of fun and interesting facts from around for the youth especially that encourage interactivity and enhances their imagination.
 
“Winter Challenge” is an event announced in the December month edition of the magazine. Readers of the magazine who are between 10 to 15 years can participate in the special challenge. Those readers who participate and give the correct answer for the challenge will avail exciting gift coupons. According to the event, the challenge published was:
 
Given 0 < x < m, where x and m are integers, the modular inverse of x is the unique integer n, 0 < n < m, such that the remainder upon dividing x Γ— n by m is 1.
For example,
if x = 4 and m = 17, we get n = 13
4 x 13 = 52 = 17 x 3 + 1, so the remainder when 52 is divided by 17 is 1, and thus 13 is the inverse of 4 modulo 17.
 
Swapna wants your help to find the correct answer for the problem based on the inputs given in the magazine.

Hence create a class named ModInverse with the following method.

Method NameDescription
int findValue(int,int)                                                 This method returns the modular inverse n or return -1 if there is no such integer n.

Create a driver class called Main. In the Main method, obtain input from the user in the console and display the modular inverse n, or -1 if there is no such integer n by calling the findValue method present in the ModInverse class.

[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.

All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]


Input Format:
First line of the input is an integer that corresponds to x.
Second line of the input is an integer that corresponds to m.

Output format:
Output should display the appropriate value or print -1 otherwise.
Refer sample input and output for formatting specifications.

Sample Input 1:
4
17
Sample Output 1:
13

Sample Input 2:
6
10
Sample Output 2:
-1

Solution

import java.util.Scanner;
class Main {
    static int findValue(int x, int m) {
        for (int i = 1; i < m; i++) {
            if ((x * i) % m == 1) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int m = sc.nextInt();
        int result = findValue(x, m);
        if (result == -1)
            System.out.println("-1");
        else
            System.out.println(result);
    }
    // sauravhathi
}
#include <iostream>
using namespace std;

int findValue(int x, int m)
{

    for (int i = 1; i < m; ++i)
    {
        if ((x * i) % m == 1)
            return i;
    }
    return -1;
}

int main()
{
    int x, m;
    scanf("%d", &x);
    scanf("%d", &m);
    printf("%d\n", findValue(x, m));
    return 0;
    // sauravhathi
}
def findValue(x, m):
    for i in range(1, m):
        if (x * i) % m == 1:
            return i
    return -1
x = int(input())
m = int(input())
print(findValue(x, m))
#sauravhathi

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *