Geek and Function |V | Practice | GeeksforGeeks

Given two integers numbers N and K, the Geek wants you to find f(f(..........f(N))) K times, where f(x) = x XOR (x%10).

Note: XOR represents bitwise xor operation
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two integers N and K.

Output: For each test case, print the answer

Constraints:
1. 1 <= T <= 10
2. 1 <= N <= 105
3. 1 <= K <= 109

Example:
Input:
2
17 1
66 3
Output:
22
74
0
lindan1232 months ago
#include <iostream>
using namespace std;

int main() {
	int n,k,t;
	cin>>t;
	while(t--)
	{
	    cin>>n>>k;
	    long long int num = n;
	    while(k-- && num%10>0){
	     num = num ^ (num%10);
	    }
	    cout<<num<<endl;
	}
	return 0;
}
Time Taken : 0.0
Cpp
0
c1phani1simha6 months ago
#include<bits/stdc++.h>using namespace std;int loop(int a, int b){  int ans=a,rule=b;  if(rule==0)  return ans;  if(ans==0)  return 0;  if(ans%10<=0)  return ans;     return loop(ans^(ans%10),rule-1);  }int main(){int t,n,k;cin>>t;while(t--){  cin>>n>>k;  cout<<loop(n,k)<<endl;    }return 0;}
0
rezwanislam6 months ago
#Python Time 0.0/7.6sec
from sys import stdin, stdout
def f1(N, K): #recursion   if K == 0:       return N   if N%10 <=0:       return N   if N == 0:       return N   N = N^(N%10)   return f1(N,K-1)
def f2(N, K): #loop   if K == 0:       return N   if N == 0:       return N   for times in range(0,K):       if N%10 > 0:           N = N ^ (N % 10)       else:           return N   return N
if __name__ == '__main__':   numCases =  stdin.readline()   numCases = int(numCases)   for i in range(0,numCases):       N, K = stdin.readline().split()       print(f1(int(N),int(K)))
0
Rohit Vishwakarma8 months ago
Rohit Vishwakarma
// Here key logic is that once n%10 ==0 then f(n) will always be equal to n so no need to process further
0
Shadab Anwar9 months ago
Shadab Anwar
0.0s
0
unicorn10 months ago
unicorn
Python solutiondef fun(n, k):    for i in range(k):        if(n%10 == 0):            return n        n = n ^ (n  % 10)    return n
0
Clinton11 months ago
Clinton
0.01 secsint main() {    //code    int T;    cin>>T;    while(T--){        int N,K,x=0;        cin>>N>>K;        while(K--){            if(N<10){                x=0;                break;            }            if(N%10==0){                x=N;                break;            }            x=N^(N%10);            N=x;        }        cout<<x<<"\n"; }="" return="" 0;="" }="" <="" code="">
0
Jyoti 1 year ago
Jyoti 
https://github.com/jYOTIHAR...
0
learnbro1 year ago
learnbro
0.01 Secint main() {//code'int t;cin>>t;while(t--){    long n,k;    cin>>n>>k;    if(n<10){        cout<<0<<"\n";    }    else{        int ans=0;        while(n%10!=0 && k>0){            ans=n;            n=n^(n%10);            k--;         }        cout<<n<<"\n";; }="" }="" return="" 0;="" }<="" code="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.