Problem Statement
Given an Integer N,the task is to create an Special array of size N
An Array is called Special if below condition holds true
- for any sub-array of the original array ,(Sum of Subarray)%(Subarray length)=0
Output any such Distinctive Array of size n
NOTE:Multiple answers are possible
Examples:
input:N=2
output:[19,33]
explanation:The possible subarrays of [19,13] are: [19]->length=1,sum=19,(sum%length)=0 [13]->length=1,sum=13,(sum%length)=0 [19,13]->length=2,sum=32,(sum%length)=0 So,it is distinctive Array
input:N=3
output:[2,8,32]
Approach:The problem might look complex but a simple observation can make it easy.If we print '1' N or any integer N times then it always satisfies the above condition.
Solution
Below is the Java implementation of the above approach:
import java.io.*;
class Solution {
public static void main(String[] args)
{
int n = 3;
// function calling
findArray(n);
}
public static void findArray(int n)
{
// printing '1' n times
for (int i = 0; i < n; i++) {
System.out.print("1 ");
}
System.out.println();
}
}
Output:
1 1 1
Time Complexity:O(n)
Auxiliary Space:O(1)
Feel free to comment if you have any doubt in comment sections
For more Data structure and algorithms,computer science,programming,coding related problems search my website.
Learn how to prepare for Information Technology Based companies here
0 Comments
Have any doubt ? contact us