×

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

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Java Get Time in UTC

UTC is the abbreviation for Universal Time Coordinated. Before the beginning of UTC, it is mentioned as the Greenwich Mean Time (GMT) but Now it is mentioned as the universal...

4 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.