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