Array Transform | CodeChef Solution

Hello coders, today we are going to solve Array Transform CodeChef Solution whose Problem Code is ARRAYTRM.

Array Transform

Contents

Task

Given n numbers, you can perform the following operation any number of times : Choose any subset of the numbers (possibly empty), none of which are 0. Decrement the numbers in the subset by 1, and increment the numbers not in the subset by K.

Is it possible to perform operations such that exactly n – 1 numbers become 0 ?

Input Format

The first line contains the number of test cases T. 2*T lines follow, 2 for each case. The first line of a test case contains the numbers n and K. The next line contains n numbers, a_1…a_n.

Output Format

Output T lines, one corresponding to each test case. For a test case, output “YES” if there is a sequence of operations as described, and “NO” otherwise.

Sample Input

3
2 1
10 10
3 2
1 2 2
3 2
1 2 3

Sample Output

YES
YES
NO

Constraints

  • 1 <= T <= 1000
  • 2 <= n <= 100
  • 1 <= K <= 10
  • 0 <= a_i <= 1000

Solution – Array Transform

C++

#include <bits/stdc++.h>
using namespace std;

int main() {

int t;
cin>>t;
while(t--)
{
    int n,k;cin>>n>>k;
    
   map<int,int>mp;
    for(int i=0;i<n;i++)
    {int x;cin>>x;
        int z=x%(k+1);
      mp[z]++;
    }

  string  find="NO";
 if(mp.size()==2) 
 { for(auto i:mp)
  {
    if(i.second==1)
    {
        find="YES";
    }
  }}
  if(mp.size()==1)
  {
      find="YES";
  }
    
   
    cout<<find<<endl;
    
}
}

Python

T = int(input())
for t in range(T):
    N, K = map(int, input().split())
    arr = list(map(int, input().split()))
    eqClsCount = [0] * (K+1)
    
    for num in arr:
        eqClsCount[num % (K+1)] += 1
    
    eqClsCount.sort(reverse=True)
    if N - eqClsCount[0] > 1:
        print("NO")
    else:
        print("YES")

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	
    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int testCase = Reader.readIntArr(reader)[0];

        while (testCase-- > 0) {
            int nk[]  = Reader.readIntArr(reader);
            int n = nk[0];
            int k = nk[1];
            int arr[]  = Reader.readIntArr(reader);
            solution(arr, k);
        }

    }

    private static void solution(int arr [], int k){
        k++;
        int mod [] = new int[k];

        for (int i = 0; i < arr.length; i++) {
            mod[arr[i]%k]++;
        }

        for (int val: mod) {
            if(val >= arr.length-1){
                System.out.println("YES");
                return;
            }
        }

        System.out.println("NO");
    }
}
class Reader {


    public static String[] readStringArr(BufferedReader reader) throws IOException {
        if(null == reader)
            throw new RuntimeException("null reader");
        return reader.readLine().trim().split(" ");
    }

    public static int[] readIntArr(BufferedReader reader) throws IOException {
        String line [] = readStringArr(reader);

        int arr [] = new int[line.length];

        for (int i = 0; i < line.length; i++) {
            arr[i] = Integer.parseInt(line[i]);
        }

        return arr;
    }
}

Disclaimer: The above Problem (Array Transform) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.