Minimize count of divisions by D to obtain at least K equal array elements | C # program to implement the above approach ; Function to return minimum number of moves required ; Stores the number of moves required to obtain respective values from the given array ; Traverse the array ; Insert 0 into V [ a [ i ] ] as it is the initial state ; Insert the moves required to obtain current a [ i ] ; Traverse v [ ] to obtain minimum count of moves ; Check if there are at least K equal elements for v [ i ] ; Add the sum of minimum K moves ; Update answer ; Return the final answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int getMinimumMoves ( int n , int k , int d , int [ ] a ) { int MAX = 100000 ; List < int > [ ] v = new List < int > [ MAX ] ; for ( int i = 0 ; i < v . Length ; i ++ ) v [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; v [ a [ i ] ] . Add ( 0 ) ; while ( a [ i ] > 0 ) { a [ i ] /= d ; cnt ++ ; v [ a [ i ] ] . Add ( cnt ) ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( v [ i ] . Count >= k ) { int move = 0 ; v [ i ] . Sort ( ) ; for ( int j = 0 ; j < k ; j ++ ) { move += v [ i ] [ j ] ; } ans = Math . Min ( ans , move ) ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 5 , K = 3 , D = 2 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; Console . Write ( getMinimumMoves ( N , K , D , A ) ) ; } }