×

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad hoc problems include software glitches, equipment malfunctions, power outages, and network issues. The Ad Hoc problems are ones that cannot be classified anywhere else in the categories with well-studied solutions due to the fact that each problem description and its associated solution are unique. The need for these problems to fall into pre-established categories prevents there from being a single or generic approach to handling them.

Types of Ad Hoc Problems

  • Card game
    Card games are associated with a lot of Ad Hoc issues. One method is to parse the input texts according to the two suits of playing cards (D/Diamond, C/Club, H/Heart, and S/Spades). For ranks, the standard order is as follows: 2, 3, 9, 10, 11, 12, Queen, King, and Ace. One alternative mapping, for instance, is to map D2 0, D3 1,..., DA 12, C2 13, C3 14,..., and SA 51. In that case, working with integer indices becomes significantly simpler.
  • Palindromes
    A word that may be read in either direction is referred to as a palindrome." ABCDCBA" is an example of a palindrome.
  • Anagrams
    A word (or phrase) that may be made into another word by rearranging its letters is called an anagram (or phrase). Sorting the letters in two words and comparing the results is a typical method for determining whether two words are anagrams. Consider the words "cab" and "bca," for instance. After sorting, wordA and wordB both equal "abc," indicating that they are anagrams.
  • Another popular game that frequently shows up in programming contest challenges, some of which are Ad Hoc difficulties, is chess.Consider the combinatorial task of counting the number of possible arrangements for eight queens on an eight-by-eight chessboard.

Problem Statement

Create an array A of length N such that, given an integer N, there exists a subsequence of length higher than 1 such that the Gcd of each element of the subsequence is i, where I is an integer between 1 and N.

It may be demonstrated that, given the limitations, it is always possible to design such an AA. Print any of the arrays, should there be more than one.

Input Format

The number of test cases is indicated by the single integer T on the first line of the input. The T test cases are described after that.

Each test case has a single line with an integer N, which is the length of the array that needs to be built.

Output Format

Provide a single line of NN space-separated numbers corresponding to the elements in the array AA, for each test scenario.

Print any of the arrays, should there be more than one.

Sample Test Case:

2

3

4

Output

2 3 6


4 24 10 15

Explanation

Test case 1:  Possible array satisfying all the conditions is [2, 3, 6]

  • For i=1: Choose S = [A1, A2, A3] = [2, 3, 6]

    Gcd (2,3,6) = 1
  • For i=2: Select S = [A1, A3] = [2, 6]

    Gcd (2,6) = 2
  • For i=3: Select S = [A2, A3] = [3, 6]

    Gcd (3,6) = 3

AdHocProblem.java

// Importing required packages
import java.util.*;
import java.lang.*;
import java.io.*;
// Main class declaration
class AdHocProblem
{
	// Main section of the program where execution begins
    public static void main (String [] s) 
    {
        // Buffer reader is used to take inputs during the run time
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
	// using stringbuilder to create string 
        StringBuilder sb=new StringBuilder();
	// enter number of test cases
        System.out.println("Enter number of test cases");
        int t=Integer.parseInt(bu.readLine());
	// run a loop for test cases
        while(t-->0)
        {
           // enter size of the array
	 System.out.println("Enter size of the array");
	// Converting into Integer
            int n=Integer.parseInt(bu.readLine());
            long ans[]=new long[n]; int i;
	// using for loop
            for(i=0;i+2<n;i+=3)
            {
                long a=i+1,b=i+2,c=i+3;
                ans[i]=a*b; ans[i+1]=b*c; ans[i+2]=c*a;
                if(i%2==1) ans[i-1]*=b;
            }


            if(i+1==n) {ans[i]=i+1; ans[0]*=i+1;}
            if(i+2==n) {ans[i]=i+1; ans[i+1]=i+2; ans[0]*=(i+1)*(i+2);}
	
            for(i=0;i<n;i++) sb.append(ans[i]+" ");
            sb.append("\n");
        }
	 System.out.print(" The resulted array is");
	// Printing the resulted array
        System.out.print(sb);
    }
}

Output

Enter the number of test cases
1
Enter size of the array
4
The resulted array is
8 6 3 4

Related Topics

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

How to compare two dates in different format in Java?

We need to compare two dates frequently when coding. Real-world examples include sorting a list of persons by age or keeping track of students' attendance. To compare two dates, we...

7 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

C# vs Java

Difference Between C# and Java C# and Java both languagesare popularly used programming languages. They both are derived from C/C++ programming and follow Object Oriented Programming approach. Even so, both these...

4 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

How to Convert int to double in Java

How to Convert int to double in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.